@traqula/core 0.0.1-alpha.148 → 0.0.1-alpha.176
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -22
- package/lib/generator-builder/builderTypes.d.ts +20 -0
- package/lib/generator-builder/builderTypes.js.map +1 -0
- package/lib/generator-builder/dynamicGenerator.d.ts +7 -0
- package/lib/generator-builder/dynamicGenerator.js +28 -0
- package/lib/generator-builder/dynamicGenerator.js.map +1 -0
- package/lib/generator-builder/generatorBuilder.d.ts +58 -0
- package/lib/generator-builder/generatorBuilder.js +101 -0
- package/lib/generator-builder/generatorBuilder.js.map +1 -0
- package/lib/generator-builder/generatorTypes.d.ts +27 -0
- package/lib/generator-builder/generatorTypes.js +2 -0
- package/lib/generator-builder/generatorTypes.js.map +1 -0
- package/lib/index.cjs +379 -320
- package/lib/index.d.ts +7 -6
- package/lib/index.js +11 -6
- package/lib/index.js.map +1 -1
- package/lib/lexer-builder/LexerBuilder.d.ts +7 -9
- package/lib/lexer-builder/LexerBuilder.js +12 -4
- package/lib/lexer-builder/LexerBuilder.js.map +1 -1
- package/lib/parser-builder/builderTypes.d.ts +24 -0
- package/lib/parser-builder/builderTypes.js +2 -0
- package/lib/parser-builder/builderTypes.js.map +1 -0
- package/lib/parser-builder/dynamicParser.d.ts +9 -0
- package/lib/parser-builder/dynamicParser.js +113 -0
- package/lib/parser-builder/dynamicParser.js.map +1 -0
- package/lib/parser-builder/parserBuilder.d.ts +73 -0
- package/lib/parser-builder/parserBuilder.js +161 -0
- package/lib/parser-builder/parserBuilder.js.map +1 -0
- package/lib/{grammar-builder → parser-builder}/ruleDefTypes.d.ts +5 -5
- package/lib/parser-builder/ruleDefTypes.js.map +1 -0
- package/lib/utils.d.ts +17 -0
- package/lib/utils.js +8 -0
- package/lib/utils.js.map +1 -0
- package/package.json +6 -6
- package/lib/Wildcard.d.ts +0 -9
- package/lib/Wildcard.js +0 -19
- package/lib/Wildcard.js.map +0 -1
- package/lib/grammar-builder/builderTypes.d.ts +0 -28
- package/lib/grammar-builder/builderTypes.js.map +0 -1
- package/lib/grammar-builder/parserBuilder.d.ts +0 -68
- package/lib/grammar-builder/parserBuilder.js +0 -283
- package/lib/grammar-builder/parserBuilder.js.map +0 -1
- package/lib/grammar-builder/ruleDefTypes.js.map +0 -1
- package/lib/grammar-helpers/utils.d.ts +0 -16
- package/lib/grammar-helpers/utils.js +0 -53
- package/lib/grammar-helpers/utils.js.map +0 -1
- package/lib/lexer-helper/utils.d.ts +0 -6
- package/lib/lexer-helper/utils.js +0 -5
- package/lib/lexer-helper/utils.js.map +0 -1
- /package/lib/{grammar-builder → generator-builder}/builderTypes.js +0 -0
- /package/lib/{grammar-builder → parser-builder}/ruleDefTypes.js +0 -0
|
@@ -3,11 +3,11 @@ import type { ConsumeMethodOpts, IToken, TokenType } from 'chevrotain';
|
|
|
3
3
|
/**
|
|
4
4
|
* Get the return-type of a RuleDef
|
|
5
5
|
*/
|
|
6
|
-
export type RuleDefReturn<T extends
|
|
6
|
+
export type RuleDefReturn<T extends ParserRule> = T extends ParserRule<any, string, infer Ret> ? Ret : never;
|
|
7
7
|
/**
|
|
8
8
|
* Type used to declare grammar rules.
|
|
9
9
|
*/
|
|
10
|
-
export type
|
|
10
|
+
export type ParserRule<
|
|
11
11
|
/**
|
|
12
12
|
* Context object available in rule implementation.
|
|
13
13
|
*/
|
|
@@ -32,16 +32,16 @@ ParamType = any> = {
|
|
|
32
32
|
* Type expected by grammar rules in the main `impl` function.
|
|
33
33
|
*/
|
|
34
34
|
export interface ImplArgs extends CstDef {
|
|
35
|
-
cache: WeakMap<
|
|
35
|
+
cache: WeakMap<ParserRule, unknown>;
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
38
|
* Type definition used by {@link CstDef.SUBRULE} and family.
|
|
39
39
|
*/
|
|
40
|
-
type SubRuleFunc = <T extends string, U = unknown, ARGS = any>(cstDef:
|
|
40
|
+
type SubRuleFunc = <T extends string, U = unknown, ARGS = any>(cstDef: ParserRule<any, T, U, ARGS>, argument: ARGS) => U;
|
|
41
41
|
/**
|
|
42
42
|
* Type definition used by {@link CstDef.BACKTRACK}.
|
|
43
43
|
*/
|
|
44
|
-
type BacktrackFunc = <T extends string, U = unknown, ARGS = any>(cstDef:
|
|
44
|
+
type BacktrackFunc = <T extends string, U = unknown, ARGS = any>(cstDef: ParserRule<any, T, U, ARGS>, argument: ARGS) => () => boolean;
|
|
45
45
|
/**
|
|
46
46
|
* Mainly a repetition of the functions exposed by the {@link EmbeddedActionsParser},
|
|
47
47
|
* with some small adjustments that allow for the API changes made by traqula.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ruleDefTypes.js","sourceRoot":"","sources":["ruleDefTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n AtLeastOneSepMethodOpts,\n DSLMethodOpts,\n DSLMethodOptsWithErr,\n GrammarAction,\n IOrAlt,\n ManySepMethodOpts,\n OrMethodOpts,\n} from '@chevrotain/types';\nimport type { ConsumeMethodOpts, IToken, TokenType } from 'chevrotain';\n\n/**\n * Get the return-type of a RuleDef\n */\nexport type RuleDefReturn<T extends ParserRule> = T extends ParserRule<any, string, infer Ret> ? Ret : never;\n\n/**\n * Type used to declare grammar rules.\n */\nexport type ParserRule<\n /**\n * Context object available in rule implementation.\n */\n Context = any,\n /**\n * Name of grammar rule, should be a strict subtype of string like 'myGrammarRule'.\n */\n NameType extends string = string,\n /**\n * Type that will be returned after a correct parse of this rule.\n * This type will be the return type of calling SUBRULE with this grammar rule.\n */\n ReturnType = unknown,\n /**\n * Function arguments that can be given to convey the state of the current parse operation.\n */\n ParamType = any,\n> = {\n name: NameType;\n impl: (def: ImplArgs) => (context: Context, params: ParamType) => ReturnType;\n};\n\n/**\n * Type expected by grammar rules in the main `impl` function.\n */\nexport interface ImplArgs extends CstDef {\n cache: WeakMap<ParserRule, unknown>;\n}\n\n/**\n * Type definition used by {@link CstDef.SUBRULE} and family.\n */\ntype SubRuleFunc = <T extends string, U = unknown, ARGS = any>(\n cstDef: ParserRule<any, T, U, ARGS>,\n argument: ARGS\n) => U;\n/**\n * Type definition used by {@link CstDef.BACKTRACK}.\n */\ntype BacktrackFunc = <T extends string, U = unknown, ARGS = any>(\n cstDef: ParserRule<any, T, U, ARGS>,\n argument: ARGS\n) => () => boolean;\n\n/**\n * Mainly a repetition of the functions exposed by the {@link EmbeddedActionsParser},\n * with some small adjustments that allow for the API changes made by traqula.\n * Specifically changes are made to the {@link CstDef.SUBRULE} (and family) interface,\n * and to the {@link CstDef.BACKTRACK} interface.\n */\nexport interface CstDef {\n /**\n *\n * A Parsing DSL method use to consume a single Token.\n * In EBNF terms this is equivalent to a Terminal.\n *\n * A Token will be consumed, IFF the next token in the token vector matches `tokType`.\n * otherwise the parser may attempt to perform error recovery (if enabled).\n *\n * The index in the method name indicates the unique occurrence of a terminal consumption\n * inside a the top level rule. What this means is that if a terminal appears\n * more than once in a single rule, each appearance must have a **different** index.\n *\n * For example:\n * ```\n * this.RULE(\"qualifiedName\", () => {\n * this.CONSUME1(Identifier);\n * this.MANY(() => {\n * this.CONSUME1(Dot);\n * // here we use CONSUME2 because the terminal\n * // 'Identifier' has already appeared previously in the\n * // the rule 'parseQualifiedName'\n * this.CONSUME2(Identifier);\n * });\n * })\n * ```\n *\n * - See more details on the [unique suffixes requirement](http://chevrotain.io/docs/FAQ.html#NUMERICAL_SUFFIXES).\n *\n * @param tokType - The Type of the token to be consumed.\n * @param options - optional properties to modify the behavior of CONSUME.\n */\n CONSUME: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n CONSUME1: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n CONSUME2: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n CONSUME3: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n CONSUME4: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n CONSUME5: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n CONSUME6: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n CONSUME7: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n CONSUME8: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n CONSUME9: (tokType: TokenType, options?: ConsumeMethodOpts) => IToken;\n /**\n * Parsing DSL Method that Indicates an Optional production.\n * in EBNF notation this is equivalent to: \"[...]\".\n *\n * Note that there are two syntax forms:\n * - Passing the grammar action directly:\n * ```\n * this.OPTION(() => {\n * this.CONSUME(Digit)}\n * );\n * ```\n *\n * - using an \"options\" object:\n * ```\n * this.OPTION({\n * GATE:predicateFunc,\n * DEF: () => {\n * this.CONSUME(Digit)\n * }});\n * ```\n *\n * The optional 'GATE' property in \"options\" object form can be used to add constraints\n * to invoking the grammar action.\n *\n * As in CONSUME the index in the method name indicates the occurrence\n * of the optional production in it's top rule.\n *\n * @param actionORMethodDef - The grammar action to optionally invoke once\n * or an \"OPTIONS\" object describing the grammar action and optional properties.\n *\n * @returns The `GrammarAction` return value (OUT) if the optional syntax is encountered\n * or `undefined` if not.\n */\n OPTION: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n OPTION1: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n OPTION2: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n OPTION3: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n OPTION4: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n OPTION5: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n OPTION6: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n OPTION7: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n OPTION8: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n OPTION9: <OUT>(\n actionORMethodDef: GrammarAction<OUT> | DSLMethodOpts<OUT>,\n ) => OUT | undefined;\n /**\n * Parsing DSL method that indicates a choice between a set of alternatives must be made.\n * This is equivalent to an EBNF alternation (A | B | C | D ...), except\n * that the alternatives are ordered like in a PEG grammar.\n * This means that the **first** matching alternative is always chosen.\n *\n * There are several forms for the inner alternatives array:\n *\n * - Passing alternatives array directly:\n * ```\n * this.OR([\n * { ALT:() => { this.CONSUME(One) }},\n * { ALT:() => { this.CONSUME(Two) }},\n * { ALT:() => { this.CONSUME(Three) }}\n * ])\n * ```\n *\n * - Passing alternative array directly with predicates (GATE):\n * ```\n * this.OR([\n * { GATE: predicateFunc1, ALT:() => { this.CONSUME(One) }},\n * { GATE: predicateFuncX, ALT:() => { this.CONSUME(Two) }},\n * { GATE: predicateFuncX, ALT:() => { this.CONSUME(Three) }}\n * ])\n * ```\n *\n * - These syntax forms can also be mixed:\n * ```\n * this.OR([\n * {\n * GATE: predicateFunc1,\n * ALT:() => { this.CONSUME(One) }\n * },\n * { ALT:() => { this.CONSUME(Two) }},\n * { ALT:() => { this.CONSUME(Three) }}\n * ])\n * ```\n *\n * - Additionally an \"options\" object may be used:\n * ```\n * this.OR({\n * DEF:[\n * { ALT:() => { this.CONSUME(One) }},\n * { ALT:() => { this.CONSUME(Two) }},\n * { ALT:() => { this.CONSUME(Three) }}\n * ],\n * // OPTIONAL property\n * ERR_MSG: \"A Number\"\n * })\n * ```\n *\n * The 'predicateFuncX' in the long form can be used to add constraints to choosing the alternative.\n *\n * As in CONSUME the index in the method name indicates the occurrence\n * of the alternation production in it's top rule.\n *\n * @param altsOrOpts - A set of alternatives or an \"OPTIONS\" object describing the alternatives\n * and optional properties.\n *\n * @returns The result of invoking the chosen alternative.\n */\n OR: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n OR1: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n OR2: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n OR3: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n OR4: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n OR5: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n OR6: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n OR7: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n OR8: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n OR9: <T>(altsOrOpts: IOrAlt<T>[] | OrMethodOpts<T>) => T;\n /**\n * Parsing DSL method, that indicates a repetition of zero or more.\n * This is equivalent to EBNF repetition \\{...\\}.\n *\n * Note that there are two syntax forms:\n * - Passing the grammar action directly:\n * ```\n * this.MANY(() => {\n * this.CONSUME(Comma)\n * this.CONSUME(Digit)\n * })\n * ```\n *\n * - using an \"options\" object:\n * ```\n * this.MANY({\n * GATE: predicateFunc,\n * DEF: () => {\n * this.CONSUME(Comma)\n * this.CONSUME(Digit)\n * }\n * });\n * ```\n *\n * The optional 'GATE' property in \"options\" object form can be used to add constraints\n * to invoking the grammar action.\n *\n * As in CONSUME the index in the method name indicates the occurrence\n * of the repetition production in it's top rule.\n *\n * @param actionORMethodDef - The grammar action to optionally invoke multiple times\n * or an \"OPTIONS\" object describing the grammar action and optional properties.\n *\n */\n MANY: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n MANY1: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n MANY2: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n MANY3: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n MANY4: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n MANY5: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n MANY6: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n MANY7: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n MANY8: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n MANY9: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOpts<any>,\n ) => void;\n /**\n * Parsing DSL method, that indicates a repetition of zero or more with a separator\n * Token between the repetitions.\n *\n * Example:\n *\n * ```\n * this.MANY_SEP({\n * SEP:Comma,\n * DEF: () => {\n * this.CONSUME(Number};\n * // ...\n * })\n * ```\n *\n * Note that because this DSL method always requires more than one argument the options object is always required\n * and it is not possible to use a shorter form like in the MANY DSL method.\n *\n * Note that for the purposes of deciding on whether or not another iteration exists\n * Only a single Token is examined (The separator). Therefore if the grammar being implemented is\n * so \"crazy\" to require multiple tokens to identify an item separator please use the more basic DSL methods\n * to implement it.\n *\n * As in CONSUME the index in the method name indicates the occurrence\n * of the repetition production in it's top rule.\n *\n * @param options - An object defining the grammar of each iteration and the separator between iterations\n *\n */\n MANY_SEP: (options: ManySepMethodOpts<any>) => void;\n MANY_SEP1: (options: ManySepMethodOpts<any>) => void;\n MANY_SEP2: (options: ManySepMethodOpts<any>) => void;\n MANY_SEP3: (options: ManySepMethodOpts<any>) => void;\n MANY_SEP4: (options: ManySepMethodOpts<any>) => void;\n MANY_SEP5: (options: ManySepMethodOpts<any>) => void;\n MANY_SEP6: (options: ManySepMethodOpts<any>) => void;\n MANY_SEP7: (options: ManySepMethodOpts<any>) => void;\n MANY_SEP8: (options: ManySepMethodOpts<any>) => void;\n MANY_SEP9: (options: ManySepMethodOpts<any>) => void;\n /**\n * Convenience method, same as MANY but the repetition is of one or more.\n * failing to match at least one repetition will result in a parsing error and\n * cause a parsing error.\n *\n * @see MANY\n *\n * @param actionORMethodDef - The grammar action to optionally invoke multiple times\n * or an \"OPTIONS\" object describing the grammar action and optional properties.\n *\n */\n AT_LEAST_ONE: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n AT_LEAST_ONE1: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n AT_LEAST_ONE2: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n AT_LEAST_ONE3: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n AT_LEAST_ONE4: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n AT_LEAST_ONE5: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n AT_LEAST_ONE6: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n AT_LEAST_ONE7: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n AT_LEAST_ONE8: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n AT_LEAST_ONE9: (\n actionORMethodDef: GrammarAction<any> | DSLMethodOptsWithErr<any>,\n ) => void;\n /**\n * Convenience method, same as MANY_SEP but the repetition is of one or more.\n * failing to match at least one repetition will result in a parsing error and\n * cause the parser to attempt error recovery.\n *\n * Note that an additional optional property ERR_MSG can be used to provide custom error messages.\n *\n * @see MANY_SEP\n *\n * @param options - An object defining the grammar of each iteration and the separator between iterations\n *\n * @return ISeparatedIterationResult<OUT>\n */\n AT_LEAST_ONE_SEP: (options: AtLeastOneSepMethodOpts<any>) => void;\n AT_LEAST_ONE_SEP1: (options: AtLeastOneSepMethodOpts<any>) => void;\n AT_LEAST_ONE_SEP2: (options: AtLeastOneSepMethodOpts<any>) => void;\n AT_LEAST_ONE_SEP3: (options: AtLeastOneSepMethodOpts<any>) => void;\n AT_LEAST_ONE_SEP4: (options: AtLeastOneSepMethodOpts<any>) => void;\n AT_LEAST_ONE_SEP5: (options: AtLeastOneSepMethodOpts<any>) => void;\n AT_LEAST_ONE_SEP6: (options: AtLeastOneSepMethodOpts<any>) => void;\n AT_LEAST_ONE_SEP7: (options: AtLeastOneSepMethodOpts<any>) => void;\n AT_LEAST_ONE_SEP8: (options: AtLeastOneSepMethodOpts<any>) => void;\n AT_LEAST_ONE_SEP9: (options: AtLeastOneSepMethodOpts<any>) => void;\n ACTION: <T>(impl: () => T) => T;\n BACKTRACK: BacktrackFunc;\n SUBRULE: SubRuleFunc;\n SUBRULE1: SubRuleFunc;\n SUBRULE2: SubRuleFunc;\n SUBRULE3: SubRuleFunc;\n SUBRULE4: SubRuleFunc;\n SUBRULE5: SubRuleFunc;\n SUBRULE6: SubRuleFunc;\n SUBRULE7: SubRuleFunc;\n SUBRULE8: SubRuleFunc;\n SUBRULE9: SubRuleFunc;\n}\n"]}
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ITokenConfig, TokenType } from '@chevrotain/types';
|
|
2
|
+
/**
|
|
3
|
+
* Check whether the first two types overlap, if no, return the 3th argument, else the 4th.
|
|
4
|
+
*/
|
|
5
|
+
export type CheckOverlap<T, U, V, W = never> = T & U extends never ? V : W;
|
|
6
|
+
export declare function unCapitalize<T extends string>(str: T): Uncapitalize<T>;
|
|
7
|
+
export type NamedToken<Name extends string> = TokenType & {
|
|
8
|
+
name: Name;
|
|
9
|
+
};
|
|
10
|
+
export declare function createToken<Name extends string>(config: ITokenConfig & {
|
|
11
|
+
name: Name;
|
|
12
|
+
}): NamedToken<Name>;
|
|
13
|
+
export type Patch<T extends object, Patch extends {
|
|
14
|
+
[Key in keyof T]?: any;
|
|
15
|
+
}> = {
|
|
16
|
+
[Key in keyof T]: Key extends keyof Patch ? Patch[Key] : T[Key];
|
|
17
|
+
};
|
package/lib/utils.js
ADDED
package/lib/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,MAAM,YAAY,CAAC;AAOnD,MAAM,UAAU,YAAY,CAAmB,GAAM;IACnD,OAAyB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACxE,CAAC;AAID,MAAM,UAAU,WAAW,CAAsB,MAAqC;IACpF,OAAoC,MAAM,CAAC,MAAM,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import type { ITokenConfig, TokenType } from '@chevrotain/types';\nimport { createToken as chevcT } from 'chevrotain';\n\n/**\n * Check whether the first two types overlap, if no, return the 3th argument, else the 4th.\n */\nexport type CheckOverlap<T, U, V, W = never> = T & U extends never ? V : W;\n\nexport function unCapitalize<T extends string>(str: T): Uncapitalize<T> {\n return <Uncapitalize<T>> (str.charAt(0).toLowerCase() + str.slice(1));\n}\n\nexport type NamedToken<Name extends string> = TokenType & { name: Name };\n\nexport function createToken<Name extends string>(config: ITokenConfig & { name: Name }): NamedToken<Name> {\n return <TokenType & { name: Name }> chevcT(config);\n}\n\nexport type Patch<T extends object, Patch extends {[Key in keyof T ]?: any }> =\n {[Key in keyof T]: Key extends keyof Patch ? Patch[Key] : T[Key] };\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@traqula/core",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1-alpha.176+9400dc5",
|
|
5
|
+
"description": "Core components of Traqula",
|
|
5
6
|
"lsd:module": true,
|
|
6
7
|
"license": "MIT",
|
|
7
8
|
"repository": {
|
|
@@ -13,19 +14,18 @@
|
|
|
13
14
|
"url": "https://github.com/comunica/traqula/issues"
|
|
14
15
|
},
|
|
15
16
|
"sideEffects": false,
|
|
16
|
-
"type": "module",
|
|
17
|
-
"main": "lib/index.js",
|
|
18
17
|
"exports": {
|
|
19
18
|
"import": "./lib/index.js",
|
|
20
19
|
"require": "./lib/index.cjs"
|
|
21
20
|
},
|
|
21
|
+
"main": "lib/index.js",
|
|
22
22
|
"publishConfig": {
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
|
+
"lib/**/*.cjs",
|
|
26
27
|
"lib/**/*.d.ts",
|
|
27
28
|
"lib/**/*.js",
|
|
28
|
-
"lib/**/*.cjs",
|
|
29
29
|
"lib/**/*.js.map"
|
|
30
30
|
],
|
|
31
31
|
"engines": {
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@chevrotain/types": "^11.0.3"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "9400dc5f8bda1d8c2b3d1f4cf56b308995744ba0"
|
|
47
47
|
}
|
package/lib/Wildcard.d.ts
DELETED
package/lib/Wildcard.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export class Wildcard {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.value = '*';
|
|
4
|
-
this.termType = 'Wildcard';
|
|
5
|
-
// eslint-disable-next-line no-constructor-return
|
|
6
|
-
return WILDCARD ?? this;
|
|
7
|
-
}
|
|
8
|
-
equals(other) {
|
|
9
|
-
return Boolean(other && (this.termType === other.termType));
|
|
10
|
-
}
|
|
11
|
-
toJSON() {
|
|
12
|
-
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
13
|
-
const { value, ...rest } = this;
|
|
14
|
-
return rest;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
let WILDCARD;
|
|
18
|
-
WILDCARD = new Wildcard();
|
|
19
|
-
//# sourceMappingURL=Wildcard.js.map
|
package/lib/Wildcard.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Wildcard.js","sourceRoot":"","sources":["Wildcard.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,QAAQ;IAGnB;QAFO,UAAK,GAAW,GAAG,CAAC;QACpB,aAAQ,GAAW,UAAU,CAAC;QAEnC,iDAAiD;QACjD,OAAO,QAAQ,IAAI,IAAI,CAAC;IAC1B,CAAC;IAEM,MAAM,CAAC,KAA+C;QAC3D,OAAO,OAAO,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,QAAQ,KAAK,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC9D,CAAC;IAEM,MAAM;QACX,yDAAyD;QACzD,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC;QAChC,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAED,IAAI,QAA8B,CAAC;AACnC,QAAQ,GAAG,IAAI,QAAQ,EAAE,CAAC","sourcesContent":["export class Wildcard {\n public value = <const> '*';\n public termType = <const> 'Wildcard';\n public constructor() {\n // eslint-disable-next-line no-constructor-return\n return WILDCARD ?? this;\n }\n\n public equals(other: { termType: unknown } | undefined | null): boolean {\n return Boolean(other && (this.termType === other.termType));\n }\n\n public toJSON(): object {\n // eslint-disable-next-line unused-imports/no-unused-vars\n const { value, ...rest } = this;\n return rest;\n }\n}\n\nlet WILDCARD: Wildcard | undefined;\nWILDCARD = new Wildcard();\n"]}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { ParserMethod } from 'chevrotain';
|
|
2
|
-
import type { RuleDef } from './ruleDefTypes';
|
|
3
|
-
/**
|
|
4
|
-
* Get union-type of names used in list of ruledefs.
|
|
5
|
-
*/
|
|
6
|
-
export type RuleNamesFromList<T extends readonly RuleDef[]> = T[number]['name'];
|
|
7
|
-
/**
|
|
8
|
-
* Convert a list of ruledefs to a record that maps each rule name to its definition.
|
|
9
|
-
*/
|
|
10
|
-
export type RuleDefMap<RuleNames extends string> = {
|
|
11
|
-
[Key in RuleNames]: RuleDef<any, Key>;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Check whether the first two types overlap, if yes, return the 3th argument, else the 4th.
|
|
15
|
-
*/
|
|
16
|
-
export type CheckOverlap<T, U, V, W = never> = T & U extends never ? V : W;
|
|
17
|
-
/**
|
|
18
|
-
* Convert a list of RuleDefs to a Record with the name of the RuleDef as the key, matching the RuleDefMap type.
|
|
19
|
-
*/
|
|
20
|
-
export type RuleListToObject<T extends readonly RuleDef[], Names extends string = RuleNamesFromList<T>, Agg extends Record<string, RuleDef> = Record<never, never>> = T extends readonly [infer First, ...infer Rest] ? (First extends RuleDef ? (Rest extends readonly RuleDef[] ? (RuleListToObject<Rest, Names, {
|
|
21
|
-
[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K];
|
|
22
|
-
}>) : never) : never) : RuleDefMap<Names> & Agg;
|
|
23
|
-
export type ParserFromRules<Context, Names extends string, RuleDefs extends RuleDefMap<Names>> = {
|
|
24
|
-
[K in Names]: RuleDefs[K] extends RuleDef<Context, K, infer RET, infer ARGS> ? (input: string, context: Context, args: ARGS) => RET : never;
|
|
25
|
-
};
|
|
26
|
-
export type ParseMethodsFromRules<Context, Names extends string, RuleDefs extends RuleDefMap<Names>> = {
|
|
27
|
-
[K in Names]: RuleDefs[K] extends RuleDef<Context, K, infer RET, infer ARGS> ? ParserMethod<[Context, ARGS], RET> : never;
|
|
28
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"builderTypes.js","sourceRoot":"","sources":["builderTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { ParserMethod } from 'chevrotain';\nimport type {RuleDef} from './ruleDefTypes';\n\n/**\n * Get union-type of names used in list of ruledefs.\n */\nexport type RuleNamesFromList<T extends readonly RuleDef[]> = T[number]['name'];\n\n/**\n * Convert a list of ruledefs to a record that maps each rule name to its definition.\n */\nexport type RuleDefMap<RuleNames extends string> = {[Key in RuleNames]: RuleDef<any, Key> };\n\n/**\n * Check whether the first two types overlap, if yes, return the 3th argument, else the 4th.\n */\nexport type CheckOverlap<T, U, V, W = never> = T & U extends never ? V : W;\n\n/**\n * Convert a list of RuleDefs to a Record with the name of the RuleDef as the key, matching the RuleDefMap type.\n */\nexport type RuleListToObject<\n T extends readonly RuleDef[],\n Names extends string = RuleNamesFromList<T>,\n Agg extends Record<string, RuleDef> = Record<never, never>,\n> = T extends readonly [infer First, ...infer Rest] ? (\n First extends RuleDef ? (\n Rest extends readonly RuleDef[] ? (\n RuleListToObject<Rest, Names, {[K in keyof Agg | First['name']]: K extends First['name'] ? First : Agg[K] }>\n ) : never\n ) : never\n) : RuleDefMap<Names> & Agg;\n\nexport type ParserFromRules<Context, Names extends string, RuleDefs extends RuleDefMap<Names>> = {\n [K in Names]: RuleDefs[K] extends RuleDef<Context, K, infer RET, infer ARGS> ? (input: string, context: Context, args: ARGS) => RET : never\n};\n\nexport type ParseMethodsFromRules<Context, Names extends string, RuleDefs extends RuleDefMap<Names>> = {\n [K in Names]: RuleDefs[K] extends RuleDef<Context, K, infer RET, infer ARGS> ? ParserMethod<[Context, ARGS], RET> : never\n};\n"]}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { ILexerConfig, IParserConfig } from '@chevrotain/types';
|
|
2
|
-
import type { TokenType, TokenVocabulary } from 'chevrotain';
|
|
3
|
-
import { EmbeddedActionsParser } from 'chevrotain';
|
|
4
|
-
import type { CheckOverlap, ParseMethodsFromRules, ParserFromRules, RuleDefMap, RuleListToObject, RuleNamesFromList } from './builderTypes';
|
|
5
|
-
import type { RuleDef } from './ruleDefTypes';
|
|
6
|
-
/**
|
|
7
|
-
* The grammar builder. This is the core of traqula (besides using the amazing chevrotain framework).
|
|
8
|
-
* Using the builder you can create a grammar + AST creator.
|
|
9
|
-
* At any point in time, a parser can be constructed from the added rules.
|
|
10
|
-
* Constructing a parser will cause a validation which will validate the correctness of the grammar.
|
|
11
|
-
*/
|
|
12
|
-
export declare class Builder<Context, Names extends string, RuleDefs extends RuleDefMap<Names>> {
|
|
13
|
-
/**
|
|
14
|
-
* Create a builder from some initial grammar rules or an existing builder.
|
|
15
|
-
* If a builder is provided, a new copy will be created.
|
|
16
|
-
*/
|
|
17
|
-
static createBuilder<Rules extends readonly RuleDef[] = readonly RuleDef[], Context = Rules[0] extends RuleDef<infer context> ? context : never, Names extends string = RuleNamesFromList<Rules>, RuleDefs extends RuleDefMap<Names> = RuleListToObject<Rules>>(start: Rules | Builder<Context, Names, RuleDefs>): Builder<Context, Names, RuleDefs>;
|
|
18
|
-
private rules;
|
|
19
|
-
private constructor();
|
|
20
|
-
/**
|
|
21
|
-
* Change the implementation of an existing grammar rule.
|
|
22
|
-
*/
|
|
23
|
-
patchRule<U extends Names, RET, ARGS>(patch: RuleDef<Context, U, RET, ARGS>): Builder<Context, Names, {
|
|
24
|
-
[Key in Names]: Key extends U ? RuleDef<Context, Key, RET, ARGS> : (RuleDefs[Key] extends RuleDef<Context, Key> ? RuleDefs[Key] : never);
|
|
25
|
-
}>;
|
|
26
|
-
/**
|
|
27
|
-
* Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.
|
|
28
|
-
*/
|
|
29
|
-
addRuleRedundant<U extends string, RET, ARGS>(rule: RuleDef<Context, U, RET, ARGS>): Builder<Context, Names | U, {
|
|
30
|
-
[K in Names | U]: K extends U ? RuleDef<Context, K, RET, ARGS> : (K extends Names ? (RuleDefs[K] extends RuleDef<Context, K> ? RuleDefs[K] : never) : never);
|
|
31
|
-
}>;
|
|
32
|
-
/**
|
|
33
|
-
* Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
|
|
34
|
-
*/
|
|
35
|
-
addRule<U extends string, RET, ARGS>(rule: CheckOverlap<U, Names, RuleDef<Context, U, RET, ARGS>>): Builder<Context, Names | U, {
|
|
36
|
-
[K in Names | U]: K extends U ? RuleDef<Context, K, RET, ARGS> : (K extends Names ? (RuleDefs[K] extends RuleDef<Context, K> ? RuleDefs[K] : never) : never);
|
|
37
|
-
}>;
|
|
38
|
-
addMany<U extends readonly RuleDef<Context>[]>(...rules: CheckOverlap<RuleNamesFromList<U>, Names, U>): Builder<Context, Names | RuleNamesFromList<U>, {
|
|
39
|
-
[K in Names | RuleNamesFromList<U>]: K extends keyof RuleListToObject<typeof rules> ? (RuleListToObject<typeof rules>[K] extends RuleDef<Context, K> ? RuleListToObject<typeof rules>[K] : never) : (K extends Names ? (RuleDefs[K] extends RuleDef<Context, K> ? RuleDefs[K] : never) : never);
|
|
40
|
-
}>;
|
|
41
|
-
/**
|
|
42
|
-
* Delete a grammar rule by its name.
|
|
43
|
-
*/
|
|
44
|
-
deleteRule<U extends Names>(ruleName: U): Builder<Context, Exclude<Names, U>, {
|
|
45
|
-
[K in Exclude<Names, U>]: RuleDefs[K] extends RuleDef<Context, K> ? RuleDefs[K] : never;
|
|
46
|
-
}>;
|
|
47
|
-
/**
|
|
48
|
-
* Merge this grammar builder with another.
|
|
49
|
-
* It is best to merge the bigger grammar with the smaller one.
|
|
50
|
-
* If the two builders both have a grammar rule with the same name, no error will be thrown case they map to the same ruledef object.
|
|
51
|
-
* If they map to a different object, an error will be thrown.
|
|
52
|
-
* To fix this problem, the overridingRules array should contain a rule with the same conflicting name, this rule implementation will be used.
|
|
53
|
-
*/
|
|
54
|
-
merge<OtherNames extends string, OtherRules extends RuleDefMap<OtherNames>, OW extends readonly RuleDef<Context>[]>(builder: Builder<Context, OtherNames, OtherRules>, overridingRules: OW): Builder<Context, Names | OtherNames | RuleNamesFromList<OW>, {
|
|
55
|
-
[K in Names | OtherNames | RuleNamesFromList<OW>]: K extends keyof RuleListToObject<OW> ? (RuleListToObject<OW>[K] extends RuleDef<Context, K> ? RuleListToObject<OW>[K] : never) : (K extends Names ? (RuleDefs[K] extends RuleDef<Context, K> ? RuleDefs[K] : never) : K extends OtherNames ? (OtherRules[K] extends RuleDef<Context, K> ? OtherRules[K] : never) : never);
|
|
56
|
-
}>;
|
|
57
|
-
consumeToParser({ tokenVocabulary, parserConfig, lexerConfig }: {
|
|
58
|
-
tokenVocabulary: TokenType[];
|
|
59
|
-
parserConfig?: IParserConfig;
|
|
60
|
-
lexerConfig?: ILexerConfig;
|
|
61
|
-
}): ParserFromRules<Context, Names, RuleDefs>;
|
|
62
|
-
consume({ tokenVocabulary, config }: {
|
|
63
|
-
tokenVocabulary: TokenVocabulary;
|
|
64
|
-
config?: IParserConfig;
|
|
65
|
-
}): EmbeddedActionsParser & ParseMethodsFromRules<Context, Names, RuleDefs> & {
|
|
66
|
-
setContext: (context: Context) => void;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
import { EmbeddedActionsParser, Lexer } from 'chevrotain';
|
|
2
|
-
/**
|
|
3
|
-
* Converts a list of ruledefs to a record mapping a name to the corresponding ruledef.
|
|
4
|
-
*/
|
|
5
|
-
function listToRuleDefMap(rules) {
|
|
6
|
-
const newRules = {};
|
|
7
|
-
for (const rule of rules) {
|
|
8
|
-
newRules[rule.name] = rule;
|
|
9
|
-
}
|
|
10
|
-
return newRules;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* The grammar builder. This is the core of traqula (besides using the amazing chevrotain framework).
|
|
14
|
-
* Using the builder you can create a grammar + AST creator.
|
|
15
|
-
* At any point in time, a parser can be constructed from the added rules.
|
|
16
|
-
* Constructing a parser will cause a validation which will validate the correctness of the grammar.
|
|
17
|
-
*/
|
|
18
|
-
// This code is wild so other code can be simple.
|
|
19
|
-
export class Builder {
|
|
20
|
-
/**
|
|
21
|
-
* Create a builder from some initial grammar rules or an existing builder.
|
|
22
|
-
* If a builder is provided, a new copy will be created.
|
|
23
|
-
*/
|
|
24
|
-
static createBuilder(start) {
|
|
25
|
-
if (start instanceof Builder) {
|
|
26
|
-
return new Builder({ ...start.rules });
|
|
27
|
-
}
|
|
28
|
-
return new Builder(listToRuleDefMap(start));
|
|
29
|
-
}
|
|
30
|
-
constructor(startRules) {
|
|
31
|
-
this.rules = startRules;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Change the implementation of an existing grammar rule.
|
|
35
|
-
*/
|
|
36
|
-
patchRule(patch) {
|
|
37
|
-
const self = this;
|
|
38
|
-
self.rules[patch.name] = patch;
|
|
39
|
-
return self;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown.
|
|
43
|
-
*/
|
|
44
|
-
addRuleRedundant(rule) {
|
|
45
|
-
const self = this;
|
|
46
|
-
const rules = self.rules;
|
|
47
|
-
if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {
|
|
48
|
-
throw new Error(`Rule ${rule.name} already exists in the builder`);
|
|
49
|
-
}
|
|
50
|
-
rules[rule.name] = rule;
|
|
51
|
-
return self;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar.
|
|
55
|
-
*/
|
|
56
|
-
addRule(rule) {
|
|
57
|
-
return this.addRuleRedundant(rule);
|
|
58
|
-
}
|
|
59
|
-
addMany(...rules) {
|
|
60
|
-
this.rules = { ...this.rules, ...listToRuleDefMap(rules) };
|
|
61
|
-
return this;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Delete a grammar rule by its name.
|
|
65
|
-
*/
|
|
66
|
-
deleteRule(ruleName) {
|
|
67
|
-
delete this.rules[ruleName];
|
|
68
|
-
return this;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Merge this grammar builder with another.
|
|
72
|
-
* It is best to merge the bigger grammar with the smaller one.
|
|
73
|
-
* If the two builders both have a grammar rule with the same name, no error will be thrown case they map to the same ruledef object.
|
|
74
|
-
* If they map to a different object, an error will be thrown.
|
|
75
|
-
* To fix this problem, the overridingRules array should contain a rule with the same conflicting name, this rule implementation will be used.
|
|
76
|
-
*/
|
|
77
|
-
merge(builder, overridingRules) {
|
|
78
|
-
// Assume the other grammar is bigger than yours. So start from that one and add this one
|
|
79
|
-
const otherRules = { ...builder.rules };
|
|
80
|
-
const myRules = this.rules;
|
|
81
|
-
for (const rule of Object.values(myRules)) {
|
|
82
|
-
if (otherRules[rule.name] === undefined) {
|
|
83
|
-
otherRules[rule.name] = rule;
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
const existingRule = otherRules[rule.name];
|
|
87
|
-
// If same rule, no issue, move on. Else
|
|
88
|
-
if (existingRule !== rule) {
|
|
89
|
-
const override = overridingRules.find(x => x.name === rule.name);
|
|
90
|
-
// If override specified, take override, else, inform user that there is a conflict
|
|
91
|
-
if (override) {
|
|
92
|
-
otherRules[rule.name] = override;
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
throw new Error(`Rule with name "${rule.name}" already exists in the builder, specify an override to resolve conflict`);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
this.rules = otherRules;
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
consumeToParser({ tokenVocabulary, parserConfig = {}, lexerConfig = {} }) {
|
|
104
|
-
const lexer = new Lexer(tokenVocabulary, {
|
|
105
|
-
positionTracking: 'onlyStart',
|
|
106
|
-
recoveryEnabled: false,
|
|
107
|
-
// SafeMode: true,
|
|
108
|
-
// SkipValidations: true,
|
|
109
|
-
ensureOptimizations: true,
|
|
110
|
-
...lexerConfig,
|
|
111
|
-
});
|
|
112
|
-
// get the chevrotain parser
|
|
113
|
-
const parser = this.consume({ tokenVocabulary, config: parserConfig });
|
|
114
|
-
// Start building a parser that does not pass input using a state, but instead gets it as a function argument.
|
|
115
|
-
const selfSufficientParser = {};
|
|
116
|
-
// To do that, we need to create a wrapper for each parser rule.
|
|
117
|
-
for (const rule of Object.values(this.rules)) {
|
|
118
|
-
selfSufficientParser[rule.name] = ((input, context, arg) => {
|
|
119
|
-
// Transform input in accordance to 19.2
|
|
120
|
-
input = input.replaceAll(/\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{8})/gu, (_, unicode4, unicode8) => {
|
|
121
|
-
if (unicode4) {
|
|
122
|
-
const charCode = Number.parseInt(unicode4, 16);
|
|
123
|
-
return String.fromCodePoint(charCode);
|
|
124
|
-
}
|
|
125
|
-
const charCode = Number.parseInt(unicode8, 16);
|
|
126
|
-
if (charCode < 0xFFFF) {
|
|
127
|
-
return String.fromCodePoint(charCode);
|
|
128
|
-
}
|
|
129
|
-
const substractedCharCode = charCode - 0x10000;
|
|
130
|
-
return String.fromCodePoint(0xD800 + (substractedCharCode >> 10), 0xDC00 + (substractedCharCode & 0x3FF));
|
|
131
|
-
});
|
|
132
|
-
// Test for invalid unicode surrogate pairs
|
|
133
|
-
if (/[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/u.test(input)) {
|
|
134
|
-
throw new Error(`Invalid unicode codepoint of surrogate pair without corresponding codepoint`);
|
|
135
|
-
}
|
|
136
|
-
const lexResult = lexer.tokenize(input);
|
|
137
|
-
// This also resets the parser
|
|
138
|
-
parser.input = lexResult.tokens;
|
|
139
|
-
parser.setContext(context);
|
|
140
|
-
const result = parser[rule.name](context, arg);
|
|
141
|
-
if (parser.errors.length > 0) {
|
|
142
|
-
// Console.log(lexResult.tokens);
|
|
143
|
-
throw new Error(`Parse error on line ${parser.errors.map(x => x.token.startLine).join(', ')}
|
|
144
|
-
${parser.errors.map(x => `${x.token.startLine}: ${x.message}`).join('\n')}`);
|
|
145
|
-
}
|
|
146
|
-
return result;
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
return selfSufficientParser;
|
|
150
|
-
}
|
|
151
|
-
consume({ tokenVocabulary, config = {} }) {
|
|
152
|
-
const rules = this.rules;
|
|
153
|
-
class MyParser extends EmbeddedActionsParser {
|
|
154
|
-
getSafeContext() {
|
|
155
|
-
if (this.context === undefined) {
|
|
156
|
-
throw new Error('context was not correctly set');
|
|
157
|
-
}
|
|
158
|
-
return this.context;
|
|
159
|
-
}
|
|
160
|
-
setContext(context) {
|
|
161
|
-
this.context = context;
|
|
162
|
-
}
|
|
163
|
-
constructor() {
|
|
164
|
-
super(tokenVocabulary, {
|
|
165
|
-
// RecoveryEnabled: true,
|
|
166
|
-
maxLookahead: 2,
|
|
167
|
-
// SkipValidations: true,
|
|
168
|
-
...config,
|
|
169
|
-
});
|
|
170
|
-
this.context = undefined;
|
|
171
|
-
const selfRef = this.getSelfRef();
|
|
172
|
-
const implArgs = {
|
|
173
|
-
...selfRef,
|
|
174
|
-
cache: new WeakMap(),
|
|
175
|
-
};
|
|
176
|
-
for (const rule of Object.values(rules)) {
|
|
177
|
-
this[rule.name] = this.RULE(rule.name, rule.impl(implArgs));
|
|
178
|
-
}
|
|
179
|
-
this.performSelfAnalysis();
|
|
180
|
-
}
|
|
181
|
-
getSelfRef() {
|
|
182
|
-
const subRuleImpl = (chevrotainSubrule) => {
|
|
183
|
-
return ((cstDef, arg) => {
|
|
184
|
-
return chevrotainSubrule(this[cstDef.name], { ARGS: [this.context, arg] });
|
|
185
|
-
});
|
|
186
|
-
};
|
|
187
|
-
return {
|
|
188
|
-
CONSUME: (tokenType, option) => this.CONSUME(tokenType, option),
|
|
189
|
-
CONSUME1: (tokenType, option) => this.CONSUME1(tokenType, option),
|
|
190
|
-
CONSUME2: (tokenType, option) => this.CONSUME2(tokenType, option),
|
|
191
|
-
CONSUME3: (tokenType, option) => this.CONSUME3(tokenType, option),
|
|
192
|
-
CONSUME4: (tokenType, option) => this.CONSUME4(tokenType, option),
|
|
193
|
-
CONSUME5: (tokenType, option) => this.CONSUME5(tokenType, option),
|
|
194
|
-
CONSUME6: (tokenType, option) => this.CONSUME6(tokenType, option),
|
|
195
|
-
CONSUME7: (tokenType, option) => this.CONSUME7(tokenType, option),
|
|
196
|
-
CONSUME8: (tokenType, option) => this.CONSUME8(tokenType, option),
|
|
197
|
-
CONSUME9: (tokenType, option) => this.CONSUME9(tokenType, option),
|
|
198
|
-
OPTION: actionORMethodDef => this.OPTION(actionORMethodDef),
|
|
199
|
-
OPTION1: actionORMethodDef => this.OPTION1(actionORMethodDef),
|
|
200
|
-
OPTION2: actionORMethodDef => this.OPTION2(actionORMethodDef),
|
|
201
|
-
OPTION3: actionORMethodDef => this.OPTION3(actionORMethodDef),
|
|
202
|
-
OPTION4: actionORMethodDef => this.OPTION4(actionORMethodDef),
|
|
203
|
-
OPTION5: actionORMethodDef => this.OPTION5(actionORMethodDef),
|
|
204
|
-
OPTION6: actionORMethodDef => this.OPTION6(actionORMethodDef),
|
|
205
|
-
OPTION7: actionORMethodDef => this.OPTION7(actionORMethodDef),
|
|
206
|
-
OPTION8: actionORMethodDef => this.OPTION8(actionORMethodDef),
|
|
207
|
-
OPTION9: actionORMethodDef => this.OPTION9(actionORMethodDef),
|
|
208
|
-
OR: altsOrOpts => this.OR(altsOrOpts),
|
|
209
|
-
OR1: altsOrOpts => this.OR1(altsOrOpts),
|
|
210
|
-
OR2: altsOrOpts => this.OR2(altsOrOpts),
|
|
211
|
-
OR3: altsOrOpts => this.OR3(altsOrOpts),
|
|
212
|
-
OR4: altsOrOpts => this.OR4(altsOrOpts),
|
|
213
|
-
OR5: altsOrOpts => this.OR5(altsOrOpts),
|
|
214
|
-
OR6: altsOrOpts => this.OR6(altsOrOpts),
|
|
215
|
-
OR7: altsOrOpts => this.OR7(altsOrOpts),
|
|
216
|
-
OR8: altsOrOpts => this.OR8(altsOrOpts),
|
|
217
|
-
OR9: altsOrOpts => this.OR9(altsOrOpts),
|
|
218
|
-
MANY: actionORMethodDef => this.MANY(actionORMethodDef),
|
|
219
|
-
MANY1: actionORMethodDef => this.MANY1(actionORMethodDef),
|
|
220
|
-
MANY2: actionORMethodDef => this.MANY2(actionORMethodDef),
|
|
221
|
-
MANY3: actionORMethodDef => this.MANY3(actionORMethodDef),
|
|
222
|
-
MANY4: actionORMethodDef => this.MANY4(actionORMethodDef),
|
|
223
|
-
MANY5: actionORMethodDef => this.MANY5(actionORMethodDef),
|
|
224
|
-
MANY6: actionORMethodDef => this.MANY6(actionORMethodDef),
|
|
225
|
-
MANY7: actionORMethodDef => this.MANY7(actionORMethodDef),
|
|
226
|
-
MANY8: actionORMethodDef => this.MANY8(actionORMethodDef),
|
|
227
|
-
MANY9: actionORMethodDef => this.MANY9(actionORMethodDef),
|
|
228
|
-
MANY_SEP: options => this.MANY_SEP(options),
|
|
229
|
-
MANY_SEP1: options => this.MANY_SEP1(options),
|
|
230
|
-
MANY_SEP2: options => this.MANY_SEP2(options),
|
|
231
|
-
MANY_SEP3: options => this.MANY_SEP3(options),
|
|
232
|
-
MANY_SEP4: options => this.MANY_SEP4(options),
|
|
233
|
-
MANY_SEP5: options => this.MANY_SEP5(options),
|
|
234
|
-
MANY_SEP6: options => this.MANY_SEP6(options),
|
|
235
|
-
MANY_SEP7: options => this.MANY_SEP7(options),
|
|
236
|
-
MANY_SEP8: options => this.MANY_SEP8(options),
|
|
237
|
-
MANY_SEP9: options => this.MANY_SEP9(options),
|
|
238
|
-
AT_LEAST_ONE: actionORMethodDef => this.AT_LEAST_ONE(actionORMethodDef),
|
|
239
|
-
AT_LEAST_ONE1: actionORMethodDef => this.AT_LEAST_ONE1(actionORMethodDef),
|
|
240
|
-
AT_LEAST_ONE2: actionORMethodDef => this.AT_LEAST_ONE2(actionORMethodDef),
|
|
241
|
-
AT_LEAST_ONE3: actionORMethodDef => this.AT_LEAST_ONE3(actionORMethodDef),
|
|
242
|
-
AT_LEAST_ONE4: actionORMethodDef => this.AT_LEAST_ONE4(actionORMethodDef),
|
|
243
|
-
AT_LEAST_ONE5: actionORMethodDef => this.AT_LEAST_ONE5(actionORMethodDef),
|
|
244
|
-
AT_LEAST_ONE6: actionORMethodDef => this.AT_LEAST_ONE6(actionORMethodDef),
|
|
245
|
-
AT_LEAST_ONE7: actionORMethodDef => this.AT_LEAST_ONE7(actionORMethodDef),
|
|
246
|
-
AT_LEAST_ONE8: actionORMethodDef => this.AT_LEAST_ONE8(actionORMethodDef),
|
|
247
|
-
AT_LEAST_ONE9: actionORMethodDef => this.AT_LEAST_ONE9(actionORMethodDef),
|
|
248
|
-
AT_LEAST_ONE_SEP: options => this.AT_LEAST_ONE_SEP(options),
|
|
249
|
-
AT_LEAST_ONE_SEP1: options => this.AT_LEAST_ONE_SEP1(options),
|
|
250
|
-
AT_LEAST_ONE_SEP2: options => this.AT_LEAST_ONE_SEP2(options),
|
|
251
|
-
AT_LEAST_ONE_SEP3: options => this.AT_LEAST_ONE_SEP3(options),
|
|
252
|
-
AT_LEAST_ONE_SEP4: options => this.AT_LEAST_ONE_SEP4(options),
|
|
253
|
-
AT_LEAST_ONE_SEP5: options => this.AT_LEAST_ONE_SEP5(options),
|
|
254
|
-
AT_LEAST_ONE_SEP6: options => this.AT_LEAST_ONE_SEP6(options),
|
|
255
|
-
AT_LEAST_ONE_SEP7: options => this.AT_LEAST_ONE_SEP7(options),
|
|
256
|
-
AT_LEAST_ONE_SEP8: options => this.AT_LEAST_ONE_SEP8(options),
|
|
257
|
-
AT_LEAST_ONE_SEP9: options => this.AT_LEAST_ONE_SEP9(options),
|
|
258
|
-
ACTION: func => this.ACTION(func),
|
|
259
|
-
BACKTRACK: (cstDef, ...args) => {
|
|
260
|
-
try {
|
|
261
|
-
return this.BACKTRACK(this[cstDef.name], { ARGS: args });
|
|
262
|
-
}
|
|
263
|
-
catch (error) {
|
|
264
|
-
throw error;
|
|
265
|
-
}
|
|
266
|
-
},
|
|
267
|
-
SUBRULE: subRuleImpl((rule, args) => this.SUBRULE(rule, args)),
|
|
268
|
-
SUBRULE1: subRuleImpl((rule, args) => this.SUBRULE1(rule, args)),
|
|
269
|
-
SUBRULE2: subRuleImpl((rule, args) => this.SUBRULE2(rule, args)),
|
|
270
|
-
SUBRULE3: subRuleImpl((rule, args) => this.SUBRULE3(rule, args)),
|
|
271
|
-
SUBRULE4: subRuleImpl((rule, args) => this.SUBRULE4(rule, args)),
|
|
272
|
-
SUBRULE5: subRuleImpl((rule, args) => this.SUBRULE5(rule, args)),
|
|
273
|
-
SUBRULE6: subRuleImpl((rule, args) => this.SUBRULE6(rule, args)),
|
|
274
|
-
SUBRULE7: subRuleImpl((rule, args) => this.SUBRULE7(rule, args)),
|
|
275
|
-
SUBRULE8: subRuleImpl((rule, args) => this.SUBRULE8(rule, args)),
|
|
276
|
-
SUBRULE9: subRuleImpl((rule, args) => this.SUBRULE9(rule, args)),
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
return new MyParser();
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
//# sourceMappingURL=parserBuilder.js.map
|