{"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.","language":"javascript","status":"active","last_verified":"Sat Apr 25","install":{"commands":["npm install parse-statements"],"cli":null},"imports":["import { parseStatements } from 'parse-statements'","import { StatementParser } from 'parse-statements'","import type { ParseOptions } from 'parse-statements'"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}