Chevrotain
Home
Features
Tutorial
Guide
FAQ
Changes
APIs
Playground
Benchmark
Discussions
GitHub
Home
Features
Tutorial
Guide
FAQ
Changes
APIs
Playground
Benchmark
Discussions
GitHub
  • Features

    • Blazing Fast
    • LL(K) Grammars
    • Separation of Grammar and Semantics
    • Easy Debugging
    • Fault Tolerance
    • Multiple Start Rules
    • Customizable Error Messages
    • Parameterized Rules
    • Gates
    • Syntactic Content Assist
    • Grammar Inheritance
    • Backtracking
    • Syntax Diagrams
    • RegExp Based Lexers
    • Position Tracking
    • Token Alternative Matches
    • Token Skipping
    • Token Categories
    • Token Grouping
    • Custom Token Patterns
    • Lexer Modes

Gates

Chevrotain supports Gates on parsing DSL method. Gates act as a type of guard condition that prevents an alternative from being taken. Gates are often used in combination with parametrized rules to represent multiple variants of the same parsing rule while avoiding code duplication.

For example:

// isConst is a parameter passed from another rule.
$.RULE("Value", (isConst) => {
  $.OR([
    // the Variable alternative is only possible when "isConst" is Falsey
    { GATE: () => !isConst, ALT: () => $.SUBRULE($.Variable) },
    { ALT: () => $.CONSUME(IntValue) },
    { ALT: () => $.CONSUME(FloatValue) },
    { ALT: () => $.CONSUME(StringValue) },
  ]);
});

Using the Look Ahead method is often helpful with the use of Gates to determine if a path should be followed or not, for example:

// SELECT LIMIT.ID FROM USER_LIMIT LIMIT
// SELECT ID, NAME FROM USER_LIMIT LIMIT 1
$.RULE("FromClause", () => {
  $.CONSUME(From);
  $.CONSUME(Identifier);

  $.OPTION({
    GATE: () => $.LA(2).tokenType !== UnsignedInteger,
    DEF: () => $.CONSUME1(Identifier, { LABEL: "alias" }),
  });
});

If LIMIT is an identifier or a keyword based on the surrounding tokens, looking ahead at subsequent tokens is required to know if the token should be consumed as an identifer or should be skipped to be parsed up by a subsequent rule.

See executable example for further details.

Edit this page on GitHub
Last Updated: 1/24/26, 5:39 PM
Contributors: Shahar Soel, bd82, Austin Turner, I060847, Gaurav
Prev
Parameterized Rules
Next
Syntactic Content Assist