I'm modifying a DSL grammar for a product that is in public use.
Currently all /*...*/
comments are silently ignored, but I need to modify it so that comments that are placed before certain key elements are parsed into the AST.
I need to maintain backwards compatibility whereby users can still add comments arbitrarily throughout the DSL and only those key comments are included.
The parser grammar currently looks a bit like this:
grammar StateGraph;
graph: 'graph' ID '{' graph_body '}';
graph_body: state+;
state: 'state' ID '{' state_body '}';
state_body: transition* ...etc...;
transition: 'transition' (transition_condition) ID ';';
COMMENT: '/*' ( options {greedy=false;} : . )* '*/' {skip();}
Comments placed before the 'graph' and 'state' elements contain meaningful description and annotations and need to be included within the parsed AST. So I've modified those two rules and am no longer skipping COMMENT:
graph: comment* 'graph' ID '{' graph_body '}';
state: comment* 'state' ID '{' state_body '}';
COMMENT: '/*' ( options {greedy=false;} : . )* '*/'
If I naively use the above, the other comments cause mismatched token errors when subsequently executing the tree parser. How do I ignore all instances of COMMENT that are not placed in front of 'graph' or 'state'?
An example DSL would be:
/* Some description
* @some.meta.info
*/
graph myGraph {
/* Some description of the state.
* @some.meta.info about the state
*/
state first {
transition if (true) second; /* this comment ignored */
}
state second {
}
/* this comment ignored */
}