1.1.11 • Published 3 years ago

combinator-node v1.1.11

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Combinator

Provides combinator functions.

Use in combination. For example as Parser.

Example

First, Define the small combinator units.

import Combinator from "combinator";

type Context<Src> = {
  src: Src;
  offset: number;
};
type Parser = Combinator<Context<string>, string>;

const any: Parser = (context) => {
  const [head, ...tails] = context.src;
  if (!head) return Combinator.err(context, "any: no more sources.");
  const next = { src: tails.join(""), offset: context.offset + 1 };
  return Combinator.ok(next, head);
};

const same = (char: string): Parser =>
  (context) => {
    const result = any(context);
    if (!result.ok) return result;
    if (result.get !== char) {
      return Combinator.err(context, `same: ${result.get} should be ${char}`);
    }
    return result;
  };

Next, Use combinator functions.

import { chain, convert, not, option, repeat } from "combinator";

const eol = same("\n");
const notEol = convert(chain(not(eol), any), ([, it]) => it);
const line = convert(chain(repeat(notEol), option(eol)), ([it]) => it.join(""));
const lines = repeat(line);

const parse = (src: string) => lines({ src, offset: 0 });
const text = "line1\nline2\nline3";

console.dir(parse(text), { depth: Number.MAX_VALUE });
1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.6

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.0

3 years ago