@weborigami/language 0.2.7 → 0.2.8
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/index.ts +17 -9
- package/package.json +3 -3
- package/src/compiler/optimize.js +36 -20
- package/src/compiler/origami.pegjs +48 -37
- package/src/compiler/parse.d.ts +2 -2
- package/src/compiler/parse.js +299 -284
- package/src/compiler/parserHelpers.js +123 -76
- package/src/runtime/errors.js +5 -2
- package/src/runtime/evaluate.js +1 -10
- package/src/runtime/expressionFunction.js +1 -1
- package/src/runtime/ops.js +18 -5
- package/test/compiler/{stripCodeLocations.js → codeHelpers.js} +24 -0
- package/test/compiler/compile.test.js +3 -3
- package/test/compiler/optimize.test.js +9 -11
- package/test/compiler/parse.test.js +22 -8
- package/test/runtime/OrigamiFiles.test.js +0 -2
- package/test/runtime/evaluate.test.js +1 -13
- package/test/runtime/mergeTrees.test.js +0 -1
- package/test/runtime/ops.test.js +1 -13
|
@@ -3,7 +3,7 @@ import { describe, test } from "node:test";
|
|
|
3
3
|
import { parse } from "../../src/compiler/parse.js";
|
|
4
4
|
import { undetermined } from "../../src/compiler/parserHelpers.js";
|
|
5
5
|
import * as ops from "../../src/runtime/ops.js";
|
|
6
|
-
import {
|
|
6
|
+
import { assertCodeEqual } from "./codeHelpers.js";
|
|
7
7
|
|
|
8
8
|
describe("Origami parser", () => {
|
|
9
9
|
test("additiveExpression", () => {
|
|
@@ -247,20 +247,20 @@ describe("Origami parser", () => {
|
|
|
247
247
|
assertParse("conditionalExpression", "true ? 1 : 0", [
|
|
248
248
|
ops.conditional,
|
|
249
249
|
[ops.scope, "true"],
|
|
250
|
-
[ops.
|
|
251
|
-
[ops.
|
|
250
|
+
[ops.literal, "1"],
|
|
251
|
+
[ops.literal, "0"],
|
|
252
252
|
]);
|
|
253
253
|
assertParse("conditionalExpression", "false ? () => 1 : 0", [
|
|
254
254
|
ops.conditional,
|
|
255
255
|
[ops.scope, "false"],
|
|
256
256
|
[ops.lambda, [], [ops.lambda, [], [ops.literal, "1"]]],
|
|
257
|
-
[ops.
|
|
257
|
+
[ops.literal, "0"],
|
|
258
258
|
]);
|
|
259
259
|
assertParse("conditionalExpression", "false ? =1 : 0", [
|
|
260
260
|
ops.conditional,
|
|
261
261
|
[ops.scope, "false"],
|
|
262
262
|
[ops.lambda, [], [ops.lambda, ["_"], [ops.literal, "1"]]],
|
|
263
|
-
[ops.
|
|
263
|
+
[ops.literal, "0"],
|
|
264
264
|
]);
|
|
265
265
|
});
|
|
266
266
|
|
|
@@ -873,6 +873,12 @@ describe("Origami parser", () => {
|
|
|
873
873
|
[ops.literal, "localhost:5000/"],
|
|
874
874
|
[ops.literal, "foo"],
|
|
875
875
|
]);
|
|
876
|
+
assertParse("protocolExpression", "files:///foo/bar.txt", [
|
|
877
|
+
[ops.builtin, "files:"],
|
|
878
|
+
[ops.literal, "/"],
|
|
879
|
+
[ops.literal, "foo/"],
|
|
880
|
+
[ops.literal, "bar.txt"],
|
|
881
|
+
]);
|
|
876
882
|
});
|
|
877
883
|
|
|
878
884
|
test("qualifiedReference", () => {
|
|
@@ -1076,7 +1082,7 @@ function assertParse(startRule, source, expected, checkLocation = true) {
|
|
|
1076
1082
|
// entire source. We skip this check in cases where the source starts or ends
|
|
1077
1083
|
// with a comment; the parser will strip those.
|
|
1078
1084
|
if (checkLocation) {
|
|
1079
|
-
|
|
1085
|
+
assertCodeLocations(code);
|
|
1080
1086
|
const resultSource = code.location.source.text.slice(
|
|
1081
1087
|
code.location.start.offset,
|
|
1082
1088
|
code.location.end.offset
|
|
@@ -1084,6 +1090,14 @@ function assertParse(startRule, source, expected, checkLocation = true) {
|
|
|
1084
1090
|
assert.equal(resultSource, source.trim());
|
|
1085
1091
|
}
|
|
1086
1092
|
|
|
1087
|
-
|
|
1088
|
-
|
|
1093
|
+
assertCodeEqual(code, expected);
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
function assertCodeLocations(code) {
|
|
1097
|
+
assert(code.location, "no location");
|
|
1098
|
+
for (const item of code) {
|
|
1099
|
+
if (Array.isArray(item)) {
|
|
1100
|
+
assertCodeLocations(item);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1089
1103
|
}
|
|
@@ -13,11 +13,9 @@ describe("OrigamiFiles", () => {
|
|
|
13
13
|
await createTempDirectory();
|
|
14
14
|
const tempFiles = new OrigamiFiles(tempDirectory);
|
|
15
15
|
const changedFileName = await new Promise(async (resolve) => {
|
|
16
|
-
// @ts-ignore
|
|
17
16
|
tempFiles.addEventListener("change", (event) => {
|
|
18
17
|
resolve(/** @type {any} */ (event).options.key);
|
|
19
18
|
});
|
|
20
|
-
// @ts-ignore
|
|
21
19
|
await tempFiles.set(
|
|
22
20
|
"foo.txt",
|
|
23
21
|
"This file is left over from testing and can be removed."
|
|
@@ -4,6 +4,7 @@ import { describe, test } from "node:test";
|
|
|
4
4
|
import * as ops from "../../src/runtime/ops.js";
|
|
5
5
|
|
|
6
6
|
import evaluate from "../../src/runtime/evaluate.js";
|
|
7
|
+
import { createCode } from "../compiler/codeHelpers.js";
|
|
7
8
|
|
|
8
9
|
describe("evaluate", () => {
|
|
9
10
|
test("can retrieve values from scope", async () => {
|
|
@@ -70,16 +71,3 @@ describe("evaluate", () => {
|
|
|
70
71
|
assert.equal(result.parent, tree);
|
|
71
72
|
});
|
|
72
73
|
});
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* @returns {import("../../index.ts").Code}
|
|
76
|
-
*/
|
|
77
|
-
function createCode(array) {
|
|
78
|
-
const code = array;
|
|
79
|
-
/** @type {any} */ (code).location = {
|
|
80
|
-
source: {
|
|
81
|
-
text: "",
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
return code;
|
|
85
|
-
}
|
package/test/runtime/ops.test.js
CHANGED
|
@@ -3,6 +3,7 @@ import assert from "node:assert";
|
|
|
3
3
|
import { describe, test } from "node:test";
|
|
4
4
|
|
|
5
5
|
import { evaluate, ops } from "../../src/runtime/internal.js";
|
|
6
|
+
import { createCode } from "../compiler/codeHelpers.js";
|
|
6
7
|
|
|
7
8
|
describe("ops", () => {
|
|
8
9
|
test("ops.addition adds two numbers", async () => {
|
|
@@ -378,19 +379,6 @@ describe("ops", () => {
|
|
|
378
379
|
});
|
|
379
380
|
});
|
|
380
381
|
|
|
381
|
-
/**
|
|
382
|
-
* @returns {import("../../index.ts").Code}
|
|
383
|
-
*/
|
|
384
|
-
function createCode(array) {
|
|
385
|
-
const code = array;
|
|
386
|
-
/** @type {any} */ (code).location = {
|
|
387
|
-
source: {
|
|
388
|
-
text: "",
|
|
389
|
-
},
|
|
390
|
-
};
|
|
391
|
-
return code;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
382
|
function errorFn() {
|
|
395
383
|
throw new Error("This should not be called");
|
|
396
384
|
}
|