@graffiticode/parser 1.4.0 → 1.4.2

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.
@@ -6,7 +6,9 @@
6
6
  "Bash(npm test:*)",
7
7
  "Bash(npm publish:*)",
8
8
  "Bash(grep:*)",
9
- "Bash(npm install:*)"
9
+ "Bash(npm install:*)",
10
+ "Bash(NODE_OPTIONS=--experimental-vm-modules node -e \"\nimport { parser } from './src/parser.js';\nimport { unparse } from './src/unparse.js';\nimport { lexicon as basisLexicon } from '@graffiticode/basis';\n\nconst src = 'case x of 1: \\\\\"one\\\\\" 2: \\\\\"two\\\\\" end..';\nconsole.log\\('Input:', src\\);\nconst ast = await parser.parse\\(0, src, basisLexicon\\);\nconsole.log\\('AST:', JSON.stringify\\(ast, null, 2\\)\\);\nconst result = unparse\\(ast, basisLexicon\\);\nconsole.log\\('Unparsed:', result\\);\n\")",
11
+ "Bash(NODE_OPTIONS=--experimental-vm-modules node -e \"\nimport { parser } from './src/parser.js';\nimport { unparse } from './src/unparse.js';\nimport { lexicon as basisLexicon } from '@graffiticode/basis';\n\n// Test nested case-of\nconst src1 = 'case x of 1: case y of 3: \\\\\"a\\\\\" 4: \\\\\"b\\\\\" end 2: \\\\\"two\\\\\" end..';\nconsole.log\\('Input:', src1\\);\ntry {\n const ast1 = await parser.parse\\(0, src1, basisLexicon\\);\n console.log\\('Unparsed:', unparse\\(ast1, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\nconsole.log\\('---'\\);\n\n// Test with expression as case target\nconst src2 = 'case add 1 2 of 3: \\\\\"yes\\\\\" 4: \\\\\"no\\\\\" end..';\nconsole.log\\('Input:', src2\\);\ntry {\n const ast2 = await parser.parse\\(0, src2, basisLexicon\\);\n console.log\\('AST:', JSON.stringify\\(ast2, null, 2\\)\\);\n console.log\\('Unparsed:', unparse\\(ast2, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\nconsole.log\\('---'\\);\n\n// Test with identifier patterns\nconst src3 = 'case x of y: y end..';\nconsole.log\\('Input:', src3\\);\ntry {\n const ast3 = await parser.parse\\(0, src3, basisLexicon\\);\n console.log\\('AST:', JSON.stringify\\(ast3, null, 2\\)\\);\n console.log\\('Unparsed:', unparse\\(ast3, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\nconsole.log\\('---'\\);\n\n// Test case-of inside a let or other expression\nconst src4 = 'let x = 5 in case x of 5: \\\\\"five\\\\\" end..';\nconsole.log\\('Input:', src4\\);\ntry {\n const ast4 = await parser.parse\\(0, src4, basisLexicon\\);\n console.log\\('Unparsed:', unparse\\(ast4, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\nconsole.log\\('---'\\);\n\n// Test case-of as argument to a function\nconst src5 = 'add \\(case x of 1: 10 2: 20 end\\) 5..';\nconsole.log\\('Input:', src5\\);\ntry {\n const ast5 = await parser.parse\\(0, src5, basisLexicon\\);\n console.log\\('Unparsed:', unparse\\(ast5, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\")"
10
12
  ]
11
13
  }
12
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graffiticode/parser",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -46,4 +46,33 @@ describe("folder", () => {
46
46
  expect(n2.tag).toBe("NUM");
47
47
  expect(n2.elts[0]).toBe("3");
48
48
  });
49
+
50
+ it("should pass 'pow 2 3' through as POW node", () => {
51
+ const ctx = {
52
+ state: {
53
+ nodePool: ["unused"],
54
+ nodeStack: [],
55
+ nodeStackStack: [],
56
+ nodeMap: {},
57
+ env: [{ name: "global", lexicon: {} }]
58
+ }
59
+ };
60
+
61
+ const n1Id = Ast.intern(ctx, { tag: "NUM", elts: ["2"] });
62
+ const n2Id = Ast.intern(ctx, { tag: "NUM", elts: ["3"] });
63
+ const powNodeId = Ast.intern(ctx, { tag: "POW", elts: [n1Id, n2Id] });
64
+
65
+ Folder.fold(ctx, powNodeId);
66
+ const resultId = ctx.state.nodeStack.pop();
67
+ const resultNode = ctx.state.nodePool[resultId];
68
+
69
+ expect(resultNode.tag).toBe("POW");
70
+ expect(resultNode.elts.length).toBe(2);
71
+ const n1 = Ast.node(ctx, resultNode.elts[0]);
72
+ const n2 = Ast.node(ctx, resultNode.elts[1]);
73
+ expect(n1.tag).toBe("NUM");
74
+ expect(n1.elts[0]).toBe("2");
75
+ expect(n2.tag).toBe("NUM");
76
+ expect(n2.elts[0]).toBe("3");
77
+ });
49
78
  });
package/src/unparse.js CHANGED
@@ -100,7 +100,7 @@ function unparseNode(node, lexicon, indent = 0, options = {}) {
100
100
  if (opts.compact) {
101
101
  // Compact mode: inline list
102
102
  const items = node.elts.map(elt => unparseNode(elt, lexicon, indent, opts));
103
- return "[" + items.join(", ") + "]";
103
+ return "[" + items.join(" ") + "]";
104
104
  } else {
105
105
  // Pretty print with each element on a new line
106
106
  const innerIndent = indent + opts.indentSize;
@@ -166,10 +166,10 @@ function unparseNode(node, lexicon, indent = 0, options = {}) {
166
166
  return "";
167
167
 
168
168
  case "LAMBDA":
169
- // Lambda function
170
- if (node.elts && node.elts.length >= 3) {
171
- const params = node.elts[1];
172
- const body = node.elts[2];
169
+ // Lambda function: elts = [param_names_list, body, pattern_list, init_list]
170
+ if (node.elts && node.elts.length >= 2) {
171
+ const params = node.elts[0];
172
+ const body = node.elts[1];
173
173
 
174
174
  // Extract parameter names
175
175
  let paramStr = "";
@@ -181,9 +181,9 @@ function unparseNode(node, lexicon, indent = 0, options = {}) {
181
181
  const bodyStr = unparseNode(body, lexicon, indent, opts);
182
182
 
183
183
  if (paramStr) {
184
- return `\\${paramStr} . ${bodyStr}`;
184
+ return `<${paramStr}: ${bodyStr}>`;
185
185
  } else {
186
- return `\\. ${bodyStr}`;
186
+ return `<: ${bodyStr}>`;
187
187
  }
188
188
  }
189
189
  return "";
@@ -79,13 +79,13 @@ describe("unparse", () => {
79
79
  it("should unparse list with multiple elements", async () => {
80
80
  const source = "[1, 2, 3]..";
81
81
  const unparsed = await testRoundTrip(source);
82
- expect(unparsed).toBe("[1, 2, 3]..");
82
+ expect(unparsed).toBe("[1 2 3]..");
83
83
  });
84
84
 
85
85
  it("should unparse nested lists", async () => {
86
86
  const source = "[[1, 2], [3, 4]]..";
87
87
  const unparsed = await testRoundTrip(source);
88
- expect(unparsed).toBe("[[1, 2], [3, 4]]..");
88
+ expect(unparsed).toBe("[[1 2] [3 4]]..");
89
89
  });
90
90
 
91
91
  it("should unparse empty record", async () => {
@@ -207,6 +207,12 @@ describe("unparse", () => {
207
207
  const unparsed = await testRoundTrip(source);
208
208
  expect(unparsed).toBe("foo (bar 42)..");
209
209
  });
210
+
211
+ it("should unparse map with lambda and list", async () => {
212
+ const source = 'map (<x: add x 10>) [\n 1\n 2\n 3\n]..';
213
+ const unparsed = await testRoundTrip(source);
214
+ expect(unparsed).toBe("map (<x: add x 10>) [1 2 3]..");
215
+ });
210
216
  });
211
217
 
212
218
  describe("control flow", () => {
@@ -337,7 +343,7 @@ describe("unparse", () => {
337
343
  it("should support compact option", async () => {
338
344
  const source = "[1, 2, 3]..";
339
345
  const reformatted = await parser.reformat(0, source, basisLexicon, { compact: true });
340
- expect(reformatted).toBe("[1, 2, 3]..");
346
+ expect(reformatted).toBe("[1 2 3]..");
341
347
  });
342
348
 
343
349
  it("should support custom indent size", async () => {