Index: src/parser.cc |
diff --git a/src/parser.cc b/src/parser.cc |
index e34854fe7adacd564655429abd05a62caa2d4b20..76bf0e5c0bf47bd9ac158618a2ebc8d630a4a009 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; |
@@ -5029,4 +5030,117 @@ 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) cookedStrings->at(i) |
+#define POS(i) cookedStrings->at(i)->position() |
+#define EXPR(i) expressions->at(i) |
+ TemplateLiteral* lit = *state; |
+ int pos = lit->position(); |
+ const ZoneList<Expression*>* cookedStrings = lit->cooked(); |
+ const ZoneList<Expression*>* expressions = lit->expressions(); |
+ CHECK(cookedStrings->length() == (expressions->length() + 1)); |
+ |
+ if (!tag) { |
+ // Build tree of BinaryOps to simplify code-generation |
+ Expression* expr = NULL; |
+ |
+ if (!expressions->length()) { |
+ // 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*>* rawStrings = new (zone()) ZoneList<Expression*>( |
+ cookedStrings->length(), zone()); |
+ Handle<String> source(String::cast(script()->source())); |
+ |
+ // Build raw strings |
+ for (int i = 0; i < cookedStrings->length(); ++i) { |
+ int offset = POS(i); |
+ int length = lengths->at(i); |
+ SmartArrayPointer<char> rawChars = source->ToCString( |
+ ALLOW_NULLS, FAST_STRING_TRAVERSAL, offset+1, length-1, &length); |
arv (Not doing code reviews)
2014/11/11 17:15:15
nit: ws around + and -
|
+ int c = 0; |
+ int j = 0; |
+ |
+ // Normalize line endings |
+ for (; j < length; ++j) { |
+ char ch = rawChars[j]; |
+ if (ch == '\r') { |
+ rawChars[c++] = '\n'; |
+ if (j < length - 1 && rawChars[j + 1] == '\n') { |
+ ++j; |
+ } |
+ } else { |
+ rawChars[c++] = ch; |
+ } |
+ } |
+ rawStrings->Add(factory()->NewStringLiteral( |
+ ast_value_factory()->GetOneByteString( |
+ OneByteVector(rawChars.get(), c)), 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*>*>(cookedStrings), cooked_idx, pos), |
+ zone()); |
+ args->Add(factory()->NewArrayLiteral( |
+ const_cast<ZoneList<Expression*>*>(rawStrings), raw_idx, pos), zone()); |
+ this->CheckPossibleEvalCall(tag, scope_); |
+ Expression* expr = factory()->NewCallRuntime( |
+ ast_value_factory()->get_template_callsite_string(), NULL, args, start); |
arv (Not doing code reviews)
2014/11/11 17:15:15
I'm not sure but isn't it better to use the functi
|
+ |
+ // Call TagFn |
+ ZoneList<Expression*>* callArgs = new (zone()) ZoneList<Expression*>( |
+ expressions->length() + 1, zone()); |
+ callArgs->Add(expr, zone()); |
+ callArgs->AddAll(*expressions, zone()); |
+ expr = factory()->NewCall(tag, callArgs, pos); |
+ if (fni_ != NULL) fni_->RemoveLastFunction(); |
arv (Not doing code reviews)
2014/11/11 17:15:15
I don't see where the function was added?
caitp (gmail)
2014/11/11 17:48:40
it's copy-pasted, I tried to make sense of it base
|
+ return expr; |
+ } |
+#undef COOKED_STRING |
+#undef POS |
+#undef EXPR |
+} |
+ |
} } // namespace v8::internal |