Skip to content

Commit

Permalink
parsing type args params
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jan 19, 2024
1 parent adc68dd commit 397741d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
99 changes: 99 additions & 0 deletions src/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,105 @@ exports[`parses a concrete type with no args as a type hint 1`] = `
}
`;

exports[`parses concrete type with 1 arg 1`] = `
{
"statements": [
{
"binding": {
"name": "x",
"span": [
4,
5,
],
},
"span": [
0,
22,
],
"type": "let",
"typeHint": {
"args": [
{
"args": [],
"name": "Int",
"type": "named",
},
],
"name": "Maybe",
"span": [
8,
18,
],
"type": "named",
},
"value": {
"span": [
21,
22,
],
"type": "constant",
"value": {
"type": "int",
"value": 0,
},
},
},
],
}
`;

exports[`parses concrete type with 2 args 1`] = `
{
"statements": [
{
"binding": {
"name": "x",
"span": [
4,
5,
],
},
"span": [
0,
29,
],
"type": "let",
"typeHint": {
"args": [
{
"args": [],
"name": "Int",
"type": "named",
},
{
"args": [],
"name": "Bool",
"type": "named",
},
],
"name": "Result",
"span": [
8,
25,
],
"type": "named",
},
"value": {
"span": [
28,
29,
],
"type": "constant",
"value": {
"type": "int",
"value": 0,
},
},
},
],
}
`;

exports[`parses underscore type 1`] = `
{
"statements": [
Expand Down
10 changes: 10 additions & 0 deletions src/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ test("parses underscore type", () => {
expect(unsafeParse(src)).toMatchSnapshot();
});

test("parses concrete type with 1 arg", () => {
const src = "let x : Maybe<Int> = 0";
expect(unsafeParse(src)).toMatchSnapshot();
});

test("parses concrete type with 2 args", () => {
const src = "let x : Result<Int, Bool> = 0";
expect(unsafeParse(src)).toMatchSnapshot();
});

function spanOf(src: string, substr: string = src): Span {
const index = src.indexOf(substr);
return [index, index + substr.length];
Expand Down
11 changes: 9 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,17 @@ semantics.addOperation<TypeHint>("typeHint()", {
return { type: "any" };
},

TypeHint_named(ident) {
TypeHint_named(ident, _lbracket, args, _rbracket) {
let args_: TypeHint[] = [];
if (args.numChildren > 0) {
args_ = args
.child(0)
.asIteration()
.children.map((c) => c.typeHint());
}
return {
type: "named",
args: [],
args: args_,
name: ident.sourceString,
};
},
Expand Down
2 changes: 1 addition & 1 deletion src/parser/grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Program {
// --- Types

TypeHint
= typeIdent -- named
= typeIdent ("<" ListOf<TypeHint, ","> ">")? -- named
| "_" -- any

typeIdent (an identifier)
Expand Down

0 comments on commit 397741d

Please sign in to comment.