{"library":"pegjs","title":"PEG.js Parser Generator","description":"PEG.js is a parser generator for JavaScript that produces fast parsers based on the Parsing Expression Grammar (PEG) formalism. It enables developers to define grammars for custom languages or complex data formats and generate a JavaScript parser function from them. The current stable version, 0.10.0, was released in August 2016. While the project's direct development on PEG.js itself has largely ceased, its successor, Peggy.js (npm: `peggy`), maintains API compatibility and active development. Key differentiators of PEG.js include its simple, expressive grammar syntax, excellent error reporting, and the ability to integrate both lexical and syntactical analysis into a single grammar. It can be used programmatically via a JavaScript API or through a command-line interface, generating parsers in multiple module formats like CommonJS (default), AMD, UMD, or global variables.","language":"javascript","status":"maintenance","last_verified":"Wed Apr 22","install":{"commands":["npm install pegjs"],"cli":{"name":"pegjs","version":null}},"imports":["const pegjs = require('pegjs');","const parser = pegjs.generate(grammarString, options);","const result = myParser.parse('input string');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const pegjs = require('pegjs');\n\n// Define a simple grammar for arithmetic expressions\nconst grammar = `\n  start = expression\n\n  expression = term (('+' / '-') term)* {\n    return arguments[0].reduce((acc, current) => {\n      const [op, val] = current;\n      return op === '+' ? acc + val : acc - val;\n    });\n  }\n\n  term = factor (('*' / '/') factor)* {\n    return arguments[0].reduce((acc, current) => {\n      const [op, val] = current;\n      return op === '*' ? acc * val : acc / val;\n    });\n  }\n\n  factor = number / '(' expression ')'\n\n  number = [0-9]+ {\n    return parseInt(text(), 10);\n  }\n\n  whitespace = [ \\t\\n\\r]*\n`;\n\n// Generate the parser\ntry {\n  const parser = pegjs.generate(grammar, { \n    output: 'parser', \n    format: 'commonjs', \n    optimize: 'speed' \n  });\n\n  // Use the generated parser\n  const input1 = \"10 + 5 * (2 - 1)\";\n  const result1 = parser.parse(input1);\n  console.log(`Input: \"${input1}\", Result: ${result1}`); // Expected: 15\n\n  const input2 = \"(100 / 2) - 15\";\n  const result2 = parser.parse(input2);\n  console.log(`Input: \"${input2}\", Result: ${result2}`); // Expected: 35\n\n  // Example of a parsing error\n  const invalidInput = \"10 + * 5\";\n  try {\n    parser.parse(invalidInput);\n  } catch (e) {\n    console.error(`Error parsing \"${invalidInput}\": ${e.message}`);\n  }\n\n} catch (e) {\n  console.error(\"Error generating parser: \", e.message);\n}","lang":"javascript","description":"This quickstart demonstrates how to use `pegjs` to define a simple arithmetic grammar, generate a parser at runtime, and then use that parser to evaluate expressions and catch parsing errors.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}