Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: src/parser.cc

Issue 663683006: Implement ES6 Template Literals (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add comments describing the parsing and scanning of TemplateLiterals Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | src/preparser.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/bailout-reason.h" 9 #include "src/bailout-reason.h"
10 #include "src/base/platform/platform.h" 10 #include "src/base/platform/platform.h"
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 pre_parse_timer_(NULL) { 808 pre_parse_timer_(NULL) {
809 DCHECK(!script().is_null() || info->source_stream() != NULL); 809 DCHECK(!script().is_null() || info->source_stream() != NULL);
810 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); 810 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
811 set_allow_modules(!info->is_native() && FLAG_harmony_modules); 811 set_allow_modules(!info->is_native() && FLAG_harmony_modules);
812 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); 812 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
813 set_allow_lazy(false); // Must be explicitly enabled. 813 set_allow_lazy(false); // Must be explicitly enabled.
814 set_allow_arrow_functions(FLAG_harmony_arrow_functions); 814 set_allow_arrow_functions(FLAG_harmony_arrow_functions);
815 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); 815 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
816 set_allow_classes(FLAG_harmony_classes); 816 set_allow_classes(FLAG_harmony_classes);
817 set_allow_harmony_object_literals(FLAG_harmony_object_literals); 817 set_allow_harmony_object_literals(FLAG_harmony_object_literals);
818 set_allow_harmony_templates(FLAG_harmony_templates);
818 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 819 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
819 ++feature) { 820 ++feature) {
820 use_counts_[feature] = 0; 821 use_counts_[feature] = 0;
821 } 822 }
822 if (info->ast_value_factory() == NULL) { 823 if (info->ast_value_factory() == NULL) {
823 // info takes ownership of AstValueFactory. 824 // info takes ownership of AstValueFactory.
824 info->SetAstValueFactory( 825 info->SetAstValueFactory(
825 new AstValueFactory(zone(), parse_info->hash_seed)); 826 new AstValueFactory(zone(), parse_info->hash_seed));
826 } 827 }
827 } 828 }
(...skipping 4194 matching lines...) Expand 10 before | Expand all | Expand 10 after
5022 info()->SetFunction(result); 5023 info()->SetFunction(result);
5023 5024
5024 // We cannot internalize on a background thread; a foreground task will take 5025 // We cannot internalize on a background thread; a foreground task will take
5025 // care of calling Parser::Internalize just before compilation. 5026 // care of calling Parser::Internalize just before compilation.
5026 5027
5027 if (compile_options() == ScriptCompiler::kProduceParserCache) { 5028 if (compile_options() == ScriptCompiler::kProduceParserCache) {
5028 if (result != NULL) *info_->cached_data() = recorder.GetScriptData(); 5029 if (result != NULL) *info_->cached_data() = recorder.GetScriptData();
5029 log_ = NULL; 5030 log_ = NULL;
5030 } 5031 }
5031 } 5032 }
5033
5034
5035 ParserTraits::TemplateLiteralState Parser::OpenTemplateLiteral(int pos) {
5036 return new (zone()) ParserTraits::TemplateLiteral(zone(), pos);
5037 }
5038
5039
5040 void Parser::AddTemplateSpan(TemplateLiteralState* state) {
5041 int pos = scanner()->location().beg_pos;
5042 const AstRawString* tv = scanner()->CurrentSymbol(ast_value_factory());
5043 const AstRawString* trv = scanner()->CurrentRawSymbol(ast_value_factory());
5044 Literal* cooked = factory()->NewStringLiteral(tv, pos);
5045 Literal* raw = factory()->NewStringLiteral(trv, pos);
5046 (*state)->AddTemplateSpan(cooked, raw, zone());
5047 }
5048
5049
5050 void Parser::AddTemplateExpression(TemplateLiteralState* state,
5051 Expression* expression) {
5052 (*state)->AddExpression(expression, zone());
5053 }
5054
5055
5056 Expression* Parser::CloseTemplateLiteral(TemplateLiteralState* state,
5057 int start, Expression* tag) {
5058 #define COOKED_STRING(i) cookedStrings->at(i)
5059 #define POS(i) cookedStrings->at(i)->position()
5060 #define EXPR(i) expressions->at(i)
5061 TemplateLiteral* lit = *state;
5062 int pos = lit->position();
5063 const ZoneList<Expression*>* cookedStrings = lit->cooked();
5064 const ZoneList<Expression*>* expressions = lit->expressions();
5065 CHECK(cookedStrings->length() == (expressions->length() + 1));
5066
5067 if (!tag) {
5068 // Build tree of BinaryOps to simplify code-generation
5069 Expression* expr = NULL;
5070
5071 if (!expressions->length()) {
5072 // Simple case: treat as string literal
5073 expr = COOKED_STRING(0);
5074 } else {
5075 int i;
5076 expr = factory()->NewBinaryOperation(Token::ADD,
5077 COOKED_STRING(0), EXPR(0), POS(0));
5078 for (i = 1; i < expressions->length(); ++i) {
5079 expr = factory()->NewBinaryOperation(Token::ADD,
5080 expr, factory()->NewBinaryOperation(Token::ADD, COOKED_STRING(i),
5081 EXPR(i), POS(i)), POS(i));
5082 }
5083 expr = factory()->NewBinaryOperation(Token::ADD, expr, COOKED_STRING(i),
5084 POS(i));
5085 }
5086 return expr;
5087 } else {
5088 const ZoneList<Expression*>* rawStrings = lit->raw();
5089 int cooked_idx = function_state_->NextMaterializedLiteralIndex();
5090 int raw_idx = function_state_->NextMaterializedLiteralIndex();
5091
5092 // GetTemplateCallSite
5093 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(4, zone());
5094 args->Add(factory()->NewArrayLiteral(
5095 const_cast<ZoneList<Expression*>*>(cookedStrings), cooked_idx, pos),
5096 zone());
5097 args->Add(factory()->NewArrayLiteral(
5098 const_cast<ZoneList<Expression*>*>(rawStrings), raw_idx, pos), zone());
5099 this->CheckPossibleEvalCall(tag, scope_);
5100 Expression* expr = factory()->NewCallRuntime(
5101 ast_value_factory()->GetTemplateCallSite_string(), NULL, args, start);
5102
5103 // Call TagFn
5104 ZoneList<Expression*>* callArgs = new (zone()) ZoneList<Expression*>(
5105 expressions->length() + 1, zone());
5106 callArgs->Add(expr, zone());
5107 callArgs->AddAll(*expressions, zone());
5108 expr = factory()->NewCall(tag, callArgs, pos);
5109 if (fni_ != NULL) fni_->RemoveLastFunction();
5110 return expr;
5111 }
5112 #undef COOKED_STRING
5113 #undef POS
5114 #undef EXPR
5115 }
5116
5032 } } // namespace v8::internal 5117 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | src/preparser.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698