@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.
@@ -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 main() {
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 parserGenerator = Sparse.fromProductions<LoLangTokenType, {}, Node>({ productions, toStringifiedTokenType });
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
- console.log(parser.parse());
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();