tparse
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

tparse

tparse is a PEG-like parser combinator for TypeScript and JavaScript.

Features

  • Statically typed
  • Parsing results are memoized for O(n) parsing

Install

npm install --save tparse

Example (parsing arithmetic expression)

import {Range, Parser, sequence, choose, string, regExp, lazy} from "tparse";
 
interface IntegerNode {
  value: number;
  range: Range;
}
 
interface BinOpNode {
  left: Node;
  op: string;
  right: Node;
}
 
type Node = IntegerNode | BinOpNode;
 
const _ = regExp(/[ \t\n\r]/).repeat();
 
const integer: Parser<IntegerNode> =
  regExp(/[0-9]/).repeat(1)
    .withRange()
    .thenSkip(_)
    .map(([chars, range]) => {
      return {value: parseInt(chars.join("")), range};
    });
 
function token(str: string) {
  return string(str).thenSkip(_);
}
 
const factor: Parser<Node> =
  choose(
    integer,
    token("(")
      .thenTake(lazy(() => expr))
      .thenSkip(token(")"))
  )
 
function buildTree([first, rest]: [Node, [string, Node][]]) {
  let left = first;
  for (const [op, right] of rest) {
    left = {left, op, right};
  }
  return left;
}
 
const term: Parser<Node> =
  sequence(
    factor,
    _.thenTake(
      sequence(
        _.thenTake(choose(string("*"), string("/"))),
        _.thenTake(factor)
      ).repeat()
    )
  )
    .map(buildTree);
 
const expr: Parser<Node> =
  sequence(
    term,
    _.thenTake(
      sequence(
        _.thenTake(choose(string("+"), string("-"))),
        _.thenTake(term)
      ).repeat()
    )
  )
    .map(buildTree);
 
const parser = _.thenTake(expr);
parser.parse("test.txt", "1 + (2 * 3)");
 

Result

{
  "left": {
    "value": 1,
    "range": {
      "begin": {
        "filePath": "test.txt",
        "index": 0,
        "line": 1,
        "column": 1
      },
      "end": {
        "filePath": "test.txt",
        "index": 1,
        "line": 1,
        "column": 2
      }
    }
  },
  "op": "+",
  "right": {
    "left": {
      "value": 2,
      "range": ...
    },
    "op": "*",
    "right": {
      "value": 3,
      "range": ...
    }
  }
}

Todo

  • Performance comparison / tuning

Readme

Keywords

none

Package Sidebar

Install

npm i tparse

Weekly Downloads

1

Version

0.1.0

License

MIT

Last publish

Collaborators

  • seanchas116