Class: EsiAttributeLanguage::SimpleGrammar

Inherits:
Object
  • Object
show all
Defined in:
lib/esi_attribute_language/grammar.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ SimpleGrammar

Returns a new instance of SimpleGrammar.



20
21
22
23
24
# File 'lib/esi_attribute_language/grammar.rb', line 20

def initialize(rules)
  @root  = rules.first.name
  @rules = {}
  rules.each { |r| @rules[r.name] = r }
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



11
12
13
# File 'lib/esi_attribute_language/grammar.rb', line 11

def rules
  @rules
end

Class Method Details

.parse(source) ⇒ Object



13
14
15
16
17
18
# File 'lib/esi_attribute_language/grammar.rb', line 13

def self.parse(source)
  result = SimpleGrammarParser.new.scan_str(source)
  result.respond_to?(:execute) ? result : DummyNode.new(result)
rescue Racc::ParseError => e
  raise ParseError, e.message
end

Instance Method Details

#parse(string) ⇒ Object



26
27
28
29
# File 'lib/esi_attribute_language/grammar.rb', line 26

def parse(string)
  index = @rules[@root].call(string, 0, @rules)
  index if index == string.length
end