-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey @maral, so the main issue is that Chevrotain does not look into the outer context of a rule to determine its lookahead. In that sense, you need some way to explain to Chevrotain that it has to take more context into account when determining whether to continue parsing the $.RULE("r2", () => {
$.CONSUME(A);
// unwrap MANY_SEP as a combination of OPTION+MANY
$.OPTION(() => {
$.CONSUME(B);
$.MANY({
// If the gate returns false, the `DEF` won't be executed
GATE: () => $.LA(2).tokenType === B,
DEF: () => {
$.CONSUME(X);
$.CONSUME(B);
}
});
});
}); |
Beta Was this translation helpful? Give feedback.
-
I was curious about this, I looked at it with Antlr4 and this grammar seems to handle the test inputs: grammar print;
print_text
: print_group* EOF
;
print_group
: print_set (COMMA print_set)*
;
print_set
: PRINT (NUMBER (COMMA NUMBER)*)?
;
PRINT: 'print';
NUMBER: [0-9]+;
COMMA: ',';
SPACE: (' ')+ -> skip;
NL: [\r\n]+ -> skip; Giving this parse: For this input:
Given Chevrotain uses the same kind of parsing algorithm as Antlr4, can this not be done the same way? |
Beta Was this translation helpful? Give feedback.
Hey @maral,
so the main issue is that Chevrotain does not look into the outer context of a rule to determine its lookahead. In that sense, you need some way to explain to Chevrotain that it has to take more context into account when determining whether to continue parsing the
MANY_SEP
inside ofr2
. Usually you would do this by using aGATE
(see the docs) on theMANY_SEP
call, but onlyMANY
supportsGATE
properties, so you'd have to rewrite the grammar slightly: