Skip to content

Add support for parsing template expressions. #1031

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

Merged
merged 1 commit into from
Nov 2, 2014
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
2 changes: 1 addition & 1 deletion src/services/resources/diagnosticCode.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module TypeScript {
Automatic_semicolon_insertion_not_allowed: "Automatic semicolon insertion not allowed.",
Unexpected_token_0_expected: "Unexpected token; '{0}' expected.",
Trailing_comma_not_allowed: "Trailing comma not allowed.",
AsteriskSlash_expected: "'*/' expected.",
public_or_private_modifier_must_precede_static: "'public' or 'private' modifier must precede 'static'.",
Unexpected_token: "Unexpected token.",
Catch_clause_parameter_cannot_have_a_type_annotation: "Catch clause parameter cannot have a type annotation.",
Expand Down Expand Up @@ -95,6 +94,7 @@ module TypeScript {
return_statement_must_be_contained_within_a_function_body: "'return' statement must be contained within a function body.",
Expression_expected: "Expression expected.",
Type_expected: "Type expected.",
Template_literal_cannot_be_used_as_an_element_name: "Template literal cannot be used as an element name.",
Duplicate_identifier_0: "Duplicate identifier '{0}'.",
The_name_0_does_not_exist_in_the_current_scope: "The name '{0}' does not exist in the current scope.",
The_name_0_does_not_refer_to_a_value: "The name '{0}' does not refer to a value.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ module TypeScript {
"'return' statement must be contained within a function body.": { "code": 1108, "category": DiagnosticCategory.Error },
"Expression expected.": { "code": 1109, "category": DiagnosticCategory.Error },
"Type expected.": { "code": 1110, "category": DiagnosticCategory.Error },
"Template literal cannot be used as an element name.": { "code": 1111, "category": DiagnosticCategory.Error },
"Duplicate identifier '{0}'.": { "code": 2000, "category": DiagnosticCategory.Error },
"The name '{0}' does not exist in the current scope.": { "code": 2001, "category": DiagnosticCategory.Error },
"The name '{0}' does not refer to a value.": { "code": 2002, "category": DiagnosticCategory.Error },
Expand Down
8 changes: 4 additions & 4 deletions src/services/resources/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
"category": "Error",
"code": 1009
},
"'*/' expected.": {
"category": "Error",
"code": 1010
},
"'public' or 'private' modifier must precede 'static'.": {
"category": "Error",
"code": 1011
Expand Down Expand Up @@ -375,6 +371,10 @@
"category": "Error",
"code": 1110
},
"Template literal cannot be used as an element name.": {
"category": "Error",
"code": 1111
},
"Duplicate identifier '{0}'.": {
"category": "Error",
"code": 2000
Expand Down
797 changes: 402 additions & 395 deletions src/services/syntax/SyntaxGenerator.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/services/syntax/defaultSyntaxVisitor.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ module TypeScript {
return this.defaultVisit(node);
}

public visitTemplateExpression(node: TemplateExpressionSyntax): any {
return this.defaultVisit(node);
}

public visitTemplateAccessExpression(node: TemplateAccessExpressionSyntax): any {
return this.defaultVisit(node);
}

public visitVariableDeclaration(node: VariableDeclarationSyntax): any {
return this.defaultVisit(node);
}
Expand Down Expand Up @@ -326,6 +334,10 @@ module TypeScript {
return this.defaultVisit(node);
}

public visitTemplateClause(node: TemplateClauseSyntax): any {
return this.defaultVisit(node);
}

public visitTypeParameter(node: TypeParameterSyntax): any {
return this.defaultVisit(node);
}
Expand Down
74 changes: 67 additions & 7 deletions src/services/syntax/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ module TypeScript.Parser {
return Syntax.emptyToken(expectedKind);
}

function getExpectedTokenDiagnostic(expectedKind: SyntaxKind, actual: ISyntaxToken, diagnosticCode: string): Diagnostic {
function getExpectedTokenDiagnostic(expectedKind: SyntaxKind, actual?: ISyntaxToken, diagnosticCode?: string): Diagnostic {
var token = currentToken();

var args: any[] = undefined;
Expand Down Expand Up @@ -2133,6 +2133,10 @@ module TypeScript.Parser {
case SyntaxKind.StringLiteral:
case SyntaxKind.RegularExpressionLiteral:

// Templates
case SyntaxKind.NoSubstitutionTemplateToken:
case SyntaxKind.TemplateStartToken:

// For array literals.
case SyntaxKind.OpenBracketToken:

Expand Down Expand Up @@ -2638,6 +2642,11 @@ module TypeScript.Parser {
case SyntaxKind.DotToken:
expression = new MemberAccessExpressionSyntax(parseNodeData, expression, consumeToken(_currentToken), eatIdentifierNameToken());
continue;

case SyntaxKind.NoSubstitutionTemplateToken:
case SyntaxKind.TemplateStartToken:
expression = new TemplateAccessExpressionSyntax(parseNodeData, expression, parseTemplateExpression(_currentToken));
continue;
}

return expression;
Expand All @@ -2657,6 +2666,11 @@ module TypeScript.Parser {
case SyntaxKind.DotToken:
expression = new MemberAccessExpressionSyntax(parseNodeData, expression, consumeToken(_currentToken), eatIdentifierNameToken());
continue;

case SyntaxKind.NoSubstitutionTemplateToken:
case SyntaxKind.TemplateStartToken:
expression = new TemplateAccessExpressionSyntax(parseNodeData, expression, parseTemplateExpression(_currentToken));
continue;
}

return expression;
Expand Down Expand Up @@ -2870,11 +2884,15 @@ module TypeScript.Parser {
case SyntaxKind.StringLiteral:
return consumeToken(_currentToken);

case SyntaxKind.FunctionKeyword: return parseFunctionExpression(_currentToken);
case SyntaxKind.OpenBracketToken: return parseArrayLiteralExpression(_currentToken);
case SyntaxKind.OpenBraceToken: return parseObjectLiteralExpression(_currentToken);
case SyntaxKind.OpenParenToken: return parseParenthesizedExpression(_currentToken);
case SyntaxKind.NewKeyword: return parseObjectCreationExpression(_currentToken);
case SyntaxKind.FunctionKeyword: return parseFunctionExpression(_currentToken);
case SyntaxKind.OpenBracketToken: return parseArrayLiteralExpression(_currentToken);
case SyntaxKind.OpenBraceToken: return parseObjectLiteralExpression(_currentToken);
case SyntaxKind.OpenParenToken: return parseParenthesizedExpression(_currentToken);
case SyntaxKind.NewKeyword: return parseObjectCreationExpression(_currentToken);

case SyntaxKind.NoSubstitutionTemplateToken:
case SyntaxKind.TemplateStartToken:
return parseTemplateExpression(_currentToken);

case SyntaxKind.SlashToken:
case SyntaxKind.SlashEqualsToken:
Expand Down Expand Up @@ -2961,6 +2979,46 @@ module TypeScript.Parser {
consumeToken(newKeyword), tryParseMemberExpressionOrHigher(currentToken(), /*force:*/ true, /*inObjectCreation:*/ true), tryParseArgumentList());
}

function parseTemplateExpression(startToken: ISyntaxToken): IPrimaryExpressionSyntax {
consumeToken(startToken);

if (startToken.kind() === SyntaxKind.NoSubstitutionTemplateToken) {
return startToken;
}

var templateClausesArray: TemplateClauseSyntax[] = getArray();

do {
// Keep consuming template spans as long as the last one we keep getting template
// middle pieces.
templateClausesArray.push(parseTemplateClause());
}
while (templateClausesArray[templateClausesArray.length - 1].templateMiddleOrEndToken.kind() === SyntaxKind.TemplateMiddleToken);

var templateClauses = Syntax.list(templateClausesArray);
returnZeroLengthArray(templateClausesArray);

return new TemplateExpressionSyntax(parseNodeData, startToken, templateClauses);
}

function parseTemplateClause(): TemplateClauseSyntax {
var expression = parseExpression(/*allowIn:*/ true);
var token = currentToken();

if (token.kind() === SyntaxKind.CloseBraceToken) {
token = currentContextualToken();
Debug.assert(token.kind() === SyntaxKind.TemplateMiddleToken || token.kind() === SyntaxKind.TemplateEndToken);
consumeToken(token);
}
else {
var diagnostic = getExpectedTokenDiagnostic(SyntaxKind.CloseBraceToken);
addDiagnostic(diagnostic);
token = Syntax.emptyToken(SyntaxKind.TemplateEndToken);
}

return new TemplateClauseSyntax(parseNodeData, expression, token);
}

function parseCastExpression(lessThanToken: ISyntaxToken): CastExpressionSyntax {
return new CastExpressionSyntax(parseNodeData,
consumeToken(lessThanToken), parseType(), eatToken(SyntaxKind.GreaterThanToken), tryParseUnaryExpressionOrHigher(currentToken(), /*force:*/ true));
Expand Down Expand Up @@ -3332,8 +3390,10 @@ module TypeScript.Parser {
}
}

// We allow a template literal while parser for error tolerance. We'll report errors
// on this later in the grammar checker walker.
var kind = token.kind();
return kind === SyntaxKind.StringLiteral || kind === SyntaxKind.NumericLiteral;
return kind === SyntaxKind.StringLiteral || kind === SyntaxKind.NumericLiteral || kind === SyntaxKind.NoSubstitutionTemplateToken;
}

function parseArrayLiteralExpression(openBracketToken: ISyntaxToken): ArrayLiteralExpressionSyntax {
Expand Down
18 changes: 18 additions & 0 deletions src/services/syntax/prettyPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1023,5 +1023,23 @@ module TypeScript.PrettyPrinter {
this.appendToken(node.debuggerKeyword);
this.appendToken(node.semicolonToken);
}

public visitTemplateExpression(node: TemplateExpressionSyntax): void {
this.appendToken(node.templateStartToken);
this.ensureSpace();
this.appendSpaceList(node.templateClauses);
}

public visitTemplateClause(node: TemplateClauseSyntax): void {
node.expression.accept(this);
this.ensureSpace();
this.appendToken(node.templateMiddleOrEndToken);
}

public visitTemplateAccessExpression(node: TemplateAccessExpressionSyntax): void {
node.expression.accept(this);
this.ensureSpace();
node.templateExpression.accept(this);
}
}
}
49 changes: 44 additions & 5 deletions src/services/syntax/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ module TypeScript.Scanner {
case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken:
return true;

// Created by the parser when it sees } while parsing a template expression.
case SyntaxKind.TemplateMiddleToken:
case SyntaxKind.TemplateEndToken:
return true;

default:
return token.isKeywordConvertedToIdentifier();
}
Expand Down Expand Up @@ -708,7 +713,7 @@ module TypeScript.Scanner {

while (true) {
if (index === end) {
reportDiagnostic(end, 0, DiagnosticCode.AsteriskSlash_expected, undefined);
reportDiagnostic(end, 0, DiagnosticCode._0_expected, ["*/"]);
return;
}

Expand Down Expand Up @@ -750,10 +755,10 @@ module TypeScript.Scanner {
index++;

switch (character) {
case CharacterCodes.exclamation /*33*/: return scanExclamationToken();
case CharacterCodes.exclamation/*33*/: return scanExclamationToken();
case CharacterCodes.doubleQuote/*34*/: return scanStringLiteral(character);
case CharacterCodes.percent /*37*/: return scanPercentToken();
case CharacterCodes.ampersand /*38*/: return scanAmpersandToken();
case CharacterCodes.percent/*37*/: return scanPercentToken();
case CharacterCodes.ampersand/*38*/: return scanAmpersandToken();
case CharacterCodes.singleQuote/*39*/: return scanStringLiteral(character);
case CharacterCodes.openParen/*40*/: return SyntaxKind.OpenParenToken;
case CharacterCodes.closeParen/*41*/: return SyntaxKind.CloseParenToken;
Expand All @@ -779,10 +784,11 @@ module TypeScript.Scanner {
case CharacterCodes.openBracket/*91*/: return SyntaxKind.OpenBracketToken;
case CharacterCodes.closeBracket/*93*/: return SyntaxKind.CloseBracketToken;
case CharacterCodes.caret/*94*/: return scanCaretToken();
case CharacterCodes.backtick/*96*/: return scanTemplateToken(character);

case CharacterCodes.openBrace/*123*/: return SyntaxKind.OpenBraceToken;
case CharacterCodes.bar/*124*/: return scanBarToken();
case CharacterCodes.closeBrace/*125*/: return SyntaxKind.CloseBraceToken;
case CharacterCodes.closeBrace/*125*/: return scanCloseBraceToken(allowContextualToken, character);
case CharacterCodes.tilde/*126*/: return SyntaxKind.TildeToken;
}

Expand Down Expand Up @@ -1081,6 +1087,39 @@ module TypeScript.Scanner {
}
}

function scanCloseBraceToken(allowContextualToken: boolean, startChar: number): SyntaxKind {
return allowContextualToken ? scanTemplateToken(startChar) : SyntaxKind.CloseBraceToken;
}

function scanTemplateToken(startChar: number): SyntaxKind {
var startedWithBacktick = startChar === CharacterCodes.backtick;

while (true) {
if (index === end) {
// Hit the end of the file.
reportDiagnostic(end, 0, DiagnosticCode._0_expected, ["`"]);
break;
}

var ch = str.charCodeAt(index);
index++;

if (ch === CharacterCodes.backtick) {
break;
}

if (ch === CharacterCodes.$ &&
index < end &&
str.charCodeAt(index) === CharacterCodes.openBrace) {

index++;
return startedWithBacktick ? SyntaxKind.TemplateStartToken : SyntaxKind.TemplateMiddleToken;
}
}

return startedWithBacktick ? SyntaxKind.NoSubstitutionTemplateToken : SyntaxKind.TemplateEndToken;
}

function scanAmpersandToken(): SyntaxKind {
var character = str.charCodeAt(index);
if (character === CharacterCodes.equals) {
Expand Down
Loading