parse-statements
raw JSON →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.
Common errors
error TypeError: parseStatements is not a function ↓
error SyntaxError: Unexpected token 'export' ↓
error Error: Token ';' is not defined ↓
Warnings
breaking parse-statements is ESM-only; using require() will throw an error. ↓
gotcha Token strings are used to create regexps with gmu flags by default; double-escape backslashes. ↓
gotcha Statements cannot overlap; if tokens match overlapping regions, only the first match is captured. ↓
gotcha Custom end index in onParse callback does not automatically parse comments in the extended region. ↓
Install
npm install parse-statements yarn add parse-statements pnpm add parse-statements Imports
- parseStatements wrong
const parseStatements = require('parse-statements')correctimport { parseStatements } from 'parse-statements' - StatementParser
import { StatementParser } from 'parse-statements' - ParseOptions
import type { ParseOptions } from 'parse-statements'
Quickstart
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);