Registry / serialization / parse-statements

parse-statements

JSON →
library1.0.12jsnpmunverified

Versatile parser for extracting non-overlapping statements from source code in any programming language. Current stable version is 1.0.12 (released October 2022), with no updates since. It enables definition of statement patterns as sequences of token strings, which are internally compiled to regular expressions with gmu flags. Comments can be defined separately and can appear between tokens. The parser supports custom callbacks for parse success, errors, and comments. Callbacks can return a custom end index for each statement, allowing manual extension or truncation. It ships TypeScript type definitions and has zero dependencies. Compared to full AST parsers, this is lightweight and language-agnostic, but requires manual token pattern definition and does not produce an abstract syntax tree.

npm install parse-statements
INSTALL
IMPORT
SIG · PARSE-STATEMENTS
P
parse-statements
serializationjavascriptv1.0.12
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.

parseStatements
import { parseStatements } from 'parse-statements'
const parseStatements = require('parse-statements')
Module is ESM-only; CJS require will fail or require default export handling.
StatementParser
import { StatementParser } from 'parse-statements'
TypeScript type for the parser instance, useful for advanced usage.
ParseOptions
import type { ParseOptions } from 'parse-statements'
Type-only import for configuration object.

Parses function and const declarations from JavaScript-like code using custom token patterns and callbacks.

import { parseStatements } from 'parse-statements'; const code = `function hello() { console.log('Hello, world!'); } const x = 42;`; const parser = parseStatements({ statements: [ { tokens: ['function', 'identifier', 'parens', 'parens', 'block'], onParse: (ctx, src, tokens) => { console.log('Found function:', tokens[1].value); }, onError: (ctx, src, tokens) => { console.log('Incomplete function'); } }, { tokens: ['const', 'identifier', '=', 'value', ';'], onParse: (ctx, src, tokens) => { console.log('Found const:', tokens[1].value); } } ], tokens: { identifier: /[a-zA-Z_$][a-zA-Z0-9_$]*/, parens: /[\(\)]/, block: /\{[^}]*\}/, value: /[^;]+/, ';': ';' }, comments: { line: /\/\/.*/ } }); parser.parse(code);
Debug
Known issues
breakingparse-statements is ESM-only; using require() will throw an error.
fix
Use import syntax or dynamic import().
affects: >=1.0.0
gotchaToken strings are used to create regexps with gmu flags by default; double-escape backslashes.
fix
Use \\ for literal backslashes in token patterns.
affects: >=1.0.0
gotchaStatements cannot overlap; if tokens match overlapping regions, only the first match is captured.
fix
Ensure token patterns are mutually exclusive or order them intentionally.
affects: >=1.0.0
gotchaCustom end index in onParse callback does not automatically parse comments in the extended region.
fix
Manually invoke comment parsing for the extended segment.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: parseStatements is not a function
Using default import instead of named import (import parseStatements from 'parse-statements')
fix
Use named import: import { parseStatements } from 'parse-statements'
SyntaxError: Unexpected token 'export'
Using require() to load ESM-only module.
fix
Switch to ESM: add 'type': 'module' in package.json or use import syntax.
Error: Token ';' is not defined
Semicolon token defined as regexp in tokens but used as literal string in statement tokens array.
fix
Define a token entry for ';' or use a regexp pattern directly in the tokens array.
Upgrade
Version history
1.0.12latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
parse-statements — npm install parse-statements · libregistry