simple-parser

raw JSON →
0.0.0 verified Sat Apr 25 auth: no javascript

A lightweight, minimalistic parser for simple grammars, currently at version 0.0.0. It aims to provide a straightforward API for tokenizing and parsing input strings with user-defined rules. Unlike more complex parser generators (e.g., ANTLR, PEG.js), simple-parser focuses on simplicity and zero dependencies, making it suitable for small projects or educational use. The release cadence is not yet established as it is in early development. Key differentiators include a tiny footprint and an intuitive rule definition syntax.

error TypeError: Parser is not a constructor
cause Importing incorrectly (CommonJS usage).
fix
Use import Parser from 'simple-parser' (ESM).
error ReferenceError: Token is not defined
cause Attempting to use Token as a runtime value when it is a type-only export.
fix
Remove Token from runtime imports; it's only for TypeScript types.
error Error: Rule 'xxx' is not a string or array
cause Passing a string to define() instead of an array of token names.
fix
Change parser.define('rule', 'TOKEN') to parser.define('rule', ['TOKEN']).
deprecated Method `parseAll` has been deprecated in favor of `parse`.
fix Replace `parser.parseAll(input)` with `parser.parse(input)`.
gotcha Rules are defined with arrays, not strings; using strings silently fails.
fix Always use array syntax: `parser.define('ruleName', ['TOKEN1', 'TOKEN2'])`.
gotcha Parser does not support regex flags like 'g' or 'i' in token definitions.
fix Use simple regex patterns without flags; pre-transform input if case-insensitivity needed.
npm install simple-parser
yarn add simple-parser
pnpm add simple-parser

Creates a parser for simple arithmetic expressions using digit and plus rules, then parses a string.

import Parser from 'simple-parser';

const parser = new Parser();
parser.define('DIGIT', /[0-9]/);
parser.define('PLUS', /\+/);
parser.define('expr', ['DIGIT', 'PLUS', 'DIGIT']);

const result = parser.parse('3+5');
console.log(result); // ['3', '+', '5']