From 3b166baa1705a2267e9b7dede17b19196f7392fa Mon Sep 17 00:00:00 2001 From: Alessandro Scandone Date: Mon, 22 Jan 2024 14:04:28 +0100 Subject: [PATCH] added trailing comma for variants --- src/__snapshots__/parser.test.ts.snap | 22 ++++++++++++++++++++++ src/parser.test.ts | 5 +++++ src/parser.ts | 9 ++++++++- src/parser/grammar.ohm | 2 +- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/__snapshots__/parser.test.ts.snap b/src/__snapshots__/parser.test.ts.snap index 1a521b66..ea9673f0 100644 --- a/src/__snapshots__/parser.test.ts.snap +++ b/src/__snapshots__/parser.test.ts.snap @@ -1259,6 +1259,28 @@ exports[`parse let block with two let stmts 1`] = ` } `; +exports[`type declarations > trailing comma after variants 1`] = ` +{ + "declarations": [], + "typeDeclarations": [ + { + "name": "T", + "type": "adt", + "variants": [ + { + "args": [], + "name": "A", + }, + { + "args": [], + "name": "B", + }, + ], + }, + ], +} +`; + exports[`type declarations > type with a variant with complex args 1`] = ` { "declarations": [], diff --git a/src/parser.test.ts b/src/parser.test.ts index 9e162b93..232312b1 100644 --- a/src/parser.test.ts +++ b/src/parser.test.ts @@ -277,6 +277,11 @@ describe("type declarations", () => { const src = `type T { A, B }`; expect(unsafeParse(src)).toMatchSnapshot(); }); + + test("trailing comma after variants", () => { + const src = `type T { A, B, }`; + expect(unsafeParse(src)).toMatchSnapshot(); + }); }); function spanOf(src: string, substr: string = src): Span { diff --git a/src/parser.ts b/src/parser.ts index 4926d8b7..7730e06e 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -188,7 +188,14 @@ semantics.addOperation("typeVariant()", { }); semantics.addOperation("statement()", { - TypeDeclaration_typeDef(_type, typeName, _lbracket, variants, _rbracket) { + TypeDeclaration_typeDef( + _type, + typeName, + _lbracket, + variants, + _trailingComma, + _rbracket, + ) { const variants_ = variants .asIteration() .children.map((n) => n.typeVariant()); diff --git a/src/parser/grammar.ohm b/src/parser/grammar.ohm index c0cc937d..7aaf6f2e 100644 --- a/src/parser/grammar.ohm +++ b/src/parser/grammar.ohm @@ -7,7 +7,7 @@ Program { = "let" ident (":" Type)? "=" Exp -- letStmt TypeDeclaration - = "type" typeName "{" ListOf "}" -- typeDef + = "type" typeName "{" ListOf ","? "}" -- typeDef // --- Expressions