Class: OrigenVerilog::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/origen_verilog/parser.rb

Class Method Summary collapse

Class Method Details

.fileObject



48
49
50
# File 'lib/origen_verilog/parser.rb', line 48

def self.file
  @file
end

.last_error_msgObject



44
45
46
# File 'lib/origen_verilog/parser.rb', line 44

def self.last_error_msg
  @last_error_msg || []
end

.parse(data, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/origen_verilog/parser.rb', line 18

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

.parse_file(path, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/origen_verilog/parser.rb', line 5

def self.parse_file(path, options = {})
  if !File.exist?(path) && options[:source_dirs]
    dir = Array(options[:source_dirs]).find do |dir|
      File.exist?(File.join(dir, path))
    end
    path = File.join(dir, path) if dir
  end
  unless File.exist?(path)
    fail "File could not be found in the current dir, or any of the source dirs: #{path}"
  end
  parse(File.read(path), options.merge(file: path))
end