Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chevrotain

Package Overview
Dependencies
Maintainers
1
Versions
167
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chevrotain

Chevrotain is a high performance fault tolerant javascript parsing DSL for building recursive decent parsers

  • 11.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is chevrotain?

Chevrotain is a fast and feature-rich parser building toolkit for JavaScript. It can be used to build parsers for DSLs, programming languages, data formats, and more. It provides a set of APIs for defining grammar rules and constructing a parser based on those rules.

What are chevrotain's main functionalities?

Defining Token Types

This code sample demonstrates how to define token types using Chevrotain. Tokens are the basic building blocks of the syntax for a language or format. In this example, we define tokens for integers and the plus and minus symbols.

const { createToken, Lexer } = require('chevrotain');

const Integer = createToken({ name: 'Integer', pattern: /\d+/ });
const Plus = createToken({ name: 'Plus', pattern: /\+/ });
const Minus = createToken({ name: 'Minus', pattern: /-/ });

const allTokens = [Plus, Minus, Integer];
const MyLexer = new Lexer(allTokens);

Building a Parser

This code sample shows how to build a parser using Chevrotain. The parser is defined as a class that extends `CstParser` and uses rules to define the grammar of the language. In this example, we define a simple grammar for addition expressions.

const { CstParser } = require('chevrotain');

class MyParser extends CstParser {
  constructor() {
    super(allTokens);
    this.RULE('expression', () => {
      this.SUBRULE(this.additionExpression);
    });

    this.RULE('additionExpression', () => {
      this.CONSUME(Integer);
      this.MANY(() => {
        this.OR([
          { ALT: () => { this.CONSUME(Plus); this.CONSUME2(Integer); } },
          { ALT: () => { this.CONSUME(Minus); this.CONSUME2(Integer); } }
        ]);
      });
    });

    this.performSelfAnalysis();
  }
}

const parser = new MyParser();

Parsing Text

This code sample illustrates how to parse text using a lexer and parser defined with Chevrotain. The text is tokenized by the lexer, and then the tokens are fed into the parser to produce a Concrete Syntax Tree (CST), which can be used for further processing such as interpretation or transformation into an Abstract Syntax Tree (AST).

const { tokenMatcher } = require('chevrotain');

const text = '1 + 2 - 3';
const lexingResult = MyLexer.tokenize(text);

if (lexingResult.errors.length === 0) {
  parser.input = lexingResult.tokens;
  const cst = parser.expression();

  if (parser.errors.length === 0) {
    // cst can now be used to create an AST or for interpretation.
  } else {
    // parser errors are present
  }
} else {
  // lexing errors are present
}

Other packages similar to chevrotain

Keywords

FAQs

Package last updated on 14 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc