|
|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
import type { Token } from './lexer'
|
|
|
|
|
import type { AST, MatchCase, Pattern } from './ast'
|
|
|
|
|
import type { AST, MatchCase, Pattern, Definition } from './ast'
|
|
|
|
|
import { ParseError } from './error'
|
|
|
|
|
|
|
|
|
|
export class Parser {
|
|
|
|
|
@ -114,8 +114,29 @@ export class Parser {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parse(): AST {
|
|
|
|
|
return this.parseExpression();
|
|
|
|
|
parse(): Definition[] {
|
|
|
|
|
const definitions: Definition[] = [];
|
|
|
|
|
|
|
|
|
|
while (this.current().kind !== 'eof') {
|
|
|
|
|
definitions.push(this.parseDefinition());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return definitions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private parseDefinition(): Definition {
|
|
|
|
|
const nameToken = this.expect('ident');
|
|
|
|
|
const name = (nameToken as { value: string }).value;
|
|
|
|
|
|
|
|
|
|
this.expect('equals');
|
|
|
|
|
|
|
|
|
|
const body = this.parseExpression();
|
|
|
|
|
|
|
|
|
|
if (this.current().kind !== 'eof') {
|
|
|
|
|
this.expect('semicolon');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { kind: 'definition', name, body, ...this.getPos(nameToken) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private parseExpression(): AST {
|
|
|
|
|
|