I have the following grammar:
grammar Test;
options {
lang = Python;
}
declaration returns [value]
: 'enum' ID { statement* }
{ $value = {'id': $ID.text,
'fields': $statement.value}
}
;
statement returns [value]
: ID ':' INT ';' { $value = {'id': $ID.text, 'value': int($INT.text)} }
;
To parse syntax of type:
enum Test {
Foo: 3;
Bar: 5;
}
However, I am struggling with getting the statement* rule into a list of statements. I want my final parsed object to look like:
declaration = {
'id': 'Test',
'fields': [
{'id': 'Foo', 'value': 3},
{'id': 'Bar', 'value': 5},
}
I can parse each of the statement
results correctly, so that each $statement.value
is correct.
But, given the asterisk on statement*
in rule declaration
, is there a way I can condense it into a list of fields easily ? I was hoping to have some sort of syntax that gets me this option for free.
Right now this just takes the last statement, so it returns:
declaration = {
'id': 'Test',
'fields': [
{'id': 'Bar', 'value': 5},
}
I want a generic solution because my grammar has a lot of rules of the form:
some_declaration
: keyword ID '{' declaration_statement* '}'
;
Note: I am coding this in Python. I have tried coding this as a parser followed by a tree grammar, but even then the last element is the only one I get, the rest are discarded.