chevrotain vs nearley vs peggy vs jison
Building Custom Languages and DSLs in JavaScript
chevrotainnearleypeggyjisonSimilar Packages:

Building Custom Languages and DSLs in JavaScript

chevrotain, jison, nearley, and peggy are tools for building parsers and compilers in JavaScript. They allow developers to define formal grammars for custom languages, domain-specific languages (DSLs), or complex data formats. chevrotain focuses on high performance and error recovery using a JavaScript-based DSL. jison is a classic LALR(1) parser generator similar to Yacc. nearley uses a compact grammar syntax based on nearley grammar rules. peggy is the maintained fork of pegjs, using Parsing Expression Grammars (PEG) for intuitive rule definitions.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
chevrotain8,806,5872,8151.24 MB313 months agoApache-2.0
nearley6,078,7103,739-1996 years agoMIT
peggy426,1241,185588 kB336 days agoMIT
jison04,380-1619 years agoMIT

Chevrotain vs Jison vs Nearley vs Peggy: Architectural Comparison

Building a custom language or parser in JavaScript requires choosing the right tool for grammar definition, error handling, and runtime performance. chevrotain, jison, nearley, and peggy all solve this problem but use different strategies. Let’s compare how they handle grammar definition, error recovery, and integration.

πŸ“ Grammar Definition: Code vs Files

chevrotain defines grammars using JavaScript objects and classes.

  • No separate grammar files are needed.
  • You write parser logic directly in TypeScript or JavaScript.
  • Better for refactoring and IDE support.
// chevrotain: Grammar in JS
const MyParser = createParser({
  MAX_LOOKAHEAD: 2,
  definedRules: {
    number: [{ CONSUME: Digit }],
    add: [{ SUBRULE: number }, { CONSUME: Plus }, { SUBRULE: number }]
  }
});
const parser = new MyParser();

jison uses JSON or Yacc-style grammar files.

  • Separates grammar from logic.
  • Requires a build step to generate the parser.
  • Familiar to those coming from C/C++ compiler backgrounds.
// jison: Grammar in JSON/Yacc
const parser = new Parser({
  bnfs: {
    "expression": [["expression", "+", "term"], ["term"]]
  },
  operators: [["left", "+"]]
});

nearley uses a compact custom grammar syntax (.ne files).

  • Very concise and readable.
  • Compiles to JavaScript via CLI.
  • Great for rapid prototyping of small languages.
// nearley: grammar.ne
expression -> expression "+" term | term
term -> [0-9]+
// nearley: Usage in JS
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
parser.feed("1 + 2");

peggy uses Parsing Expression Grammar (PEG) syntax.

  • Grammar looks very close to the input data.
  • Actions (JavaScript code) can be embedded directly in rules.
  • No need for separate semantic handling files.
// peggy: grammar.peggy
expression = head:term "+" tail:term { return head + tail; }
term = [0-9]+ { return parseInt(text(), 10); }
// peggy: Usage in JS
const parser = peggy.generate(grammar);
const result = parser.parse("1 + 2");

🚨 Error Handling & Recovery

chevrotain excels at error recovery.

  • Can continue parsing after encountering an error.
  • Provides detailed error messages with line and column numbers.
  • Ideal for IDEs or editors where you don’t want parsing to stop at the first mistake.
// chevrotain: Error recovery
const errors = parser.errors;
if (errors.length > 0) {
  console.log("Found " + errors.length + " errors");
  // Parser still returns a partial CST
}

jison stops on the first syntax error.

  • Basic error messages.
  • No built-in recovery mechanism.
  • Suitable for batch processing where failure should be immediate.
// jison: Error handling
try {
  parser.parse("1 +");
} catch (e) {
  console.log("Parse failed:", e.message);
}

nearley throws on ambiguity or syntax errors.

  • Can handle ambiguous grammars (returns multiple results).
  • Less focused on recovery, more on correctness.
// nearley: Error handling
try {
  parser.feed("invalid");
} catch (e) {
  console.log("Syntax error");
}

peggy throws detailed exceptions on failure.

  • Expected set is provided in the error object.
  • No automatic recovery, but errors are very clear.
// peggy: Error handling
try {
  parser.parse("1 +");
} catch (e) {
  console.log("Expected:", e.expected);
}

βš™οΈ Runtime & Integration

chevrotain produces a Visitor pattern for walking the tree.

  • You define a visitor class to process the Concrete Syntax Tree (CST).
  • Separates parsing logic from business logic cleanly.
// chevrotain: Visitor
class MyVisitor extends CstVisitor {
  visitAdd(ctx) { /*...*/ }
}
const visitor = new MyVisitor();
parser.parse(input);
visitor.visit(parser CST);

jison embeds actions directly in grammar rules.

  • Logic is mixed with grammar definition.
  • Can become hard to maintain for large languages.
// jison: Embedded actions
"expression": [["expression", "+", "term", "$$ = $1 + $3;"]]

nearley uses postprocessors or embedded JS.

  • Can attach functions to rules to transform output.
  • Flexible but requires managing the compilation step.
// nearley: Postprocessor
expression -> expression "+" term {% (d) => d[0] + d[2] %}

peggy embeds actions directly in rules (like Jison but cleaner).

  • Returns values directly from rules.
  • Very intuitive for transforming data during parsing.
// peggy: Embedded actions
expression = head:term "+" tail:term { return head + tail; }

πŸ“… Maintenance & Ecosystem

chevrotain is actively maintained.

  • Regular updates and strong TypeScript support.
  • Large community in the Java/JS compiler space.
  • Recommended for long-term projects.

jison is largely legacy.

  • Low activity on GitHub and npm.
  • Many users have migrated to chevrotain or peggy.
  • Avoid for new development unless maintaining old code.

nearley is stable but niche.

  • Maintenance is sporadic.
  • Great for specific use cases (ambiguous grammars) but less general purpose.

peggy is actively maintained.

  • It is the successor to pegjs (which is in maintenance mode).
  • Strong community support and regular releases.
  • Safe choice for new PEG-based projects.

πŸ“Š Summary: Key Differences

Featurechevrotainjisonnearleypeggy
Grammar StyleπŸ’» JavaScript ObjectsπŸ“„ JSON / YaccπŸ“„ Compact .neπŸ“„ PEG .peggy
Error Recoveryβœ… Advanced Recovery❌ Fail Fast⚠️ Ambiguity Support❌ Fail Fast
Integration🧱 Visitor PatternπŸ”€ Embedded ActionsπŸ”€ PostprocessorsπŸ”€ Embedded Actions
Maintenance🟒 ActiveπŸ”΄ Legacy🟑 Stable / Niche🟒 Active
Best For🏒 Enterprise / IDEsπŸ•°οΈ Legacy SystemsπŸ” Ambiguous GrammarsπŸš€ General DSLs

πŸ’‘ The Big Picture

chevrotain is like a professional workshop πŸ› οΈ β€” built for heavy-duty tasks where control, error handling, and performance matter most. Use it for IDEs, complex compilers, or when you need to keep parsing even when errors occur.

jison is like an old reliable tool πŸ”§ β€” still works, but parts are hard to find. Only use it if you are maintaining an existing system that depends on it.

nearley is like a sketchpad πŸ““ β€” perfect for quick ideas and ambiguous structures. Great for research or small config languages where brevity is key.

peggy is like a modern Swiss Army knife πŸ”ͺ β€” balanced, sharp, and ready for most tasks. It is the default choice for new projects needing a PEG parser with active support.

Final Thought: All four tools can parse text, but they shape how you think about your language. chevrotain gives you control. peggy gives you clarity. nearley gives you flexibility. jison gives you history. Choose based on how much control you need versus how quickly you want to define rules.

How to Choose: chevrotain vs nearley vs peggy vs jison

  • chevrotain:

    Choose chevrotain for enterprise-grade parsers requiring robust error recovery, custom validation, and high performance. It is ideal when you need full control over the parsing process without separate grammar files, and when debugging parse errors is a priority.

  • nearley:

    Choose nearley when you need a concise grammar syntax and support for ambiguous grammars. It is suitable for smaller DSLs or configuration files where grammar readability and compactness are more important than raw speed.

  • peggy:

    Choose peggy for most new projects requiring a PEG-based parser. It offers a great balance of readability, performance, and active maintenance. It is perfect for configuration languages, query languages, or any text format where the grammar should look like the data itself.

  • jison:

    Choose jison only for maintaining legacy systems or when you specifically need an LALR(1) parser compatible with Yacc/Bison traditions. It is not recommended for new projects due to lower maintenance activity compared to modern alternatives.

README for chevrotain

Chevrotain

For details see:

Install

Using npm:

npm install chevrotain

or using yarn:

yarn add chevrotain