@traqula/core 0.0.14 → 0.0.16

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.
@@ -7,7 +7,7 @@ export type SubTyped<Type extends PropertyKey, Subtype extends PropertyKey> = {
7
7
  type: Type;
8
8
  subType: Subtype;
9
9
  };
10
- export declare class CoreFactory {
10
+ export declare class AstCoreFactory {
11
11
  wrap<T>(val: T, loc: SourceLocation): Wrap<T>;
12
12
  isLocalized(obj: unknown): obj is Localized;
13
13
  sourceLocation(...elements: (undefined | IToken | Localized)[]): SourceLocation;
@@ -1,4 +1,4 @@
1
- export class CoreFactory {
1
+ export class AstCoreFactory {
2
2
  wrap(val, loc) {
3
3
  return { val, loc };
4
4
  }
@@ -132,4 +132,4 @@ export class CoreFactory {
132
132
  return temp.type === type && temp.subType === subType;
133
133
  }
134
134
  }
135
- //# sourceMappingURL=CoreFactory.js.map
135
+ //# sourceMappingURL=AstCoreFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AstCoreFactory.js","sourceRoot":"","sources":["AstCoreFactory.ts"],"names":[],"mappings":"AAiBA,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,MAA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AC,GAAW;QACjD,OAAO,IAAI,CAAC,gBAAgB,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 'chevrotain';\n\nimport type {\n SourceLocation,\n SourceLocationNodeAutoGenerate,\n SourceLocationNodeReplace,\n SourceLocationNoMaterialize,\n SourceLocationSource,\n SourceLocationStringReplace,\n Node,\n Localized,\n Wrap,\n} from './nodeTypings.js';\n\nexport type Typed<Type extends PropertyKey> = { type: Type };\nexport type SubTyped<Type extends PropertyKey, Subtype extends PropertyKey> = { type: Type; subType: Subtype };\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[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,9 +1,10 @@
1
- import { CoreFactory } from '../CoreFactory.js';
1
+ import { AstCoreFactory } from '../AstCoreFactory.js';
2
+ import { traqulaIndentation } from '../utils.js';
2
3
  import type { GenRuleMap } from './builderTypes.js';
3
4
  import type { RuleDefArg } from './generatorTypes.js';
4
5
  export declare class DynamicGenerator<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {
5
6
  protected rules: RuleDefs;
6
- protected readonly factory: CoreFactory;
7
+ protected readonly factory: AstCoreFactory;
7
8
  protected __context: Context | undefined;
8
9
  protected origSource: string;
9
10
  protected generatedUntil: number;
@@ -11,7 +12,9 @@ export declare class DynamicGenerator<Context, Names extends string, RuleDefs ex
11
12
  protected readonly stringBuilder: string[];
12
13
  constructor(rules: RuleDefs);
13
14
  setContext(context: Context): void;
14
- protected getSafeContext(): Context;
15
+ protected getSafeContext(): Context & {
16
+ [traqulaIndentation]?: unknown;
17
+ };
15
18
  protected readonly subrule: RuleDefArg['SUBRULE'];
16
19
  protected readonly handleLoc: RuleDefArg['HANDLE_LOC'];
17
20
  protected readonly catchup: RuleDefArg['CATCHUP'];
@@ -19,4 +22,5 @@ export declare class DynamicGenerator<Context, Names extends string, RuleDefs ex
19
22
  private readonly printWord;
20
23
  private readonly printSpaceLeft;
21
24
  private readonly printWords;
25
+ private readonly printOnEmpty;
22
26
  }
@@ -1,7 +1,8 @@
1
- import { CoreFactory } from '../CoreFactory.js';
1
+ import { AstCoreFactory } from '../AstCoreFactory.js';
2
+ import { traqulaIndentation } from '../utils.js';
2
3
  export class DynamicGenerator {
3
4
  rules;
4
- factory = new CoreFactory();
5
+ factory = new AstCoreFactory();
5
6
  __context = undefined;
6
7
  origSource = '';
7
8
  generatedUntil = 0;
@@ -42,6 +43,7 @@ export class DynamicGenerator {
42
43
  PRINT_SPACE_LEFT: this.printSpaceLeft,
43
44
  PRINT_WORD: this.printWord,
44
45
  PRINT_WORDS: this.printWords,
46
+ PRINT_ON_EMPTY: this.printOnEmpty,
45
47
  CATCHUP: this.catchup,
46
48
  HANDLE_LOC: this.handleLoc,
47
49
  })(ast, this.getSafeContext(), ...arg);
@@ -86,7 +88,7 @@ export class DynamicGenerator {
86
88
  print = (...args) => {
87
89
  const pureArgs = args.filter(x => x.length > 0);
88
90
  if (pureArgs.length > 0) {
89
- const [head, ...tail] = pureArgs;
91
+ const head = pureArgs[0];
90
92
  if (this.expectsSpace) {
91
93
  this.expectsSpace = false;
92
94
  const blanks = new Set(['\n', ' ', '\t']);
@@ -95,8 +97,23 @@ export class DynamicGenerator {
95
97
  this.stringBuilder.push(' ');
96
98
  }
97
99
  }
98
- this.stringBuilder.push(head);
99
- this.stringBuilder.push(...tail);
100
+ const context = this.getSafeContext();
101
+ if (context[traqulaIndentation] && typeof context[traqulaIndentation] === 'number') {
102
+ const indent = context[traqulaIndentation];
103
+ for (const str of pureArgs) {
104
+ const [noNl, ...postNl] = str.split('\n');
105
+ this.stringBuilder.push(noNl);
106
+ for (const subStr of postNl.map(line => line.trimStart())) {
107
+ this.stringBuilder.push('\n', ' '.repeat(indent));
108
+ if (subStr.length > 0) {
109
+ this.stringBuilder.push(subStr);
110
+ }
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ this.stringBuilder.push(...pureArgs);
116
+ }
100
117
  }
101
118
  };
102
119
  printWord = (...args) => {
@@ -113,5 +130,33 @@ export class DynamicGenerator {
113
130
  this.printWord(arg);
114
131
  }
115
132
  };
133
+ printOnEmpty = (...args) => {
134
+ // Check if pointer already on empty line
135
+ let counter = this.stringBuilder.length - 1;
136
+ let onEmpty = true;
137
+ const isEmptyTest = /^[ \t\n]*$/u;
138
+ while (counter >= 0 && onEmpty) {
139
+ const cur = this.stringBuilder[counter];
140
+ const indexOfNl = cur.lastIndexOf('\n');
141
+ if (indexOfNl >= 0) {
142
+ // This is the last newline that is printed
143
+ onEmpty = isEmptyTest.test(cur.slice(indexOfNl));
144
+ if (onEmpty) {
145
+ // We still want to have the current indent printed
146
+ const newVal = cur.slice(0, indexOfNl);
147
+ if (newVal) {
148
+ this.stringBuilder[counter] = cur.slice(0, indexOfNl);
149
+ }
150
+ else {
151
+ this.stringBuilder.splice(counter, 1);
152
+ }
153
+ }
154
+ break;
155
+ }
156
+ onEmpty = isEmptyTest.test(cur);
157
+ counter--;
158
+ }
159
+ this.print('\n', ...args);
160
+ };
116
161
  }
117
162
  //# sourceMappingURL=dynamicGenerator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dynamicGenerator.js","sourceRoot":"","sources":["dynamicGenerator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIhD,MAAM,OAAO,gBAAgB;IAQE;IAPV,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;IACrC,SAAS,GAAwB,SAAS,CAAC;IAC3C,UAAU,GAAG,EAAE,CAAC;IAChB,cAAc,GAAG,CAAC,CAAC;IACnB,YAAY,CAAU;IACb,aAAa,GAAa,EAAE,CAAC;IAEhD,YAA6B,KAAe;QAAf,UAAK,GAAL,KAAK,CAAU;QAC1C,4DAA4D;QAC5D,KAAK,MAAM,IAAI,IAAsB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1D,iCAAiC;YACjC,IAAI,CAAuB,IAAI,CAAC,IAAI,CAAC;gBAC7B,CAAC,CAAC,KAAU,EAAE,OAA0D,EAAE,IAAS,EAAE,EAAE;oBAC3F,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;oBAC1B,IAAI,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,CAAC,EAAE,CAAC,CAAC,CAAC,CAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAE,CAAC,CAAC,EAAE,CAAC;oBAC3E,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBAC/B,CAAC;YACH,CAAC;YACD,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC9B,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;QACnC,CAAC;IACH,CAAC,CAAC;IAEe,SAAS,GAA6B,CAAC,GAAG,IAAI,EAAE,EAAE;QACjE,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;QACzB,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;QACpB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;IAC3B,CAAC,CAAC;IAEe,cAAc,GAA6B,CAAC,GAAG,IAAI,EAAE,EAAE;QACtE,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;QACzB,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;IACtB,CAAC,CAAC;IAEe,UAAU,GAA6B,CAAC,GAAG,IAAI,EAAE,EAAE;QAClE,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QACtB,CAAC;IACH,CAAC,CAAC;CACH","sourcesContent":["import { CoreFactory } from '../CoreFactory.js';\nimport type { GenRuleMap } from './builderTypes.js';\nimport type { GeneratorRule, RuleDefArg } from './generatorTypes.js';\n\nexport class DynamicGenerator<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {\n protected readonly factory = new CoreFactory();\n protected __context: Context | undefined = undefined;\n protected origSource = '';\n protected generatedUntil = 0;\n protected expectsSpace: boolean;\n protected readonly stringBuilder: string[] = [];\n\n public constructor(protected rules: RuleDefs) {\n // eslint-disable-next-line ts/no-unnecessary-type-assertion\n for (const rule of <GeneratorRule[]> Object.values(rules)) {\n // Define function implementation\n this[<keyof (typeof this)> rule.name] =\n <any> ((input: any, context: Context & { origSource: string; offset?: number }, args: any) => {\n this.expectsSpace = false;\n this.stringBuilder.length = 0;\n this.origSource = context.origSource;\n this.generatedUntil = context?.offset ?? 0;\n this.setContext(context);\n\n this.subrule(rule, input, args);\n\n this.catchup(this.origSource.length);\n\n return this.stringBuilder.join('');\n });\n }\n }\n\n public setContext(context: Context): void {\n this.__context = context;\n }\n\n protected getSafeContext(): Context {\n return <Context> this.__context;\n }\n\n protected readonly subrule: RuleDefArg['SUBRULE'] = (cstDef, ast, ...arg) => {\n const def = this.rules[<Names> cstDef.name];\n if (!def) {\n throw new Error(`Rule ${cstDef.name} not found`);\n }\n\n const generate = (): void => def.gImpl({\n SUBRULE: this.subrule,\n PRINT: this.print,\n PRINT_SPACE_LEFT: this.printSpaceLeft,\n PRINT_WORD: this.printWord,\n PRINT_WORDS: this.printWords,\n CATCHUP: this.catchup,\n HANDLE_LOC: this.handleLoc,\n })(ast, this.getSafeContext(), ...arg);\n\n if (this.factory.isLocalized(ast)) {\n this.handleLoc(ast, generate);\n } else {\n generate();\n }\n };\n\n protected readonly handleLoc: RuleDefArg['HANDLE_LOC'] = (localized, handle) => {\n if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {\n return;\n }\n if (this.factory.isSourceLocationStringReplace(localized.loc)) {\n this.catchup(localized.loc.start);\n this.print(localized.loc.newSource);\n this.generatedUntil = localized.loc.end;\n return;\n }\n if (this.factory.isSourceLocationNodeReplace(localized.loc)) {\n this.catchup(localized.loc.start);\n this.generatedUntil = localized.loc.end;\n }\n if (this.factory.isSourceLocationSource(localized.loc)) {\n this.catchup(localized.loc.start);\n }\n // If autoGenerate - do nothing\n\n const ret = handle();\n\n if (this.factory.isSourceLocationSource(localized.loc)) {\n this.catchup(localized.loc.end);\n }\n return ret;\n };\n\n protected readonly catchup: RuleDefArg['CATCHUP'] = (until) => {\n const start = this.generatedUntil;\n if (start < until) {\n this.print(this.origSource.slice(start, until));\n }\n this.generatedUntil = Math.max(this.generatedUntil, until);\n };\n\n protected readonly print: RuleDefArg['PRINT'] = (...args) => {\n const pureArgs = args.filter(x => x.length > 0);\n if (pureArgs.length > 0) {\n const [ head, ...tail ] = pureArgs;\n if (this.expectsSpace) {\n this.expectsSpace = false;\n const blanks = new Set([ '\\n', ' ', '\\t' ]);\n if (this.stringBuilder.length > 0 &&\n !(blanks.has(head[0]) || blanks.has(this.stringBuilder.at(-1)!.at(-1)!))) {\n this.stringBuilder.push(' ');\n }\n }\n this.stringBuilder.push(head);\n this.stringBuilder.push(...tail);\n }\n };\n\n private readonly printWord: RuleDefArg['PRINT_WORD'] = (...args) => {\n this.expectsSpace = true;\n this.print(...args);\n this.expectsSpace = true;\n };\n\n private readonly printSpaceLeft: RuleDefArg['PRINT_WORD'] = (...args) => {\n this.expectsSpace = true;\n this.print(...args);\n };\n\n private readonly printWords: RuleDefArg['PRINT_WORD'] = (...args) => {\n for (const arg of args) {\n this.printWord(arg);\n }\n };\n}\n"]}
1
+ {"version":3,"file":"dynamicGenerator.js","sourceRoot":"","sources":["dynamicGenerator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAIjD,MAAM,OAAO,gBAAgB;IAQE;IAPV,OAAO,GAAG,IAAI,cAAc,EAAE,CAAC;IACxC,SAAS,GAAwB,SAAS,CAAC;IAC3C,UAAU,GAAG,EAAE,CAAC;IAChB,cAAc,GAAG,CAAC,CAAC;IACnB,YAAY,CAAU;IACb,aAAa,GAAa,EAAE,CAAC;IAEhD,YAA6B,KAAe;QAAf,UAAK,GAAL,KAAK,CAAU;QAC1C,4DAA4D;QAC5D,KAAK,MAAM,IAAI,IAAsB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1D,iCAAiC;YACjC,IAAI,CA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GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;oBACvC,IAAI,MAAM,EAAE,CAAC;wBACX,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;oBACxD,CAAC;yBAAM,CAAC;wBACN,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;oBACxC,CAAC;gBACH,CAAC;gBACD,MAAM;YACR,CAAC;YACD,OAAO,GAAG,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAChC,OAAO,EAAE,CAAC;QACZ,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;IAC5B,CAAC,CAAC;CACH","sourcesContent":["import { AstCoreFactory } from '../AstCoreFactory.js';\nimport { traqulaIndentation } from '../utils.js';\nimport type { GenRuleMap } from './builderTypes.js';\nimport type { GeneratorRule, RuleDefArg } from './generatorTypes.js';\n\nexport class DynamicGenerator<Context, Names extends string, RuleDefs extends GenRuleMap<Names>> {\n protected readonly factory = new AstCoreFactory();\n protected __context: Context | undefined = undefined;\n protected origSource = '';\n protected generatedUntil = 0;\n protected expectsSpace: boolean;\n protected readonly stringBuilder: string[] = [];\n\n public constructor(protected rules: RuleDefs) {\n // eslint-disable-next-line ts/no-unnecessary-type-assertion\n for (const rule of <GeneratorRule[]> Object.values(rules)) {\n // Define function implementation\n this[<keyof (typeof this)> rule.name] =\n <any> ((input: any, context: Context & { origSource: string; offset?: number }, args: any) => {\n this.expectsSpace = false;\n this.stringBuilder.length = 0;\n this.origSource = context.origSource;\n this.generatedUntil = context?.offset ?? 0;\n this.setContext(context);\n\n this.subrule(rule, input, args);\n\n this.catchup(this.origSource.length);\n\n return this.stringBuilder.join('');\n });\n }\n }\n\n public setContext(context: Context): void {\n this.__context = context;\n }\n\n protected getSafeContext(): Context & { [traqulaIndentation]?: unknown } {\n return <Context & { [traqulaIndentation]?: unknown }> this.__context;\n }\n\n protected readonly subrule: RuleDefArg['SUBRULE'] = (cstDef, ast, ...arg) => {\n const def = this.rules[<Names> cstDef.name];\n if (!def) {\n throw new Error(`Rule ${cstDef.name} not found`);\n }\n\n const generate = (): void => def.gImpl({\n SUBRULE: this.subrule,\n PRINT: this.print,\n PRINT_SPACE_LEFT: this.printSpaceLeft,\n PRINT_WORD: this.printWord,\n PRINT_WORDS: this.printWords,\n PRINT_ON_EMPTY: this.printOnEmpty,\n CATCHUP: this.catchup,\n HANDLE_LOC: this.handleLoc,\n })(ast, this.getSafeContext(), ...arg);\n\n if (this.factory.isLocalized(ast)) {\n this.handleLoc(ast, generate);\n } else {\n generate();\n }\n };\n\n protected readonly handleLoc: RuleDefArg['HANDLE_LOC'] = (localized, handle) => {\n if (this.factory.isSourceLocationNoMaterialize(localized.loc)) {\n return;\n }\n if (this.factory.isSourceLocationStringReplace(localized.loc)) {\n this.catchup(localized.loc.start);\n this.print(localized.loc.newSource);\n this.generatedUntil = localized.loc.end;\n return;\n }\n if (this.factory.isSourceLocationNodeReplace(localized.loc)) {\n this.catchup(localized.loc.start);\n this.generatedUntil = localized.loc.end;\n }\n if (this.factory.isSourceLocationSource(localized.loc)) {\n this.catchup(localized.loc.start);\n }\n // If autoGenerate - do nothing\n\n const ret = handle();\n\n if (this.factory.isSourceLocationSource(localized.loc)) {\n this.catchup(localized.loc.end);\n }\n return ret;\n };\n\n protected readonly catchup: RuleDefArg['CATCHUP'] = (until) => {\n const start = this.generatedUntil;\n if (start < until) {\n this.print(this.origSource.slice(start, until));\n }\n this.generatedUntil = Math.max(this.generatedUntil, until);\n };\n\n protected readonly print: RuleDefArg['PRINT'] = (...args) => {\n const pureArgs = args.filter(x => x.length > 0);\n if (pureArgs.length > 0) {\n const head = pureArgs[0];\n if (this.expectsSpace) {\n this.expectsSpace = false;\n const blanks = new Set([ '\\n', ' ', '\\t' ]);\n if (this.stringBuilder.length > 0 &&\n !(blanks.has(head[0]) || blanks.has(this.stringBuilder.at(-1)!.at(-1)!))) {\n this.stringBuilder.push(' ');\n }\n }\n const context = this.getSafeContext();\n if (context[traqulaIndentation] && typeof context[traqulaIndentation] === 'number') {\n const indent = context[traqulaIndentation];\n for (const str of pureArgs) {\n const [ noNl, ...postNl ] = str.split('\\n');\n this.stringBuilder.push(noNl);\n for (const subStr of postNl.map(line => line.trimStart())) {\n this.stringBuilder.push('\\n', ' '.repeat(indent));\n if (subStr.length > 0) {\n this.stringBuilder.push(subStr);\n }\n }\n }\n } else {\n this.stringBuilder.push(...pureArgs);\n }\n }\n };\n\n private readonly printWord: RuleDefArg['PRINT_WORD'] = (...args) => {\n this.expectsSpace = true;\n this.print(...args);\n this.expectsSpace = true;\n };\n\n private readonly printSpaceLeft: RuleDefArg['PRINT_WORD'] = (...args) => {\n this.expectsSpace = true;\n this.print(...args);\n };\n\n private readonly printWords: RuleDefArg['PRINT_WORD'] = (...args) => {\n for (const arg of args) {\n this.printWord(arg);\n }\n };\n\n private readonly printOnEmpty: RuleDefArg['PRINT_ON_EMPTY'] = (...args) => {\n // Check if pointer already on empty line\n let counter = this.stringBuilder.length - 1;\n let onEmpty = true;\n const isEmptyTest = /^[ \\t\\n]*$/u;\n while (counter >= 0 && onEmpty) {\n const cur = this.stringBuilder[counter];\n const indexOfNl = cur.lastIndexOf('\\n');\n if (indexOfNl >= 0) {\n // This is the last newline that is printed\n onEmpty = isEmptyTest.test(cur.slice(indexOfNl));\n if (onEmpty) {\n // We still want to have the current indent printed\n const newVal = cur.slice(0, indexOfNl);\n if (newVal) {\n this.stringBuilder[counter] = cur.slice(0, indexOfNl);\n } else {\n this.stringBuilder.splice(counter, 1);\n }\n }\n break;\n }\n onEmpty = isEmptyTest.test(cur);\n counter--;\n }\n this.print('\\n', ...args);\n };\n}\n"]}
@@ -31,6 +31,7 @@ export interface RuleDefArg {
31
31
  PRINT_SPACE_LEFT: (...args: string[]) => void;
32
32
  PRINT_WORD: (...args: string[]) => void;
33
33
  PRINT_WORDS: (...args: string[]) => void;
34
+ PRINT_ON_EMPTY: (...args: string[]) => void;
34
35
  HANDLE_LOC: <T>(loc: Localized, nodeHandle: () => T) => T | undefined;
35
36
  CATCHUP: (until: number) => void;
36
37
  }
@@ -1 +1 @@
1
- {"version":3,"file":"generatorTypes.js","sourceRoot":"","sources":["generatorTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { Localized } from '../nodeTypings.js';\n\n/**\n * Type used to declare generator rules.\n */\nexport type GeneratorRule<\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 of the AST that we will generate the string for.\n * This type will be the provided when calling SUBRULE with this generator rule.\n * Generation happens on a per AST node basis.\n * The core will implement the generation as such. If this ever changes, we will cross that bridge when we get there.\n */\n AstType = any,\n /**\n * Function arguments that can be given to convey the state of the current parse operation.\n */\n ParamType extends any[] = any[],\n> = {\n name: NameType;\n gImpl: (def: RuleDefArg) =>\n (ast: AstType, context: Context, ...params: ParamType) => void;\n};\n\nexport interface RuleDefArg {\n SUBRULE: <T, U extends any[]>(cstDef: GeneratorRule<any, any, T, U>, input: T, ...arg: U) => void;\n PRINT: (...args: string[]) => void;\n PRINT_SPACE_LEFT: (...args: string[]) => void;\n PRINT_WORD: (...args: string[]) => void;\n PRINT_WORDS: (...args: string[]) => void;\n HANDLE_LOC: <T>(loc: Localized, nodeHandle: () => T) => T | undefined;\n CATCHUP: (until: number) => void;\n}\n"]}
1
+ {"version":3,"file":"generatorTypes.js","sourceRoot":"","sources":["generatorTypes.ts"],"names":[],"mappings":"","sourcesContent":["import type { Localized } from '../nodeTypings.js';\n\n/**\n * Type used to declare generator rules.\n */\nexport type GeneratorRule<\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 of the AST that we will generate the string for.\n * This type will be the provided when calling SUBRULE with this generator rule.\n * Generation happens on a per AST node basis.\n * The core will implement the generation as such. If this ever changes, we will cross that bridge when we get there.\n */\n AstType = any,\n /**\n * Function arguments that can be given to convey the state of the current parse operation.\n */\n ParamType extends any[] = any[],\n> = {\n name: NameType;\n gImpl: (def: RuleDefArg) =>\n (ast: AstType, context: Context, ...params: ParamType) => void;\n};\n\nexport interface RuleDefArg {\n SUBRULE: <T, U extends any[]>(cstDef: GeneratorRule<any, any, T, U>, input: T, ...arg: U) => void;\n PRINT: (...args: string[]) => void;\n PRINT_SPACE_LEFT: (...args: string[]) => void;\n PRINT_WORD: (...args: string[]) => void;\n PRINT_WORDS: (...args: string[]) => void;\n PRINT_ON_EMPTY: (...args: string[]) => void;\n HANDLE_LOC: <T>(loc: Localized, nodeHandle: () => T) => T | undefined;\n CATCHUP: (until: number) => void;\n}\n"]}