Class: OrigenSTIL::Syntax::Parser

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

Class Method Summary collapse

Class Method Details

.fileObject



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

def self.file
  @file
end

.last_error_msgObject



44
45
46
# File 'lib/origen_stil/syntax/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_stil/syntax/parser.rb', line 18

def self.parse(data, options = {})
  @file = options[:file]
  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)}^"
    Origen.log.error "Failed parsing STIL file: #{file}"
    @last_error_msg.each do |line|
      Origen.log.error line.rstrip
    end
  end
  if tree
    tree.to_ast
  end
end

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



13
14
15
16
# File 'lib/origen_stil/syntax/parser.rb', line 13

def self.parse_file(path, options = {})
  stil = OrigenSTIL::Pattern.new(path)
  parse(stil.frontmatter, options.merge(file: stil.path))
end

.parserObject



6
7
8
9
10
11
# File 'lib/origen_stil/syntax/parser.rb', line 6

def self.parser
  @parser ||= begin
    require "#{Origen.root!}/grammars/stil"
    GrammarParser.new
  end
end