Skip to content

Commit

Permalink
feat(parse): add/update combinators
Browse files Browse the repository at this point in the history
- add startsWith, endsWith, entireLine, entirely
- add wrap()
- rename dalt/dseq => altD/seqD
  • Loading branch information
postspectacular committed Apr 19, 2020
1 parent 719b437 commit e4eab03
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/parse/src/combinators/alt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export const alt = <T>(parsers: Parser<T>[]): Parser<T> => (ctx) => {
return false;
};

export const dalt = <T>(parsers: Parser<T>[]) => discard(alt(parsers));
export const altD = <T>(parsers: Parser<T>[]) => discard(alt(parsers));
13 changes: 13 additions & 0 deletions packages/parse/src/combinators/boundary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Parser } from "../api";
import { inputEnd, inputStart, lineEnd, lineStart } from "../prims/anchor";
import { seq } from "./seq";

export const startsWith = <T>(parser: Parser<T>) => seq([inputStart, parser]);

export const endsWith = <T>(parser: Parser<T>) => seq([parser, inputEnd]);

export const entireLine = (parser: Parser<string>) =>
seq([lineStart, parser, lineEnd]);

export const entirely = <T>(parser: Parser<T>) =>
seq([inputStart, parser, inputEnd]);
2 changes: 1 addition & 1 deletion packages/parse/src/combinators/seq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export const seq = <T>(parsers: Parser<T>[], id = "seq"): Parser<T> => (
return ctx.end();
};

export const dseq = <T>(parsers: Parser<T>[]) => discard(seq(parsers));
export const seqD = <T>(parsers: Parser<T>[]) => discard(seq(parsers));
11 changes: 11 additions & 0 deletions packages/parse/src/combinators/wrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Parser } from "../api";
import { litD } from "../prims/lit";
import { hoist } from "../xform/hoist";
import { seq } from "./seq";

export const wrap = <T>(
parser: Parser<T>,
pre: T,
post: T = pre,
id = "wrap"
) => hoist(seq([litD(pre), parser, litD(post)], id));

0 comments on commit e4eab03

Please sign in to comment.