@weborigami/language 0.2.1 → 0.2.3
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/LICENSE +21 -0
- package/main.js +1 -0
- package/package.json +7 -7
- package/src/compiler/compile.js +43 -23
- package/src/compiler/origami.pegjs +22 -28
- package/src/compiler/parse.js +135 -153
- package/src/compiler/parserHelpers.js +18 -27
- package/src/runtime/errors.js +34 -12
- package/src/runtime/expressionObject.js +56 -13
- package/src/runtime/ops.js +10 -1
- package/src/runtime/taggedTemplate.js +1 -1
- package/src/runtime/taggedTemplateIndent.js +115 -0
- package/test/compiler/compile.test.js +19 -5
- package/test/compiler/parse.test.js +10 -7
- package/test/runtime/expressionObject.test.js +19 -1
- package/test/runtime/taggedTemplateIndent.test.js +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jan Miksovsky and other contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/main.js
CHANGED
|
@@ -13,5 +13,6 @@ export { default as InvokeFunctionsTransform } from "./src/runtime/InvokeFunctio
|
|
|
13
13
|
export { default as OrigamiFiles } from "./src/runtime/OrigamiFiles.js";
|
|
14
14
|
export * as symbols from "./src/runtime/symbols.js";
|
|
15
15
|
export { default as taggedTemplate } from "./src/runtime/taggedTemplate.js";
|
|
16
|
+
export { default as taggedTemplateIndent } from "./src/runtime/taggedTemplateIndent.js";
|
|
16
17
|
export { default as TreeEvent } from "./src/runtime/TreeEvent.js";
|
|
17
18
|
export { default as WatchFilesMixin } from "./src/runtime/WatchFilesMixin.js";
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weborigami/language",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Web Origami expression language compiler and runtime",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./main.js",
|
|
7
7
|
"types": "./index.ts",
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"@types/node": "22.
|
|
10
|
-
"peggy": "4.0.
|
|
11
|
-
"typescript": "5.
|
|
12
|
-
"yaml": "2.
|
|
9
|
+
"@types/node": "22.10.2",
|
|
10
|
+
"peggy": "4.2.0.",
|
|
11
|
+
"typescript": "5.7.2",
|
|
12
|
+
"yaml": "2.6.1"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@weborigami/async-tree": "0.2.
|
|
16
|
-
"@weborigami/types": "0.2.
|
|
15
|
+
"@weborigami/async-tree": "0.2.3",
|
|
16
|
+
"@weborigami/types": "0.2.3",
|
|
17
17
|
"watcher": "2.3.1"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
package/src/compiler/compile.js
CHANGED
|
@@ -5,7 +5,7 @@ import { parse } from "./parse.js";
|
|
|
5
5
|
import { annotate, undetermined } from "./parserHelpers.js";
|
|
6
6
|
|
|
7
7
|
function compile(source, options) {
|
|
8
|
-
const { startRule } = options;
|
|
8
|
+
const { macros, startRule } = options;
|
|
9
9
|
const enableCaching = options.scopeCaching ?? true;
|
|
10
10
|
if (typeof source === "string") {
|
|
11
11
|
source = { text: source };
|
|
@@ -15,7 +15,7 @@ function compile(source, options) {
|
|
|
15
15
|
startRule,
|
|
16
16
|
});
|
|
17
17
|
const cache = {};
|
|
18
|
-
const modified =
|
|
18
|
+
const modified = transformReferences(code, cache, enableCaching, macros);
|
|
19
19
|
const fn = createExpressionFunction(modified);
|
|
20
20
|
return fn;
|
|
21
21
|
}
|
|
@@ -27,14 +27,34 @@ export function expression(source, options = {}) {
|
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
export function program(source, options = {}) {
|
|
31
|
+
return compile(source, {
|
|
32
|
+
...options,
|
|
33
|
+
startRule: "program",
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function templateDocument(source, options = {}) {
|
|
38
|
+
return compile(source, {
|
|
39
|
+
...options,
|
|
40
|
+
startRule: "templateDocument",
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Transform any remaining undetermined references to scope references.
|
|
46
|
+
*
|
|
47
|
+
* At the same time, transform those or explicit ops.scope calls to ops.external
|
|
48
|
+
* calls unless they refer to local variables (variables defined by object
|
|
49
|
+
* literals or lambda parameters).
|
|
50
|
+
*
|
|
51
|
+
* Also apply any macros to the code.
|
|
52
|
+
*/
|
|
53
|
+
export function transformReferences(
|
|
35
54
|
code,
|
|
36
55
|
cache,
|
|
37
56
|
enableCaching,
|
|
57
|
+
macros,
|
|
38
58
|
locals = {}
|
|
39
59
|
) {
|
|
40
60
|
const [fn, ...args] = code;
|
|
@@ -45,7 +65,20 @@ export function transformScopeReferences(
|
|
|
45
65
|
case ops.scope:
|
|
46
66
|
const key = args[0];
|
|
47
67
|
const normalizedKey = trailingSlash.remove(key);
|
|
48
|
-
if (
|
|
68
|
+
if (macros?.[normalizedKey]) {
|
|
69
|
+
// Apply macro
|
|
70
|
+
const macroBody = macros[normalizedKey];
|
|
71
|
+
const modified = transformReferences(
|
|
72
|
+
macroBody,
|
|
73
|
+
cache,
|
|
74
|
+
enableCaching,
|
|
75
|
+
macros,
|
|
76
|
+
locals
|
|
77
|
+
);
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
annotate(modified, code.location);
|
|
80
|
+
return modified;
|
|
81
|
+
} else if (enableCaching && !locals[normalizedKey]) {
|
|
49
82
|
// Upgrade to cached external reference
|
|
50
83
|
const modified = [ops.external, key, cache];
|
|
51
84
|
// @ts-ignore
|
|
@@ -87,10 +120,11 @@ export function transformScopeReferences(
|
|
|
87
120
|
// be preferable to only descend into instructions. This would require
|
|
88
121
|
// surrounding ops.lambda parameters with ops.literal, and ops.object
|
|
89
122
|
// entries with ops.array.
|
|
90
|
-
return
|
|
123
|
+
return transformReferences(
|
|
91
124
|
child,
|
|
92
125
|
cache,
|
|
93
126
|
enableCaching,
|
|
127
|
+
macros,
|
|
94
128
|
updatedLocals
|
|
95
129
|
);
|
|
96
130
|
} else {
|
|
@@ -103,17 +137,3 @@ export function transformScopeReferences(
|
|
|
103
137
|
}
|
|
104
138
|
return modified;
|
|
105
139
|
}
|
|
106
|
-
|
|
107
|
-
export function program(source, options = {}) {
|
|
108
|
-
return compile(source, {
|
|
109
|
-
...options,
|
|
110
|
-
startRule: "program",
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function templateDocument(source, options = {}) {
|
|
115
|
-
return compile(source, {
|
|
116
|
-
...options,
|
|
117
|
-
startRule: "templateDocument",
|
|
118
|
-
});
|
|
119
|
-
}
|
|
@@ -61,7 +61,7 @@ arrayEntries
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
arrayEntry
|
|
64
|
-
=
|
|
64
|
+
= spreadElement
|
|
65
65
|
/ pipelineExpression
|
|
66
66
|
// JavaScript treats a missing value as `undefined`
|
|
67
67
|
/ __ !"]" {
|
|
@@ -113,7 +113,7 @@ callExpression "function call"
|
|
|
113
113
|
closingBrace
|
|
114
114
|
= "}"
|
|
115
115
|
/ .? {
|
|
116
|
-
error(
|
|
116
|
+
error(`An object ended without a closing brace, or contained something that wasn't expected.\nThe top level of an object can only contain definitions ("a: b" or "a = b") or spreads ("...a").`);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
// Required closing bracket
|
|
@@ -147,18 +147,20 @@ comment "comment"
|
|
|
147
147
|
/ singleLineComment
|
|
148
148
|
|
|
149
149
|
conditionalExpression
|
|
150
|
-
= condition:logicalOrExpression __
|
|
151
|
-
"?" __
|
|
152
|
-
":" __
|
|
150
|
+
= condition:logicalOrExpression tail:(__
|
|
151
|
+
"?" __ @pipelineExpression __
|
|
152
|
+
":" __ @pipelineExpression)?
|
|
153
153
|
{
|
|
154
|
+
if (!tail) {
|
|
155
|
+
return condition;
|
|
156
|
+
}
|
|
154
157
|
return annotate([
|
|
155
158
|
ops.conditional,
|
|
156
159
|
downgradeReference(condition),
|
|
157
|
-
[ops.lambda, [], downgradeReference(
|
|
158
|
-
[ops.lambda, [], downgradeReference(
|
|
160
|
+
[ops.lambda, [], downgradeReference(tail[0])],
|
|
161
|
+
[ops.lambda, [], downgradeReference(tail[1])]
|
|
159
162
|
], location());
|
|
160
163
|
}
|
|
161
|
-
/ logicalOrExpression
|
|
162
164
|
|
|
163
165
|
digits
|
|
164
166
|
= @[0-9]+
|
|
@@ -197,10 +199,9 @@ escapedChar "backslash-escaped character"
|
|
|
197
199
|
/ "\\" @.
|
|
198
200
|
|
|
199
201
|
exponentiationExpression
|
|
200
|
-
= left:unaryExpression __ "**" __
|
|
201
|
-
return annotate([ops.exponentiation, left, right], location());
|
|
202
|
+
= left:unaryExpression right:(__ "**" __ @exponentiationExpression)? {
|
|
203
|
+
return right ? annotate([ops.exponentiation, left, right], location()) : left;
|
|
202
204
|
}
|
|
203
|
-
/ unaryExpression
|
|
204
205
|
|
|
205
206
|
// A top-level expression, possibly with leading/trailing whitespace
|
|
206
207
|
expression
|
|
@@ -358,7 +359,7 @@ objectEntries
|
|
|
358
359
|
}
|
|
359
360
|
|
|
360
361
|
objectEntry
|
|
361
|
-
=
|
|
362
|
+
= spreadElement
|
|
362
363
|
/ objectProperty
|
|
363
364
|
/ objectGetter
|
|
364
365
|
/ objectShorthandProperty
|
|
@@ -557,8 +558,8 @@ singleQuoteString "single quote string"
|
|
|
557
558
|
singleQuoteStringChar
|
|
558
559
|
= !("'" / newLine) @textChar
|
|
559
560
|
|
|
560
|
-
|
|
561
|
-
= ellipsis __ value:
|
|
561
|
+
spreadElement
|
|
562
|
+
= ellipsis __ value:pipelineExpression {
|
|
562
563
|
return annotate([ops.spread, value], location());
|
|
563
564
|
}
|
|
564
565
|
|
|
@@ -570,20 +571,17 @@ stringLiteral "string"
|
|
|
570
571
|
// A top-level document defining a template. This is the same as a template
|
|
571
572
|
// literal, but can contain backticks at the top level.
|
|
572
573
|
templateDocument "template"
|
|
573
|
-
=
|
|
574
|
-
return annotate(
|
|
574
|
+
= head:templateDocumentText tail:(templateSubstitution templateDocumentText)* {
|
|
575
|
+
return annotate(
|
|
576
|
+
[ops.lambda, ["_"], makeTemplate(ops.templateIndent, head, tail)],
|
|
577
|
+
location()
|
|
578
|
+
);
|
|
575
579
|
}
|
|
576
580
|
|
|
577
581
|
// Template documents can contain backticks at the top level.
|
|
578
582
|
templateDocumentChar
|
|
579
583
|
= !("${") @textChar
|
|
580
584
|
|
|
581
|
-
// The contents of a template document containing plain text and substitutions
|
|
582
|
-
templateDocumentContents
|
|
583
|
-
= head:templateDocumentText tail:(templateSubstitution templateDocumentText)* {
|
|
584
|
-
return annotate(makeTemplate(ops.template, head, tail), location());
|
|
585
|
-
}
|
|
586
|
-
|
|
587
585
|
templateDocumentText "template text"
|
|
588
586
|
= chars:templateDocumentChar* {
|
|
589
587
|
return chars.join("");
|
|
@@ -591,17 +589,13 @@ templateDocumentText "template text"
|
|
|
591
589
|
|
|
592
590
|
// A backtick-quoted template literal
|
|
593
591
|
templateLiteral "template literal"
|
|
594
|
-
= "`"
|
|
595
|
-
return annotate(makeTemplate(ops.template,
|
|
592
|
+
= "`" head:templateLiteralText tail:(templateSubstitution templateLiteralText)* "`" {
|
|
593
|
+
return annotate(makeTemplate(ops.template, head, tail), location());
|
|
596
594
|
}
|
|
597
595
|
|
|
598
596
|
templateLiteralChar
|
|
599
597
|
= !("`" / "${") @textChar
|
|
600
598
|
|
|
601
|
-
// The contents of a template literal containing plain text and substitutions
|
|
602
|
-
templateLiteralContents
|
|
603
|
-
= head:templateLiteralText tail:(templateSubstitution templateLiteralText)*
|
|
604
|
-
|
|
605
599
|
// Plain text in a template literal
|
|
606
600
|
templateLiteralText
|
|
607
601
|
= chars:templateLiteralChar* {
|