peberminta
peberminta is a simple, transparent, and highly generic parser combinator toolkit for TypeScript/JavaScript, currently at version 0.10.0 and appearing actively maintained. Its core design principle is to be agnostic to token types, options, and output, making it suitable for 'weird things with parsers' where traditional string-based parsers fall short. It is lightweight with zero dependencies and provides a comprehensive set of building blocks, enabling users to easily define custom parsing logic. A key differentiator is its emphasis on transparency, allowing full access to the parser state, and its opinionated stance on input, accepting only fixed arrays of tokens rather than streams. It does not include a built-in lexer, requiring users to supply their own. The library prioritizes practicality and clear typing, offering a robust foundation for complex parsing tasks without imposing strict constraints on input structures.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/peberminta/dist/index.js from ... not supported.
cause Attempting to import peberminta using `require()` in a CommonJS module, but the package is an ES Module.fixConvert your project or the specific file to an ES Module by adding `"type": "module"` to your `package.json` or by changing the file extension to `.mjs`. Then use `import * as p from 'peberminta';`. -
Argument of type '{ tokens: string[]; offset: number; }' is not assignable to parameter of type 'PState<unknown>'cause Mismatch in the generic type parameters for `PState` (Parser State) or `Parser` functions, often when the token type is not explicitly handled or inferred correctly.fixEnsure that the `tokens` array in your `PState` matches the expected token type of your parser. If your parser expects `char` tokens (strings of length 1), make sure your `tokens` array contains `string[]`. Explicitly typing `PState<YourTokenType>` can help resolve ambiguities. -
Type 'Fail' is not assignable to type 'Ok<TOutput, TToken>'
cause Directly trying to access `value` or other properties on a `ParseResult` without checking its `type` property, which could be 'Fail' if parsing fails.fixAlways check the `type` property of the `ParseResult` object. Use a conditional check like `if (result.type === 'Ok') { /* access result.value */ } else { /* handle error */ }`. -
No overload matches this call.
cause Incorrect number or type of arguments passed to a parser combinator function, or a combinator expects a different parser signature than provided.fixReview the documentation and type definitions for the specific combinator function. Ensure all required arguments are present and their types (especially the generic `Parser<TOutput, TToken>`) match the expected signature. For example, `p.seq` expects an array of parsers.
Warnings
- gotcha peberminta does not support streaming input; it expects a complete, fixed array of tokens. Users must tokenize their entire input upfront.
- breaking As a pre-1.0 package (current version 0.10.0), API stability is not guaranteed. Breaking changes may occur in minor versions without deprecation cycles.
- gotcha The core `peberminta` library is token-agnostic and does not provide an integrated lexer/tokenizer. Users must 'bring their own' lexer to convert raw input (e.g., strings) into a token array.
- gotcha peberminta is written with TypeScript and modern `import` syntax, suggesting a primary focus on ESM. While not explicitly stated as 'ESM-only', using `require()` in CommonJS environments might lead to import errors.
Install
-
npm install peberminta -
yarn add peberminta -
pnpm add peberminta
Imports
- p
const p = require('peberminta');import * as p from 'peberminta';
- pc
const pc = require('peberminta/char');import * as pc from 'peberminta/char';
- Parser
import type { Parser } from 'peberminta';
Quickstart
import * as p from 'peberminta';
import * as pc from 'peberminta/char';
// Define a parser for a single hexadecimal digit (0-9, a-f, A-F)
const hexDigit = p.choice(
pc.digit, // 0-9
pc.charIn('abcdef'), // a-f
pc.charIn('ABCDEF') // A-F
);
// Define a parser for a hex color code, e.g., #RRGGBB
const hexColorParser = p.seq(
pc.char('#'),
p.repeat(hexDigit, { min: 6, max: 6 })
);
// Function to parse and format the result
const parseHexColor = (input: string) => {
const tokens = input.split(''); // Simple tokenizer for char-based parsing
const result = hexColorParser({ tokens, offset: 0 });
if (result.type === 'Fail') {
return `Failed to parse: ${result.kind} at offset ${result.offset}`;
} else {
const hexChars = result.value[1].join(''); // Extract the hex digits
return `Successfully parsed hex color: #${hexChars}`;
}
};
console.log(parseHexColor('#AABBCC'));
console.log(parseHexColor('#123xyz'));
console.log(parseHexColor('ABCDEF'));