Skip to content

Commit

Permalink
parsed type params
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jan 22, 2024
1 parent 3b166ba commit 29b9e59
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1259,12 +1259,47 @@ exports[`parse let block with two let stmts 1`] = `
}
`;

exports[`type declarations > many type params 1`] = `
{
"declarations": [],
"typeDeclarations": [
{
"name": "T",
"params": [
"a",
"b",
"c",
],
"type": "adt",
"variants": [],
},
],
}
`;

exports[`type declarations > single type param 1`] = `
{
"declarations": [],
"typeDeclarations": [
{
"name": "T",
"params": [
"a",
],
"type": "adt",
"variants": [],
},
],
}
`;

exports[`type declarations > trailing comma after variants 1`] = `
{
"declarations": [],
"typeDeclarations": [
{
"name": "T",
"params": [],
"type": "adt",
"variants": [
{
Expand All @@ -1287,6 +1322,7 @@ exports[`type declarations > type with a variant with complex args 1`] = `
"typeDeclarations": [
{
"name": "T",
"params": [],
"type": "adt",
"variants": [
{
Expand Down Expand Up @@ -1327,6 +1363,7 @@ exports[`type declarations > type with a variant with no args 1`] = `
"typeDeclarations": [
{
"name": "T",
"params": [],
"type": "adt",
"variants": [
{
Expand All @@ -1345,6 +1382,7 @@ exports[`type declarations > type with a variant with no args 2`] = `
"typeDeclarations": [
{
"name": "T",
"params": [],
"type": "adt",
"variants": [
{
Expand All @@ -1369,6 +1407,7 @@ exports[`type declarations > type with many variants 1`] = `
"typeDeclarations": [
{
"name": "T",
"params": [],
"type": "adt",
"variants": [
{
Expand All @@ -1391,6 +1430,7 @@ exports[`type declarations > type with no variants 1`] = `
"typeDeclarations": [
{
"name": "Never",
"params": [],
"type": "adt",
"variants": [],
},
Expand Down
1 change: 1 addition & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type Declaration<TypeMeta = {}> = TypeMeta & {
export type TypeVariant = { name: string; args: TypeAst[] };
export type TypeDeclaration = {
type: "adt";
params: string[];
name: string;
variants: TypeVariant[];
};
Expand Down
10 changes: 10 additions & 0 deletions src/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ describe("type declarations", () => {
const src = `type T { A, B, }`;
expect(unsafeParse(src)).toMatchSnapshot();
});

test("single type param", () => {
const src = `type T<a> { }`;
expect(unsafeParse(src)).toMatchSnapshot();
});

test("many type params", () => {
const src = `type T<a, b, c> { }`;
expect(unsafeParse(src)).toMatchSnapshot();
});
});

function spanOf(src: string, substr: string = src): Span {
Expand Down
12 changes: 12 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ semantics.addOperation<Statement>("statement()", {
TypeDeclaration_typeDef(
_type,
typeName,
_lT,
params,
_rT,
_lbracket,
variants,
_trailingComma,
Expand All @@ -200,10 +203,19 @@ semantics.addOperation<Statement>("statement()", {
.asIteration()
.children.map<TypeVariant>((n) => n.typeVariant());

let params_: string[] = [];
if (params.numChildren > 0) {
params_ = params
.child(0)
.asIteration()
.children.map((c) => c.sourceString);
}

return {
type: "typeDeclaration",
decl: {
type: "adt",
params: params_,
name: typeName.sourceString,
variants: variants_,
},
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 ("<" NonemptyListOf<typeVar, ","> ">")? "{" ListOf<TypeVariant, ","> ","? "}" -- typeDef

// --- Expressions

Expand Down

0 comments on commit 29b9e59

Please sign in to comment.