Skip to content

Latest commit

 

History

History
60 lines (51 loc) · 1.21 KB

README.md

File metadata and controls

60 lines (51 loc) · 1.21 KB

parsing

A parser combinator library.

Usage Example

A simple calculator for addition and subtraction of natural numbers:

import either from '@matt.kantor/either'
import {
  lazy,
  literal,
  map,
  oneOf,
  oneOrMore,
  sequence,
  type Parser,
} from '@matt.kantor/parsing'

const operator = oneOf([literal('+'), literal('-')])

const number = map(
  oneOrMore(
    oneOf([
      literal('0'),
      literal('1'),
      literal('2'),
      literal('3'),
      literal('4'),
      literal('5'),
      literal('6'),
      literal('7'),
      literal('8'),
      literal('9'),
    ]),
  ),
  Number,
)

const compoundExpression = map(
  sequence([number, operator, lazy(() => expression)]),
  ([a, operator, b]) => {
    switch (operator) {
      case '+': return a + b
      case '-': return a - b
    }
  },
)

const expression: Parser<number> = oneOf([compoundExpression, number])

const evaluate = (input: string) =>
  either.flatMap(expression(input), ({ remainingInput, output }) =>
    remainingInput.length !== 0
      ? either.makeLeft('excess content followed valid input')
      : either.makeRight(output),
  )

console.log(evaluate('2+2-1').value) // logs "3"