Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concat template string and string #383

Merged
merged 2 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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):
Expand Down
18 changes: 16 additions & 2 deletions src/parser/tests/expression/TemplateStringExpression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
Expand Down Expand Up @@ -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');
});
});
});
});