From 6063b5f591840fc7f6c6c1762b4dd8670fc057d1 Mon Sep 17 00:00:00 2001 From: Sam Vervaeck Date: Wed, 31 Aug 2022 13:53:57 +0200 Subject: [PATCH] Enable parsing let declaration block bodies --- src/parser.ts | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 5fa7fd066..e4af57ae9 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -37,6 +37,8 @@ import { TextFile, CallExpression, NamedTupleExpression, + LetBodyElement, + ReturnStatement, } from "./cst" import { Stream } from "./util"; @@ -448,7 +450,20 @@ export class Parser { 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 pubKeyword = null; let mutKeyword = null; @@ -530,6 +545,17 @@ export class Parser { 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 { const importKeyword = this.expectToken(SyntaxKind.ImportKeyword); const importSource = this.expectToken(SyntaxKind.StringLiteral); @@ -541,7 +567,7 @@ export class Parser { const t0 = this.peekTokenAfterModifiers(); switch (t0.kind) { case SyntaxKind.LetKeyword: - return this.parseDeclartionWithLetKeyword(); + return this.parseLetDeclaration(); case SyntaxKind.ImportKeyword: return this.parseImportDeclaration(); case SyntaxKind.StructKeyword: