Sha256: 8d6ad0d13345ab355f0963cb83f8090ee5022457f5290c3f784f6541d6c77b8a

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

require 'treetop'
require 'origen_verilog/node'
module OrigenVerilog
  class Parser
    def self.parse_file(path, options = {})
      parse(File.read(path), options.merge(file: path))
    end

    def self.parse(data, options = {})
      # This will be appended to all nodes if supplied
      @file = options[:file]
      Treetop.origen_verilog_parser = self
      tree = parser.parse(data)

      # If the AST is nil then there was an error during parsing
      # we need to report a simple error message to help the user
      if tree.nil? && !options[:quiet]
        parser.failure_reason =~ /^(Expected .+) (after|at)/m
        @last_error_msg = []
        @last_error_msg << "#{Regexp.last_match(1).gsub("\n", '$NEWLINE')}:" if Regexp.last_match(1)
        if parser.failure_line >= data.lines.to_a.size
          @last_error_msg << 'EOF'
        else
          @last_error_msg << data.lines.to_a[parser.failure_line - 1].gsub("\t", ' ')
        end
        @last_error_msg << "#{'~' * (parser.failure_column - 1)}^"
        puts "Failed parsing Verilog file: #{file}"
        puts @last_error_msg
      end
      if tree
        tree.to_ast
      end
    end

    def self.last_error_msg
      @last_error_msg || []
    end

    def self.file
      @file
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
origen_verilog-0.4.0 lib/origen_verilog/parser.rb
origen_verilog-0.3.1 lib/origen_verilog/parser.rb
origen_verilog-0.3.0 lib/origen_verilog/parser.rb
origen_verilog-0.2.2 lib/origen_verilog/parser.rb
origen_verilog-0.2.1 lib/origen_verilog/parser.rb
origen_verilog-0.2.0 lib/origen_verilog/parser.rb