Skip to content

Commit

Permalink
feat(parse): update ESC & whitespace parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 20, 2020
1 parent e09a2c4 commit 069a6ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
20 changes: 18 additions & 2 deletions packages/parse/src/presets/escape.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { always } from "../prims/always";
import { litD } from "../prims/lit";
import { seq } from "../combinators/seq";
import { hoist } from "../xform/hoist";
import { xform } from "../combinators/xform";
import { repeat } from "../combinators/repeat";
import { HEX_DIGIT } from "./hex";
import { stringD } from "../prims/string";
import { xfInt } from "../xform/number";
import { IObjectOf } from "@thi.ng/api";

export const ESC = hoist(seq([litD("\\"), always()], "esc"));
const ESC_VALUES: IObjectOf<string> = {
0: "\0",
b: "\b",
t: "\t",
n: "\n",
v: "\v",
f: "\f",
r: "\r",
};

export const ESC = xform(seq([litD("\\"), always()], "esc"), ($) => {
const id = $!.children![0].result;
const resolved = ESC_VALUES[id];
$!.result = resolved !== undefined ? resolved : id;
$!.children = null;
return $;
});

/**
* Matches a single `\uNNNN` escaped unicode hex literal and transforms
Expand Down
14 changes: 7 additions & 7 deletions packages/parse/src/presets/whitespace.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { discard } from "../combinators/discard";
import { oneOrMore, zeroOrMore } from "../combinators/repeat";
import { WS as _WS } from "@thi.ng/strings";
import { oneOrMoreD, zeroOrMoreD } from "../combinators/repeat";
import { oneOf, oneOfD } from "../prims/one-of";

/**
* Matches & discards single whitespace char: ` \t\n\r`.
*/
export const WS = oneOfD(" \t\n\r");
export const WS = oneOfD(_WS);

/**
* Matches & discards single space or tab char.
Expand All @@ -25,19 +25,19 @@ export const DNL = oneOfD("\n\r");
/**
* Zero or more {@link WS}. Result will be discarded.
*/
export const WS0 = discard(zeroOrMore(WS));
export const WS0 = zeroOrMoreD(WS);

/**
* One or more {@link WS}. Result will be discarded.
*/
export const WS1 = discard(oneOrMore(WS));
export const WS1 = oneOrMoreD(WS);

/**
* Zero or more {@link SPACE}. Result will be discarded.
*/
export const SPACES0 = discard(zeroOrMore(SPACE));
export const SPACES0 = zeroOrMoreD(SPACE);

/**
* One or more {@link SPACE}. Result will be discarded.
*/
export const SPACES = discard(oneOrMore(SPACE));
export const SPACES = oneOrMoreD(SPACE);

0 comments on commit 069a6ef

Please sign in to comment.