{"library":"nearley","title":"nearley Parser Toolkit","description":"nearley is a comprehensive parsing toolkit for JavaScript that enables developers to define and parse custom languages. It utilizes the Earley parsing algorithm, making it capable of handling any context-free grammar, including notoriously difficult cases like left recursion, which often trip up other parser generators like PEG.js or Jison. The current stable version is 2.20.1, with releases synchronized to Zenodo for academic citation. Key differentiators include its streaming capabilities, graceful error handling, support for ambiguous grammars by providing all possible parsings, and compatibility with various lexers (e.g., moo). It also provides a robust toolchain for testing, railroad diagrams, and fuzzers, and works seamlessly in both Node.js and browser environments.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install nearley"],"cli":{"name":"nearleyc","version":null}},"imports":["import { Grammar } from 'nearley';","import { Parser } from 'nearley';","npx nearleyc mygrammar.ne -o mygrammar.js"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const nearley = require('nearley');\nconst fs = require('fs');\nconst path = require('path');\n\n// 1. Define your grammar in a .ne file or directly as a JS object\n// For this example, let's create a simple grammar dynamically\nconst grammarSource = `\n@{% function id(x) {return x[0];} %}\nstart -> expression:id\nexpression -> number:id (operation number):id* {% function(data) {\n  let result = data[0];\n  for (let i = 0; i < data[1].length; i++) {\n    const op = data[1][i][0];\n    const num = data[1][i][1];\n    if (op === '+') result += num;\n    if (op === '-') result -= num;\n  }\n  return result;\n} %}\nnumber -> [0-9]:+ {% function(data) { return parseInt(data.join('')); } %}\noperation -> '+' | '-'\n`;\n\n// In a real scenario, you'd compile this via CLI:\n// npx nearleyc mygrammar.ne -o mygrammar.js\n// Then load it: const grammar = require('./mygrammar.js');\n\n// For a quickstart, we'll compile it in-memory (requires nearley compiler)\nconst compile = require('nearley/lib/compile');\nconst generate = require('nearley/lib/generate');\nconst nearleyGrammar = require('nearley/lib/nearley-language-grammar');\n\nconst compiledGrammar = compile(nearley.Grammar.fromCompiled(nearleyGrammar), grammarSource);\nconst grammar = nearley.Grammar.fromCompiled(eval(generate(compiledGrammar, {}))); // Eval is risky, avoid in production\n\n// 2. Create a Parser instance\nconst parser = new nearley.Parser(grammar);\n\n// 3. Feed input to the parser\ntry {\n  parser.feed('10+20-5');\n  console.log('Parsed results:', parser.results); // Should output [25]\n  parser.feed('3*4'); // This will fail as '*' is not in grammar\n} catch (error) {\n  console.error('Parsing error:', error.message);\n}\n\n// Example of parsing another valid input\nconst parser2 = new nearley.Parser(grammar);\nparser2.feed('1+2+3-1');\nconsole.log('Another result:', parser2.results); // Should output [5]\n","lang":"javascript","description":"This quickstart defines a simple arithmetic grammar, compiles it dynamically (for demonstration), and then uses the nearley Parser to parse an input string and print the result. It also demonstrates error handling for invalid input.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}