@scinorandex/sparse 0.0.7 → 0.1.1
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/AGENTS.md +40 -0
- package/README.md +6 -5
- package/dist/cli.js +2 -2
- package/dist/cli.js.map +1 -1
- package/dist/generator.d.ts +6 -1
- package/dist/generator.js +151 -46
- package/dist/generator.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js.map +1 -1
- package/dist/meta/common.d.ts +12 -10
- package/dist/meta/common.js +12 -10
- package/dist/meta/common.js.map +1 -1
- package/dist/meta/selfhosted.js +68 -17
- package/dist/meta/selfhosted.js.map +1 -1
- package/dist/meta/states.d.ts +108 -0
- package/dist/meta/states.js +213 -47
- package/dist/meta/states.js.map +1 -1
- package/dist/parser.d.ts +2 -0
- package/dist/parser.js +1 -1
- package/dist/parser.js.map +1 -1
- package/example/LoLang/example.ts +27 -4
- package/example/LoLang/table2.txt +497 -0
- package/example/LoLang/tablelalr.txt +381 -0
- package/example/kleene-test/example.ts +86 -0
- package/example/kleene-test/grammar.txt +2 -0
- package/example/selfhosted/01-meta-grammar.txt +1 -1
- package/example/selfhosted/codegen-meta.ts +213 -47
- package/opencode.json +18 -0
- package/package.json +9 -3
- package/src/cli.ts +2 -2
- package/src/generator.ts +191 -62
- package/src/index.ts +1 -0
- package/src/meta/common.ts +2 -0
- package/src/meta/selfhosted.ts +97 -33
- package/src/meta/states.ts +213 -47
- package/src/parser.ts +3 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fs from "fs/promises";
|
|
2
|
-
import { buildProductions, LR1StackSymbol, Sparse } from "../../src/index";
|
|
2
|
+
import { buildProductions, buildStates, LR1StackSymbol, Sparse } from "../../src/index";
|
|
3
3
|
import { ColumnAndRow, Slex, Token } from "@scinorandex/slex";
|
|
4
4
|
|
|
5
5
|
// prettier-ignore
|
|
@@ -165,13 +165,14 @@ class Node {
|
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
async function
|
|
168
|
+
async function parse(tablePath: string) {
|
|
169
169
|
const exampleSourceCode = await fs.readFile("./example/LoLang/Features_Array_Methods.lol", "utf8");
|
|
170
170
|
const lexer = lexerGenerator.generate(exampleSourceCode, () => ({}));
|
|
171
171
|
|
|
172
172
|
const toStringifiedTokenType = (type: LoLangTokenType) => LoLangTokenType[type];
|
|
173
173
|
const productions = buildProductions(await fs.readFile("./example/LoLang/grammar.txt", "utf8"));
|
|
174
|
-
const
|
|
174
|
+
const states = buildStates(await fs.readFile(tablePath, "utf8"));
|
|
175
|
+
const parserGenerator = new Sparse<LoLangTokenType, {}, Node>({ productions, toStringifiedTokenType, states });
|
|
175
176
|
|
|
176
177
|
const parser = parserGenerator.generate(lexer, {
|
|
177
178
|
reducer: (_, { input }) => new Node(input),
|
|
@@ -215,7 +216,29 @@ async function main() {
|
|
|
215
216
|
},
|
|
216
217
|
});
|
|
217
218
|
|
|
218
|
-
|
|
219
|
+
const parsingResult = parser.parse();
|
|
220
|
+
return JSON.stringify(parsingResult.result?.toObject());
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function main() {
|
|
224
|
+
let nsTime = Date.now();
|
|
225
|
+
const lr1Result = await parse("./example/LoLang/table.txt");
|
|
226
|
+
const lr1Time = Date.now() - nsTime;
|
|
227
|
+
|
|
228
|
+
nsTime = Date.now();
|
|
229
|
+
const lalrResult = await parse("./example/LoLang/tablelalr.txt");
|
|
230
|
+
const lalrTime = Date.now() - nsTime;
|
|
231
|
+
|
|
232
|
+
console.log(`LR(1) time: ${lr1Time}ms`);
|
|
233
|
+
console.log(`LALR(1) time: ${lalrTime}ms`);
|
|
234
|
+
const delta = Math.abs(lalrTime - lr1Time);
|
|
235
|
+
console.log("LR(1) is " + (lr1Time < lalrTime ? "faster" : "slower") + " than LALR(1)" + ` (${delta}ms)`);
|
|
236
|
+
|
|
237
|
+
console.log("==============================");
|
|
238
|
+
console.log(`LR(1) result: ${lr1Result}`);
|
|
239
|
+
console.log(`LALR(1) result: ${lalrResult}`);
|
|
240
|
+
console.log("==============================");
|
|
241
|
+
console.log(lr1Result === lalrResult ? "Equal output" : "Not equal output");
|
|
219
242
|
}
|
|
220
243
|
|
|
221
244
|
main();
|