peberminta

0.10.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates defining and using basic parser combinators to parse a hexadecimal color string, including tokenization and result handling.

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'));

view raw JSON →