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

- (Parser) initialize(str)



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

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

Instance Attribute Details

- (Object) last_match (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

+ (Object) register(node)



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

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

+ (Object) to_kpeg



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

- (Object) parse(rule = nil)



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