-
I want to achieve the following effect (automatic semicolon insertion), which determines the semantics based on whether there is a line break a = b ++ infix
a = b
++prefix But I hide all the whitespace, WHITE_SPACE: [\p{White_Space}]+ -> channel(HIDDEN); How do I individually require this rule to consider whitespace? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The parser cannot influence the lexer! Newlines should remain on the hidden channel. Introduce a parser rule
Test:
You may want to change the alts to reflect the precedence you want. |
Beta Was this translation helpful? Give feedback.
-
This is a cool idea, insert dummy token to interrupt parser. PS. If anyone uses java, please try boolean check() {
CommonTokenStream ct = (CommonTokenStream)this._input;
Token lb1 = ct.LT(-1);
Token la1 = ct.LT(1);
if (!(la1.getType() == <%YOUR_NON_NEWLINE_TOKEN%>)) {
return false;
}
for (int i = la1.getTokenIndex() - 1; i >= 0; i--) {
Token c = ct.get(i);
if (c.getChannel() != Token.HIDDEN_CHANNEL) {
return true;
}
if (c.getType() == <%YOUR_NON_NEWLINE%>) {
return false;
}
}
return true;
} |
Beta Was this translation helpful? Give feedback.
The parser cannot influence the lexer!
Newlines should remain on the hidden channel. Introduce a parser rule
not_nl
to force the parser to not allow certain alternatives for expressions, then add references tonot_nl
just before '++' operators. Note, since semantic predicates must be at the beginning of the alt, you have to introduce thenot_nl
rule rather than insert the semantic predicate directly inexpr
to satisfy the constraint that Antlr requires. The assumption here is that you cannot have an infix or postfix '++' operator that crosses to the next line.