Skip to content

Commit

Permalink
feat(parse): add discarding combinators, move discard
Browse files Browse the repository at this point in the history
- add repeatD, oneOrMoreD, zeroOrMoreD
  • Loading branch information
postspectacular committed Apr 20, 2020
1 parent 822fcba commit e09a2c4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/parse/src/combinators/alt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Parser } from "../api";
import { discard } from "./discard";
import { discard } from "../xform/discard";

export const alt = <T>(parsers: Parser<T>[]): Parser<T> => (ctx) => {
if (ctx.done) return false;
Expand Down
4 changes: 0 additions & 4 deletions packages/parse/src/combinators/discard.ts

This file was deleted.

10 changes: 10 additions & 0 deletions packages/parse/src/combinators/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Parser } from "../api";
import { discard } from "../xform/discard";

export const repeat = <T>(
parser: Parser<T>,
Expand Down Expand Up @@ -32,3 +33,12 @@ export const oneOrMore = <T>(
id = "repeat1",
max = Infinity
) => repeat(parser, 1, max, id);

export const repeatD = <T>(parser: Parser<T>, min: number, max: number) =>
discard(repeat(parser, min, max));

export const zeroOrMoreD = <T>(parser: Parser<T>, max = Infinity) =>
repeatD(parser, 0, max);

export const oneOrMoreD = <T>(parser: Parser<T>, max = Infinity) =>
repeatD(parser, 1, max);
2 changes: 1 addition & 1 deletion packages/parse/src/combinators/seq.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Parser } from "../api";
import { discard } from "./discard";
import { discard } from "../xform/discard";

export const seq = <T>(parsers: Parser<T>[], id = "seq"): Parser<T> => (
ctx
Expand Down
2 changes: 1 addition & 1 deletion packages/parse/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export * from "./grammar";
export * from "./combinators/alt";
export * from "./combinators/boundary";
export * from "./combinators/check";
export * from "./combinators/discard";
export * from "./combinators/dynamic";
export * from "./combinators/expect";
export * from "./combinators/maybe";
Expand Down Expand Up @@ -41,6 +40,7 @@ export * from "./readers/string-reader";

export * from "./xform/collect";
export * from "./xform/comp";
export * from "./xform/discard";
export * from "./xform/hoist";
export * from "./xform/join";
export * from "./xform/number";
Expand Down
14 changes: 14 additions & 0 deletions packages/parse/src/xform/discard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Parser } from "../api";
import { xform } from "../combinators/xform";

/**
* Discards AST node and any of its children.
*/
export const xfDiscard = () => null;

/**
* Syntax sugar for `xform(parser, xfDiscard)`.
*
* @param parser
*/
export const discard = (parser: Parser<any>) => xform(parser, xfDiscard);

0 comments on commit e09a2c4

Please sign in to comment.