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-statementsVerified import paths — ran on the pinned version, not inferred.
Parses function and const declarations from JavaScript-like code using custom token patterns and callbacks.
Use import syntax or dynamic import().
Use \\ for literal backslashes in token patterns.
Ensure token patterns are mutually exclusive or order them intentionally.
Manually invoke comment parsing for the extended segment.
Use named import: import { parseStatements } from 'parse-statements'Switch to ESM: add 'type': 'module' in package.json or use import syntax.
Define a token entry for ';' or use a regexp pattern directly in the tokens array.
No dependency data recorded yet.