Class: EsiAttributeLanguage::Grammar

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) ⇒ Grammar

Returns a new instance of Grammar.



43
44
45
46
47
# File 'lib/esi_attribute_language/grammar.rb', line 43

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.



34
35
36
# File 'lib/esi_attribute_language/grammar.rb', line 34

def rules
  @rules
end

Class Method Details

.parse(source) ⇒ Object



36
37
38
39
40
41
# File 'lib/esi_attribute_language/grammar.rb', line 36

def self.parse(source)
  result = GrammarParser.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



49
50
51
52
# File 'lib/esi_attribute_language/grammar.rb', line 49

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