diff --git a/src/parser/Parser.ts b/src/parser/Parser.ts index 385a21959..bef4ee9b4 100644 --- a/src/parser/Parser.ts +++ b/src/parser/Parser.ts @@ -1948,13 +1948,6 @@ export class Parser { } } - //template string - if (this.check(TokenKind.BackTick)) { - return this.templateString(false); - //tagged template string (currently we do not support spaces between the identifier and the backtick - } else if (this.checkAny(TokenKind.Identifier, ...AllowedLocalIdentifiers) && this.checkNext(TokenKind.BackTick)) { - return this.templateString(true); - } let expr = this.boolean(); if (this.check(TokenKind.Question)) { @@ -2234,6 +2227,12 @@ export class Parser { //capture source literals (LINE_NUM if brightscript, or a bunch of them if brighterscript) case this.matchAny(TokenKind.LineNumLiteral, ...(this.options.mode === ParseMode.BrightScript ? [] : BrighterScriptSourceLiterals)): return new SourceLiteralExpression(this.previous()); + //template string + case this.check(TokenKind.BackTick): + return this.templateString(false); + //tagged template string (currently we do not support spaces between the identifier and the backtick) + case this.checkAny(TokenKind.Identifier, ...AllowedLocalIdentifiers) && this.checkNext(TokenKind.BackTick): + return this.templateString(true); case this.matchAny(TokenKind.Identifier, ...this.allowedLocalIdentifiers): return new VariableExpression(this.previous() as Identifier, this.currentNamespaceName); case this.match(TokenKind.LeftParen): diff --git a/src/parser/tests/expression/TemplateStringExpression.spec.ts b/src/parser/tests/expression/TemplateStringExpression.spec.ts index f406cd103..7b2970e8a 100644 --- a/src/parser/tests/expression/TemplateStringExpression.spec.ts +++ b/src/parser/tests/expression/TemplateStringExpression.spec.ts @@ -7,7 +7,7 @@ import { Lexer } from '../../../lexer'; import { Parser, ParseMode } from '../../Parser'; import { AssignmentStatement } from '../../Statement'; import { Program } from '../../../Program'; -import { getTestTranspile } from '../../../testHelpers.spec'; +import { expectZeroDiagnostics, getTestTranspile } from '../../../testHelpers.spec'; describe('TemplateStringExpression', () => { describe('parser template String', () => { @@ -48,7 +48,7 @@ describe('TemplateStringExpression', () => { let { tokens } = Lexer.scan('a = ["one", "two", `I am a complex example\n${a.isRunning(["a","b","c"])}`]'); let { statements, diagnostics } = Parser.parse(tokens, { mode: ParseMode.BrighterScript }); - expect(diagnostics).to.be.lengthOf(0); + expectZeroDiagnostics(diagnostics); expect(statements[0]).instanceof(AssignmentStatement); }); }); @@ -240,6 +240,20 @@ describe('TemplateStringExpression', () => { end sub `); }); + + it('can be concatenated with regular string', () => { + testTranspile(` + sub main() + thing = "this" + \`that\` + otherThing = \`that\` + "this" + end sub + `, ` + sub main() + thing = "this" + "that" + otherThing = "that" + "this" + end sub + `, undefined, 'source/main.bs'); + }); }); }); });