I have a bunch of data in (what i think is) a tcl array. Basically it's in the form of {a {b c} d {e f} g}
. It's only nested one deep, but isn't always nested, that is to say, a
may just be a
or it may be {aa bb}
or possibly {}
, but never {aa {bb cc}}
. I want to extract this array so I can use it in ruby.
My first thought was, "No problem, I'll write a little grammar to parse this." I installed the treetop gem, and wrote up a parser, which seemed to work just fine. I started having problems when I tried to extract an array from the parsed tree. I would like to better understand the cause of the problems and what I am doing wrong.
Here is my parser code so far: (tcl_array.treetop)
grammar TCLArray
rule array
"{" [\s]* "}" {
def content
[]
end
}
/
"{" [\s]* array_element_list [\s]* "}" {
def content
array_element_list.content
end
}
end
rule array_element_list
array_element {
def content
[array_element.content]
end
}
/
array_element [\s]+ array_element_list {
def content
[array_element.content] + array_element_list.content
end
}
end
rule array_element
[^{}\s]+ {
def content
return text_value
end
}
/
array {
def content
array.content
end
}
end
end
Invoking p.parse("{a}").content
yields tcl_array.rb:99:in 'content': undefined local variable or method 'array_element'
The first term in array_element_list (array_element) says that array_element is an undefined local variable, but accessor methods are supposed to be automatically defined according to the treetop documentation.
Earlier, I tried a solution that was based off of a grammar with fewer but slightly more complicated rules:
grammar TCLArray
rule array
"{" ([\s]* array_element ([\s]+ array_element)* )? [\s]* "}"
end
rule array_element
[^{}\s]+ / array
end
end
But with this grammar I had issues where the parser seemed to be creating several different expressions for the array rule even though it did not use any alternative expressions (/). The result was that I couldn't figure out how to access the various bits of the array rule to return them as a ruby array.