-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parse): add/update/rename parser presets
- Loading branch information
1 parent
e16426b
commit 12f2499
Showing
7 changed files
with
92 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import { ALPHA as _ALPHA, ALPHA_NUM as _ALPHA_NUM } from "@thi.ng/strings"; | ||
import { oneOf } from "../prims/one-of"; | ||
import { range } from "../prims/range"; | ||
import { alt } from "../combinators/alt"; | ||
import { DIGIT } from "./digits"; | ||
|
||
/** | ||
* Matches single char in `a` - `z` range. | ||
*/ | ||
export const LOWER_CASE = range<string>("a", "z"); | ||
export const LOWER_CASE = range("a", "z"); | ||
|
||
/** | ||
* Matches single char in `A` - `Z` range. | ||
*/ | ||
export const UPPER_CASE = range<string>("A", "Z"); | ||
export const UPPER_CASE = range("A", "Z"); | ||
|
||
/** | ||
* Matches single in {@link LOWER_CASE} or {@link UPPER_CASE}. | ||
*/ | ||
export const ALPHA = alt([LOWER_CASE, UPPER_CASE]); | ||
export const ALPHA = oneOf(_ALPHA); | ||
|
||
/** | ||
* Matches single in {@link ALPHA} or {@link DIGIT}. | ||
*/ | ||
export const ALPHA_NUM = alt([ALPHA, DIGIT]); | ||
export const ALPHA_NUM = oneOf(_ALPHA_NUM); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { oneOf } from "../prims/one-of"; | ||
import { xfInt } from "../xform/number"; | ||
import { repeat } from "../combinators/repeat"; | ||
import { xform } from "../combinators/xform"; | ||
|
||
export const BIT = oneOf("01"); | ||
|
||
export const BINARY_UINT = xform(repeat(BIT, 1, 32, "uint"), xfInt(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,12 @@ | ||
import { alt } from "../combinators/alt"; | ||
import { oneOrMore, zeroOrMore } from "../combinators/repeat"; | ||
import { oneOrMore } from "../combinators/repeat"; | ||
import { range } from "../prims/range"; | ||
|
||
/** | ||
* Matches single decimal digit. | ||
*/ | ||
export const DIGIT = range<string>("0", "9", "digit"); | ||
|
||
/** | ||
* Matches single hex digit (case insensitive). | ||
*/ | ||
export const HEX_DIGIT = alt([ | ||
DIGIT, | ||
range<string>("a", "f"), | ||
range<string>("A", "F"), | ||
]); | ||
|
||
/** | ||
* Matches zero or more {@link DIGIT}s. | ||
*/ | ||
export const DIGITS_0 = zeroOrMore(DIGIT); | ||
export const DIGIT = range("0", "9", "digit"); | ||
|
||
/** | ||
* Matches one or more {@link DIGIT}s. | ||
*/ | ||
export const DIGITS_1 = oneOrMore(DIGIT); | ||
|
||
/** | ||
* Matches one or more {@link HEX_DIGIT}s. | ||
*/ | ||
export const HEX_DIGITS_1 = oneOrMore(HEX_DIGIT); | ||
export const DIGITS = oneOrMore(DIGIT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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"; | ||
|
||
export const ESC = hoist(seq([litD("\\"), always()], "esc")); | ||
|
||
/** | ||
* Matches a single `\uNNNN` escaped unicode hex literal and transforms | ||
* it into it actual character via `String.fromCharCode()`. | ||
*/ | ||
export const UNICODE = xform( | ||
seq([stringD("\\u"), repeat(HEX_DIGIT, 4, 4)], "unicode"), | ||
($, ctx) => ( | ||
($!.result = String.fromCharCode(xfInt(16)($, ctx)!.result)), $ | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { HEX } from "@thi.ng/strings"; | ||
import { oneOrMore, repeat } from "../combinators/repeat"; | ||
import { xform } from "../combinators/xform"; | ||
import { oneOf } from "../prims/one-of"; | ||
import { xfInt } from "../xform/number"; | ||
|
||
/** | ||
* Matches single hex digit (case insensitive). | ||
*/ | ||
export const HEX_DIGIT = oneOf(HEX); | ||
|
||
/** | ||
* Matches one or more {@link HEX_DIGIT}s. | ||
*/ | ||
export const HEX_DIGITS = oneOrMore(HEX_DIGIT); | ||
|
||
/** | ||
* Matches 1-8 successive {@link HEX_DIGIT} and transforms with | ||
* {@link xfInt} to JS number. | ||
*/ | ||
export const HEX_UINT = xform(repeat(HEX_DIGIT, 1, 8, "uint"), xfInt(16)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,49 @@ | ||
import { alt } from "../combinators/alt"; | ||
import { maybe } from "../combinators/maybe"; | ||
import { zeroOrMore } from "../combinators/repeat"; | ||
import { seq } from "../combinators/seq"; | ||
import { xform } from "../combinators/xform"; | ||
import { lit } from "../prims/lit"; | ||
import { oneOf } from "../prims/one-of"; | ||
import { comp } from "../xform/comp"; | ||
import { join } from "../xform/join"; | ||
import { xfFloat, xfInt } from "../xform/number"; | ||
import { DIGITS_0, DIGITS_1, HEX_DIGITS_1 } from "./digits"; | ||
import { xfID } from "../xform/with-id"; | ||
import { DIGIT, DIGITS } from "./digits"; | ||
|
||
/** | ||
* Matches single `+` or `-` char. | ||
*/ | ||
export const SIGN = maybe(oneOf("-+")); | ||
|
||
/** | ||
* Matches optionally signed {@link DIGITS_1} and result transformed w/ | ||
* Matches optionally signed {@link DIGITS} and result transformed w/ | ||
* {@link xfInt} to JS number. | ||
*/ | ||
export const INT = xform(seq([SIGN, DIGITS_1], "int"), xfInt()); | ||
export const INT = xform(seq([SIGN, DIGITS], "int"), xfInt()); | ||
|
||
/** | ||
* Matches same as {@link DIGITS_1}, but result transformed w/ | ||
* Matches same as {@link DIGITS} but result transformed w/ | ||
* {@link xfInt} to JS number. | ||
*/ | ||
export const UINT = xform(DIGITS_1, xfInt()); | ||
export const UINT = xform(DIGITS, comp(xfID("uint"), xfInt())); | ||
|
||
/** | ||
* Matches same as {@link HEX_DIGITS_1}, but result transformed w/ | ||
* {@link xfInt} to JS number. | ||
*/ | ||
export const HEX_UINT = xform(HEX_DIGITS_1, xfInt(16)); | ||
|
||
const EXP = maybe(seq([maybe(oneOf("eE")), SIGN, DIGITS_1])); | ||
const EXP = maybe(seq([maybe(oneOf("eE")), SIGN, DIGITS])); | ||
|
||
const DOT = lit("."); | ||
|
||
const FRACT0 = maybe(seq([DOT, DIGITS_0])); | ||
const FRACT1 = seq([DOT, DIGITS_1]); | ||
const FRACT0 = maybe(seq([DOT, zeroOrMore(DIGIT)])); | ||
const FRACT1 = seq([DOT, DIGITS]); | ||
|
||
const _REAL = seq([SIGN, alt([FRACT1, seq([DIGITS, FRACT0])]), EXP], "real"); | ||
|
||
/** | ||
* Matches IEEE754 floating point number. | ||
*/ | ||
export const REAL = join(_REAL); | ||
|
||
/** | ||
* Matches IEEE754 floating point number and transforms result w/ | ||
* {@link xfFloat}. | ||
* Like {@link REAL} but transforms result w/ {@link xfFloat} to JS | ||
* number. | ||
*/ | ||
export const FLOAT = xform( | ||
seq([SIGN, alt([FRACT1, seq([DIGITS_1, FRACT0])]), EXP], "float"), | ||
xfFloat | ||
); | ||
export const FLOAT = xform(_REAL, xfFloat); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters