Registry / serialization / pratt-parser

pratt-parser

JSON →
library12.0.6jsnpmunverified

`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.

npm install pratt-parser
INSTALL
IMPORT
SIG · PRATT-PARSER
P
pratt-parser
serializationjavascriptv12.0.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
import { Parser } from 'pratt-parser'
const Parser = require('pratt-parser').Parser
The library is exclusively distributed as an ES Module (ESM). Direct CommonJS `require()` calls will result in import errors in Node.js environments configured for ESM.
WhiteSpaceToken
import { WhiteSpaceToken } from 'pratt-parser'
import WhiteSpaceToken from 'pratt-parser/tokens/WhiteSpaceToken'
Common utility tokens like `WhiteSpaceToken` and `NumberToken` are named exports from the main `pratt-parser` package, not individual file paths.
Tokenizer
import { Tokenizer } from 'pratt-parser'
const { Tokenizer } = require('pratt-parser')
The `Tokenizer` class is used for low-level token stream management and is also an ESM-only named export.

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.

import { Parser, WhiteSpaceToken, NumberToken } from "pratt-parser"; // Helper function to create value objects for AST nodes function Value(value) { return Object.create(null, { value: { value: value } }); } // Define the grammar for arithmetic expressions const myGrammar = new Parser({ // Specify base tokens for parsing tokens: [WhiteSpaceToken, NumberToken], // Define prefix operators prefix: { "(": { nud(grammar) { // Null Denotation: parse expression inside parentheses const e = grammar.expression(0); grammar.advance(")"); // Expect closing parenthesis return e; } } }, // Define infix operators and their precedence infix: { ")": {}, // Right parenthesis acts as a terminator, no operation associated "+": { precedence: 50, combine: (left, right) => Value(left.value + right.value) // Addition operation }, "-": { precedence: 50, combine: (left, right) => Value(left.value - right.value) // Subtraction operation }, "*": { precedence: 60, // Higher precedence than + or - combine: (left, right) => Value(left.value * right.value) // Multiplication operation }, "/": { precedence: 60, // Higher precedence than + or - combine: (left, right) => Value(left.value / right.value) // Division operation } } }); // Example expression to parse and evaluate const expression = "(1 + (1 + 4 * 3)) * (2 + 1)"; const result = myGrammar.parse(expression).value; // Output the evaluated result console.log(`Parsed expression: "${expression}"`); console.log(`Evaluated result: ${result}`); // Expected output: 42
Debug
Known issues
breakingThe primary parsing method on the `Parser` class was renamed from `parseString` to `parse`. Attempting to call `parseString` in versions 12.0.0 or later will result in a `TypeError`.
fix
Update all calls from `parserInstance.parseString(input)` to `parserInstance.parse(input)`.
affects: >=12.0.0
breakingBeginning with version 11.0.0, `pratt-parser` explicitly requires Node.js version 22.15.0 or higher. Running the package on older Node.js environments will lead to runtime errors or module resolution failures.
fix
Upgrade your Node.js installation to version 22.15.0 or newer. Tools like `nvm` (Node Version Manager) can simplify switching between Node.js versions.
affects: >=11.0.0
gotchaThe `pratt-parser` library is published as an ES Module (ESM). Direct `require()` statements in CommonJS modules will fail with errors such as `ReferenceError: require is not defined` or `ERR_REQUIRE_ESM`.
fix
Ensure your project's `package.json` includes `"type": "module"` for ESM compatibility, and use `import` statements. If a mixed CommonJS/ESM setup is unavoidable, use dynamic `import('pratt-parser')` within CommonJS.
affects: >=11.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined in ES module scope
You are attempting to import `pratt-parser` using CommonJS `require()` syntax in a file that is treated as an ES Module (or `pratt-parser` itself is an ESM package).
fix
Change your import statement to `import { Parser } from 'pratt-parser';` and ensure your project's `package.json` has `"type": "module"` set for your entry files, or that you are in an ESM context.
TypeError: parserInstance.parseString is not a function
Your code is calling the deprecated `parseString` method, which was renamed to `parse` in version 12.0.0.
fix
Replace all occurrences of `parserInstance.parseString(source)` with `parserInstance.parse(source)`.
Error: The 'pratt-parser' package requires Node.js version 22.15.0 or higher.
The `engines` field in `pratt-parser`'s `package.json` specifies a minimum Node.js version (22.15.0). Your current Node.js version is too old.
fix
Upgrade your Node.js environment to version 22.15.0 or newer. You might need to use `nvm install 22` or update your Node.js distribution.
Upgrade
Version history
12.0.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
pratt-parser — npm install pratt-parser · libregistry