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 Categories

When parsing it is sometimes useful to match a terminal against a set of Token Types. This can be accomplished by using Token Categories.

For example:

// "KeywordOrIdentifier" is our Token category used to match any keyword or Identifier
const KeywordOrIdentifier = createToken({
  name: "AnyWord",
  pattern: Lexer.NA,
});

// General Identifier
export const Identifier = createToken({
  name: "Identifier",
  pattern: /[a-zA-Z]\w*/,
  categories: [KeywordOrIdentifier],
});

// a Keyword
export const Class = createToken({
  name: "Class",
  pattern: /Class/,
  longer_alt: Identifier,
  categories: [KeywordOrIdentifier],
});
$.RULE("SomeRule", () => {
  // This would match either an Identifier or a keyword thus allowing for
  // "None Reserved keywords"
  $.CONSUME(KeywordOrIdentifier);
});

Note that:

  • A Token category is simply another Token Type.
  • A Token Type may have multiple Token Categories.
Edit this page on GitHub
Last Updated: 7/9/23, 12:55 AM
Contributors: bd82, Shahar Soel
Prev
Token Skipping
Next
Token Grouping