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

Token Alternative Matches

Chevrotain supports attempting a secondary longer match after a token has (already) been matched. This capability is most often used to disambiguate the keywords vs identifiers ambiguity.

For example:

import { createToken } from "chevrotain";

const Identifier = createToken({
  name: "Identifier",
  pattern: /[a-zA-Z][\w+]/,
});

const ClassKeyword = createToken({
  name: "ClassKeyword",
  pattern: /class/,
  longer_alt: Identifier,
});

Note that the longer_alt capability cannot be chained, only a single longer_alt will be checked for a specific Token. A token may define multiple longer alternatives using an array. As per usual with the lexer, the first matching token in the array will be chosen for lexing.

See executable example for further details.

Edit this page on GitHub
Last Updated: 7/9/23, 12:55 AM
Contributors: Shahar Soel, bd82, Mark Sujew
Prev
Position Tracking
Next
Token Skipping