Skip to content

Commit

Permalink
feat(parse): add ctx getters, add presets, update maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 16, 2020
1 parent bef1d4f commit 02597bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/parse/src/combinators/maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export const maybe = <T, R = any>(
parser: Parser<T>,
fn?: Lift<R>,
id = "maybe"
): Parser<T> => (ctx) => parser(ctx) || lift(fn != null ? fn : null, id)(ctx);
): Parser<T> => (ctx) => parser(ctx) || lift(fn, id)(ctx);
8 changes: 5 additions & 3 deletions packages/parse/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import { oneOf } from "./prims/one-of";
import { range } from "./prims/range";
import { xfFloat, xfInt } from "./xform/number";

export const WS = oneOf(" \t\n\r");
export const WS = oneOf(" \t\n\r", "ws");
export const WS_0 = zeroOrMore(WS, "ws0");
export const WS_1 = oneOrMore(WS, "ws1");

export const DIGIT = range<string>("0", "9");
export const DIGIT = range<string>("0", "9", "digit");

export const HEX_DIGIT = alt([
DIGIT,
Expand All @@ -34,7 +36,7 @@ const EXP = maybe(seq([maybe(oneOf("eE")), SIGN, DIGITS_1]));

const DOT = lit(".");

const FRACT0 = maybe(seq([DOT, maybe(DIGITS_0)]));
const FRACT0 = maybe(seq([DOT, DIGITS_0]));
const FRACT1 = seq([DOT, DIGITS_1]);

export const INT = xform(seq([SIGN, DIGITS_1], "int"), xfInt());
Expand Down
21 changes: 20 additions & 1 deletion packages/parse/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,27 @@ export class ParseContext<T> {
return this._curr.state.done;
}

/**
* Returns root node.
*/
get root() {
return this._scopes[0];
}

/**
* Returns root node's `result` or `undefined`.
*/
get result() {
return this._curr.children ? this._curr.children[0].result : undefined;
const children = this.root.children;
return children ? children[0].result : undefined;
}

/**
* Returns root node's children or `undefined`.
*/
get children() {
const children = this.root.children;
return children ? children[0].children : undefined;
}
}

Expand Down

0 comments on commit 02597bf

Please sign in to comment.