Class: Gobstones::Parser::TreetopParser

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

Instance Method Summary collapse

Constructor Details

#initializeTreetopParser

Returns a new instance of TreetopParser.



8
9
10
11
# File 'lib/gobstones/parser/treetop_parser.rb', line 8

def initialize
  Treetop.load grammar_file
  @parser = GobstonesParser.new
end

Instance Method Details

#failure_columnObject



29
30
31
# File 'lib/gobstones/parser/treetop_parser.rb', line 29

def failure_column
  @parser.failure_column
end

#failure_lineObject



25
26
27
# File 'lib/gobstones/parser/treetop_parser.rb', line 25

def failure_line
  @parser.failure_line
end

#failure_reasonObject



21
22
23
# File 'lib/gobstones/parser/treetop_parser.rb', line 21

def failure_reason
  @parser.failure_reason
end

#parse(code) ⇒ Object

Raises:



13
14
15
16
17
18
19
# File 'lib/gobstones/parser/treetop_parser.rb', line 13

def parse(code)
  code_without_comments = remove_comments_from(code)
  result = @parser.parse(code_without_comments)
  raise ParseError.new(self, code_without_comments) if result.nil?

  result.value
end

#remove_comments_from(code) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/gobstones/parser/treetop_parser.rb', line 33

def remove_comments_from(code)
  code.
    gsub(single_line_c_style_comments_regex, '').
    gsub(single_line_haskell_style_comments_regex, '').
    gsub(multi_line_c_style_comments_regex, '').
    gsub(multi_line_haskell_style_comments_regex, '')
end