-
-
Notifications
You must be signed in to change notification settings - Fork 150
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/rename/reorg parsers, xforms, ctx
- add dlit(), dstring() - add fail() - rename lift() => pass(), Lift<T> => PassValue<T> - rename merge()/xfMerge() => join()/xfJoin() - add hoist()/xfHoist() - migrate xform syntax sugars to /xform - add indent() util for ParseContext & print()
- Loading branch information
1 parent
8ca5c7f
commit ee537f4
Showing
24 changed files
with
166 additions
and
96 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 was deleted.
Oops, something went wrong.
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,8 +1,8 @@ | ||
import type { Lift, Parser } from "../api"; | ||
import { lift } from "../prims/lift"; | ||
import type { Parser, PassValue } from "../api"; | ||
import { pass } from "../prims/pass"; | ||
|
||
export const maybe = <T, R = any>( | ||
parser: Parser<T>, | ||
fn?: Lift<R>, | ||
result?: PassValue<R>, | ||
id = "maybe" | ||
): Parser<T> => (ctx) => parser(ctx) || lift(fn, id)(ctx); | ||
): Parser<T> => (ctx) => parser(ctx) || pass(result, id)(ctx); |
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
import { isFunction } from "@thi.ng/checks"; | ||
import type { Lift, Parser } from "../api"; | ||
import type { Parser, PassValue } from "../api"; | ||
|
||
export const not = <T, R = any>( | ||
parser: Parser<T>, | ||
lift?: Lift<R>, | ||
result?: PassValue<R>, | ||
id = "not" | ||
): Parser<T> => (ctx) => { | ||
if (ctx.done) return false; | ||
const scope = ctx.start(id); | ||
if (parser(ctx)) { | ||
ctx.discard(); | ||
return false; | ||
return ctx.discard(); | ||
} | ||
scope.result = lift != null ? (isFunction(lift) ? lift() : lift) : null; | ||
scope.result = | ||
result != null ? (isFunction(result) ? result() : result) : null; | ||
return ctx.end(); | ||
}; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import type { Parser } from "../api"; | ||
import { parseError } from "../error"; | ||
|
||
export const fail = (msg: string): Parser<any> => (ctx) => parseError(ctx, msg); |
This file was deleted.
Oops, something went wrong.
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,3 +1,17 @@ | ||
import { discard } from "../combinators/discard"; | ||
import { satisfy } from "./satisfy"; | ||
|
||
/** | ||
* Matches single char/value `c`. | ||
* | ||
* @param c | ||
* @param id | ||
*/ | ||
export const lit = <T>(c: T, id = "lit") => satisfy<T>((x) => x === c, id); | ||
|
||
/** | ||
* Discarded literal. Same as {@link lit}, but result will be discarded. | ||
* | ||
* @param c | ||
*/ | ||
export const dlit = <T>(c: T) => discard(lit(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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { isFunction } from "@thi.ng/checks"; | ||
import type { PassValue, Parser } from "../api"; | ||
|
||
export const pass = <R = any>( | ||
result: PassValue<R>, | ||
id = "lift" | ||
): Parser<any> => (ctx) => | ||
ctx.addChild(id, isFunction(result) ? result() : 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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const cache: string[] = []; | ||
|
||
/** | ||
* Memoized indentation. | ||
* | ||
* @param x | ||
* | ||
* @internal | ||
*/ | ||
export const indent = (x: number) => | ||
x > 0 ? cache[x] || (cache[x] = " ".repeat(x)) : ""; |
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,7 +1,15 @@ | ||
import { ScopeTransform } from "../api"; | ||
import { Parser, ScopeTransform } from "../api"; | ||
import { xform } from "../combinators/xform"; | ||
|
||
export const xfCollect: ScopeTransform<any> = (scope) => { | ||
scope!.result = scope!.children!.map((c) => c.result); | ||
scope!.children = null; | ||
return scope; | ||
}; | ||
|
||
/** | ||
* Syntax sugar for `xform(parser, xfCollect)`. | ||
* | ||
* @param parser | ||
*/ | ||
export const collect = <T>(parser: Parser<T>) => xform(parser, xfCollect); |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Parser, ScopeTransform } from "../api"; | ||
import { xform } from "../combinators/xform"; | ||
|
||
export const xfHoist: ScopeTransform<any> = (scope) => { | ||
scope!.result = scope!.children![0].result; | ||
scope!.children = null; | ||
return scope; | ||
}; | ||
|
||
export const hoist = <T>(parser: Parser<T>) => xform(parser, xfHoist); |
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 type { Nullable } from "@thi.ng/api"; | ||
import type { Parser, ParseScope } from "../api"; | ||
import { xform } from "../combinators/xform"; | ||
|
||
export const xfJoin = <T>(scope: Nullable<ParseScope<T>>) => { | ||
if (!scope || !scope.children) return null; | ||
const res = []; | ||
for (let c of scope.children) { | ||
xfJoin(c); | ||
if (c.result) res.push(c.result); | ||
} | ||
scope.result = res.join(""); | ||
scope.children = null; | ||
return scope; | ||
}; | ||
|
||
/** | ||
* Syntax sugar for `xform(parser, xfJoin)`. | ||
* | ||
* @param parser | ||
*/ | ||
export const join = <T>(parser: Parser<T>) => xform(parser, xfJoin); |
This file was deleted.
Oops, something went wrong.
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,12 +1,12 @@ | ||
import { ScopeTransform } from "../api"; | ||
import { xfMerge } from "./merge"; | ||
import { xfJoin } from "./join"; | ||
|
||
export const xfInt = (radix = 10): ScopeTransform<string> => (scope) => { | ||
scope!.result = parseInt(xfMerge(scope)!.result, radix); | ||
scope!.result = parseInt(xfJoin(scope)!.result, radix); | ||
return scope; | ||
}; | ||
|
||
export const xfFloat: ScopeTransform<string> = (scope) => { | ||
scope!.result = parseFloat(xfMerge(scope)!.result); | ||
scope!.result = parseFloat(xfJoin(scope)!.result); | ||
return scope; | ||
}; |
Oops, something went wrong.