Hello,
Very new to using Treetop and working on a simple proof-of-concept grammar where I need to define my own internal methods in Treetop nodes.
A very simple example such as this is working fine:
grammar SimpleHTML
rule document
(text / tag)* {
def content
elements.map{ |e| e.content }
end
}
end
rule text
[^<]+ {
def content
[:text, text_value]
end
}
end
rule tag
"<" [^>]+ ">" {
def content
[:tag, text_value]
end
}
end
end
However, When I try to use another grammar of mine that is working (as far as parsing) to add the "content" methods I keep getting:
NoMethodError: undefined method `content' for #<Treetop::Runtime::SyntaxNode:0x007f91e414d228>
no matter what I try. I've added def content ... end in EVERY node of the grammar, and tried many variations:
grammar EquipmentGrammar
rule top
(equipment)* {
def content
elements.map{ |e| e.content }
end
}
end
rule equipment
(manufacturer ws capacity ws system) /
(manufacturer ws system) {
def content
elements.map{ |e| e.content }
end
}
end
rule manufacturer
'Frigoboat'i / 'Jabsco'i {
def content
[text_value]
end
}
end
rule system
system_ac / system_plumbing {
def content
[elements.first.content]
end
}
end
rule system_ac
'air conditioning system'i / 'air conditioning'i / 'a/c'i {
def content
['A/C']
end
}
end
rule system_plumbing
'plumbing' / 'plumbing system' {
def content
['Plumbing']
end
}
end
rule capacity
'6,000 BTU' {
def content
[elements.first.content]
end
}
end
rule ws
[\s\n\t\r]* {
def content
[' ']
end
}
end
end
Now if I use that to parse a sample string:
[22] pry(main)> Treetop.load 'app/grammars/equipment_grammar'
=> EquipmentGrammarParser
[23] pry(main)> parser = EquipmentGrammarParser.new
=> #<EquipmentGrammarParser:0x007f91dcea94b0 @consume_all_input=true>
[24] pry(main)> tree = parser.parse("Frigoboat 6,000 BTU air conditioning system")
=> SyntaxNode+Top0 offset=0, "... conditioning system" (content):
SyntaxNode+Equipment0 offset=0, "... conditioning system" (system,manufacturer,ws1,capacity,ws2):
SyntaxNode offset=0, "Frigoboat"
SyntaxNode+Ws0 offset=9, " " (content):
SyntaxNode offset=9, " "
SyntaxNode+Capacity0 offset=10, "6,000 BTU" (content)
SyntaxNode+Ws0 offset=19, " " (content):
SyntaxNode offset=19, " "
SyntaxNode offset=20, "... conditioning system"
[25] pry(main)> tree.content
NoMethodError: undefined method `content' for #<Treetop::Runtime::SyntaxNode:0x007f91e414d228>
from (eval):10:in `block in content'
Any help or suggestions on defining and/or using your own methods would be greatly appreciated!