Registry / serialization / fastparse

fastparse

JSON →
library1.1.2jsnpmunverified

The `fastparse` library, currently at version 1.1.2, offers a very simple and efficient state machine-based parser driven entirely by regular expressions. It's designed for lightweight text processing tasks, not complex language parsing, and excels at extracting specific patterns from structured text or simple domain-specific languages. The library's core mechanism involves compiling a user-defined state machine description into optimized regular expressions for each state, leveraging the native JavaScript regex engine for high-performance pattern matching. This approach makes it exceptionally fast for its intended use cases. While its release cadence appears slow, suggesting a stable and mature project rather than one with frequent new feature development, its explicit simplicity is a key differentiator, focusing on minimal overhead and direct regex delegation. This makes it an ideal choice when a full-fledged AST-generating parser is overkill, and targeted extraction of information is the primary goal.

npm install fastparse
INSTALL
IMPORT
SIG · FASTPARSE
F
fastparse
serializationjavascriptv1.1.2
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 'fastparse';
import { Parser } from 'fastparse';
The library primarily exports `Parser` as a default export, though older bundlers might handle CommonJS `require` differently.
Parser
const Parser = require('fastparse');
CommonJS usage as shown in the original documentation example.
ParserInstance
const parser = new Parser(description);
const parser = Parser(description);
`Parser` is a class and must be instantiated with `new`.

Demonstrates how to define a state machine using regular expressions and transition functions to extract license information from a JavaScript source code string.

const Parser = require("fastparse"); // A simple parser that extracts @licence ... from comments in a JS file const parser = new Parser({ // The "source" state "source": { // matches comment start "/\\*": "comment", "//": "linecomment" }, // The "comment" state "comment": { "\\*/": "source", "@licen[cs]e\\s((?:[^*/\n]|\\*+[^*/\n])*)?": function(match, licenseText) { this.licences.push(licenseText ? licenseText.trim() : ''); } }, // The "linecomment" state "linecomment": { "\n": "source", "@licen[cs]e\\s(.*)": function(match, licenseText) { this.licences.push(licenseText.trim()); } } }); const sourceCode = ` /* @license MIT */ // @licence Apache const x = 1; /* Some other comment * @license BSD */ `; const licences = parser.parse("source", sourceCode, { licences: [] }).licences; console.log(licences); // Expected output: [ 'MIT', 'Apache', 'BSD' ]
Debug
Known issues
breakingThe parser's matching order for regular expressions within a state historically relied on the JavaScript `description` object's key order being preserved. While modern JavaScript engines (ES2015+) generally guarantee insertion order for string keys, reliance on this implicit behavior in older environments or with non-compliant engines could lead to non-deterministic parsing behavior if regex patterns overlap.
fix
Ensure regex patterns within a state are mutually exclusive where possible, or explicitly order them such that the most specific patterns appear first, acknowledging the potential for engine-specific behavior if order preservation is not guaranteed.
affects: <=1.1.2
gotchaIf multiple regular expressions within a single state can match the same part of the input string, the one defined earlier in the state object takes precedence. Incorrect ordering can lead to unintended parsing paths, incomplete matches, or infinite loops if no transition is made.
fix
Carefully design and test your state machine. Place more specific or restrictive regular expressions before more general ones within the same state definition to ensure correct matching precedence.
affects: >=1.0.0
gotcha`fastparse` delegates parsing to native JavaScript regular expressions. While efficient, using overly complex or backtracking-prone regex patterns (e.g., `(a+)*`) can lead to exponential time complexity, causing performance bottlenecks or potential ReDoS (Regular Expression Denial of Service) vulnerabilities, especially when processing untrusted input.
fix
Simplify regular expressions where possible. Avoid nested quantifiers and excessive backtracking. Thoroughly test regex performance with various input sizes and edge cases, especially if patterns are derived from external sources.
affects: >=1.0.0
gotchaThe `context` object passed to `parser.parse()` is directly accessible as `this` within transition functions, allowing for stateful modifications. While powerful, this mutable shared context can introduce side effects and make debugging challenging if not managed carefully, especially in complex parsers.
fix
Document the expected properties and modifications for the `context` object. Consider defensive programming practices, such as deep cloning sensitive parts of the context if immutability is desired for certain operations.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Parser is not a constructor
Attempting to instantiate `Parser` without the `new` keyword, or an incorrect module import pattern (e.g., using named import for a default export, or CommonJS `require` with `new` on the module object itself).
fix
Ensure you are using `new Parser(...)` for instantiation. If using ES modules, `import Parser from 'fastparse';`. If using CommonJS, `const Parser = require('fastparse');`.
TypeError: Cannot read properties of undefined (reading 'push')
A transition function attempts to access or modify a property on the `this` (context) object that was not initialized or provided in the `context` argument to `parser.parse()`.
fix
Verify that the initial `context` object passed to `parser.parse(initialState, parsedString, context)` contains all the properties your transition functions expect to use, e.g., `{ licences: [] }`.
SyntaxError: Invalid regular expression: /.../: Nothing to repeat
One of the regular expressions defined in your state machine description is syntactically incorrect, often due to unescaped special characters (e.g., `.` `*` `+` `?` `(` `)` `[` `]` `{` `}` `|` `^` `$` `\` `/`) when they are intended to be literal characters.
fix
Review the problematic regular expression in your state definition. Escape all special regex characters with a double backslash (`\\`) if you intend for them to be matched literally in the input string.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
fastparse — npm install fastparse · libregistry