Skip to content

ka-fuachie/live-parser

Repository files navigation

live-parser

live-parser is a JavaScript parser combinator library designed for streaming data inputs. It is based on packrat parsing which ensure parsing with linear time complexity.

  • Incremental parsing without waiting for full input.
  • Build complex parsers from simple composable primitives.
  • Fully type safe

Install

npm install live-parser

Quick Start

import { literal, charFrom, oneOf, sequenceOf, zeroOrMany } from "live-parser";

const join = (sep: string) => (values: string[]) => values.join(sep);

const zero = literal("0");
const nonZeroDigit = charFrom("1-9");
const digit = oneOf(zero, nonZeroDigit);

const positiveInteger = sequenceOf(nonZeroDigit, zeroOrMany(digit).map(join(""))).map(join(""));
const negativeInteger = sequenceOf(literal("-"), positiveInteger).map(join(""));

const integer = oneOf(zero, positiveInteger, negativeInteger).map(Number);

// Parse a string
const result = integer.parseString("-123");
console.log(result);
// {
//     status: "success",
//     offset: 4,
//     value: -123
//     error: null
// }

Acknowledgements

This library was inspired by the following projects and resources:

License

MIT

About

Type-safe streaming parser combinator library for JavaScript

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors