Skip to content

Checking Token is already consumed or not while parsing #2020

Answered by msujew
Siva-Harish98 asked this question in Q&A
Discussion options

You must be logged in to vote

I see, that's what you mean. I've seen in the other issue that you're using an OR inside the MANY. You can use a GATE property for the ALT that consumes on to prevent it from being parsed again. Just flip a boolean after consuming the token once.

T.RULE('additionExpression', () => {
  T.SUBRULE(T.multiplicationExpression, { LABEL: 'lhs' });
  let on = false;
  T.MANY(() => {
    T.CONSUME(ArithmeticOperator);
    T.OR([
      { 
        ALT: () => { 
          T.SUBRULE3(T.onKey); 
          on = true; 
        } 
        GATE: () => !on
      },
      { ALT: () => T.SUBRULE2(T.multiplicationExpression, { LABEL: 'rhs' }) }
    ])
  });
});

Note that I would recommend to perform a validati…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@Siva-Harish98
Comment options

@msujew
Comment options

msujew Mar 13, 2024
Collaborator

Answer selected by Siva-Harish98
@Siva-Harish98
Comment options

@swami-sanapathi
Comment options

@msujew
Comment options

msujew Mar 14, 2024
Collaborator

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