@ja-platform/parser-expressions 0.0.0-stage → 7.1.0
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 +111 -2
- package/dist/@types/CombinedParser.d.ts +11 -0
- package/dist/@types/ExpressionParser.d.ts +12 -0
- package/dist/@types/code-parsers/CodeParser.d.ts +25 -0
- package/dist/@types/code-parsers/JavaScriptParser.d.ts +18 -0
- package/dist/@types/definitions/ValidationContext.d.ts +8 -0
- package/dist/@types/expressions/AbstractExpression.d.ts +42 -0
- package/dist/@types/expressions/EvaluatedExpression.d.ts +35 -0
- package/dist/@types/expressions/ListExpression.d.ts +31 -0
- package/dist/@types/expressions/constant/ConstantExpression.d.ts +28 -0
- package/dist/@types/expressions/function/FunctionExpression.d.ts +38 -0
- package/dist/@types/expressions/shorthand/FormatShorthandExpression.d.ts +31 -0
- package/dist/@types/expressions/shorthand/ShorthandExpression.d.ts +43 -0
- package/dist/@types/expressions/text/AbstractFormatExpression.d.ts +45 -0
- package/dist/@types/expressions/text/FormatStringExpression.d.ts +18 -0
- package/dist/@types/index.d.ts +26 -0
- package/dist/@types/tokens/AbstractToken.d.ts +12 -0
- package/dist/@types/tokens/DelimiterToken.d.ts +10 -0
- package/dist/@types/tokens/EnumToken.d.ts +10 -0
- package/dist/@types/tokens/code/CodeToken.d.ts +142 -0
- package/dist/@types/tokens/code/IdentifierToken.d.ts +12 -0
- package/dist/@types/tokens/code/JSToken.d.ts +31 -0
- package/dist/@types/tokens/code/PrefixToken.d.ts +9 -0
- package/dist/@types/tokens/text/PlaceholderToken.d.ts +12 -0
- package/dist/@types/tokens/text/TextToken.d.ts +12 -0
- package/dist/@types/utils/parserUtils.d.ts +7 -0
- package/dist/@types/utils/tokenUtils.d.ts +11 -0
- package/dist/@types/utils/valueFormatting.d.ts +9 -0
- package/dist/CombinedParser.js +24 -0
- package/dist/CombinedParser.js.map +1 -0
- package/dist/ExpressionParser.js +20 -0
- package/dist/ExpressionParser.js.map +1 -0
- package/dist/code-parsers/CodeParser.js +45 -0
- package/dist/code-parsers/CodeParser.js.map +1 -0
- package/dist/code-parsers/JavaScriptParser.js +56 -0
- package/dist/code-parsers/JavaScriptParser.js.map +1 -0
- package/dist/definitions/ValidationContext.js +3 -0
- package/dist/definitions/ValidationContext.js.map +1 -0
- package/dist/expressions/AbstractExpression.js +47 -0
- package/dist/expressions/AbstractExpression.js.map +1 -0
- package/dist/expressions/EvaluatedExpression.js +106 -0
- package/dist/expressions/EvaluatedExpression.js.map +1 -0
- package/dist/expressions/ListExpression.js +63 -0
- package/dist/expressions/ListExpression.js.map +1 -0
- package/dist/expressions/constant/ConstantExpression.js +54 -0
- package/dist/expressions/constant/ConstantExpression.js.map +1 -0
- package/dist/expressions/function/FunctionExpression.js +79 -0
- package/dist/expressions/function/FunctionExpression.js.map +1 -0
- package/dist/expressions/shorthand/FormatShorthandExpression.js +61 -0
- package/dist/expressions/shorthand/FormatShorthandExpression.js.map +1 -0
- package/dist/expressions/shorthand/ShorthandExpression.js +92 -0
- package/dist/expressions/shorthand/ShorthandExpression.js.map +1 -0
- package/dist/expressions/text/AbstractFormatExpression.js +179 -0
- package/dist/expressions/text/AbstractFormatExpression.js.map +1 -0
- package/dist/expressions/text/FormatStringExpression.js +44 -0
- package/dist/expressions/text/FormatStringExpression.js.map +1 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/tokens/AbstractToken.js +22 -0
- package/dist/tokens/AbstractToken.js.map +1 -0
- package/dist/tokens/DelimiterToken.js +22 -0
- package/dist/tokens/DelimiterToken.js.map +1 -0
- package/dist/tokens/EnumToken.js +22 -0
- package/dist/tokens/EnumToken.js.map +1 -0
- package/dist/tokens/code/CodeToken.js +33 -0
- package/dist/tokens/code/CodeToken.js.map +1 -0
- package/dist/tokens/code/IdentifierToken.js +28 -0
- package/dist/tokens/code/IdentifierToken.js.map +1 -0
- package/dist/tokens/code/JSToken.js +251 -0
- package/dist/tokens/code/JSToken.js.map +1 -0
- package/dist/tokens/code/PrefixToken.js +24 -0
- package/dist/tokens/code/PrefixToken.js.map +1 -0
- package/dist/tokens/text/PlaceholderToken.js +25 -0
- package/dist/tokens/text/PlaceholderToken.js.map +1 -0
- package/dist/tokens/text/TextToken.js +28 -0
- package/dist/tokens/text/TextToken.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/utils/parserUtils.js +23 -0
- package/dist/utils/parserUtils.js.map +1 -0
- package/dist/utils/tokenUtils.js +43 -0
- package/dist/utils/tokenUtils.js.map +1 -0
- package/dist/utils/valueFormatting.js +34 -0
- package/dist/utils/valueFormatting.js.map +1 -0
- package/package.json +37 -3
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { AbstractToken } from '../AbstractToken';
|
|
2
|
+
/**
|
|
3
|
+
* An unbound reference or direct call, independent of a language's syntax tree.
|
|
4
|
+
*/
|
|
5
|
+
export interface CodeReference {
|
|
6
|
+
/**
|
|
7
|
+
* The name resolved outside the code's local bindings.
|
|
8
|
+
*/
|
|
9
|
+
readonly name: string;
|
|
10
|
+
/**
|
|
11
|
+
* Whether the reference is a direct call rather than a value lookup.
|
|
12
|
+
*/
|
|
13
|
+
readonly isCall: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Call argument source tokens, including any nested reference replacements.
|
|
16
|
+
* Value references have no arguments.
|
|
17
|
+
*/
|
|
18
|
+
readonly arguments: readonly AbstractToken[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Static reference segments, independent of the source language.
|
|
22
|
+
*/
|
|
23
|
+
export type ReferencePath = readonly [string, ...(string | number)[]];
|
|
24
|
+
/**
|
|
25
|
+
* Original source and executable code for a language-specific token.
|
|
26
|
+
* Subclasses own their parser and syntax tree.
|
|
27
|
+
* Argument describes that language's argument representation; consumers can use
|
|
28
|
+
* sourceOf() to read its source without inspecting the language's syntax tree.
|
|
29
|
+
*/
|
|
30
|
+
export declare abstract class CodeToken<Argument = unknown> extends AbstractToken {
|
|
31
|
+
readonly source: string;
|
|
32
|
+
/**
|
|
33
|
+
* Preserve the original source and its position in the surrounding expression.
|
|
34
|
+
*/
|
|
35
|
+
protected constructor(source: string, start?: number);
|
|
36
|
+
/**
|
|
37
|
+
* Executable source supplied to the evaluator, without surrounding source trivia.
|
|
38
|
+
*/
|
|
39
|
+
abstract readonly code: string;
|
|
40
|
+
/**
|
|
41
|
+
* Free identifier names, including direct calls, in first-reference order.
|
|
42
|
+
* Local bindings and non-computed property names are excluded.
|
|
43
|
+
*/
|
|
44
|
+
abstract readonly freeIdentifiers: readonly string[];
|
|
45
|
+
/**
|
|
46
|
+
* Arguments of the root call. Requires isCallExpression() to be true.
|
|
47
|
+
*/
|
|
48
|
+
abstract readonly arguments: readonly Argument[];
|
|
49
|
+
/**
|
|
50
|
+
* Whether the root expression is a function call.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* new JSToken('save(true)').isCallExpression(); // true
|
|
55
|
+
* new JSToken('ready ? save() : null').isCallExpression(); // false
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
abstract isCallExpression(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Whether the root expression identifies a value that can be resolved as shorthand.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* new JSToken('user.name').isReferenceExpression(); // true
|
|
65
|
+
* new JSToken('count + 1').isReferenceExpression(); // false
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
abstract isReferenceExpression(): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Normalize a static reference. Dynamic keys and receivers are not paths.
|
|
71
|
+
*/
|
|
72
|
+
referencePath(): ReferencePath | null;
|
|
73
|
+
/**
|
|
74
|
+
* Source of the root call's target. Requires isCallExpression() to be true.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* new JSToken('worker.save(true)').functionName(); // 'worker.save'
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
abstract functionName(): string;
|
|
82
|
+
/**
|
|
83
|
+
* Original source for an argument belonging to this token, including its delimiters.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* const token = new JSToken('save((count + 1), true)');
|
|
88
|
+
* token.arguments.map((argument) => token.sourceOf(argument));
|
|
89
|
+
* // ['(count + 1)', 'true']
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
abstract sourceOf(argument: Argument): string;
|
|
93
|
+
/**
|
|
94
|
+
* Return a new token with the root call's target replaced by the supplied source.
|
|
95
|
+
* Requires isCallExpression() to be true; leaves this token unchanged.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* const token = new JSToken('save(true)');
|
|
100
|
+
* token.withName('worker.persist').raw(); // 'worker.persist(true)'
|
|
101
|
+
* token.raw(); // 'save(true)'
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
abstract withName(name: string): CodeToken<Argument>;
|
|
105
|
+
/**
|
|
106
|
+
* Return a new token with the root call's arguments replaced by language-specific source.
|
|
107
|
+
* Requires isCallExpression() to be true; leaves this token unchanged.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* new JSToken('save(true)').withArguments(['user.name', 'false']).raw();
|
|
112
|
+
* // 'save(user.name, false)'
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
abstract withArguments(args: readonly string[]): CodeToken<Argument>;
|
|
116
|
+
/**
|
|
117
|
+
* Return a new token after rewriting unbound references and direct calls.
|
|
118
|
+
* Return null from the callback to keep a reference unchanged. Replacement code
|
|
119
|
+
* must be valid in this token's language and is not visited again.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* const token = new JSToken('user.name + count');
|
|
124
|
+
* const rewritten = token.rewriteReferences((reference) => {
|
|
125
|
+
* if (reference.name === 'count' && !reference.isCall) {
|
|
126
|
+
* return new JSToken('state.total');
|
|
127
|
+
* }
|
|
128
|
+
* return null;
|
|
129
|
+
* });
|
|
130
|
+
* rewritten.raw(); // 'user.name + state.total'
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
abstract rewriteReferences(rewrite: (reference: CodeReference) => CodeToken | null): CodeToken<Argument>;
|
|
134
|
+
/**
|
|
135
|
+
* Copy this token with an independent syntax tree and the same source position.
|
|
136
|
+
*/
|
|
137
|
+
abstract clone(): CodeToken<Argument>;
|
|
138
|
+
/**
|
|
139
|
+
* Reproduce the original source, including surrounding whitespace and comments.
|
|
140
|
+
*/
|
|
141
|
+
raw(): string;
|
|
142
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractToken } from '../AbstractToken';
|
|
2
|
+
/**
|
|
3
|
+
* A single identifier with its original source position.
|
|
4
|
+
*/
|
|
5
|
+
export declare class IdentifierToken extends AbstractToken {
|
|
6
|
+
readonly source: string;
|
|
7
|
+
static isValid(source: string): boolean;
|
|
8
|
+
constructor(source: string, start?: number);
|
|
9
|
+
isValid(): boolean;
|
|
10
|
+
raw(): string;
|
|
11
|
+
clone(): this;
|
|
12
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Expression, Node, CallExpression } from '@babel/types';
|
|
2
|
+
import { CodeToken, CodeReference, ReferencePath } from './CodeToken';
|
|
3
|
+
/**
|
|
4
|
+
* JavaScript stays as one source token containing Babel's tree, without a second expression tree.
|
|
5
|
+
* Babel node offsets are relative to this token's source; start locates it in the surrounding source.
|
|
6
|
+
*/
|
|
7
|
+
export declare class JSToken extends CodeToken<CallExpression['arguments'][number]> {
|
|
8
|
+
private static readonly parser;
|
|
9
|
+
readonly ast: Expression;
|
|
10
|
+
readonly code: string;
|
|
11
|
+
readonly freeIdentifiers: readonly string[];
|
|
12
|
+
private readonly lexemes;
|
|
13
|
+
constructor(source: string, start?: number);
|
|
14
|
+
get arguments(): CallExpression['arguments'];
|
|
15
|
+
functionName(): string;
|
|
16
|
+
isCallExpression(): boolean;
|
|
17
|
+
isReferenceExpression(): boolean;
|
|
18
|
+
referencePath(): ReferencePath | null;
|
|
19
|
+
withName(name: string): JSToken;
|
|
20
|
+
withArguments(args: readonly string[]): JSToken;
|
|
21
|
+
/**
|
|
22
|
+
* Rewrite unbound references and direct calls, retaining scopes, property names and source trivia.
|
|
23
|
+
* Return null to leave a reference unchanged. Replacement source is not visited again.
|
|
24
|
+
*/
|
|
25
|
+
rewriteReferences(rewrite: (reference: CodeReference) => CodeToken | null): JSToken;
|
|
26
|
+
private call;
|
|
27
|
+
private replace;
|
|
28
|
+
sourceOf(node: Node): string;
|
|
29
|
+
punctuationAfter(text: string, start: number): number;
|
|
30
|
+
clone(): JSToken;
|
|
31
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AbstractToken } from '../AbstractToken';
|
|
2
|
+
export declare const FUNCTION_PREFIX = "$:";
|
|
3
|
+
export declare class PrefixToken extends AbstractToken {
|
|
4
|
+
readonly source: string;
|
|
5
|
+
constructor(start?: number, source?: string);
|
|
6
|
+
raw(): string;
|
|
7
|
+
isValid(): boolean;
|
|
8
|
+
clone(): this;
|
|
9
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractToken } from '../AbstractToken';
|
|
2
|
+
import type { AbstractExpression } from '../../expressions/AbstractExpression';
|
|
3
|
+
/**
|
|
4
|
+
* A format-string delimiter pair containing one expression.
|
|
5
|
+
*/
|
|
6
|
+
export declare class PlaceholderToken<T extends AbstractExpression = AbstractExpression> extends AbstractToken {
|
|
7
|
+
readonly expression: T;
|
|
8
|
+
constructor(expression: T, start?: number);
|
|
9
|
+
get children(): readonly AbstractToken[];
|
|
10
|
+
raw(): string;
|
|
11
|
+
clone(): PlaceholderToken<T>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractToken } from '../AbstractToken';
|
|
2
|
+
/**
|
|
3
|
+
* Original text, including any escaped format-string braces.
|
|
4
|
+
*/
|
|
5
|
+
export declare class TextToken extends AbstractToken {
|
|
6
|
+
readonly source: string;
|
|
7
|
+
static fromValue(value: string, start?: number): TextToken;
|
|
8
|
+
constructor(source: string, start?: number);
|
|
9
|
+
raw(): string;
|
|
10
|
+
clone(): this;
|
|
11
|
+
get decodedText(): string;
|
|
12
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AbstractExpression } from '../expressions/AbstractExpression';
|
|
2
|
+
import { ExpressionSource } from '../tokens/AbstractToken';
|
|
3
|
+
/**
|
|
4
|
+
* Recognize evaluated code and references before preserving remaining input as constant text.
|
|
5
|
+
* Callers can compose their own parser order instead.
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseExpression(source: ExpressionSource, start?: number): AbstractExpression | null;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AbstractToken, ExpressionSource } from '../tokens/AbstractToken';
|
|
2
|
+
import { CodeToken } from '../tokens/code/CodeToken';
|
|
3
|
+
import { JSToken } from '../tokens/code/JSToken';
|
|
4
|
+
export declare function stringify(source: ExpressionSource): string;
|
|
5
|
+
export declare function codeToken(tokens: readonly AbstractToken[]): CodeToken;
|
|
6
|
+
export declare function javascriptToken(tokens: readonly AbstractToken[]): JSToken;
|
|
7
|
+
export declare function hasFunctionPrefix(source: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Whitespace and comments before a Journey prefix remain original source text.
|
|
10
|
+
*/
|
|
11
|
+
export declare function leadingTrivia(source: string): string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Formatter } from '@ja-platform/parser-type-system';
|
|
2
|
+
/**
|
|
3
|
+
* Format an expression with a specific format.
|
|
4
|
+
*/
|
|
5
|
+
export declare function formatValue(value: any, formatter: Formatter | null, format?: string | null): string;
|
|
6
|
+
/**
|
|
7
|
+
* Resolve an object's asynchronous display value before falling back to its type formatter.
|
|
8
|
+
*/
|
|
9
|
+
export declare function formatValueAsync(value: any, formatter: Formatter | null, format?: string | null): Promise<string>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CombinedParser = void 0;
|
|
4
|
+
const ExpressionParser_1 = require("./ExpressionParser");
|
|
5
|
+
/**
|
|
6
|
+
* Try parsers in order and stop at the first matching expression.
|
|
7
|
+
*/
|
|
8
|
+
class CombinedParser extends ExpressionParser_1.ExpressionParser {
|
|
9
|
+
constructor(parsers) {
|
|
10
|
+
super();
|
|
11
|
+
this.parsers = Object.freeze([...parsers]);
|
|
12
|
+
}
|
|
13
|
+
tryParse(source, start = 0) {
|
|
14
|
+
let expression = null;
|
|
15
|
+
let index = 0;
|
|
16
|
+
while (expression === null && index < this.parsers.length) {
|
|
17
|
+
expression = this.parsers[index].tryParse(source, start);
|
|
18
|
+
index++;
|
|
19
|
+
}
|
|
20
|
+
return expression;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.CombinedParser = CombinedParser;
|
|
24
|
+
//# sourceMappingURL=CombinedParser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CombinedParser.js","sourceRoot":"","sources":["../src/CombinedParser.ts"],"names":[],"mappings":";;;AAAA,yDAAsD;AAItD;;GAEG;AACH,oBAA+E,SAAQ,mCAAmB;IAGxG,YAAY,OAAuC;QACjD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAC7C,CAAC;IAED,QAAQ,CAAC,MAAwB,EAAE,KAAK,GAAG,CAAC;QAC1C,IAAI,UAAU,GAAa,IAAI,CAAC;QAChC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,OAAO,UAAU,KAAK,IAAI,IAAI,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YAC1D,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;YACzD,KAAK,EAAE,CAAC;QACV,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;CACF","sourcesContent":["import { ExpressionParser } from './ExpressionParser';\nimport { AbstractExpression } from './expressions/AbstractExpression';\nimport { ExpressionSource } from './tokens/AbstractToken';\n\n/**\n * Try parsers in order and stop at the first matching expression.\n */\nexport class CombinedParser<T extends AbstractExpression = AbstractExpression> extends ExpressionParser<T> {\n readonly parsers: readonly ExpressionParser<T>[];\n\n constructor(parsers: readonly ExpressionParser<T>[]) {\n super();\n this.parsers = Object.freeze([...parsers]);\n }\n\n tryParse(source: ExpressionSource, start = 0): T | null {\n let expression: T | null = null;\n let index = 0;\n while (expression === null && index < this.parsers.length) {\n expression = this.parsers[index].tryParse(source, start);\n index++;\n }\n return expression;\n }\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExpressionParser = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Return null for unrecognized syntax; throw for malformed input belonging to this parser.
|
|
6
|
+
*/
|
|
7
|
+
class ExpressionParser {
|
|
8
|
+
/**
|
|
9
|
+
* Require a valid match, preserving any syntax error from the concrete parser.
|
|
10
|
+
*/
|
|
11
|
+
parse(source, start = 0) {
|
|
12
|
+
const expression = this.tryParse(source, start);
|
|
13
|
+
if (!expression || !expression.isValid()) {
|
|
14
|
+
throw new SyntaxError(`Input does not match ${this.constructor.name}.`);
|
|
15
|
+
}
|
|
16
|
+
return expression;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.ExpressionParser = ExpressionParser;
|
|
20
|
+
//# sourceMappingURL=ExpressionParser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpressionParser.js","sourceRoot":"","sources":["../src/ExpressionParser.ts"],"names":[],"mappings":";;;AAGA;;GAEG;AACH;IAGE;;OAEG;IACH,KAAK,CAAC,MAAwB,EAAE,KAAK,GAAG,CAAC;QACvC,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAChD,IAAI,CAAC,UAAU,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC;YACzC,MAAM,IAAI,WAAW,CAAC,wBAAwB,IAAI,CAAC,WAAW,CAAC,IAAI,GAAG,CAAC,CAAC;QAC1E,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;CACF","sourcesContent":["import { ExpressionSource } from './tokens/AbstractToken';\nimport { AbstractExpression } from './expressions/AbstractExpression';\n\n/**\n * Return null for unrecognized syntax; throw for malformed input belonging to this parser.\n */\nexport abstract class ExpressionParser<T extends AbstractExpression = AbstractExpression> {\n abstract tryParse(source: ExpressionSource, start?: number): T | null;\n\n /**\n * Require a valid match, preserving any syntax error from the concrete parser.\n */\n parse(source: ExpressionSource, start = 0): T {\n const expression = this.tryParse(source, start);\n if (!expression || !expression.isValid()) {\n throw new SyntaxError(`Input does not match ${this.constructor.name}.`);\n }\n return expression;\n }\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CodeParser = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Shared code parsing and caching, independent of the language's syntax tree.
|
|
6
|
+
*/
|
|
7
|
+
class CodeParser {
|
|
8
|
+
static getInstance() {
|
|
9
|
+
const globals = globalThis;
|
|
10
|
+
// Use the runtime global so separate evaluator bundles share the same cache owner.
|
|
11
|
+
if (!globals.__journeyappsCodeParser) {
|
|
12
|
+
globals.__journeyappsCodeParser = new CodeParser();
|
|
13
|
+
}
|
|
14
|
+
return globals.__journeyappsCodeParser;
|
|
15
|
+
}
|
|
16
|
+
constructor() {
|
|
17
|
+
this.cache = new Map();
|
|
18
|
+
this.limit = 256;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Releases cached results without affecting existing tokens.
|
|
22
|
+
*/
|
|
23
|
+
clear() {
|
|
24
|
+
this.cache.clear();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Reuses language parsing by exact source, returning an independent tree for each occurrence.
|
|
28
|
+
*/
|
|
29
|
+
parse(source, parser) {
|
|
30
|
+
// Include the language so identical source in different grammars cannot share a parse result.
|
|
31
|
+
const key = JSON.stringify([parser.language, source]);
|
|
32
|
+
if (!this.cache.has(key)) {
|
|
33
|
+
const parsed = parser.parse(source);
|
|
34
|
+
// Bound retained trees even when callers do not explicitly clear their parsing batch.
|
|
35
|
+
if (this.cache.size === this.limit) {
|
|
36
|
+
this.cache.delete(this.cache.keys().next().value);
|
|
37
|
+
}
|
|
38
|
+
this.cache.set(key, parsed);
|
|
39
|
+
}
|
|
40
|
+
// Never expose the cached tree, even on a miss: edits to a token must not affect later parses.
|
|
41
|
+
return structuredClone(this.cache.get(key));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.CodeParser = CodeParser;
|
|
45
|
+
//# sourceMappingURL=CodeParser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CodeParser.js","sourceRoot":"","sources":["../../src/code-parsers/CodeParser.ts"],"names":[],"mappings":";;;AASA;;GAEG;AACH;IACE,MAAM,CAAC,WAAW;QAChB,MAAM,OAAO,GAAG,UAEf,CAAC;QACF,mFAAmF;QACnF,IAAI,CAAC,OAAO,CAAC,uBAAuB,EAAE,CAAC;YACrC,OAAO,CAAC,uBAAuB,GAAG,IAAI,UAAU,EAAE,CAAC;QACrD,CAAC;QACD,OAAO,OAAO,CAAC,uBAAuB,CAAC;IACzC,CAAC;IAKD;QAHiB,UAAK,GAAG,IAAI,GAAG,EAAmB,CAAC;QACnC,UAAK,GAAG,GAAG,CAAC;IAEN,CAAC;IAExB;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,KAAK,CAAI,MAAc,EAAE,MAA6B;QACpD,8FAA8F;QAC9F,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACzB,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;YACpC,sFAAsF;YACtF,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;gBACnC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC;YACpD,CAAC;YACD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAC9B,CAAC;QACD,+FAA+F;QAC/F,OAAO,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAM,CAAC,CAAC;IACnD,CAAC;CACF","sourcesContent":["/**\n * Language-specific parsing injected into the shared code parser.\n * Results must support structuredClone; language identifies the grammar used for cache entries.\n */\nexport interface CodeLanguageParser<T> {\n readonly language: string;\n parse(source: string): T;\n}\n\n/**\n * Shared code parsing and caching, independent of the language's syntax tree.\n */\nexport class CodeParser {\n static getInstance(): CodeParser {\n const globals = globalThis as typeof globalThis & {\n __journeyappsCodeParser?: CodeParser;\n };\n // Use the runtime global so separate evaluator bundles share the same cache owner.\n if (!globals.__journeyappsCodeParser) {\n globals.__journeyappsCodeParser = new CodeParser();\n }\n return globals.__journeyappsCodeParser;\n }\n\n private readonly cache = new Map<string, unknown>();\n private readonly limit = 256;\n\n private constructor() {}\n\n /**\n * Releases cached results without affecting existing tokens.\n */\n clear(): void {\n this.cache.clear();\n }\n\n /**\n * Reuses language parsing by exact source, returning an independent tree for each occurrence.\n */\n parse<T>(source: string, parser: CodeLanguageParser<T>): T {\n // Include the language so identical source in different grammars cannot share a parse result.\n const key = JSON.stringify([parser.language, source]);\n if (!this.cache.has(key)) {\n const parsed = parser.parse(source);\n // Bound retained trees even when callers do not explicitly clear their parsing batch.\n if (this.cache.size === this.limit) {\n this.cache.delete(this.cache.keys().next().value);\n }\n this.cache.set(key, parsed);\n }\n // Never expose the cached tree, even on a miss: edits to a token must not affect later parses.\n return structuredClone(this.cache.get(key) as T);\n }\n}\n"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.JavaScriptParser = void 0;
|
|
7
|
+
const parser_1 = require("@babel/parser");
|
|
8
|
+
const types_1 = require("@babel/types");
|
|
9
|
+
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
10
|
+
/**
|
|
11
|
+
* Parses JavaScript source and preserves the ranges needed for source edits.
|
|
12
|
+
*/
|
|
13
|
+
class JavaScriptParser {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.language = 'javascript';
|
|
16
|
+
}
|
|
17
|
+
parse(source) {
|
|
18
|
+
let result;
|
|
19
|
+
let ast;
|
|
20
|
+
try {
|
|
21
|
+
result = (0, parser_1.parseExpression)(source, { allowAwaitOutsideFunction: true, tokens: true });
|
|
22
|
+
ast = result;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
// The statement parser also accepts a trailing semicolon and its comments.
|
|
26
|
+
result = (0, parser_1.parse)(source, { allowAwaitOutsideFunction: true, tokens: true });
|
|
27
|
+
if (result.program.body.length !== 1 || !(0, types_1.isExpressionStatement)(result.program.body[0])) {
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
ast = result.program.body[0].expression;
|
|
31
|
+
}
|
|
32
|
+
// Executable text excludes surrounding trivia and a trailing semicolon. Token offsets preserve
|
|
33
|
+
// grouping parentheses, which Babel's expression node range may omit.
|
|
34
|
+
const tokens = result.tokens.filter((token) => token.type !== 'CommentLine' &&
|
|
35
|
+
token.type !== 'CommentBlock' &&
|
|
36
|
+
token.end > token.start &&
|
|
37
|
+
source.slice(token.start, token.end) !== ';');
|
|
38
|
+
// Discover bindings while parsing so CodeParser caches this alongside the tree.
|
|
39
|
+
const freeIdentifiers = new Set();
|
|
40
|
+
(0, traverse_1.default)((0, types_1.file)((0, types_1.program)([(0, types_1.expressionStatement)(ast)])), {
|
|
41
|
+
ReferencedIdentifier(path) {
|
|
42
|
+
if (!path.scope.hasBinding(path.node.name, true)) {
|
|
43
|
+
freeIdentifiers.add(path.node.name);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return {
|
|
48
|
+
ast,
|
|
49
|
+
freeIdentifiers: [...freeIdentifiers],
|
|
50
|
+
code: source.slice(tokens[0].start, tokens[tokens.length - 1].end),
|
|
51
|
+
lexemes: Object.freeze(tokens.map((token) => Object.freeze({ start: token.start, end: token.end })))
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.JavaScriptParser = JavaScriptParser;
|
|
56
|
+
//# sourceMappingURL=JavaScriptParser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JavaScriptParser.js","sourceRoot":"","sources":["../../src/code-parsers/JavaScriptParser.ts"],"names":[],"mappings":";;;;;;AAAA,0CAAuD;AACvD,wCAAqG;AACrG,+DAAuC;AAUvC;;GAEG;AACH;IAAA;QACW,aAAQ,GAAG,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AAC,CAAC,CAAC,CAAC,EAAE;YAClD,oBAAoB,CAAC,IAAI;gBACvB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC;oBACjD,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACtC,CAAC;YACH,CAAC;SACF,CAAC,CAAC;QACH,OAAO;YACL,GAAG;YACH,eAAe,EAAE,CAAC,GAAG,eAAe,CAAC;YACrC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;YAClE,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAU,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;SAC1G,CAAC;IACJ,CAAC;CACF","sourcesContent":["import { parseExpression, parse } from '@babel/parser';\nimport { Expression, isExpressionStatement, file, program, expressionStatement } from '@babel/types';\nimport traverse from '@babel/traverse';\nimport { CodeLanguageParser } from './CodeParser';\n\nexport interface ParsedJavaScript {\n ast: Expression;\n code: string;\n freeIdentifiers: readonly string[];\n lexemes: readonly { start: number; end: number }[];\n}\n\n/**\n * Parses JavaScript source and preserves the ranges needed for source edits.\n */\nexport class JavaScriptParser implements CodeLanguageParser<ParsedJavaScript> {\n readonly language = 'javascript';\n\n parse(source: string): ParsedJavaScript {\n let result: any;\n let ast: Expression;\n try {\n result = parseExpression(source, { allowAwaitOutsideFunction: true, tokens: true });\n ast = result;\n } catch (error) {\n // The statement parser also accepts a trailing semicolon and its comments.\n result = parse(source, { allowAwaitOutsideFunction: true, tokens: true });\n if (result.program.body.length !== 1 || !isExpressionStatement(result.program.body[0])) {\n throw error;\n }\n ast = result.program.body[0].expression;\n }\n // Executable text excludes surrounding trivia and a trailing semicolon. Token offsets preserve\n // grouping parentheses, which Babel's expression node range may omit.\n const tokens = result.tokens.filter(\n (token: any) =>\n token.type !== 'CommentLine' &&\n token.type !== 'CommentBlock' &&\n token.end > token.start &&\n source.slice(token.start, token.end) !== ';'\n );\n // Discover bindings while parsing so CodeParser caches this alongside the tree.\n const freeIdentifiers = new Set<string>();\n traverse(file(program([expressionStatement(ast)])), {\n ReferencedIdentifier(path) {\n if (!path.scope.hasBinding(path.node.name, true)) {\n freeIdentifiers.add(path.node.name);\n }\n }\n });\n return {\n ast,\n freeIdentifiers: [...freeIdentifiers],\n code: source.slice(tokens[0].start, tokens[tokens.length - 1].end),\n lexemes: Object.freeze(tokens.map((token: any) => Object.freeze({ start: token.start, end: token.end })))\n };\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationContext.js","sourceRoot":"","sources":["../../src/definitions/ValidationContext.ts"],"names":[],"mappings":"","sourcesContent":["import type { ReferencePath } from '../tokens/code/CodeToken';\nimport type { Type } from '@ja-platform/parser-type-system';\n\n/**\n * Resolve declared references for expression validation without reading or evaluating values.\n */\nexport interface ValidationContext {\n getReference(path: ReferencePath): Type | null;\n}\n"]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractExpression = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Shared source tokens and positions for concrete expression types.
|
|
6
|
+
* Concrete types represent constant text such as `42`, references such as `user.name`,
|
|
7
|
+
* evaluated code such as `$:count + 1`, or format strings such as `Hello {user.name}`.
|
|
8
|
+
* raw() reproduces source syntax.
|
|
9
|
+
*/
|
|
10
|
+
class AbstractExpression {
|
|
11
|
+
constructor(type, options) {
|
|
12
|
+
var _a;
|
|
13
|
+
var _b, _c;
|
|
14
|
+
this.type = type;
|
|
15
|
+
this.options = Object.assign(Object.assign({}, options), { start: (_c = (_b = options.start) !== null && _b !== void 0 ? _b : (_a = options.tokens[0]) === null || _a === void 0 ? void 0 : _a.start) !== null && _c !== void 0 ? _c : 0, tokens: Object.freeze([...options.tokens]) });
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validate references against declarations without evaluating values.
|
|
19
|
+
*/
|
|
20
|
+
validate(_context) {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
clone() {
|
|
24
|
+
const Type = this.constructor;
|
|
25
|
+
return new Type(Object.assign(Object.assign({}, this.options), { tokens: this.tokens.map((token) => token.clone()) }));
|
|
26
|
+
}
|
|
27
|
+
get tokens() {
|
|
28
|
+
return this.options.tokens;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Reproduce source syntax, including whitespace, delimiters and escapes.
|
|
32
|
+
*/
|
|
33
|
+
raw() {
|
|
34
|
+
return this.tokens.map((token) => token.raw()).join('');
|
|
35
|
+
}
|
|
36
|
+
get start() {
|
|
37
|
+
return this.options.start;
|
|
38
|
+
}
|
|
39
|
+
set start(start) {
|
|
40
|
+
this.options.start = start;
|
|
41
|
+
}
|
|
42
|
+
toString() {
|
|
43
|
+
return '[object ' + this.constructor.name + ' <' + this.raw() + ', ' + this.start + '>]';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.AbstractExpression = AbstractExpression;
|
|
47
|
+
//# sourceMappingURL=AbstractExpression.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractExpression.js","sourceRoot":"","sources":["../../src/expressions/AbstractExpression.ts"],"names":[],"mappings":";;;AAiBA;;;;;GAKG;AACH;IAIE,YAAsB,IAAY,EAAE,OAAU;;;QAC5C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,mCACP,OAAO,KACV,KAAK,cAAE,OAAO,CAAC,KAAK,mCAAI,MAAA,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,0CAAE,KAAK,mCAAI,CAAC,EACrD,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAC3C,CAAC;IACJ,CAAC;IAOD;;OAEG;IACH,QAAQ,CAAC,QAA2B;QAClC,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,KAAK;QACH,MAAM,IAAI,GAAG,IAAI,CAAC,WAAuC,CAAC;QAC1D,OAAO,IAAI,IAAI,iCAAM,IAAI,CAAC,OAAO,KAAE,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,IAAG,CAAC;IAC1F,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,GAAG;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1D,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;IAC5B,CAAC;IAED,IAAI,KAAK,CAAC,KAAa;QACrB,IAAI,CAAC,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,OAAO,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IAC3F,CAAC;CACF","sourcesContent":["import { AbstractToken } from '../tokens/AbstractToken';\nimport type { ValidationContext } from '../definitions/ValidationContext';\n\nexport interface ExpressionDiagnostic {\n token: AbstractToken;\n message: string;\n}\n\nexport interface AbstractExpressionOptions {\n start?: number;\n /**\n * Complete source tokens prepared by a parser or value factory.\n * Constructors store these tokens without interpreting their source.\n */\n tokens: readonly AbstractToken[];\n}\n\n/**\n * Shared source tokens and positions for concrete expression types.\n * Concrete types represent constant text such as `42`, references such as `user.name`,\n * evaluated code such as `$:count + 1`, or format strings such as `Hello {user.name}`.\n * raw() reproduces source syntax.\n */\nexport abstract class AbstractExpression<O extends AbstractExpressionOptions = AbstractExpressionOptions> {\n type: string;\n options: O;\n\n protected constructor(type: string, options: O) {\n this.type = type;\n this.options = {\n ...options,\n start: options.start ?? options.tokens[0]?.start ?? 0,\n tokens: Object.freeze([...options.tokens])\n };\n }\n\n /**\n * Check whether the stored value and tokens represent this expression type.\n */\n abstract isValid(): boolean;\n\n /**\n * Validate references against declarations without evaluating values.\n */\n validate(_context: ValidationContext): ExpressionDiagnostic[] {\n return [];\n }\n\n clone(): this {\n const Type = this.constructor as new (options: O) => this;\n return new Type({ ...this.options, tokens: this.tokens.map((token) => token.clone()) });\n }\n\n get tokens(): readonly AbstractToken[] {\n return this.options.tokens;\n }\n\n /**\n * Reproduce source syntax, including whitespace, delimiters and escapes.\n */\n raw(): string {\n return this.tokens.map((token) => token.raw()).join('');\n }\n\n get start(): number | null {\n return this.options.start;\n }\n\n set start(start: number) {\n this.options.start = start;\n }\n\n toString(): string {\n return '[object ' + this.constructor.name + ' <' + this.raw() + ', ' + this.start + '>]';\n }\n}\n"]}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EvaluatedExpressionParser = exports.EvaluatedExpression = void 0;
|
|
4
|
+
const ExpressionParser_1 = require("../ExpressionParser");
|
|
5
|
+
const AbstractExpression_1 = require("./AbstractExpression");
|
|
6
|
+
const CodeToken_1 = require("../tokens/code/CodeToken");
|
|
7
|
+
const tokenUtils_1 = require("../utils/tokenUtils");
|
|
8
|
+
const JSToken_1 = require("../tokens/code/JSToken");
|
|
9
|
+
const PrefixToken_1 = require("../tokens/code/PrefixToken");
|
|
10
|
+
const TextToken_1 = require("../tokens/text/TextToken");
|
|
11
|
+
/**
|
|
12
|
+
* Code explicitly marked for evaluation by the `$:` prefix.
|
|
13
|
+
* Examples include `$:count + 1`, `$:true`, `$:{name: user.name}` and `$:[1, 2]`.
|
|
14
|
+
* The code token owns the language's syntax tree; the supplied scope executes its code.
|
|
15
|
+
* Prefixed calls are specialized by FunctionExpression when using the combined parser.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* EvaluatedExpression.parse('$:count + 1').code(); // 'count + 1'
|
|
20
|
+
* EvaluatedExpression.parse('$:{active: true}').raw(); // '$:{active: true}'
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
class EvaluatedExpression extends AbstractExpression_1.AbstractExpression {
|
|
24
|
+
static parse(source, start = 0) {
|
|
25
|
+
return new EvaluatedExpressionParser().parse(source, start);
|
|
26
|
+
}
|
|
27
|
+
constructor(options) {
|
|
28
|
+
super(EvaluatedExpression.TYPE, options);
|
|
29
|
+
}
|
|
30
|
+
isValid() {
|
|
31
|
+
const prefixes = this.tokens.filter((token) => token instanceof PrefixToken_1.PrefixToken);
|
|
32
|
+
const code = this.tokens.filter((token) => token instanceof CodeToken_1.CodeToken);
|
|
33
|
+
return (prefixes.length === 1 &&
|
|
34
|
+
prefixes[0].raw() === PrefixToken_1.FUNCTION_PREFIX &&
|
|
35
|
+
code.length === 1 &&
|
|
36
|
+
this.tokens.indexOf(prefixes[0]) < this.tokens.indexOf(code[0]));
|
|
37
|
+
}
|
|
38
|
+
get codeToken() {
|
|
39
|
+
return (0, tokenUtils_1.codeToken)(this.tokens);
|
|
40
|
+
}
|
|
41
|
+
withCode(token) {
|
|
42
|
+
const original = this.codeToken;
|
|
43
|
+
const Type = this.constructor;
|
|
44
|
+
return new Type(Object.assign(Object.assign({}, this.options), { tokens: this.tokens.map((current) => {
|
|
45
|
+
let replacement = current;
|
|
46
|
+
if (current === original) {
|
|
47
|
+
replacement = token;
|
|
48
|
+
}
|
|
49
|
+
return replacement;
|
|
50
|
+
}) }));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Executable code without the evaluation prefix or surrounding source trivia.
|
|
54
|
+
*/
|
|
55
|
+
code() {
|
|
56
|
+
return this.codeToken.code;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.EvaluatedExpression = EvaluatedExpression;
|
|
60
|
+
EvaluatedExpression.TYPE = 'evaluated-expression';
|
|
61
|
+
/**
|
|
62
|
+
* Recognize the syntax owned by EvaluatedExpression.
|
|
63
|
+
*/
|
|
64
|
+
class EvaluatedExpressionParser extends ExpressionParser_1.ExpressionParser {
|
|
65
|
+
tryParse(source, start = 0) {
|
|
66
|
+
let matches;
|
|
67
|
+
if (typeof source === 'string') {
|
|
68
|
+
matches = (0, tokenUtils_1.hasFunctionPrefix)(source);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
matches = source.some((token) => token instanceof PrefixToken_1.PrefixToken);
|
|
72
|
+
}
|
|
73
|
+
let expression = null;
|
|
74
|
+
if (matches) {
|
|
75
|
+
let tokens;
|
|
76
|
+
if (typeof source === 'string') {
|
|
77
|
+
tokens = this.sourceTokens(source, start);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
tokens = source;
|
|
81
|
+
}
|
|
82
|
+
expression = new EvaluatedExpression({
|
|
83
|
+
tokens
|
|
84
|
+
});
|
|
85
|
+
if (!expression.isValid()) {
|
|
86
|
+
throw new SyntaxError('Expected a prefixed code expression.');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return expression;
|
|
90
|
+
}
|
|
91
|
+
sourceTokens(source, start = 0) {
|
|
92
|
+
const trivia = (0, tokenUtils_1.leadingTrivia)(source);
|
|
93
|
+
if (!source.slice(trivia.length).startsWith(PrefixToken_1.FUNCTION_PREFIX)) {
|
|
94
|
+
throw new SyntaxError('Expected an evaluation prefix.');
|
|
95
|
+
}
|
|
96
|
+
const tokens = [];
|
|
97
|
+
if (trivia) {
|
|
98
|
+
tokens.push(new TextToken_1.TextToken(trivia, start));
|
|
99
|
+
}
|
|
100
|
+
tokens.push(new PrefixToken_1.PrefixToken(start + trivia.length));
|
|
101
|
+
tokens.push(new JSToken_1.JSToken(source.slice(trivia.length + PrefixToken_1.FUNCTION_PREFIX.length), start + trivia.length + PrefixToken_1.FUNCTION_PREFIX.length));
|
|
102
|
+
return tokens;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.EvaluatedExpressionParser = EvaluatedExpressionParser;
|
|
106
|
+
//# sourceMappingURL=EvaluatedExpression.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EvaluatedExpression.js","sourceRoot":"","sources":["../../src/expressions/EvaluatedExpression.ts"],"names":[],"mappings":";;;AAAA,0DAAuD;AACvD,6DAAqF;AAErF,wDAAqD;AACrD,oDAAkF;AAClF,oDAAiD;AACjD,4DAA0E;AAC1E,wDAAqD;AAErD;;;;;;;;;;;GAWG;AACH,yBAAiC,SAAQ,uCAA6C;IAGpF,MAAM,CAAC,KAAK,CAAC,MAAwB,EAAE,KAAK,GAAG,CAAC;QAC9C,OAAO,IAAI,yBAAyB,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC9D,CAAC;IAED,YAAY,OAAkC;QAC5C,KAAK,CAAC,mBAAmB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAC3C,CAAC;IAED,OAAO;QACL,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,YAAY,yBAAW,CAAC,CAAC;QAC7E,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,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AAI,CACT,IAAI,iBAAO,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,6BAAe,CAAC,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC,MAAM,GAAG,6BAAe,CAAC,MAAM,CAAC,CAClH,CAAC;QACF,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["import { ExpressionParser } from '../ExpressionParser';\nimport { AbstractExpression, AbstractExpressionOptions } from './AbstractExpression';\nimport { AbstractToken, ExpressionSource } from '../tokens/AbstractToken';\nimport { CodeToken } from '../tokens/code/CodeToken';\nimport { codeToken, hasFunctionPrefix, leadingTrivia } from '../utils/tokenUtils';\nimport { JSToken } from '../tokens/code/JSToken';\nimport { PrefixToken, FUNCTION_PREFIX } from '../tokens/code/PrefixToken';\nimport { TextToken } from '../tokens/text/TextToken';\n\n/**\n * Code explicitly marked for evaluation by the `$:` prefix.\n * Examples include `$:count + 1`, `$:true`, `$:{name: user.name}` and `$:[1, 2]`.\n * The code token owns the language's syntax tree; the supplied scope executes its code.\n * Prefixed calls are specialized by FunctionExpression when using the combined parser.\n *\n * @example\n * ```ts\n * EvaluatedExpression.parse('$:count + 1').code(); // 'count + 1'\n * EvaluatedExpression.parse('$:{active: true}').raw(); // '$:{active: true}'\n * ```\n */\nexport class EvaluatedExpression extends AbstractExpression<AbstractExpressionOptions> {\n static TYPE = 'evaluated-expression';\n\n static parse(source: ExpressionSource, start = 0): EvaluatedExpression {\n return new EvaluatedExpressionParser().parse(source, start);\n }\n\n constructor(options: AbstractExpressionOptions) {\n super(EvaluatedExpression.TYPE, options);\n }\n\n isValid(): boolean {\n const prefixes = this.tokens.filter((token) => token instanceof PrefixToken);\n const code = this.tokens.filter((token) => token instanceof CodeToken);\n return (\n prefixes.length === 1 &&\n prefixes[0].raw() === FUNCTION_PREFIX &&\n code.length === 1 &&\n this.tokens.indexOf(prefixes[0]) < this.tokens.indexOf(code[0])\n );\n }\n\n get codeToken(): CodeToken {\n return codeToken(this.tokens);\n }\n\n withCode(token: CodeToken): this {\n const original = this.codeToken;\n const Type = this.constructor as new (options: AbstractExpressionOptions) => this;\n return new Type({\n ...this.options,\n tokens: this.tokens.map((current) => {\n let replacement = current;\n if (current === original) {\n replacement = token;\n }\n return replacement;\n })\n });\n }\n\n /**\n * Executable code without the evaluation prefix or surrounding source trivia.\n */\n code(): string {\n return this.codeToken.code;\n }\n}\n\n/**\n * Recognize the syntax owned by EvaluatedExpression.\n */\nexport class EvaluatedExpressionParser extends ExpressionParser<EvaluatedExpression> {\n tryParse(source: ExpressionSource, start = 0): EvaluatedExpression | null {\n let matches: boolean;\n if (typeof source === 'string') {\n matches = hasFunctionPrefix(source);\n } else {\n matches = source.some((token) => token instanceof PrefixToken);\n }\n let expression: EvaluatedExpression | null = null;\n if (matches) {\n let tokens: readonly AbstractToken[];\n if (typeof source === 'string') {\n tokens = this.sourceTokens(source, start);\n } else {\n tokens = source;\n }\n expression = new EvaluatedExpression({\n tokens\n });\n if (!expression.isValid()) {\n throw new SyntaxError('Expected a prefixed code expression.');\n }\n }\n return expression;\n }\n\n sourceTokens(source: string, start = 0): readonly AbstractToken[] {\n const trivia = leadingTrivia(source);\n if (!source.slice(trivia.length).startsWith(FUNCTION_PREFIX)) {\n throw new SyntaxError('Expected an evaluation prefix.');\n }\n const tokens: AbstractToken[] = [];\n if (trivia) {\n tokens.push(new TextToken(trivia, start));\n }\n tokens.push(new PrefixToken(start + trivia.length));\n tokens.push(\n new JSToken(source.slice(trivia.length + FUNCTION_PREFIX.length), start + trivia.length + FUNCTION_PREFIX.length)\n );\n return tokens;\n }\n}\n"]}
|