{"id":18651,"library":"parse-statements","title":"parse-statements","description":"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.","status":"active","version":"1.0.12","language":"javascript","source_language":"en","source_url":"https://github.com/joomcode/parse-statements","tags":["javascript","parse","parser","source","statement","typescript"],"install":[{"cmd":"npm install parse-statements","lang":"bash","label":"npm"},{"cmd":"yarn add parse-statements","lang":"bash","label":"yarn"},{"cmd":"pnpm add parse-statements","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Module is ESM-only; CJS require will fail or require default export handling.","wrong":"const parseStatements = require('parse-statements')","symbol":"parseStatements","correct":"import { parseStatements } from 'parse-statements'"},{"note":"TypeScript type for the parser instance, useful for advanced usage.","symbol":"StatementParser","correct":"import { StatementParser } from 'parse-statements'"},{"note":"Type-only import for configuration object.","symbol":"ParseOptions","correct":"import type { ParseOptions } from 'parse-statements'"}],"quickstart":{"code":"import { parseStatements } from 'parse-statements';\n\nconst code = `function hello() {\n  console.log('Hello, world!');\n}\nconst x = 42;`;\n\nconst parser = parseStatements({\n  statements: [\n    {\n      tokens: ['function', 'identifier', 'parens', 'parens', 'block'],\n      onParse: (ctx, src, tokens) => {\n        console.log('Found function:', tokens[1].value);\n      },\n      onError: (ctx, src, tokens) => {\n        console.log('Incomplete function');\n      }\n    },\n    {\n      tokens: ['const', 'identifier', '=', 'value', ';'],\n      onParse: (ctx, src, tokens) => {\n        console.log('Found const:', tokens[1].value);\n      }\n    }\n  ],\n  tokens: {\n    identifier: /[a-zA-Z_$][a-zA-Z0-9_$]*/,\n    parens: /[\\(\\)]/,\n    block: /\\{[^}]*\\}/,\n    value: /[^;]+/,\n    ';': ';'\n  },\n  comments: {\n    line: /\\/\\/.*/\n  }\n});\n\nparser.parse(code);","lang":"typescript","description":"Parses function and const declarations from JavaScript-like code using custom token patterns and callbacks."},"warnings":[{"fix":"Use import syntax or dynamic import().","message":"parse-statements is ESM-only; using require() will throw an error.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Use \\\\ for literal backslashes in token patterns.","message":"Token strings are used to create regexps with gmu flags by default; double-escape backslashes.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure token patterns are mutually exclusive or order them intentionally.","message":"Statements cannot overlap; if tokens match overlapping regions, only the first match is captured.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Manually invoke comment parsing for the extended segment.","message":"Custom end index in onParse callback does not automatically parse comments in the extended region.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use named import: import { parseStatements } from 'parse-statements'","cause":"Using default import instead of named import (import parseStatements from 'parse-statements')","error":"TypeError: parseStatements is not a function"},{"fix":"Switch to ESM: add 'type': 'module' in package.json or use import syntax.","cause":"Using require() to load ESM-only module.","error":"SyntaxError: Unexpected token 'export'"},{"fix":"Define a token entry for ';' or use a regexp pattern directly in the tokens array.","cause":"Semicolon token defined as regexp in tokens but used as literal string in statement tokens array.","error":"Error: Token ';' is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}