typescript-parser-combinator
TypeScript icon, indicating that this package has built-in type declarations

0.5.4 • Public • Published

TypeScript Parser Combinator

A simple parser combinator in TypeScript.

It has been built with the sole aim of learning parser combinators and npm package management.

Getting Started

Consider the following

import { text, digits, takeUntil, char, unit, space, word, newLineToken, between } from 'typescript-parser-combinator'

let input = "Weather (today): Sunny\r\n" +
            "Weather (yesterday): Cloudy\r\n" +
            "Weather (one week ago): Rainy"

let parser = text('Weather')
                .then(space)
                .then(between(text('('),text(')')))            .take()
                .then(text(': '))                              .drop()
                .then(word)
                .then(newLineToken.maybe())                    .drop()
                .map(([date, weather]) => ({ date, weather }))
                .many1()

let result = parser(input)

if (result.success) {
    expect(result.value[0].date).to.equal('today')
    expect(result.value[0].weather).to.equal('Sunny')
    expect(result.value[1].date).to.equal('yesterday')
    expect(result.value[1].weather).to.equal('Cloudy')
    expect(result.value[2].date).to.equal('one week ago')
    expect(result.value[2].weather).to.equal('Rainy')
}

Dependencies (1)

Dev Dependencies (8)

Package Sidebar

Install

npm i typescript-parser-combinator

Weekly Downloads

5

Version

0.5.4

License

ISC

Unpacked Size

14.9 kB

Total Files

4

Last publish

Collaborators

  • mal30