Index: src/parser.cc |
diff --git a/src/parser.cc b/src/parser.cc |
index e34854fe7adacd564655429abd05a62caa2d4b20..afc59ee5a0fb653083e2ac7f72ef343366c0195f 100644 |
--- a/src/parser.cc |
+++ b/src/parser.cc |
@@ -815,6 +815,7 @@ Parser::Parser(CompilationInfo* info, ParseInfo* parse_info) |
set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); |
set_allow_classes(FLAG_harmony_classes); |
set_allow_harmony_object_literals(FLAG_harmony_object_literals); |
+ set_allow_harmony_templates(FLAG_harmony_templates); |
for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; |
++feature) { |
use_counts_[feature] = 0; |
@@ -3911,6 +3912,7 @@ PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser( |
reusable_preparser_->set_allow_classes(allow_classes()); |
reusable_preparser_->set_allow_harmony_object_literals( |
allow_harmony_object_literals()); |
+ reusable_preparser_->set_allow_harmony_templates(allow_harmony_templates()); |
} |
PreParser::PreParseResult result = |
reusable_preparser_->PreParseLazyFunction(strict_mode(), |
@@ -5029,4 +5031,120 @@ void Parser::ParseOnBackground() { |
log_ = NULL; |
} |
} |
+ |
+ |
+ParserTraits::TemplateLiteralState Parser::OpenTemplateLiteral(int pos) { |
+ return new (zone()) ParserTraits::TemplateLiteral(zone(), pos); |
+} |
+ |
+ |
+void Parser::AddTemplateSpan(TemplateLiteralState* state, bool tail) { |
+ int pos = scanner()->location().beg_pos; |
+ int end = scanner()->location().end_pos - (tail ? 1 : 2); |
+ const AstRawString* tv = scanner()->CurrentSymbol(ast_value_factory()); |
+ Literal* cooked = factory()->NewStringLiteral(tv, pos); |
+ (*state)->AddTemplateSpan(cooked, end, zone()); |
+} |
+ |
+ |
+void Parser::AddTemplateExpression(TemplateLiteralState* state, |
+ Expression* expression) { |
+ (*state)->AddExpression(expression, zone()); |
+} |
+ |
+ |
+Expression* Parser::CloseTemplateLiteral(TemplateLiteralState* state, |
+ int start, Expression* tag) { |
+#define COOKED_STRING(i) cooked_strings->at(i) |
+#define POS(i) cooked_strings->at(i)->position() |
+#define EXPR(i) expressions->at(i) |
+ TemplateLiteral* lit = *state; |
+ int pos = lit->position(); |
+ const ZoneList<Expression*>* cooked_strings = lit->cooked(); |
+ const ZoneList<Expression*>* expressions = lit->expressions(); |
+ CHECK(cooked_strings->length() == (expressions->length() + 1)); |
+ |
+ if (!tag) { |
+ // Build tree of BinaryOps to simplify code-generation |
+ Expression* expr = NULL; |
+ |
+ if (expressions->length() == 0) { |
+ // Simple case: treat as string literal |
+ expr = COOKED_STRING(0); |
+ } else { |
+ int i; |
+ expr = factory()->NewBinaryOperation(Token::ADD, |
+ COOKED_STRING(0), EXPR(0), POS(0)); |
+ for (i = 1; i < expressions->length(); ++i) { |
+ expr = factory()->NewBinaryOperation(Token::ADD, |
+ expr, factory()->NewBinaryOperation(Token::ADD, COOKED_STRING(i), |
+ EXPR(i), POS(i)), POS(i)); |
+ } |
+ expr = factory()->NewBinaryOperation(Token::ADD, expr, COOKED_STRING(i), |
+ POS(i)); |
+ } |
+ return expr; |
+ } else { |
+ const ZoneList<int>* lengths = lit->lengths(); |
+ ZoneList<Expression*>* raw_strings = new (zone()) ZoneList<Expression*>( |
+ cooked_strings->length(), zone()); |
+ Handle<String> source(String::cast(script()->source())); |
+ |
+ // Build raw strings |
+ for (int string_index = 0; string_index < cooked_strings->length(); |
+ ++string_index) { |
+ int offset = POS(string_index); |
+ int length = lengths->at(string_index); |
+ SmartArrayPointer<char> raw_chars = source->ToCString( |
+ ALLOW_NULLS, FAST_STRING_TRAVERSAL, offset + 1, length - 1, &length); |
+ int raw_chars_to_index = 0; |
+ int raw_chars_from_index = 0; |
+ |
+ // Normalize line endings in raw_chars... CRLF sequence is translated to |
+ // LF, CR is translated to LF |
+ for (; raw_chars_from_index < length; ++raw_chars_from_index) { |
+ char ch = raw_chars[raw_chars_from_index]; |
+ if (ch == '\r') { |
+ raw_chars[raw_chars_to_index++] = '\n'; |
+ if (raw_chars_from_index < length - 1 && |
+ raw_chars[raw_chars_from_index + 1] == '\n') { |
+ ++raw_chars_from_index; |
+ } |
+ } else { |
+ raw_chars[raw_chars_to_index++] = ch; |
+ } |
+ } |
+ raw_strings->Add(factory()->NewStringLiteral( |
+ ast_value_factory()->GetOneByteString( |
+ OneByteVector(raw_chars.get(), raw_chars_to_index)), offset), |
+ zone()); |
+ } |
+ |
+ int cooked_idx = function_state_->NextMaterializedLiteralIndex(); |
+ int raw_idx = function_state_->NextMaterializedLiteralIndex(); |
+ |
+ // GetTemplateCallSite |
+ ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(4, zone()); |
+ args->Add(factory()->NewArrayLiteral( |
+ const_cast<ZoneList<Expression*>*>(cooked_strings), cooked_idx, pos), |
+ zone()); |
+ args->Add(factory()->NewArrayLiteral( |
+ const_cast<ZoneList<Expression*>*>(raw_strings), raw_idx, pos), zone()); |
+ this->CheckPossibleEvalCall(tag, scope_); |
+ Expression* expr = factory()->NewCallRuntime( |
+ ast_value_factory()->get_template_callsite_string(), NULL, args, start); |
+ |
+ // Call TagFn |
+ ZoneList<Expression*>* call_args = new (zone()) ZoneList<Expression*>( |
+ expressions->length() + 1, zone()); |
+ call_args->Add(expr, zone()); |
+ call_args->AddAll(*expressions, zone()); |
+ expr = factory()->NewCall(tag, call_args, pos); |
+ return expr; |
+ } |
+#undef COOKED_STRING |
+#undef POS |
+#undef EXPR |
+} |
+ |
} } // namespace v8::internal |