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.
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.
chevrotain defines grammars using JavaScript objects and classes.
// 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.
// 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).
// 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.
// 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");
chevrotain excels at error recovery.
// 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.
// jison: Error handling
try {
parser.parse("1 +");
} catch (e) {
console.log("Parse failed:", e.message);
}
nearley throws on ambiguity or syntax errors.
// nearley: Error handling
try {
parser.feed("invalid");
} catch (e) {
console.log("Syntax error");
}
peggy throws detailed exceptions on failure.
// peggy: Error handling
try {
parser.parse("1 +");
} catch (e) {
console.log("Expected:", e.expected);
}
chevrotain produces a Visitor pattern for walking the tree.
// 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.
// jison: Embedded actions
"expression": [["expression", "+", "term", "$$ = $1 + $3;"]]
nearley uses postprocessors or embedded JS.
// nearley: Postprocessor
expression -> expression "+" term {% (d) => d[0] + d[2] %}
peggy embeds actions directly in rules (like Jison but cleaner).
// peggy: Embedded actions
expression = head:term "+" tail:term { return head + tail; }
chevrotain is actively maintained.
jison is largely legacy.
chevrotain or peggy.nearley is stable but niche.
peggy is actively maintained.
pegjs (which is in maintenance mode).| Feature | chevrotain | jison | nearley | peggy |
|---|---|---|---|---|
| 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 |
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.
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.
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.
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.
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.