I'm trying to parse a DSL with OMetaJS and produce an AST that includes a token value as well as it's index in the original stream.
I know I can use the Index Capture Rule syntax ( @<rule>
) to give me an object containing the indices framing the token but is it possible to capture that as well as the token value?
E.g for the grammar:
export ometa Test {
start = @<identifier>,
identifier = (letter | digit)+
}
Parsing "Bob" gives:
{ fromIdx : 0, toIdx : 3 }
If I remove the '@' from 'identifier' then parsing gives "Bob" as the result. What I'd ideally like to get is combination of the two:
{ fromIdx : 0, toIdx : 3, value: 'Bob' }
I could of course hack the source, but is there a better way to do this?
I want to have both value and position because I'm trying to create a visual representation of the DSL which allows editing of identifier names for example. In this case I need to know where in the original source the identifier appeared so I can modify it.