From 069a6ef11c9423bdb2974b11823cc39743dfceec Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Mon, 20 Apr 2020 14:49:03 +0100 Subject: [PATCH] feat(parse): update ESC & whitespace parsers --- packages/parse/src/presets/escape.ts | 20 ++++++++++++++++++-- packages/parse/src/presets/whitespace.ts | 14 +++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/parse/src/presets/escape.ts b/packages/parse/src/presets/escape.ts index 263616a177..c761da1dad 100644 --- a/packages/parse/src/presets/escape.ts +++ b/packages/parse/src/presets/escape.ts @@ -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 = { + 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 diff --git a/packages/parse/src/presets/whitespace.ts b/packages/parse/src/presets/whitespace.ts index a14f078d7c..72925c582b 100644 --- a/packages/parse/src/presets/whitespace.ts +++ b/packages/parse/src/presets/whitespace.ts @@ -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. @@ -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);