@graffiticode/parser 1.5.0 → 1.5.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/package.json +1 -1
- package/src/parse.js +5 -0
- package/src/parser.spec.js +32 -0
package/package.json
CHANGED
package/src/parse.js
CHANGED
|
@@ -824,6 +824,11 @@ export const parse = (function () {
|
|
|
824
824
|
return tagLiteral(ctx, cc);
|
|
825
825
|
}
|
|
826
826
|
if (match(ctx, TK_IDENT)) {
|
|
827
|
+
if (lexeme === "_") {
|
|
828
|
+
eat(ctx, TK_IDENT);
|
|
829
|
+
Ast.tag(ctx, "_", getCoord(ctx));
|
|
830
|
+
return cc;
|
|
831
|
+
}
|
|
827
832
|
return ident(ctx, cc);
|
|
828
833
|
}
|
|
829
834
|
if (match(ctx, TK_NUM)) {
|
package/src/parser.spec.js
CHANGED
|
@@ -708,4 +708,36 @@ describe("parser integration tests", () => {
|
|
|
708
708
|
expect(strNode.tag).toBe("STR");
|
|
709
709
|
expect(strNode.elts[0]).toBe('Line 1\nTab\t"Quote"');
|
|
710
710
|
});
|
|
711
|
+
|
|
712
|
+
it("should parse case-of with wildcard _ pattern", async () => {
|
|
713
|
+
const src = "case 42 of 1: 'one' _: 'other' end..";
|
|
714
|
+
const result = await parser.parse(0, src, basisLexicon);
|
|
715
|
+
|
|
716
|
+
expect(result).toHaveProperty("root");
|
|
717
|
+
|
|
718
|
+
// Find nodes by walking the pool (AST uses integer IDs as references)
|
|
719
|
+
let caseNode = null;
|
|
720
|
+
let wildcardNode = null;
|
|
721
|
+
for (const key in result) {
|
|
722
|
+
if (key === "root") continue;
|
|
723
|
+
const node = result[key];
|
|
724
|
+
if (node.tag === "CASE") caseNode = node;
|
|
725
|
+
if (node.tag === "TAG" && node.elts[0] === "_") wildcardNode = node;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
expect(caseNode).not.toBeNull();
|
|
729
|
+
expect(caseNode.tag).toBe("CASE");
|
|
730
|
+
// CASE elts: [expr, OF clause 1, OF clause 2]
|
|
731
|
+
expect(caseNode.elts.length).toBe(3);
|
|
732
|
+
|
|
733
|
+
// Wildcard _ is stored as a TAG node to match basis compiler's match function
|
|
734
|
+
expect(wildcardNode).not.toBeNull();
|
|
735
|
+
expect(wildcardNode.tag).toBe("TAG");
|
|
736
|
+
expect(wildcardNode.elts).toEqual(["_"]);
|
|
737
|
+
|
|
738
|
+
// Unparse should reproduce the case-of expression
|
|
739
|
+
const source = unparse(result, basisLexicon);
|
|
740
|
+
expect(source).toContain("case");
|
|
741
|
+
expect(source).toContain("_:");
|
|
742
|
+
});
|
|
711
743
|
});
|