forked from thi-ng/umbrella
-
Notifications
You must be signed in to change notification settings - Fork 0
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 primitives
- add LitParser type to annotate single-char parsers - add satisfyD() - add stringOf() for predicated strings - add wordBoundary anchor - add/update/rename discarding parser prims: - litD(), stringD(), noneOfD(), oneOfD(), rangeD() - export predicate versions: - litP(), noneOfP(), oneOfP(), rangeP() - update skipWhile() behavior
- Loading branch information
1 parent
d47c0a2
commit 328103f
Showing
10 changed files
with
177 additions
and
66 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
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
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
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,17 +1,18 @@ | ||
import { discard } from "../combinators/discard"; | ||
import { satisfy } from "./satisfy"; | ||
import { satisfyD, satisfy } from "./satisfy"; | ||
|
||
export const litP = <T>(c: T) => (x: T) => x === c; | ||
|
||
/** | ||
* Matches single char/value `c`. | ||
* | ||
* @param c | ||
* @param id | ||
*/ | ||
export const lit = <T>(c: T, id = "lit") => satisfy<T>((x) => x === c, id); | ||
export const lit = <T>(c: T, id = "lit") => satisfy<T>(litP(c), id); | ||
|
||
/** | ||
* Discarded literal. Same as {@link lit}, but result will be discarded. | ||
* | ||
* @param c | ||
*/ | ||
export const dlit = <T>(c: T) => discard(lit(c)); | ||
export const litD = <T>(c: T) => satisfyD<T>(litP(c)); |
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,16 +1,28 @@ | ||
import { isSet } from "@thi.ng/checks"; | ||
import type { Parser } from "../api"; | ||
import { satisfy } from "./satisfy"; | ||
import type { Predicate } from "@thi.ng/api"; | ||
import { isPlainObject, isSet } from "@thi.ng/checks"; | ||
import type { CharSet, LitParser } from "../api"; | ||
import { satisfy, satisfyD } from "./satisfy"; | ||
|
||
export function noneOf(opts: string, id?: string): Parser<string>; | ||
export function noneOf<T>(opts: T[], id?: string): Parser<T>; | ||
export function noneOf<T>(opts: Set<T>, id?: string): Parser<T>; | ||
export const noneOfP = ( | ||
opts: string | CharSet | any[] | Set<any> | ||
): Predicate<any> => | ||
isSet(opts) | ||
? (x) => !opts.has(x) | ||
: isPlainObject(opts) | ||
? (x) => !(<any>opts)[x] | ||
: (x) => opts.indexOf(x) < 0; | ||
|
||
export function noneOf(opts: string | CharSet, id?: string): LitParser<string>; | ||
export function noneOf<T>(opts: T[] | Set<T>, id?: string): LitParser<T>; | ||
export function noneOf( | ||
opts: string | any[] | Set<any>, | ||
id = "oneOf" | ||
): Parser<any> { | ||
return satisfy( | ||
isSet(opts) ? (x) => !opts.has(x) : (x) => opts.indexOf(x) < 0, | ||
id | ||
); | ||
opts: string | CharSet | any[] | Set<any>, | ||
id = "noneOf" | ||
) { | ||
return satisfy(noneOfP(opts), id); | ||
} | ||
|
||
export function noneOfD(opts: string | CharSet): LitParser<string>; | ||
export function noneOfD<T>(opts: T[] | Set<T>): LitParser<T>; | ||
export function noneOfD(opts: string | CharSet | any[] | Set<any>) { | ||
return satisfyD(noneOfP(opts)); | ||
} |
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,16 +1,25 @@ | ||
import { isSet } from "@thi.ng/checks"; | ||
import type { Parser } from "../api"; | ||
import { satisfy } from "./satisfy"; | ||
import type { Predicate } from "@thi.ng/api"; | ||
import { isPlainObject, isSet } from "@thi.ng/checks"; | ||
import type { CharSet, LitParser } from "../api"; | ||
import { satisfy, satisfyD } from "./satisfy"; | ||
|
||
export function oneOf(opts: string, id?: string): Parser<string>; | ||
export function oneOf<T>(opts: T[], id?: string): Parser<T>; | ||
export function oneOf<T>(opts: Set<T>, id?: string): Parser<T>; | ||
export function oneOf( | ||
opts: string | any[] | Set<any>, | ||
id = "oneOf" | ||
): Parser<any> { | ||
return satisfy( | ||
isSet(opts) ? (x) => opts.has(x) : (x) => opts.indexOf(x) >= 0, | ||
id | ||
); | ||
export const oneOfP = ( | ||
opts: string | any[] | Set<any> | CharSet | ||
): Predicate<any> => | ||
isSet(opts) | ||
? (x) => opts.has(x) | ||
: isPlainObject(opts) | ||
? (x) => (<any>opts)[x] | ||
: (x) => opts.indexOf(x) >= 0; | ||
|
||
export function oneOf(opts: string | CharSet, id?: string): LitParser<string>; | ||
export function oneOf<T>(opts: T[] | Set<T>, id?: string): LitParser<T>; | ||
export function oneOf(opts: string | CharSet | any[] | Set<any>, id = "oneOf") { | ||
return satisfy(oneOfP(opts), id); | ||
} | ||
|
||
export function oneOfD(opts: string | CharSet): LitParser<string>; | ||
export function oneOfD<T>(opts: T[] | Set<T>): LitParser<T>; | ||
export function oneOfD(opts: string | CharSet | any[] | Set<any>) { | ||
return satisfyD(oneOfP(opts)); | ||
} |
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
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,10 +1,27 @@ | ||
import type { Predicate } from "@thi.ng/api"; | ||
import type { Parser } from "../api"; | ||
import type { Parser, LitParser } from "../api"; | ||
|
||
export const satisfy = <T>(fn: Predicate<T>, id = "lit"): Parser<T> => ( | ||
ctx | ||
) => { | ||
if (ctx.done) return false; | ||
const r = ctx.reader.read(ctx.state); | ||
return fn(r) ? ctx.addChild(id, r, true) : false; | ||
}; | ||
export function satisfy<T>(pred: Predicate<T>, id = "lit") { | ||
const parser: Parser<T> = (ctx) => { | ||
if (ctx.done) return false; | ||
const r = ctx.reader.read(ctx.state); | ||
return pred(r) ? ctx.addChild(id, r, true) : false; | ||
}; | ||
return <LitParser<T>>parser; | ||
} | ||
|
||
/** | ||
* Like {@link satisfy}, but avoids creating AST node and discards | ||
* result. | ||
* | ||
* @param pred | ||
*/ | ||
export function satisfyD<T>(pred: Predicate<T>) { | ||
const parser: Parser<T> = (ctx) => { | ||
if (ctx.done) return false; | ||
const state = ctx.state!; | ||
const reader = ctx.reader; | ||
return pred(reader.read(state)) ? (reader.next(state), true) : false; | ||
}; | ||
return <LitParser<T>>parser; | ||
} |
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
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