-
-
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 withID() xform, add doc strings
- Loading branch information
1 parent
e4eab03
commit e16426b
Showing
6 changed files
with
71 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import { Parser, ScopeTransform } from "../api"; | ||
import { xform } from "../combinators/xform"; | ||
|
||
/** | ||
* Moves the result of first child node to this node, then discards all | ||
* children. Also see {@link hoist}. | ||
* | ||
* @param scope | ||
*/ | ||
export const xfHoist: ScopeTransform<any> = (scope) => { | ||
scope!.result = scope!.children![0].result; | ||
scope!.children = null; | ||
return scope; | ||
}; | ||
|
||
/** | ||
* Syntax sugar for `xform(parser, xfHoist)`. | ||
* | ||
* @param parser | ||
*/ | ||
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
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,15 @@ | ||
import { ScopeTransform, Parser } from "../api"; | ||
import { xform } from "../combinators/xform"; | ||
|
||
/** | ||
* Assigns given `id` to AST node. E.g. used for re-labeling parser | ||
* results defined by {@link defGrammar}. | ||
* | ||
* @param id | ||
*/ | ||
export const xfID = (id: string): ScopeTransform<any> => (scope) => ( | ||
(scope!.id = id), scope | ||
); | ||
|
||
export const withID = <T>(id: string, parser: Parser<T>) => | ||
xform(parser, xfID(id)); |