npm.io
0.5.4 • Published 6 years ago

typescript-parser-combinator

Licence
ISC
Version
0.5.4
Deps
1
Size
15 kB
Vulns
0
Weekly
0

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