{"library":"pratt-parser","title":"Pratt Parser","description":"`pratt-parser` is a JavaScript/TypeScript library that implements a Top Down Operator Precedence (TDOP) parser, commonly known as a Pratt parser. It draws inspiration from both the general TDOP concept and specific implementations like Douglas Crockford's TDOP work. The library enables developers to construct custom parsers for various syntaxes, such as mathematical expressions or domain-specific languages, by defining tokens, null denotation (nud) functions for prefix operators, and left denotation (led) functions for infix operators, along with their respective precedences. The current stable version is 12.0.6, indicating active maintenance and frequent, albeit minor, updates. Major versions introduce breaking changes primarily related to API refinement and Node.js compatibility. It provides comprehensive TypeScript type definitions, making it suitable for modern TypeScript projects seeking a robust and extensible parsing solution.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install pratt-parser"],"cli":null},"imports":["import { Parser } from 'pratt-parser'","import { WhiteSpaceToken } from 'pratt-parser'","import { Tokenizer } from 'pratt-parser'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Parser, WhiteSpaceToken, NumberToken } from \"pratt-parser\";\n\n// Helper function to create value objects for AST nodes\nfunction Value(value) {\n  return Object.create(null, {\n    value: {\n      value: value\n    }\n  });\n}\n\n// Define the grammar for arithmetic expressions\nconst myGrammar = new Parser({\n  // Specify base tokens for parsing\n  tokens: [WhiteSpaceToken, NumberToken],\n  // Define prefix operators\n  prefix: {\n    \"(\": {\n      nud(grammar) { // Null Denotation: parse expression inside parentheses\n        const e = grammar.expression(0);\n        grammar.advance(\")\"); // Expect closing parenthesis\n        return e;\n      }\n    }\n  },\n  // Define infix operators and their precedence\n  infix: {\n    \")\": {}, // Right parenthesis acts as a terminator, no operation associated\n    \"+\": {\n      precedence: 50,\n      combine: (left, right) => Value(left.value + right.value) // Addition operation\n    },\n    \"-\": {\n      precedence: 50,\n      combine: (left, right) => Value(left.value - right.value) // Subtraction operation\n    },\n    \"*\": {\n      precedence: 60, // Higher precedence than + or -\n      combine: (left, right) => Value(left.value * right.value) // Multiplication operation\n    },\n    \"/\": {\n      precedence: 60, // Higher precedence than + or -\n      combine: (left, right) => Value(left.value / right.value) // Division operation\n    }\n  }\n});\n\n// Example expression to parse and evaluate\nconst expression = \"(1 + (1 + 4 * 3)) * (2 + 1)\";\nconst result = myGrammar.parse(expression).value;\n\n// Output the evaluated result\nconsole.log(`Parsed expression: \"${expression}\"`);\nconsole.log(`Evaluated result: ${result}`); // Expected output: 42","lang":"javascript","description":"This code demonstrates how to define a basic arithmetic grammar using `pratt-parser`, including custom tokens, prefix, and infix operators, then parses and evaluates an example expression.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}