Enable parsing let declaration block bodies

This commit is contained in:
Sam Vervaeck 2022-08-31 13:53:57 +02:00
parent 3381a2bd5d
commit 6063b5f591

View file

@ -37,6 +37,8 @@ import {
TextFile, TextFile,
CallExpression, CallExpression,
NamedTupleExpression, NamedTupleExpression,
LetBodyElement,
ReturnStatement,
} from "./cst" } from "./cst"
import { Stream } from "./util"; import { Stream } from "./util";
@ -448,7 +450,20 @@ export class Parser {
return new Param(pattern); return new Param(pattern);
} }
public parseDeclartionWithLetKeyword(): LetDeclaration { public parseLetBodyElement(): LetBodyElement {
const t0 = this.peekTokenAfterModifiers();
switch (t0.kind) {
case SyntaxKind.LetKeyword:
return this.parseLetDeclaration();
case SyntaxKind.ReturnKeyword:
return this.parseReturnStatement();
default:
// TODO convert parse errors to include LetKeyword and ReturnKeyword
return this.parseExpressionStatement();
}
}
public parseLetDeclaration(): LetDeclaration {
let t0 = this.getToken(); let t0 = this.getToken();
let pubKeyword = null; let pubKeyword = null;
let mutKeyword = null; let mutKeyword = null;
@ -530,6 +545,17 @@ export class Parser {
return new ExpressionStatement(expression); return new ExpressionStatement(expression);
} }
public parseReturnStatement(): ReturnStatement {
const returnKeyword = this.expectToken(SyntaxKind.ReturnKeyword);
let expression = null;
const t1 = this.peekToken();
if (t1.kind !== SyntaxKind.LineFoldEnd) {
expression = this.parseExpression();
}
this.expectToken(SyntaxKind.LineFoldEnd);
return new ReturnStatement(returnKeyword, expression);
}
public parseImportDeclaration(): ImportDeclaration { public parseImportDeclaration(): ImportDeclaration {
const importKeyword = this.expectToken(SyntaxKind.ImportKeyword); const importKeyword = this.expectToken(SyntaxKind.ImportKeyword);
const importSource = this.expectToken(SyntaxKind.StringLiteral); const importSource = this.expectToken(SyntaxKind.StringLiteral);
@ -541,7 +567,7 @@ export class Parser {
const t0 = this.peekTokenAfterModifiers(); const t0 = this.peekTokenAfterModifiers();
switch (t0.kind) { switch (t0.kind) {
case SyntaxKind.LetKeyword: case SyntaxKind.LetKeyword:
return this.parseDeclartionWithLetKeyword(); return this.parseLetDeclaration();
case SyntaxKind.ImportKeyword: case SyntaxKind.ImportKeyword:
return this.parseImportDeclaration(); return this.parseImportDeclaration();
case SyntaxKind.StructKeyword: case SyntaxKind.StructKeyword: