Skip to content

Commit

Permalink
added trailing comma for variants
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jan 22, 2024
1 parent e33a2dd commit 3b166ba
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
5 changes: 5 additions & 0 deletions src/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,14 @@ semantics.addOperation<TypeVariant>("typeVariant()", {
});

semantics.addOperation<Statement>("statement()", {
TypeDeclaration_typeDef(_type, typeName, _lbracket, variants, _rbracket) {
TypeDeclaration_typeDef(
_type,
typeName,
_lbracket,
variants,
_trailingComma,
_rbracket,
) {
const variants_ = variants
.asIteration()
.children.map<TypeVariant>((n) => n.typeVariant());
Expand Down
2 changes: 1 addition & 1 deletion src/parser/grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Program {
= "let" ident (":" Type)? "=" Exp -- letStmt

TypeDeclaration
= "type" typeName "{" ListOf<TypeVariant, ","> "}" -- typeDef
= "type" typeName "{" ListOf<TypeVariant, ","> ","? "}" -- typeDef

// --- Expressions

Expand Down

0 comments on commit 3b166ba

Please sign in to comment.