@traqula/core 0.0.24 → 0.0.25
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 +2 -0
- package/dist/cjs/lib/AstCoreFactory.js +50 -5
- package/dist/cjs/lib/AstCoreFactory.js.map +1 -1
- package/dist/cjs/lib/generator-builder/dynamicGenerator.js +20 -0
- package/dist/cjs/lib/generator-builder/dynamicGenerator.js.map +1 -1
- package/dist/cjs/lib/parser-builder/dynamicParser.js +1 -0
- package/dist/cjs/lib/parser-builder/dynamicParser.js.map +1 -1
- package/dist/cjs/lib/parser-builder/parserBuilder.js +2 -2
- package/dist/cjs/lib/parser-builder/parserBuilder.js.map +1 -1
- package/dist/cjs/lib/transformers/TransformerObject.js.map +1 -1
- package/dist/cjs/lib/transformers/TransformerSubTyped.js +30 -40
- package/dist/cjs/lib/transformers/TransformerSubTyped.js.map +1 -1
- package/dist/cjs/lib/transformers/TransformerTyped.js +23 -35
- package/dist/cjs/lib/transformers/TransformerTyped.js.map +1 -1
- package/dist/cjs/lib/types.js.map +1 -1
- package/dist/cjs/lib/utils.js +7 -0
- package/dist/cjs/lib/utils.js.map +1 -1
- package/dist/esm/lib/AstCoreFactory.d.ts +19 -7
- package/dist/esm/lib/AstCoreFactory.js +50 -5
- package/dist/esm/lib/AstCoreFactory.js.map +1 -1
- package/dist/esm/lib/generator-builder/dynamicGenerator.d.ts +5 -0
- package/dist/esm/lib/generator-builder/dynamicGenerator.js +20 -0
- package/dist/esm/lib/generator-builder/dynamicGenerator.js.map +1 -1
- package/dist/esm/lib/parser-builder/dynamicParser.js +1 -0
- package/dist/esm/lib/parser-builder/dynamicParser.js.map +1 -1
- package/dist/esm/lib/parser-builder/parserBuilder.js +2 -2
- package/dist/esm/lib/parser-builder/parserBuilder.js.map +1 -1
- package/dist/esm/lib/transformers/TransformerObject.d.ts +1 -1
- package/dist/esm/lib/transformers/TransformerObject.js.map +1 -1
- package/dist/esm/lib/transformers/TransformerSubTyped.d.ts +31 -22
- package/dist/esm/lib/transformers/TransformerSubTyped.js +30 -40
- package/dist/esm/lib/transformers/TransformerSubTyped.js.map +1 -1
- package/dist/esm/lib/transformers/TransformerTyped.d.ts +24 -20
- package/dist/esm/lib/transformers/TransformerTyped.js +23 -35
- package/dist/esm/lib/transformers/TransformerTyped.js.map +1 -1
- package/dist/esm/lib/types.d.ts +67 -5
- package/dist/esm/lib/types.js.map +1 -1
- package/dist/esm/lib/utils.d.ts +10 -0
- package/dist/esm/lib/utils.js +7 -0
- package/dist/esm/lib/utils.js.map +1 -1
- package/package.json +3 -3
|
@@ -11,12 +11,20 @@ class TransformerSubTyped extends TransformerTyped_js_1.TransformerTyped {
|
|
|
11
11
|
return new TransformerSubTyped({ ...this.defaultContext, ...newDefaultContext }, { ...this.defaultNodePreVisitor, ...newDefaultNodePreVisitor });
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @param nodeCallBacks
|
|
19
|
-
*
|
|
14
|
+
* Transform a single node ({@link Typed}).
|
|
15
|
+
* Similar to {@link this.transformNode} but also allowing you to target the subTypes.
|
|
16
|
+
* @param startObject the object from which we will start the transformation,
|
|
17
|
+
* potentially visiting and transforming its descendants along the way.
|
|
18
|
+
* @param nodeCallBacks a dictionary mapping the various operation types to objects optionally
|
|
19
|
+
* containing preVisitor and transformer.
|
|
20
|
+
* The preVisitor allows you to provide {@link TransformContext} for the current object,
|
|
21
|
+
* altering how it will be transformed.
|
|
22
|
+
* The transformer allows you to manipulate the copy of the current object,
|
|
23
|
+
* and expects you to return the value that should take the current objects place.
|
|
24
|
+
* @param nodeSpecificCallBacks Same as nodeCallBacks but using an additional level of indirection to
|
|
25
|
+
* indicate the subType.
|
|
26
|
+
* @return the result of transforming the requested descendant operations (based on the preVisitor)
|
|
27
|
+
* using a transformer that works its way back up from the descendant to the startObject.
|
|
20
28
|
*/
|
|
21
29
|
transformNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
|
|
22
30
|
const transformWrapper = (copy, orig) => {
|
|
@@ -50,8 +58,22 @@ class TransformerSubTyped extends TransformerTyped_js_1.TransformerTyped {
|
|
|
50
58
|
return this.transformObject(startObject, transformWrapper, preVisitWrapper);
|
|
51
59
|
}
|
|
52
60
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
61
|
+
* Visit a selected subTree given a startObject, steering the visits based on {@link Typed} nodes.
|
|
62
|
+
* Similar to {@link this.visitNode}, but also allowing you to target subTypes.
|
|
63
|
+
* Will call the preVisitor on the outer distinct, then the visitor of the special distinct,
|
|
64
|
+
* followed by the visiting the outer distinct, printing '231'.
|
|
65
|
+
* The pre-visitor visits starting from the root, going deeper, while the actual visitor goes in reverse.
|
|
66
|
+
* @param startObject the object from which we will start visiting,
|
|
67
|
+
* potentially visiting its descendants along the way.
|
|
68
|
+
* @param nodeCallBacks a dictionary mapping the various operation types to objects optionally
|
|
69
|
+
* containing preVisitor and visitor.
|
|
70
|
+
* The preVisitor allows you to provide {@link VisitContext} for the current object,
|
|
71
|
+
* altering how it will be visited.
|
|
72
|
+
* The visitor allows you to visit the object from deepest to the outermost object.
|
|
73
|
+
* This is useful if you for example want to manipulate the objects you visit during your visits,
|
|
74
|
+
* similar to {@link mapOperation}.
|
|
75
|
+
* @param nodeSpecificCallBacks Same as nodeCallBacks but using an additional level of indirection to
|
|
76
|
+
* indicate the subType.
|
|
55
77
|
*/
|
|
56
78
|
visitNodeSpecific(startObject, nodeCallBacks, nodeSpecificCallBacks) {
|
|
57
79
|
const visitWrapper = (curObject) => {
|
|
@@ -86,38 +108,6 @@ class TransformerSubTyped extends TransformerTyped_js_1.TransformerTyped {
|
|
|
86
108
|
};
|
|
87
109
|
this.visitObject(startObject, visitWrapper, preVisitWrapper);
|
|
88
110
|
}
|
|
89
|
-
/**
|
|
90
|
-
* Similar to {@link this.traverseNodes} but also allows you to match based on the subtype of objects.
|
|
91
|
-
* @param currentNode
|
|
92
|
-
* @param traverseNode
|
|
93
|
-
* @param traverseSubNode
|
|
94
|
-
*/
|
|
95
|
-
traverseSubNodes(currentNode, traverseNode, traverseSubNode) {
|
|
96
|
-
let didShortCut = false;
|
|
97
|
-
const recurse = (curNode) => {
|
|
98
|
-
let traverser;
|
|
99
|
-
const subObj = traverseSubNode[curNode.type];
|
|
100
|
-
if (subObj) {
|
|
101
|
-
traverser = subObj[curNode.subType];
|
|
102
|
-
}
|
|
103
|
-
if (!traverser) {
|
|
104
|
-
traverser = traverseNode[curNode.type];
|
|
105
|
-
}
|
|
106
|
-
if (traverser) {
|
|
107
|
-
const { next, shortcut } = traverser(curNode);
|
|
108
|
-
didShortCut = shortcut ?? false;
|
|
109
|
-
if (!didShortCut) {
|
|
110
|
-
for (const node of next ?? []) {
|
|
111
|
-
if (didShortCut) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
recurse(node);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
recurse(currentNode);
|
|
120
|
-
}
|
|
121
111
|
}
|
|
122
112
|
exports.TransformerSubTyped = TransformerSubTyped;
|
|
123
113
|
//# sourceMappingURL=TransformerSubTyped.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransformerSubTyped.js","sourceRoot":"","sources":["../../../../lib/transformers/TransformerSubTyped.ts"],"names":[],"mappings":";;;AAOA,+DAAyD;AAEzD,MAAa,mBAAyC,SAAQ,sCAAuB;IACnF,YACE,iBAAmC,EAAE,EACrC,wBAAsD,EAAE;QAExD,KAAK,CAAC,cAAc,EAAE,qBAAqB,CAAC,CAAC;IAC/C,CAAC;IAAA,CAAC;IAEc,KAAK,CACnB,oBAAsC,EAAE,EACxC,2BAAyD,EAAE;QAE3D,OAAO,IAAI,mBAAmB,CAC5B,EAAE,GAAG,IAAI,CAAC,cAAc,EAAE,GAAG,iBAAiB,EAAE,EAChD,EAAE,GA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;QACH,CAAC,CAAC;QAEF,OAAO,CAAC,WAAW,CAAC,CAAC;IACvB,CAAC;CACF;AA/JD,kDA+JC","sourcesContent":["import type { SubTyped, Typed } from '../types.js';\nimport type {\n SelectiveTraversalContext,\n TransformContext,\n VisitContext,\n} from './TransformerObject.js';\nimport type { DefaultNodePreVisitor, Safeness, SafeWrap } from './TransformerTyped.js';\nimport { TransformerTyped } from './TransformerTyped.js';\n\nexport class TransformerSubTyped<Nodes extends Typed> extends TransformerTyped<Nodes> {\n public constructor(\n defaultContext: TransformContext = {},\n defaultNodePreVisitor: DefaultNodePreVisitor<Nodes> = {},\n ) {\n super(defaultContext, defaultNodePreVisitor);\n };\n\n public override clone(\n newDefaultContext: TransformContext = {},\n newDefaultNodePreVisitor: DefaultNodePreVisitor<Nodes> = {},\n ): TransformerSubTyped<Nodes> {\n return new TransformerSubTyped(\n { ...this.defaultContext, ...newDefaultContext },\n { ...this.defaultNodePreVisitor, ...newDefaultNodePreVisitor },\n );\n }\n\n /**\n * Shares the functionality and first two arguments with {@link this.transformNode}.\n * The third argument allows you to also transform based on the subType of objects.\n * Note that when a callback for the subtype is provided, the callback for the general type is **NOT** executed.\n * @param startObject\n * @param nodeCallBacks\n * @param nodeSpecificCallBacks\n */\n public transformNodeSpecific<Safe extends Safeness = 'safe', OutType = unknown>(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n transform?: (copy: SafeWrap<Safe, Extract<Nodes, Typed<T>>>, orig: Extract<Nodes, Typed<T>>) => unknown;\n preVisitor?: (orig: Extract<Nodes, Typed<T>>) => TransformContext;\n }},\n nodeSpecificCallBacks: {[Type in Nodes['type']]?: {\n [SubType in Extract<Nodes, SubTyped<Type>>['subType']]?: {\n transform?: (op: SafeWrap<Safe, Extract<Nodes, SubTyped<Type, SubType>>>) => unknown;\n preVisitor?: (op: Extract<Nodes, SubTyped<Type, SubType>>) => TransformContext;\n }}},\n ): Safe extends 'unsafe' ? OutType : unknown {\n const transformWrapper = (copy: object, orig: object): unknown => {\n let ogTransform: ((copy: any, orig: any) => unknown) | undefined;\n const casted = <SubTyped<Nodes['type']>>copy;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogTransform = specific[<keyof typeof specific> casted.subType]?.transform;\n }\n if (!ogTransform) {\n ogTransform = nodeCallBacks[casted.type]?.transform;\n }\n }\n return ogTransform ? ogTransform(casted, orig) : copy;\n };\n const preVisitWrapper = (curObject: object): VisitContext => {\n let ogPreVisit: ((node: any) => VisitContext) | undefined;\n const casted = <SubTyped<Nodes['type']>>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogPreVisit = specific[<keyof typeof specific> casted.subType]?.preVisitor;\n }\n if (!ogPreVisit) {\n ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;\n }\n }\n return ogPreVisit ? ogPreVisit(casted) : {};\n };\n return <any> this.transformObject(startObject, transformWrapper, preVisitWrapper);\n }\n\n /**\n * Similar to {@link this.visitNode} but also allows you to match based on the subtype of objects.\n * When both nodeCallBack and NodeSpecific callBack are matched, will only visit nodeSpecifCallback\n */\n public visitNodeSpecific(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n visitor?: (op: Extract<Nodes, Typed<T>>) => void;\n preVisitor?: (op: Extract<Nodes, Typed<T>>) => VisitContext;\n }},\n nodeSpecificCallBacks: {[Type in Nodes['type']]?:\n {[Subtype in Extract<Nodes, SubTyped<Type>>['subType']]?: {\n visitor?: (op: Extract<Nodes, SubTyped<Type, Subtype>>) => void;\n preVisitor?: (op: Extract<Nodes, SubTyped<Type, Subtype>>) => VisitContext;\n }}},\n ): void {\n const visitWrapper = (curObject: object): void => {\n let ogTransform: ((node: any) => void) | undefined;\n const casted = <SubTyped<Nodes['type']>>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogTransform = specific[<keyof typeof specific> casted.subType]?.visitor;\n }\n if (!ogTransform) {\n ogTransform = nodeCallBacks[casted.type]?.visitor;\n }\n }\n if (ogTransform) {\n ogTransform(casted);\n }\n };\n const preVisitWrapper = (curObject: object): VisitContext => {\n let ogPreVisit: ((node: any) => VisitContext) | undefined;\n const casted = <SubTyped<Nodes['type']>>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogPreVisit = specific[<keyof typeof specific> casted.subType]?.preVisitor;\n }\n if (!ogPreVisit) {\n ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;\n }\n }\n return ogPreVisit ? ogPreVisit(casted) : {};\n };\n this.visitObject(startObject, visitWrapper, preVisitWrapper);\n }\n\n /**\n * Similar to {@link this.traverseNodes} but also allows you to match based on the subtype of objects.\n * @param currentNode\n * @param traverseNode\n * @param traverseSubNode\n */\n public traverseSubNodes(\n currentNode: Nodes,\n traverseNode: {[Type in Nodes['type']]?:\n (op: Extract<Nodes, Typed<Type>>) => SelectiveTraversalContext<Nodes> },\n traverseSubNode: {[Type in Nodes['type']]?:\n {[Subtype in Extract<Nodes, SubTyped<Type>>['subType']]?:\n (op: Extract<Nodes, SubTyped<Type, Subtype>>) => SelectiveTraversalContext<Nodes> }},\n ): void {\n let didShortCut = false;\n\n const recurse = (curNode: Nodes): void => {\n let traverser: ((call: any) => SelectiveTraversalContext<Nodes>) | undefined;\n const subObj = traverseSubNode[<Nodes['type']>curNode.type];\n if (subObj) {\n traverser = subObj[<keyof typeof subObj>curNode.subType];\n }\n if (!traverser) {\n traverser = traverseNode[<Nodes['type']>curNode.type];\n }\n if (traverser) {\n const { next, shortcut } = traverser(<any>curNode);\n didShortCut = shortcut ?? false;\n if (!didShortCut) {\n for (const node of next ?? []) {\n if (didShortCut) {\n return;\n }\n recurse(node);\n }\n }\n }\n };\n\n recurse(currentNode);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"TransformerSubTyped.js","sourceRoot":"","sources":["../../../../lib/transformers/TransformerSubTyped.ts"],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;gBAChB,WAAW,CAAC,MAAM,CAAC,CAAC;YACtB,CAAC;QACH,CAAC,CAAC;QACF,MAAM,eAAe,GAAG,CAAC,SAAiB,EAAgB,EAAE;YAC1D,IAAI,UAAqD,CAAC;YAC1D,MAAM,MAAM,GAA4B,SAAS,CAAC;YAClD,IAAI,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBAClC,MAAM,QAAQ,GAAG,qBAAqB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;gBACpD,IAAI,QAAQ,EAAE,CAAC;oBACb,UAAU,GAAG,QAAQ,CAAyB,MAAM,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC;gBAC5E,CAAC;gBACD,IAAI,CAAC,UAAU,EAAE,CAAC;oBAChB,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,UAAU,CAAC;gBACtD,CAAC;YACH,CAAC;YACD,OAAO,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9C,CAAC,CAAC;QACF,IAAI,CAAC,WAAW,CAAC,WAAW,EAAE,YAAY,EAAE,eAAe,CAAC,CAAC;IAC/D,CAAC;CACF;AA3ID,kDA2IC","sourcesContent":["import type { SubTyped, Typed } from '../types.js';\nimport type {\n TransformContext,\n VisitContext,\n} from './TransformerObject.js';\nimport type { DefaultNodePreVisitor, Safeness, SafeWrap } from './TransformerTyped.js';\nimport { TransformerTyped } from './TransformerTyped.js';\n\nexport class TransformerSubTyped<Nodes extends Typed> extends TransformerTyped<Nodes> {\n public constructor(\n defaultContext: TransformContext = {},\n defaultNodePreVisitor: DefaultNodePreVisitor<Nodes> = {},\n ) {\n super(defaultContext, defaultNodePreVisitor);\n };\n\n public override clone(\n newDefaultContext: TransformContext = {},\n newDefaultNodePreVisitor: DefaultNodePreVisitor<Nodes> = {},\n ): TransformerSubTyped<Nodes> {\n return new TransformerSubTyped(\n { ...this.defaultContext, ...newDefaultContext },\n { ...this.defaultNodePreVisitor, ...newDefaultNodePreVisitor },\n );\n }\n\n /**\n * Transform a single node ({@link Typed}).\n * Similar to {@link this.transformNode} but also allowing you to target the subTypes.\n * @param startObject the object from which we will start the transformation,\n * potentially visiting and transforming its descendants along the way.\n * @param nodeCallBacks a dictionary mapping the various operation types to objects optionally\n * containing preVisitor and transformer.\n * The preVisitor allows you to provide {@link TransformContext} for the current object,\n * altering how it will be transformed.\n * The transformer allows you to manipulate the copy of the current object,\n * and expects you to return the value that should take the current objects place.\n * @param nodeSpecificCallBacks Same as nodeCallBacks but using an additional level of indirection to\n * indicate the subType.\n * @return the result of transforming the requested descendant operations (based on the preVisitor)\n * using a transformer that works its way back up from the descendant to the startObject.\n */\n public transformNodeSpecific<Safe extends Safeness = 'safe', OutType = unknown>(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n transform?: (copy: SafeWrap<Safe, Extract<Nodes, Typed<T>>>, orig: Extract<Nodes, Typed<T>>) => unknown;\n preVisitor?: (orig: Extract<Nodes, Typed<T>>) => TransformContext;\n }},\n nodeSpecificCallBacks: {[Type in Nodes['type']]?: {\n [SubType in Extract<Nodes, SubTyped<Type>>['subType']]?: {\n transform?: (op: SafeWrap<Safe, Extract<Nodes, SubTyped<Type, SubType>>>) => unknown;\n preVisitor?: (op: Extract<Nodes, SubTyped<Type, SubType>>) => TransformContext;\n }}},\n ): Safe extends 'unsafe' ? OutType : unknown {\n const transformWrapper = (copy: object, orig: object): unknown => {\n let ogTransform: ((copy: any, orig: any) => unknown) | undefined;\n const casted = <SubTyped<Nodes['type']>>copy;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogTransform = specific[<keyof typeof specific> casted.subType]?.transform;\n }\n if (!ogTransform) {\n ogTransform = nodeCallBacks[casted.type]?.transform;\n }\n }\n return ogTransform ? ogTransform(casted, orig) : copy;\n };\n const preVisitWrapper = (curObject: object): VisitContext => {\n let ogPreVisit: ((node: any) => VisitContext) | undefined;\n const casted = <SubTyped<Nodes['type']>>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogPreVisit = specific[<keyof typeof specific> casted.subType]?.preVisitor;\n }\n if (!ogPreVisit) {\n ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;\n }\n }\n return ogPreVisit ? ogPreVisit(casted) : {};\n };\n return <any> this.transformObject(startObject, transformWrapper, preVisitWrapper);\n }\n\n /**\n * Visit a selected subTree given a startObject, steering the visits based on {@link Typed} nodes.\n * Similar to {@link this.visitNode}, but also allowing you to target subTypes.\n * Will call the preVisitor on the outer distinct, then the visitor of the special distinct,\n * followed by the visiting the outer distinct, printing '231'.\n * The pre-visitor visits starting from the root, going deeper, while the actual visitor goes in reverse.\n * @param startObject the object from which we will start visiting,\n * potentially visiting its descendants along the way.\n * @param nodeCallBacks a dictionary mapping the various operation types to objects optionally\n * containing preVisitor and visitor.\n * The preVisitor allows you to provide {@link VisitContext} for the current object,\n * altering how it will be visited.\n * The visitor allows you to visit the object from deepest to the outermost object.\n * This is useful if you for example want to manipulate the objects you visit during your visits,\n * similar to {@link mapOperation}.\n * @param nodeSpecificCallBacks Same as nodeCallBacks but using an additional level of indirection to\n * indicate the subType.\n */\n public visitNodeSpecific(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n visitor?: (op: Extract<Nodes, Typed<T>>) => void;\n preVisitor?: (op: Extract<Nodes, Typed<T>>) => VisitContext;\n }},\n nodeSpecificCallBacks: {[Type in Nodes['type']]?:\n {[Subtype in Extract<Nodes, SubTyped<Type>>['subType']]?: {\n visitor?: (op: Extract<Nodes, SubTyped<Type, Subtype>>) => void;\n preVisitor?: (op: Extract<Nodes, SubTyped<Type, Subtype>>) => VisitContext;\n }}},\n ): void {\n const visitWrapper = (curObject: object): void => {\n let ogTransform: ((node: any) => void) | undefined;\n const casted = <SubTyped<Nodes['type']>>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogTransform = specific[<keyof typeof specific> casted.subType]?.visitor;\n }\n if (!ogTransform) {\n ogTransform = nodeCallBacks[casted.type]?.visitor;\n }\n }\n if (ogTransform) {\n ogTransform(casted);\n }\n };\n const preVisitWrapper = (curObject: object): VisitContext => {\n let ogPreVisit: ((node: any) => VisitContext) | undefined;\n const casted = <SubTyped<Nodes['type']>>curObject;\n if (casted.type && casted.subType) {\n const specific = nodeSpecificCallBacks[casted.type];\n if (specific) {\n ogPreVisit = specific[<keyof typeof specific> casted.subType]?.preVisitor;\n }\n if (!ogPreVisit) {\n ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;\n }\n }\n return ogPreVisit ? ogPreVisit(casted) : {};\n };\n this.visitObject(startObject, visitWrapper, preVisitWrapper);\n }\n}\n"]}
|
|
@@ -13,14 +13,17 @@ class TransformerTyped extends TransformerObject_js_1.TransformerObject {
|
|
|
13
13
|
return new TransformerTyped({ ...this.defaultContext, ...newDefaultContext }, { ...this.defaultNodePreVisitor, ...newDefaultNodePreVisitor });
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* Transform a single node.
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
16
|
+
* Transform a single node ({@link Typed}).
|
|
17
|
+
* @param startObject the object from which we will start the transformation,
|
|
18
|
+
* potentially visiting and transforming its descendants along the way.
|
|
19
|
+
* @param nodeCallBacks a dictionary mapping the various node types to objects optionally
|
|
20
|
+
* containing preVisitor and transformer.
|
|
21
|
+
* The preVisitor allows you to provide {@link TransformContext} for the current object,
|
|
22
|
+
* altering how it will be transformed.
|
|
23
|
+
* The transformer allows you to manipulate the copy of the current object,
|
|
24
|
+
* and expects you to return the value that should take the current objects place.
|
|
25
|
+
* @return the result of transforming the requested descendant operations (based on the preVisitor)
|
|
26
|
+
* using a transformer that works its way back up from the descendant to the startObject.
|
|
24
27
|
*/
|
|
25
28
|
transformNode(startObject, nodeCallBacks) {
|
|
26
29
|
const transformWrapper = (copy, orig) => {
|
|
@@ -45,10 +48,19 @@ class TransformerTyped extends TransformerObject_js_1.TransformerObject {
|
|
|
45
48
|
return this.transformObject(startObject, transformWrapper, preVisitWrapper);
|
|
46
49
|
}
|
|
47
50
|
/**
|
|
48
|
-
*
|
|
51
|
+
* Visit a selected subTree given a startObject, steering the visits based on {@link Typed} nodes.
|
|
52
|
+
* Will first call the preVisitor on the project and notice it should not iterate on its descendants.
|
|
53
|
+
* It then visits the project, and the outermost distinct, printing '21'.
|
|
49
54
|
* The pre-visitor visits starting from the root, going deeper, while the actual visitor goes in reverse.
|
|
50
|
-
* @param startObject
|
|
51
|
-
*
|
|
55
|
+
* @param startObject the object from which we will start visiting,
|
|
56
|
+
* potentially visiting its descendants along the way.
|
|
57
|
+
* @param nodeCallBacks a dictionary mapping the various operation types to objects optionally
|
|
58
|
+
* containing preVisitor and visitor.
|
|
59
|
+
* The preVisitor allows you to provide {@link VisitContext} for the current object,
|
|
60
|
+
* altering how it will be visited.
|
|
61
|
+
* The visitor allows you to visit the object from deepest to the outermost object.
|
|
62
|
+
* This is useful if you for example want to manipulate the objects you visit during your visits,
|
|
63
|
+
* similar to {@link this.transformNode}.
|
|
52
64
|
*/
|
|
53
65
|
visitNode(startObject, nodeCallBacks) {
|
|
54
66
|
const visitorWrapper = (curObject) => {
|
|
@@ -73,30 +85,6 @@ class TransformerTyped extends TransformerObject_js_1.TransformerObject {
|
|
|
73
85
|
};
|
|
74
86
|
return this.visitObject(startObject, visitorWrapper, preVisitWrapper);
|
|
75
87
|
}
|
|
76
|
-
/**
|
|
77
|
-
* Traverses only selected nodes as returned by the function.
|
|
78
|
-
* @param currentNode
|
|
79
|
-
* @param traverse
|
|
80
|
-
*/
|
|
81
|
-
traverseNodes(currentNode, traverse) {
|
|
82
|
-
let didShortCut = false;
|
|
83
|
-
const recurse = (curNode) => {
|
|
84
|
-
const traverser = traverse[curNode.type];
|
|
85
|
-
if (traverser) {
|
|
86
|
-
const { next, shortcut } = traverser(curNode);
|
|
87
|
-
didShortCut = shortcut ?? false;
|
|
88
|
-
if (!didShortCut) {
|
|
89
|
-
for (const node of next ?? []) {
|
|
90
|
-
if (didShortCut) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
recurse(node);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
recurse(currentNode);
|
|
99
|
-
}
|
|
100
88
|
}
|
|
101
89
|
exports.TransformerTyped = TransformerTyped;
|
|
102
90
|
//# sourceMappingURL=TransformerTyped.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransformerTyped.js","sourceRoot":"","sources":["../../../../lib/transformers/TransformerTyped.ts"],"names":[],"mappings":";;;AAEA,iEAA2D;AAQ3D,MAAa,gBAAsC,SAAQ,wCAAiB;IAG9D;IAFZ,YACE,iBAAmC,EAAE,EAC3B,wBAAsD,EAAE;QAElE,KAAK,CAAC,cAAc,CAAC,CAAC;QAFZ,0BAAqB,GAArB,qBAAqB,CAAmC;IAGpE,CAAC;IAAA,CAAC;IAEc,KAAK,CACnB,oBAAsC,EAAE,EACxC,2BAAyD,EAAE;QAE3D,OAAO,IAAI,gBAAgB,CACzB,EAAE,GAAG,IAAI,CAAC,cAAc,EAAE,GAAG,iBAAiB,EAAE,EAChD,EAAE,GAAG,IAAI,CAAC,qBAAqB,EAAE,GAAG,wBAAwB,EAAE,CAC/D,CAAC;IACJ,CAAC;IAED
|
|
1
|
+
{"version":3,"file":"TransformerTyped.js","sourceRoot":"","sources":["../../../../lib/transformers/TransformerTyped.ts"],"names":[],"mappings":";;;AAEA,iEAA2D;AAQ3D,MAAa,gBAAsC,SAAQ,wCAAiB;IAG9D;IAFZ,YACE,iBAAmC,EAAE,EAC3B,wBAAsD,EAAE;QAElE,KAAK,CAAC,cAAc,CAAC,CAA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TransformerObject.js';\nimport { TransformerObject } from './TransformerObject.js';\n\nexport type Safeness = 'safe' | 'unsafe';\nexport type SafeWrap<Safe extends Safeness, obj extends object> =\n Safe extends 'safe' ? {[key in keyof obj]: unknown } : obj;\n\nexport type DefaultNodePreVisitor<Nodes extends Typed> = {[T in Nodes['type']]?: TransformContext };\n\nexport class TransformerTyped<Nodes extends Typed> extends TransformerObject {\n public constructor(\n defaultContext: TransformContext = {},\n protected defaultNodePreVisitor: DefaultNodePreVisitor<Nodes> = {},\n ) {\n super(defaultContext);\n };\n\n public override clone(\n newDefaultContext: TransformContext = {},\n newDefaultNodePreVisitor: DefaultNodePreVisitor<Nodes> = {},\n ): TransformerTyped<Nodes> {\n return new TransformerTyped(\n { ...this.defaultContext, ...newDefaultContext },\n { ...this.defaultNodePreVisitor, ...newDefaultNodePreVisitor },\n );\n }\n\n /**\n * Transform a single node ({@link Typed}).\n * @param startObject the object from which we will start the transformation,\n * potentially visiting and transforming its descendants along the way.\n * @param nodeCallBacks a dictionary mapping the various node types to objects optionally\n * containing preVisitor and transformer.\n * The preVisitor allows you to provide {@link TransformContext} for the current object,\n * altering how it will be transformed.\n * The transformer allows you to manipulate the copy of the current object,\n * and expects you to return the value that should take the current objects place.\n * @return the result of transforming the requested descendant operations (based on the preVisitor)\n * using a transformer that works its way back up from the descendant to the startObject.\n */\n public transformNode<Safe extends Safeness = 'safe', OutType = unknown>(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n transform?: (copy: SafeWrap<Safe, Extract<Nodes, Typed<T>>>, orig: Extract<Nodes, Typed<T>>) => unknown;\n preVisitor?: (orig: Extract<Nodes, Typed<T>>) => TransformContext;\n }},\n ): Safe extends 'unsafe' ? OutType : unknown {\n const transformWrapper = (copy: object, orig: object): unknown => {\n let ogTransform: ((copy: any, orig: any) => unknown) | undefined;\n const casted = <Typed<Nodes['type']>>copy;\n if (casted.type) {\n ogTransform = nodeCallBacks[casted.type]?.transform;\n }\n return ogTransform ? ogTransform(casted, orig) : copy;\n };\n const nodeDefaults = this.defaultNodePreVisitor;\n const preVisitWrapper = (curObject: object): VisitContext => {\n let ogPreVisit: ((node: any) => VisitContext) | undefined;\n let nodeContext: VisitContext = {};\n const casted = <Typed<Nodes['type']>>curObject;\n if (casted.type) {\n ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;\n nodeContext = nodeDefaults[casted.type] ?? nodeContext;\n }\n return ogPreVisit ? { ...nodeContext, ...ogPreVisit(casted) } : nodeContext;\n };\n return <any> this.transformObject(startObject, transformWrapper, preVisitWrapper);\n }\n\n /**\n * Visit a selected subTree given a startObject, steering the visits based on {@link Typed} nodes.\n * Will first call the preVisitor on the project and notice it should not iterate on its descendants.\n * It then visits the project, and the outermost distinct, printing '21'.\n * The pre-visitor visits starting from the root, going deeper, while the actual visitor goes in reverse.\n * @param startObject the object from which we will start visiting,\n * potentially visiting its descendants along the way.\n * @param nodeCallBacks a dictionary mapping the various operation types to objects optionally\n * containing preVisitor and visitor.\n * The preVisitor allows you to provide {@link VisitContext} for the current object,\n * altering how it will be visited.\n * The visitor allows you to visit the object from deepest to the outermost object.\n * This is useful if you for example want to manipulate the objects you visit during your visits,\n * similar to {@link this.transformNode}.\n */\n public visitNode(\n startObject: object,\n nodeCallBacks: {[T in Nodes['type']]?: {\n visitor?: (op: Extract<Nodes, Typed<T>>) => void;\n preVisitor?: (op: Extract<Nodes, Typed<T>>) => VisitContext;\n }},\n ): void {\n const visitorWrapper = (curObject: object): void => {\n const casted = <Typed<Nodes['type']>>curObject;\n if (casted.type) {\n const ogTransform = nodeCallBacks[casted.type]?.visitor;\n if (ogTransform) {\n ogTransform(<any> casted);\n }\n }\n };\n const nodeDefaults = this.defaultNodePreVisitor;\n const preVisitWrapper = (curObject: object): VisitContext => {\n let ogPreVisit: ((node: any) => VisitContext) | undefined;\n let nodeContext: VisitContext = {};\n const casted = <Typed<Nodes['type']>>curObject;\n if (casted.type) {\n ogPreVisit = nodeCallBacks[casted.type]?.preVisitor;\n nodeContext = nodeDefaults[casted.type] ?? nodeContext;\n }\n return ogPreVisit ? { ...nodeContext, ...ogPreVisit(casted) } : nodeContext;\n };\n return this.visitObject(startObject, visitorWrapper, preVisitWrapper);\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../lib/types.ts"],"names":[],"mappings":"","sourcesContent":["
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../lib/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Type that is used by the Traqula core package to type raw objects/ Data Transfer Objects (DTO).\n * Enforces the presence of a type string, and that in case the subType key is present, it also be a string.\n */\nexport interface Typed<Type extends string = string> {\n type: Type;\n subType?: string;\n}\n\n/**\n * A {@link Typed} object that is sure to have a subType\n */\nexport interface SubTyped<Type extends string = string, SubType extends string = string> extends Typed<Type> {\n subType: SubType;\n}\n\n/**\n * Type used by the parser and generator builders of the core Traqula package to annotate\n * the relation between AST objects and the source string.\n */\nexport interface Localized {\n loc: SourceLocation;\n}\n\n/**\n * Wrapper type for localization,\n * can be used to state localization information about any other type (including primitive types)\n */\nexport interface Wrap<T> extends Localized {\n val: T;\n}\n\n/**\n * A AST node. Nodes are indexable by their types.\n * When generating, the SUBRULES called should be located within the current location range.\n */\nexport interface Node extends Localized, Typed {}\n\nexport interface SourceLocationBase {\n sourceLocationType: string;\n}\n\n/**\n * SourceLocation type that annotates that the node represents the AST representation of\n * the given range in the source string.\n */\nexport interface SourceLocationSource extends SourceLocationBase {\n sourceLocationType: 'source';\n start: number;\n end: number;\n}\n\n/**\n * Similar to {@link SourceLocationSource} but also carrying the source it related too.\n * All sourceLocation nodes that are descends of this one, relate themselves to this source.\n */\nexport interface SourceLocationInlinedSource extends SourceLocationBase, Localized {\n sourceLocationType: 'inlinedSource';\n /**\n * The string that will be used as the source for this node and the descends of this node.\n * Note that the generator will print the characters from newSource[0] until start. Likewise for the suffix.\n */\n newSource: string;\n /**\n * Behavior of the current loc, after replacing the source string.\n */\n loc: SourceLocation;\n /**\n * The range that this node replaces in the context of the original source.\n */\n start: number;\n end: number;\n startOnNew: number;\n endOnNew: number;\n}\n\n/**\n * NoStringManifestation means the node does not have a string representation.\n * For example the literal '5' has an integer type (which is an AST node),\n * but the type does not have an associated string representation.\n * When set to true, the node will not be printed, start and end are meaningless in this case.\n */\nexport interface SourceLocationNoMaterialize extends SourceLocationBase {\n sourceLocationType: 'noMaterialize';\n}\n\n/**\n * SourceLocation type that annotates that the node has been replaced\n * in relation to the original string by a new string.\n * It means that this node should be generated, and that the source string from start to end has been changed.\n */\nexport interface SourceLocationStringReplace extends SourceLocationBase {\n sourceLocationType: 'stringReplace';\n /**\n * The string that will replace the given range in the original source.\n */\n newSource: string;\n /**\n * The start of the region in the original source string that this node replaces.\n */\n start: number;\n /**\n * The end of the region in the original source string that this node replaces.\n */\n end: number;\n}\n\n/**\n * SourceLocation type that annotates that the node has been replaced in relation to the original string.\n * It means that this node should be generated, and that the source string from start to end has been changed.\n */\nexport interface SourceLocationNodeReplace extends SourceLocationBase {\n sourceLocationType: 'nodeReplace';\n /**\n * The start of the region in the original source string that this node replaces.\n */\n start: number;\n /**\n * The end of the region in the original source string that this node replaces.\n */\n end: number;\n}\n/**\n * Must have an ancestor of type {@link SourceLocationNodeReplace}\n */\nexport interface SourceLocationNodeAutoGenerate extends SourceLocationBase {\n sourceLocationType: 'autoGenerate';\n}\n\nexport type SourceLocation =\n // Relate yourself to the source\n | SourceLocationSource\n // Add a new source\n | SourceLocationInlinedSource\n // No not generate anything for the current node and descendants\n | SourceLocationNoMaterialize\n // Replace some range by a string\n | SourceLocationStringReplace\n // Replace this node by some autogen\n | SourceLocationNodeReplace\n // Auto gen current node.\n | SourceLocationNodeAutoGenerate;\n"]}
|
package/dist/cjs/lib/utils.js
CHANGED
|
@@ -4,9 +4,16 @@ exports.traqulaIndentation = void 0;
|
|
|
4
4
|
exports.unCapitalize = unCapitalize;
|
|
5
5
|
exports.createToken = createToken;
|
|
6
6
|
const chevrotain_1 = require("@traqula/chevrotain");
|
|
7
|
+
/**
|
|
8
|
+
* Lowercase the first letter of a given string.
|
|
9
|
+
* @param str
|
|
10
|
+
*/
|
|
7
11
|
function unCapitalize(str) {
|
|
8
12
|
return (str.charAt(0).toLowerCase() + str.slice(1));
|
|
9
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Create a token in a typesafe way necessary by the traqula core builders.
|
|
16
|
+
*/
|
|
10
17
|
function createToken(config) {
|
|
11
18
|
return (0, chevrotain_1.createToken)(config);
|
|
12
19
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../lib/utils.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../lib/utils.ts"],"names":[],"mappings":";;;AAYA,oCAEC;AAOD,kCAEC;AAtBD,oDAA4D;AAO5D;;;GAGG;AACH,SAAgB,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;;GAEG;AACH,SAAgB,WAAW,CAAsB,MAAqC;IACpF,OAAoC,IAAA,wBAAM,EAAC,MAAM,CAAC,CAAC;AACrD,CAAC;AAQD;;;GAGG;AACU,QAAA,kBAAkB,GAC7B,wFAAwF,CAAC","sourcesContent":["import type { ITokenConfig, TokenType } from '@traqula/chevrotain';\nimport { createToken as chevcT } from '@traqula/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\n/**\n * Lowercase the first letter of a given string.\n * @param str\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 = string> = TokenType & { name: Name };\n\n/**\n * Create a token in a typesafe way necessary by the traqula core builders.\n */\nexport function createToken<Name extends string>(config: ITokenConfig & { name: Name }): NamedToken<Name> {\n return <TokenType & { name: Name }> chevcT(config);\n}\n\n/**\n * Patch a given type by changing the value-type of a specific key in the given object type.\n */\nexport type Patch<T extends object, Patch extends {[Key in keyof T ]?: unknown }> =\n {[Key in keyof T]: Key extends keyof Patch ? Patch[Key] : T[Key] };\n\n/**\n * Key that allows you to set the space based indentation of your query when generating newlines using the NEW_LINE.\n * A value of -1 disables the generation of newlines from the NEW_LINE function\n */\nexport const traqulaIndentation =\n 'When you use this string, you expect traqula to handle indentation after every newline';\n"]}
|
|
@@ -1,30 +1,42 @@
|
|
|
1
1
|
import type { IToken } from '@traqula/chevrotain';
|
|
2
|
-
import type { SourceLocation, SourceLocationNodeAutoGenerate, SourceLocationNodeReplace, SourceLocationNoMaterialize, SourceLocationSource, SourceLocationStringReplace, Node, Localized, Wrap, Typed, SubTyped } from './types.js';
|
|
3
|
-
export
|
|
2
|
+
import type { SourceLocation, SourceLocationNodeAutoGenerate, SourceLocationNodeReplace, SourceLocationNoMaterialize, SourceLocationSource, SourceLocationStringReplace, Node, Localized, Wrap, Typed, SubTyped, SourceLocationInlinedSource } from './types.js';
|
|
3
|
+
export interface AstCoreFactoryArgs {
|
|
4
|
+
/**
|
|
5
|
+
* Whether the AstFactory can track sources, if not, the sourceLocation function returns autoGen. Default true
|
|
6
|
+
*/
|
|
7
|
+
tracksSourceLocation: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare class AstCoreFactory implements AstCoreFactoryArgs {
|
|
10
|
+
tracksSourceLocation: boolean;
|
|
11
|
+
constructor(args?: Partial<AstCoreFactoryArgs>);
|
|
4
12
|
wrap<T>(val: T, loc: SourceLocation): Wrap<T>;
|
|
5
13
|
isLocalized(obj: unknown): obj is Localized;
|
|
6
14
|
sourceLocation(...elements: (undefined | IToken | Localized)[]): SourceLocation;
|
|
7
|
-
sourceLocationNoMaterialize():
|
|
15
|
+
sourceLocationNoMaterialize(): SourceLocation;
|
|
8
16
|
/**
|
|
9
17
|
* Returns a copy of the argument that is not materialized
|
|
10
18
|
*/
|
|
11
|
-
dematerialized<T extends Node>(arg: T): T
|
|
12
|
-
loc: SourceLocationNoMaterialize;
|
|
13
|
-
};
|
|
19
|
+
dematerialized<T extends Node>(arg: T): T;
|
|
14
20
|
safeObjectTransform(value: unknown, mapper: (some: object) => any): any;
|
|
15
21
|
forcedAutoGenTree<T extends object>(obj: T): T;
|
|
16
22
|
forceMaterialized<T extends Node>(arg: T): T;
|
|
17
23
|
isSourceLocation(loc: object): loc is SourceLocation;
|
|
18
24
|
sourceLocationSource(start: number, end: number): SourceLocationSource;
|
|
25
|
+
/**
|
|
26
|
+
* {@inheritDoc SourceLocationInlinedSource}
|
|
27
|
+
*/
|
|
28
|
+
sourceLocationInlinedSource(newSource: string, subLoc: SourceLocation, start: number, end: number, startOnNew?: number, endOnNew?: number): SourceLocation;
|
|
29
|
+
isSourceLocationInlinedSource(loc: object): loc is SourceLocationInlinedSource;
|
|
19
30
|
gen(): SourceLocationNodeAutoGenerate;
|
|
20
31
|
isSourceLocationSource(loc: object): loc is SourceLocationSource;
|
|
32
|
+
sourceLocationStringReplace(newSource: string, start: number, end: number): SourceLocation;
|
|
21
33
|
isSourceLocationStringReplace(loc: object): loc is SourceLocationStringReplace;
|
|
22
34
|
sourceLocationNodeReplaceUnsafe(loc: SourceLocation): SourceLocationNodeReplace;
|
|
23
35
|
sourceLocationNodeReplace(location: SourceLocationSource): SourceLocationNodeReplace;
|
|
24
36
|
sourceLocationNodeReplace(start: number, end: number): SourceLocationNodeReplace;
|
|
25
37
|
isSourceLocationNodeReplace(loc: object): loc is SourceLocationNodeReplace;
|
|
26
38
|
isSourceLocationNodeAutoGenerate(loc: object): loc is SourceLocationNodeAutoGenerate;
|
|
27
|
-
|
|
39
|
+
isPrintingLoc(loc: SourceLocation): boolean;
|
|
28
40
|
printFilter(node: Localized, callback: () => void): void;
|
|
29
41
|
isSourceLocationNoMaterialize(loc: object): loc is SourceLocationNoMaterialize;
|
|
30
42
|
isOfType<Type extends string>(obj: object, type: Type): obj is Typed<Type>;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export class AstCoreFactory {
|
|
2
|
+
tracksSourceLocation;
|
|
3
|
+
constructor(args = {}) {
|
|
4
|
+
this.tracksSourceLocation = args.tracksSourceLocation ?? true;
|
|
5
|
+
}
|
|
2
6
|
wrap(val, loc) {
|
|
3
7
|
return { val, loc };
|
|
4
8
|
}
|
|
@@ -7,6 +11,9 @@ export class AstCoreFactory {
|
|
|
7
11
|
typeof obj.loc === 'object' && obj.loc !== null && 'sourceLocationType' in obj.loc;
|
|
8
12
|
}
|
|
9
13
|
sourceLocation(...elements) {
|
|
14
|
+
if (!this.tracksSourceLocation) {
|
|
15
|
+
return this.gen();
|
|
16
|
+
}
|
|
10
17
|
const pureElements = elements.filter(x => x !== undefined);
|
|
11
18
|
if (pureElements.length === 0) {
|
|
12
19
|
return this.sourceLocationNoMaterialize();
|
|
@@ -29,6 +36,9 @@ export class AstCoreFactory {
|
|
|
29
36
|
};
|
|
30
37
|
}
|
|
31
38
|
sourceLocationNoMaterialize() {
|
|
39
|
+
if (!this.tracksSourceLocation) {
|
|
40
|
+
return this.gen();
|
|
41
|
+
}
|
|
32
42
|
return { sourceLocationType: 'noMaterialize' };
|
|
33
43
|
}
|
|
34
44
|
/**
|
|
@@ -73,12 +83,43 @@ export class AstCoreFactory {
|
|
|
73
83
|
end,
|
|
74
84
|
};
|
|
75
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* {@inheritDoc SourceLocationInlinedSource}
|
|
88
|
+
*/
|
|
89
|
+
sourceLocationInlinedSource(newSource, subLoc, start, end, startOnNew = 0, endOnNew = newSource.length) {
|
|
90
|
+
if (!this.tracksSourceLocation) {
|
|
91
|
+
return this.gen();
|
|
92
|
+
}
|
|
93
|
+
if (this.isSourceLocationSource(subLoc)) {
|
|
94
|
+
startOnNew = subLoc.start;
|
|
95
|
+
endOnNew = subLoc.end;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
sourceLocationType: 'inlinedSource',
|
|
99
|
+
newSource,
|
|
100
|
+
start,
|
|
101
|
+
end,
|
|
102
|
+
loc: subLoc,
|
|
103
|
+
startOnNew,
|
|
104
|
+
endOnNew,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
;
|
|
108
|
+
isSourceLocationInlinedSource(loc) {
|
|
109
|
+
return this.isSourceLocation(loc) && loc.sourceLocationType === 'inlinedSource';
|
|
110
|
+
}
|
|
76
111
|
gen() {
|
|
77
112
|
return { sourceLocationType: 'autoGenerate' };
|
|
78
113
|
}
|
|
79
114
|
isSourceLocationSource(loc) {
|
|
80
115
|
return this.isSourceLocation(loc) && loc.sourceLocationType === 'source';
|
|
81
116
|
}
|
|
117
|
+
sourceLocationStringReplace(newSource, start, end) {
|
|
118
|
+
if (!this.tracksSourceLocation) {
|
|
119
|
+
return this.gen();
|
|
120
|
+
}
|
|
121
|
+
return { sourceLocationType: 'stringReplace', newSource, start, end };
|
|
122
|
+
}
|
|
82
123
|
isSourceLocationStringReplace(loc) {
|
|
83
124
|
return this.isSourceLocation(loc) && loc.sourceLocationType === 'stringReplace';
|
|
84
125
|
}
|
|
@@ -86,7 +127,10 @@ export class AstCoreFactory {
|
|
|
86
127
|
if (this.isSourceLocationSource(loc)) {
|
|
87
128
|
return this.sourceLocationNodeReplace(loc);
|
|
88
129
|
}
|
|
89
|
-
|
|
130
|
+
if (this.isSourceLocationInlinedSource(loc)) {
|
|
131
|
+
return this.sourceLocationNodeReplaceUnsafe(loc.loc);
|
|
132
|
+
}
|
|
133
|
+
throw new Error(`Cannot convert SourceLocation of type ${loc.sourceLocationType} to SourceLocationNodeReplace`);
|
|
90
134
|
}
|
|
91
135
|
sourceLocationNodeReplace(startOrLoc, end) {
|
|
92
136
|
let starting;
|
|
@@ -111,12 +155,13 @@ export class AstCoreFactory {
|
|
|
111
155
|
isSourceLocationNodeAutoGenerate(loc) {
|
|
112
156
|
return this.isSourceLocation(loc) && loc.sourceLocationType === 'autoGenerate';
|
|
113
157
|
}
|
|
114
|
-
|
|
115
|
-
return this.isSourceLocationNodeReplace(
|
|
116
|
-
this.isSourceLocationNodeAutoGenerate(
|
|
158
|
+
isPrintingLoc(loc) {
|
|
159
|
+
return this.isSourceLocationNodeReplace(loc) ||
|
|
160
|
+
this.isSourceLocationNodeAutoGenerate(loc) ||
|
|
161
|
+
(this.isSourceLocationInlinedSource(loc) && this.isPrintingLoc(loc.loc));
|
|
117
162
|
}
|
|
118
163
|
printFilter(node, callback) {
|
|
119
|
-
if (this.
|
|
164
|
+
if (this.isPrintingLoc(node.loc)) {
|
|
120
165
|
callback();
|
|
121
166
|
}
|
|
122
167
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AstCoreFactory.js","sourceRoot":"","sources":["../../../lib/AstCoreFactory.ts"],"names":[],"mappings":"AAgBA,MAAM,OAAO,cAAc;IAClB,IAAI,CAAI,GAAM,EAAE,GAAmB;QACxC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;IACtB,CAAC;IAEM,WAAW,CAAC,GAAY;QAC7B,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,IAAI,KAAK,IAAI,GAAG;YAC5D,OAAO,GAAG,CAAC,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,GAAG,KAAK,IAAI,IAAI,oBAAoB,IAAI,GAAG,CAAC,GAAG,CAAC;IACvF,CAAC;IAEM,cAAc,CAAC,GAAG,QAA4C;QACnE,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;QAC3D,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,IAAI,CAAC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gB,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,kBAAkB,KAAK,cAAc,CAAC;IACjF,CAAC;IAEM,eAAe,CAAC,IAAe;QACpC,OAAO,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,GAAG,CAAC;YAC/C,IAAI,CAAC,gCAAgC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACpD,CAAC;IAEM,WAAW,CAAC,IAAe,EAAE,QAAoB;QACtD,IAAI,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC;YAC/B,QAAQ,EAAE,CAAC;QACb,CAAC;IACH,CAAC;IAEM,6BAA6B,CAAC,GAAW;QAC9C,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,kBAAkB,KAAK,eAAe,CAAC;IAClF,CAAC;IAEM,QAAQ,CAAsB,GAAW,EAAE,IAAU;QAC1D,MAAM,MAAM,GAAmB,GAAG,CAAC;QACnC,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC;IAC9B,CAAC;IAEM,WAAW,CAA8C,GAAW,EAAE,IAAU,EAAE,OAAgB;QAEvG,MAAM,IAAI,GAA0C,GAAG,CAAC;QACxD,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,OAAO,KAAK,OAAO,CAAC;IACxD,CAAC;CACF","sourcesContent":["import type { IToken } from '@traqula/chevrotain';\n\nimport type {\n SourceLocation,\n SourceLocationNodeAutoGenerate,\n SourceLocationNodeReplace,\n SourceLocationNoMaterialize,\n SourceLocationSource,\n SourceLocationStringReplace,\n Node,\n Localized,\n Wrap,\n Typed,\n SubTyped,\n} from './types.js';\n\nexport class AstCoreFactory {\n public wrap<T>(val: T, loc: SourceLocation): Wrap<T> {\n return { val, loc };\n }\n\n public isLocalized(obj: unknown): obj is Localized {\n return typeof obj === 'object' && obj !== null && 'loc' in obj &&\n typeof obj.loc === 'object' && obj.loc !== null && 'sourceLocationType' in obj.loc;\n }\n\n public sourceLocation(...elements: (undefined | IToken | Localized)[]): SourceLocation {\n const pureElements = elements.filter(x => x !== undefined);\n if (pureElements.length === 0) {\n return this.sourceLocationNoMaterialize();\n }\n const filtered = pureElements.filter(element =>\n !this.isLocalized(element) || this.isSourceLocationSource(element.loc) ||\n this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));\n if (filtered.length === 0) {\n return this.gen();\n }\n const first = filtered.at(0)!;\n const last = filtered.at(-1)!;\n return {\n sourceLocationType: 'source',\n start: this.isLocalized(first) ?\n (<SourceLocationSource | SourceLocationStringReplace> first.loc).start :\n first.startOffset,\n end: this.isLocalized(last) ?\n (<SourceLocationSource | SourceLocationStringReplace> last.loc).end :\n (last.endOffset! + 1),\n };\n }\n\n public sourceLocationNoMaterialize(): SourceLocationNoMaterialize {\n return { sourceLocationType: 'noMaterialize' };\n }\n\n /**\n * Returns a copy of the argument that is not materialized\n */\n public dematerialized<T extends Node>(arg: T): T & { loc: SourceLocationNoMaterialize } {\n return { ...arg, loc: this.sourceLocationNoMaterialize() };\n }\n\n public safeObjectTransform(value: unknown, mapper: (some: object) => any): any {\n if (value && typeof value === 'object') {\n // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects\n if (Array.isArray(value)) {\n return value.map(x => this.safeObjectTransform(x, mapper));\n }\n return mapper(value);\n }\n return value;\n }\n\n public forcedAutoGenTree<T extends object>(obj: T): T {\n const copy = { ...obj };\n for (const [ key, value ] of Object.entries(copy)) {\n (<Record<string, object>> copy)[key] = this.safeObjectTransform(value, obj => this.forcedAutoGenTree(obj));\n }\n if (this.isLocalized(copy)) {\n copy.loc = this.gen();\n }\n return copy;\n }\n\n public forceMaterialized<T extends Node>(arg: T): T {\n if (this.isSourceLocationNoMaterialize(arg.loc)) {\n return this.forcedAutoGenTree(arg);\n }\n return { ...arg };\n }\n\n public isSourceLocation(loc: object): loc is SourceLocation {\n return 'sourceLocationType' in loc;\n }\n\n public sourceLocationSource(start: number, end: number): SourceLocationSource {\n return {\n sourceLocationType: 'source',\n start,\n end,\n };\n }\n\n public gen(): SourceLocationNodeAutoGenerate {\n return { sourceLocationType: 'autoGenerate' };\n }\n\n public isSourceLocationSource(loc: object): loc is SourceLocationSource {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'source';\n }\n\n public isSourceLocationStringReplace(loc: object): loc is SourceLocationStringReplace {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'stringReplace';\n }\n\n public sourceLocationNodeReplaceUnsafe(loc: SourceLocation): SourceLocationNodeReplace {\n if (this.isSourceLocationSource(loc)) {\n return this.sourceLocationNodeReplace(loc);\n }\n throw new Error('not possible');\n }\n\n public sourceLocationNodeReplace(location: SourceLocationSource): SourceLocationNodeReplace;\n public sourceLocationNodeReplace(start: number, end: number): SourceLocationNodeReplace;\n public sourceLocationNodeReplace(startOrLoc: number | SourceLocationSource, end?: number): SourceLocationNodeReplace {\n let starting;\n let ending;\n if (typeof startOrLoc === 'number') {\n starting = startOrLoc;\n ending = end!;\n } else {\n starting = startOrLoc.start;\n ending = startOrLoc.end;\n }\n return {\n sourceLocationType: 'nodeReplace',\n start: starting,\n end: ending,\n };\n }\n\n public isSourceLocationNodeReplace(loc: object): loc is SourceLocationNodeReplace {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'nodeReplace';\n }\n\n public isSourceLocationNodeAutoGenerate(loc: object): loc is SourceLocationNodeAutoGenerate {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'autoGenerate';\n }\n\n public nodeShouldPrint(node: Localized): boolean {\n return this.isSourceLocationNodeReplace(node.loc) ||\n this.isSourceLocationNodeAutoGenerate(node.loc);\n }\n\n public printFilter(node: Localized, callback: () => void): void {\n if (this.nodeShouldPrint(node)) {\n callback();\n }\n }\n\n public isSourceLocationNoMaterialize(loc: object): loc is SourceLocationNoMaterialize {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'noMaterialize';\n }\n\n public isOfType<Type extends string>(obj: object, type: Type): obj is Typed<Type> {\n const casted: { type?: any } = obj;\n return casted.type === type;\n }\n\n public isOfSubType<Type extends string, SubType extends string>(obj: object, type: Type, subType: SubType):\n obj is SubTyped<Type, SubType> {\n const temp: { type?: unknown; subType?: unknown } = obj;\n return temp.type === type && temp.subType === subType;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AstCoreFactory.js","sourceRoot":"","sources":["../../../lib/AstCoreFactory.ts"],"names":[],"mappings":"AAwBA,MAAM,OAAO,cAAc;IAClB,oBAAoB,CAAU;IACrC,YAAmB,OAAoC,EAAE;QACvD,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,oBAAoB,IAAI,IAAI,CAAC;IAChE,CAAC;IAEM,IAAI,CAAI,GAAM,EAAE,GAAmB;QACxC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;IACtB,CAAC;IAEM,WAAW,CAAC,GAAY;QAC7B,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,IAAI,KAAK,IAAI,GAAG;YAC5D,OAAO,GAAG,CAAC,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,GAAG,KAAK,IAAI,IAAI,oBAAoB,IAAI,GAAG,CAAC,GAAG,CAAC;IACvF,CAAC;IAEM,cAAc,CAAC,GAAG,QAA4C;QACnE,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE,CAAC;YAC/B,OAAO,IAAI,CAAC,GAAG,EAAE,CAAC;QACpB,CAAC;QAED,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;QAC3D,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,IAAI,CAAC,2BAA2B,EAAE,CAAC;QAC5C,CAAC;QAED,MAAM,QAAQ,GAAG,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAC7C,CAAC,IAAI,CAAC,WAAW,C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G,CAAC;YAC1C,IAAI,CAAC,gCAAgC,CAAC,GAAG,CAAC;YAC1C,CAAC,IAAI,CAAC,6BAA6B,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IAC7E,CAAC;IAEM,WAAW,CAAC,IAAe,EAAE,QAAoB;QACtD,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YACjC,QAAQ,EAAE,CAAC;QACb,CAAC;IACH,CAAC;IAEM,6BAA6B,CAAC,GAAW;QAC9C,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,kBAAkB,KAAK,eAAe,CAAC;IAClF,CAAC;IAEM,QAAQ,CAAsB,GAAW,EAAE,IAAU;QAC1D,MAAM,MAAM,GAAmB,GAAG,CAAC;QACnC,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC;IAC9B,CAAC;IAEM,WAAW,CAA8C,GAAW,EAAE,IAAU,EAAE,OAAgB;QAEvG,MAAM,IAAI,GAA0C,GAAG,CAAC;QACxD,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,OAAO,KAAK,OAAO,CAAC;IACxD,CAAC;CACF","sourcesContent":["import type { IToken } from '@traqula/chevrotain';\n\nimport type {\n SourceLocation,\n SourceLocationNodeAutoGenerate,\n SourceLocationNodeReplace,\n SourceLocationNoMaterialize,\n SourceLocationSource,\n SourceLocationStringReplace,\n Node,\n Localized,\n Wrap,\n Typed,\n SubTyped,\n SourceLocationInlinedSource,\n} from './types.js';\n\nexport interface AstCoreFactoryArgs {\n /**\n * Whether the AstFactory can track sources, if not, the sourceLocation function returns autoGen. Default true\n */\n tracksSourceLocation: boolean;\n}\n\nexport class AstCoreFactory implements AstCoreFactoryArgs {\n public tracksSourceLocation: boolean;\n public constructor(args: Partial<AstCoreFactoryArgs> = {}) {\n this.tracksSourceLocation = args.tracksSourceLocation ?? true;\n }\n\n public wrap<T>(val: T, loc: SourceLocation): Wrap<T> {\n return { val, loc };\n }\n\n public isLocalized(obj: unknown): obj is Localized {\n return typeof obj === 'object' && obj !== null && 'loc' in obj &&\n typeof obj.loc === 'object' && obj.loc !== null && 'sourceLocationType' in obj.loc;\n }\n\n public sourceLocation(...elements: (undefined | IToken | Localized)[]): SourceLocation {\n if (!this.tracksSourceLocation) {\n return this.gen();\n }\n\n const pureElements = elements.filter(x => x !== undefined);\n if (pureElements.length === 0) {\n return this.sourceLocationNoMaterialize();\n }\n\n const filtered = pureElements.filter(element =>\n !this.isLocalized(element) || this.isSourceLocationSource(element.loc) ||\n this.isSourceLocationStringReplace(element.loc) || this.isSourceLocationNodeReplace(element.loc));\n if (filtered.length === 0) {\n return this.gen();\n }\n const first = filtered.at(0)!;\n const last = filtered.at(-1)!;\n return {\n sourceLocationType: 'source',\n start: this.isLocalized(first) ?\n (<SourceLocationSource | SourceLocationStringReplace> first.loc).start :\n first.startOffset,\n end: this.isLocalized(last) ?\n (<SourceLocationSource | SourceLocationStringReplace> last.loc).end :\n (last.endOffset! + 1),\n };\n }\n\n public sourceLocationNoMaterialize(): SourceLocation {\n if (!this.tracksSourceLocation) {\n return this.gen();\n }\n return { sourceLocationType: 'noMaterialize' };\n }\n\n /**\n * Returns a copy of the argument that is not materialized\n */\n public dematerialized<T extends Node>(arg: T): T {\n return { ...arg, loc: this.sourceLocationNoMaterialize() };\n }\n\n public safeObjectTransform(value: unknown, mapper: (some: object) => any): any {\n if (value && typeof value === 'object') {\n // If you wonder why this is all so hard, this is the reason. We cannot lose the methods of our Array objects\n if (Array.isArray(value)) {\n return value.map(x => this.safeObjectTransform(x, mapper));\n }\n return mapper(value);\n }\n return value;\n }\n\n public forcedAutoGenTree<T extends object>(obj: T): T {\n const copy = { ...obj };\n for (const [ key, value ] of Object.entries(copy)) {\n (<Record<string, object>> copy)[key] = this.safeObjectTransform(value, obj => this.forcedAutoGenTree(obj));\n }\n if (this.isLocalized(copy)) {\n copy.loc = this.gen();\n }\n return copy;\n }\n\n public forceMaterialized<T extends Node>(arg: T): T {\n if (this.isSourceLocationNoMaterialize(arg.loc)) {\n return this.forcedAutoGenTree(arg);\n }\n return { ...arg };\n }\n\n public isSourceLocation(loc: object): loc is SourceLocation {\n return 'sourceLocationType' in loc;\n }\n\n public sourceLocationSource(start: number, end: number): SourceLocationSource {\n return {\n sourceLocationType: 'source',\n start,\n end,\n };\n }\n\n /**\n * {@inheritDoc SourceLocationInlinedSource}\n */\n public sourceLocationInlinedSource(\n newSource: string,\n subLoc: SourceLocation,\n start: number,\n end: number,\n startOnNew = 0,\n endOnNew: number = newSource.length,\n ): SourceLocation {\n if (!this.tracksSourceLocation) {\n return this.gen();\n }\n if (this.isSourceLocationSource(subLoc)) {\n startOnNew = subLoc.start;\n endOnNew = subLoc.end;\n }\n return {\n sourceLocationType: 'inlinedSource',\n newSource,\n start,\n end,\n loc: subLoc,\n startOnNew,\n endOnNew,\n }satisfies SourceLocationInlinedSource;\n };\n\n public isSourceLocationInlinedSource(loc: object): loc is SourceLocationInlinedSource {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'inlinedSource';\n }\n\n public gen(): SourceLocationNodeAutoGenerate {\n return { sourceLocationType: 'autoGenerate' };\n }\n\n public isSourceLocationSource(loc: object): loc is SourceLocationSource {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'source';\n }\n\n public sourceLocationStringReplace(newSource: string, start: number, end: number): SourceLocation {\n if (!this.tracksSourceLocation) {\n return this.gen();\n }\n return { sourceLocationType: 'stringReplace', newSource, start, end } satisfies SourceLocationStringReplace;\n }\n\n public isSourceLocationStringReplace(loc: object): loc is SourceLocationStringReplace {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'stringReplace';\n }\n\n public sourceLocationNodeReplaceUnsafe(loc: SourceLocation): SourceLocationNodeReplace {\n if (this.isSourceLocationSource(loc)) {\n return this.sourceLocationNodeReplace(loc);\n }\n if (this.isSourceLocationInlinedSource(loc)) {\n return this.sourceLocationNodeReplaceUnsafe(loc.loc);\n }\n throw new Error(`Cannot convert SourceLocation of type ${loc.sourceLocationType} to SourceLocationNodeReplace`);\n }\n\n public sourceLocationNodeReplace(location: SourceLocationSource): SourceLocationNodeReplace;\n public sourceLocationNodeReplace(start: number, end: number): SourceLocationNodeReplace;\n public sourceLocationNodeReplace(startOrLoc: number | SourceLocationSource, end?: number): SourceLocationNodeReplace {\n let starting;\n let ending;\n if (typeof startOrLoc === 'number') {\n starting = startOrLoc;\n ending = end!;\n } else {\n starting = startOrLoc.start;\n ending = startOrLoc.end;\n }\n return {\n sourceLocationType: 'nodeReplace',\n start: starting,\n end: ending,\n };\n }\n\n public isSourceLocationNodeReplace(loc: object): loc is SourceLocationNodeReplace {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'nodeReplace';\n }\n\n public isSourceLocationNodeAutoGenerate(loc: object): loc is SourceLocationNodeAutoGenerate {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'autoGenerate';\n }\n\n public isPrintingLoc(loc: SourceLocation): boolean {\n return this.isSourceLocationNodeReplace(loc) ||\n this.isSourceLocationNodeAutoGenerate(loc) ||\n (this.isSourceLocationInlinedSource(loc) && this.isPrintingLoc(loc.loc));\n }\n\n public printFilter(node: Localized, callback: () => void): void {\n if (this.isPrintingLoc(node.loc)) {\n callback();\n }\n }\n\n public isSourceLocationNoMaterialize(loc: object): loc is SourceLocationNoMaterialize {\n return this.isSourceLocation(loc) && loc.sourceLocationType === 'noMaterialize';\n }\n\n public isOfType<Type extends string>(obj: object, type: Type): obj is Typed<Type> {\n const casted: { type?: any } = obj;\n return casted.type === type;\n }\n\n public isOfSubType<Type extends string, SubType extends string>(obj: object, type: Type, subType: SubType):\n obj is SubTyped<Type, SubType> {\n const temp: { type?: unknown; subType?: unknown } = obj;\n return temp.type === type && temp.subType === subType;\n }\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AstCoreFactory } from '../AstCoreFactory.js';
|
|
2
|
+
import type { SourceLocationInlinedSource } from '../types.js';
|
|
2
3
|
import { traqulaIndentation } from '../utils.js';
|
|
3
4
|
import type { GenRuleMap } from './builderTypes.js';
|
|
4
5
|
import type { RuleDefArg } from './generatorTypes.js';
|
|
@@ -7,6 +8,7 @@ export declare class DynamicGenerator<Context, Names extends string, RuleDefs ex
|
|
|
7
8
|
protected readonly factory: AstCoreFactory;
|
|
8
9
|
protected __context: Context | undefined;
|
|
9
10
|
protected origSource: string;
|
|
11
|
+
protected handledInlineSource: SourceLocationInlinedSource | undefined;
|
|
10
12
|
protected generatedUntil: number;
|
|
11
13
|
protected toEnsure: ((willPrint: string) => void)[];
|
|
12
14
|
/**
|
|
@@ -21,6 +23,9 @@ export declare class DynamicGenerator<Context, Names extends string, RuleDefs ex
|
|
|
21
23
|
};
|
|
22
24
|
protected readonly subrule: RuleDefArg['SUBRULE'];
|
|
23
25
|
protected readonly handleLoc: RuleDefArg['HANDLE_LOC'];
|
|
26
|
+
/**
|
|
27
|
+
* Catchup until, excluding
|
|
28
|
+
*/
|
|
24
29
|
protected readonly catchup: RuleDefArg['CATCHUP'];
|
|
25
30
|
private handeEnsured;
|
|
26
31
|
protected readonly print: RuleDefArg['PRINT'];
|
|
@@ -5,6 +5,7 @@ export class DynamicGenerator {
|
|
|
5
5
|
factory = new AstCoreFactory();
|
|
6
6
|
__context = undefined;
|
|
7
7
|
origSource = '';
|
|
8
|
+
handledInlineSource;
|
|
8
9
|
generatedUntil = 0;
|
|
9
10
|
toEnsure = [];
|
|
10
11
|
/**
|
|
@@ -77,6 +78,22 @@ export class DynamicGenerator {
|
|
|
77
78
|
if (this.factory.isSourceLocationSource(localized.loc)) {
|
|
78
79
|
this.catchup(localized.loc.start);
|
|
79
80
|
}
|
|
81
|
+
if (this.factory.isSourceLocationInlinedSource(localized.loc) && this.handledInlineSource !== localized.loc) {
|
|
82
|
+
// Calling handleLoc on the same AST multiple times should be the same as doing it once.
|
|
83
|
+
this.handledInlineSource = localized.loc;
|
|
84
|
+
this.catchup(localized.loc.start);
|
|
85
|
+
const origSource = this.origSource;
|
|
86
|
+
const origPointer = this.generatedUntil;
|
|
87
|
+
this.origSource = localized.loc.newSource;
|
|
88
|
+
this.generatedUntil = 0;
|
|
89
|
+
this.catchup(localized.loc.startOnNew);
|
|
90
|
+
this.handleLoc(localized.loc, handle);
|
|
91
|
+
this.generatedUntil = localized.loc.endOnNew;
|
|
92
|
+
this.catchup(this.origSource.length);
|
|
93
|
+
this.origSource = origSource;
|
|
94
|
+
this.generatedUntil = Math.max(origPointer, localized.loc.end);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
80
97
|
// If autoGenerate - do nothing
|
|
81
98
|
const ret = handle();
|
|
82
99
|
if (this.factory.isSourceLocationSource(localized.loc)) {
|
|
@@ -84,6 +101,9 @@ export class DynamicGenerator {
|
|
|
84
101
|
}
|
|
85
102
|
return ret;
|
|
86
103
|
};
|
|
104
|
+
/**
|
|
105
|
+
* Catchup until, excluding
|
|
106
|
+
*/
|
|
87
107
|
catchup = (until) => {
|
|
88
108
|
const start = this.generatedUntil;
|
|
89
109
|
if (start < until) {
|