Skip to content

Grammar problem - two rules, same separator #1873

Answered by msujew
maral asked this question in Q&A
Discussion options

You must be logged in to vote

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 of r2. Usually you would do this by using a GATE (see the docs) on the MANY_SEP call, but only MANY supports GATE properties, so you'd have to rewrite the grammar slightly:

$.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).t…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@maral
Comment options

Answer selected by maral
Comment options

You must be logged in to vote
1 reply
@msujew
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants