From d9da90ba461b3fca7c42999f700d47cd93ac6a0d Mon Sep 17 00:00:00 2001 From: Bronley Plumb Date: Tue, 1 Oct 2019 22:00:30 -0400 Subject: [PATCH] Allow comments as their own lines in AA literals. (#29) Fixes #28 --- src/Program.spec.ts | 1 - src/files/BrsFile.spec.ts | 3 ++- src/parser/parser/Parser.spec.ts | 11 +++++++++++ src/parser/parser/Parser.ts | 7 +++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Program.spec.ts b/src/Program.spec.ts index 5a48bb38f..8910e9e1c 100644 --- a/src/Program.spec.ts +++ b/src/Program.spec.ts @@ -133,7 +133,6 @@ describe('Program', () => { it('normalizes file paths', async () => { let filePath = `${rootDir}/source\\main.brs`; - console.log('filePath', filePath); await program.addOrReplaceFile(filePath, ''); expect(program.contexts.global.getFile(filePath)).to.exist; diff --git a/src/files/BrsFile.spec.ts b/src/files/BrsFile.spec.ts index 228be85c4..a3ce5135b 100644 --- a/src/files/BrsFile.spec.ts +++ b/src/files/BrsFile.spec.ts @@ -1386,7 +1386,7 @@ describe('BrsFile', () => { `); }); - it('works for a complex function with comments at the end of each line', async () => { + it('works for a complex function with comments all over the place', async () => { await testTranspile(` 'import some library library "v30/bslCore.brs" 'comment @@ -1396,6 +1396,7 @@ describe('BrsFile', () => { person = { 'comment name: "parent", 'comment "age": 12, + 'comment as whole line child: { 'comment name: "child" 'comment } diff --git a/src/parser/parser/Parser.spec.ts b/src/parser/parser/Parser.spec.ts index 63fdf1483..1237e7e36 100644 --- a/src/parser/parser/Parser.spec.ts +++ b/src/parser/parser/Parser.spec.ts @@ -95,6 +95,17 @@ describe('parser', () => { expect((statements as any)[2].text).to.equal('comment 2'); }); + it('works in aa literal as its own statement', () => { + let { tokens } = Lexer.scan(` + obj = { + "name": true, + 'comment + } + `); + let { errors, statements } = parser.parse(tokens); + expect(errors).to.be.lengthOf(0, 'Error count should be 0'); + }); + it('parses after function call', () => { let { tokens } = Lexer.scan(` sub Main() diff --git a/src/parser/parser/Parser.ts b/src/parser/parser/Parser.ts index 5e5b3fe7b..f30457492 100644 --- a/src/parser/parser/Parser.ts +++ b/src/parser/parser/Parser.ts @@ -1749,11 +1749,18 @@ export class Parser { } while (match(Lexeme.Comma, Lexeme.Newline, Lexeme.Colon, Lexeme.Comment)) { + //check for comment at the end of the current line if (check(Lexeme.Comment) || checkPrevious(Lexeme.Comment)) { let token = checkPrevious(Lexeme.Comment) ? previous() : advance(); members.push(new Stmt.CommentStatement([token])); } else { while (match(Lexeme.Newline, Lexeme.Colon)); + //check for a comment on its own line + if (check(Lexeme.Comment) || checkPrevious(Lexeme.Comment)) { + let token = checkPrevious(Lexeme.Comment) ? previous() : advance(); + members.push(new Stmt.CommentStatement([token])); + continue; + } if (check(Lexeme.RightBrace)) { break;