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

Parameterized Rules

Chevrotain supports passing parameters to rules. This means that grammar rules may accept arguments from the calling rule. This is often used in combination with gates to represent multiple variants of the same parsing rule while avoiding code duplication.

For example:

$.RULE("ArgumentInConst", () => {
  $.CONSUME(Name);
  $.CONSUME(Colon);
  // passing the argument using the "ARGS" property
  $.SUBRULE($.Value, { ARGS: [true] });
});

// 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) },
  ]);
});

See executable example for further details.

Edit this page on GitHub
Last Updated: 7/9/23, 12:55 AM
Contributors: Shahar Soel, bd82
Prev
Customizable Error Messages
Next
Gates