Class: Prattle::Parser

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

Defined Under Namespace

Classes: ParseError

Constant Summary

Grammar =
to_kpeg

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Parser

Returns a new instance of Parser



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

def initialize(str)
  @parser = KPeg::Parser.new(str, Grammar)
end

Instance Attribute Details

#last_matchObject (readonly)

Returns the value of attribute last_match



49
50
51
# File 'lib/prattle/parser.rb', line 49

def last_match
  @last_match
end

Class Method Details

.register(node) ⇒ Object



9
10
11
# File 'lib/prattle/parser.rb', line 9

def self.register(node)
  @nodes << node
end

.to_kpegObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/prattle/parser.rb', line 13

def self.to_kpeg
  gram = KPeg::Grammar.new

  @nodes.each do |node|
    node.grammar(gram)
  end

  AST.grammar(gram)

  gram
end

Instance Method Details

#parse(rule = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/prattle/parser.rb', line 39

def parse(rule=nil)
  @last_match = match = @parser.parse(rule ? rule.to_s : nil)

  if @parser.failed?
    raise ParseError.new(@parser, match)
  end

  return match.value if match
end