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

Side by Side Diff: src/parser.cc

Issue 661367: Refactor the scanner interface... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 9 months 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/objects.cc ('k') | src/scanner.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 bool PreParseProgram(Handle<String> source, unibrow::CharacterStream* stream); 100 bool PreParseProgram(Handle<String> source, unibrow::CharacterStream* stream);
101 101
102 void ReportMessage(const char* message, Vector<const char*> args); 102 void ReportMessage(const char* message, Vector<const char*> args);
103 virtual void ReportMessageAt(Scanner::Location loc, 103 virtual void ReportMessageAt(Scanner::Location loc,
104 const char* message, 104 const char* message,
105 Vector<const char*> args) = 0; 105 Vector<const char*> args) = 0;
106 106
107 107
108 // Returns NULL if parsing failed. 108 // Returns NULL if parsing failed.
109 FunctionLiteral* ParseProgram(Handle<String> source, 109 FunctionLiteral* ParseProgram(Handle<String> source,
110 unibrow::CharacterStream* stream,
111 bool in_global_context); 110 bool in_global_context);
112 FunctionLiteral* ParseLazy(Handle<String> source, 111 FunctionLiteral* ParseLazy(Handle<String> source,
113 Handle<String> name, 112 Handle<String> name,
114 int start_position, bool is_expression); 113 int start_position,
115 FunctionLiteral* ParseJson(Handle<String> source, 114 int end_position,
116 unibrow::CharacterStream* stream); 115 bool is_expression);
116 FunctionLiteral* ParseJson(Handle<String> source);
117 117
118 // The minimum number of contiguous assignment that will 118 // The minimum number of contiguous assignment that will
119 // be treated as an initialization block. Benchmarks show that 119 // be treated as an initialization block. Benchmarks show that
120 // the overhead exceeds the savings below this limit. 120 // the overhead exceeds the savings below this limit.
121 static const int kMinInitializationBlock = 3; 121 static const int kMinInitializationBlock = 3;
122 122
123 protected: 123 protected:
124 124
125 enum Mode { 125 enum Mode {
126 PARSE_LAZILY, 126 PARSE_LAZILY,
(...skipping 1078 matching lines...) Expand 10 before | Expand all | Expand 10 after
1205 pre_data_(pre_data) { 1205 pre_data_(pre_data) {
1206 } 1206 }
1207 1207
1208 1208
1209 bool Parser::PreParseProgram(Handle<String> source, 1209 bool Parser::PreParseProgram(Handle<String> source,
1210 unibrow::CharacterStream* stream) { 1210 unibrow::CharacterStream* stream) {
1211 HistogramTimerScope timer(&Counters::pre_parse); 1211 HistogramTimerScope timer(&Counters::pre_parse);
1212 AssertNoZoneAllocation assert_no_zone_allocation; 1212 AssertNoZoneAllocation assert_no_zone_allocation;
1213 AssertNoAllocation assert_no_allocation; 1213 AssertNoAllocation assert_no_allocation;
1214 NoHandleAllocation no_handle_allocation; 1214 NoHandleAllocation no_handle_allocation;
1215 scanner_.Init(source, stream, 0, JAVASCRIPT); 1215 scanner_.Initialize(source, stream, JAVASCRIPT);
1216 ASSERT(target_stack_ == NULL); 1216 ASSERT(target_stack_ == NULL);
1217 mode_ = PARSE_EAGERLY; 1217 mode_ = PARSE_EAGERLY;
1218 DummyScope top_scope; 1218 DummyScope top_scope;
1219 LexicalScope scope(this, &top_scope); 1219 LexicalScope scope(this, &top_scope);
1220 TemporaryScope temp_scope(this); 1220 TemporaryScope temp_scope(this);
1221 ZoneListWrapper<Statement> processor; 1221 ZoneListWrapper<Statement> processor;
1222 bool ok = true; 1222 bool ok = true;
1223 ParseSourceElements(&processor, Token::EOS, &ok); 1223 ParseSourceElements(&processor, Token::EOS, &ok);
1224 return !scanner().stack_overflow(); 1224 return !scanner().stack_overflow();
1225 } 1225 }
1226 1226
1227 1227
1228 FunctionLiteral* Parser::ParseProgram(Handle<String> source, 1228 FunctionLiteral* Parser::ParseProgram(Handle<String> source,
1229 unibrow::CharacterStream* stream,
1230 bool in_global_context) { 1229 bool in_global_context) {
1231 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); 1230 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT);
1232 1231
1233 HistogramTimerScope timer(&Counters::parse); 1232 HistogramTimerScope timer(&Counters::parse);
1234 Counters::total_parse_size.Increment(source->length()); 1233 Counters::total_parse_size.Increment(source->length());
1235 1234
1236 // Initialize parser state. 1235 // Initialize parser state.
1237 source->TryFlatten(); 1236 source->TryFlatten();
1238 scanner_.Init(source, stream, 0, JAVASCRIPT); 1237 scanner_.Initialize(source, JAVASCRIPT);
1239 ASSERT(target_stack_ == NULL); 1238 ASSERT(target_stack_ == NULL);
1240 1239
1241 // Compute the parsing mode. 1240 // Compute the parsing mode.
1242 mode_ = FLAG_lazy ? PARSE_LAZILY : PARSE_EAGERLY; 1241 mode_ = FLAG_lazy ? PARSE_LAZILY : PARSE_EAGERLY;
1243 if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY; 1242 if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY;
1244 1243
1245 Scope::Type type = 1244 Scope::Type type =
1246 in_global_context 1245 in_global_context
1247 ? Scope::GLOBAL_SCOPE 1246 ? Scope::GLOBAL_SCOPE
1248 : Scope::EVAL_SCOPE; 1247 : Scope::EVAL_SCOPE;
(...skipping 30 matching lines...) Expand all
1279 // If there was a syntax error we have to get rid of the AST 1278 // If there was a syntax error we have to get rid of the AST
1280 // and it is not safe to do so before the scope has been deleted. 1279 // and it is not safe to do so before the scope has been deleted.
1281 if (result == NULL) zone_scope.DeleteOnExit(); 1280 if (result == NULL) zone_scope.DeleteOnExit();
1282 return result; 1281 return result;
1283 } 1282 }
1284 1283
1285 1284
1286 FunctionLiteral* Parser::ParseLazy(Handle<String> source, 1285 FunctionLiteral* Parser::ParseLazy(Handle<String> source,
1287 Handle<String> name, 1286 Handle<String> name,
1288 int start_position, 1287 int start_position,
1288 int end_position,
1289 bool is_expression) { 1289 bool is_expression) {
1290 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); 1290 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT);
1291 HistogramTimerScope timer(&Counters::parse_lazy); 1291 HistogramTimerScope timer(&Counters::parse_lazy);
1292 source->TryFlatten();
1293 Counters::total_parse_size.Increment(source->length()); 1292 Counters::total_parse_size.Increment(source->length());
1294 SafeStringInputBuffer buffer(source.location());
1295 1293
1296 // Initialize parser state. 1294 // Initialize parser state.
1297 scanner_.Init(source, &buffer, start_position, JAVASCRIPT); 1295 source->TryFlatten();
1296 scanner_.Initialize(source, start_position, end_position, JAVASCRIPT);
1298 ASSERT(target_stack_ == NULL); 1297 ASSERT(target_stack_ == NULL);
1299 mode_ = PARSE_EAGERLY; 1298 mode_ = PARSE_EAGERLY;
1300 1299
1301 // Place holder for the result. 1300 // Place holder for the result.
1302 FunctionLiteral* result = NULL; 1301 FunctionLiteral* result = NULL;
1303 1302
1304 { 1303 {
1305 // Parse the function literal. 1304 // Parse the function literal.
1306 Handle<String> no_name = factory()->EmptySymbol(); 1305 Handle<String> no_name = factory()->EmptySymbol();
1307 Scope* scope = 1306 Scope* scope =
(...skipping 15 matching lines...) Expand all
1323 1322
1324 // If there was a stack overflow we have to get rid of AST and it is 1323 // If there was a stack overflow we have to get rid of AST and it is
1325 // not safe to do before scope has been deleted. 1324 // not safe to do before scope has been deleted.
1326 if (result == NULL) { 1325 if (result == NULL) {
1327 Top::StackOverflow(); 1326 Top::StackOverflow();
1328 zone_scope.DeleteOnExit(); 1327 zone_scope.DeleteOnExit();
1329 } 1328 }
1330 return result; 1329 return result;
1331 } 1330 }
1332 1331
1333 FunctionLiteral* Parser::ParseJson(Handle<String> source, 1332 FunctionLiteral* Parser::ParseJson(Handle<String> source) {
1334 unibrow::CharacterStream* stream) {
1335 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); 1333 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT);
1336 1334
1337 HistogramTimerScope timer(&Counters::parse); 1335 HistogramTimerScope timer(&Counters::parse);
1338 Counters::total_parse_size.Increment(source->length()); 1336 Counters::total_parse_size.Increment(source->length());
1339 1337
1340 // Initialize parser state. 1338 // Initialize parser state.
1341 source->TryFlatten(TENURED); 1339 source->TryFlatten(TENURED);
1342 scanner_.Init(source, stream, 0, JSON); 1340 scanner_.Initialize(source, JSON);
1343 ASSERT(target_stack_ == NULL); 1341 ASSERT(target_stack_ == NULL);
1344 1342
1345 FunctionLiteral* result = NULL; 1343 FunctionLiteral* result = NULL;
1346 Handle<String> no_name = factory()->EmptySymbol(); 1344 Handle<String> no_name = factory()->EmptySymbol();
1347 1345
1348 { 1346 {
1349 Scope* scope = factory()->NewScope(top_scope_, Scope::GLOBAL_SCOPE, false); 1347 Scope* scope = factory()->NewScope(top_scope_, Scope::GLOBAL_SCOPE, false);
1350 LexicalScope lexical_scope(this, scope); 1348 LexicalScope lexical_scope(this, scope);
1351 TemporaryScope temp_scope(this); 1349 TemporaryScope temp_scope(this);
1352 bool ok = true; 1350 bool ok = true;
(...skipping 3705 matching lines...) Expand 10 before | Expand all | Expand 10 after
5058 Vector<const char*> args = pre_data->BuildArgs(); 5056 Vector<const char*> args = pre_data->BuildArgs();
5059 parser.ReportMessageAt(loc, message, args); 5057 parser.ReportMessageAt(loc, message, args);
5060 DeleteArray(message); 5058 DeleteArray(message);
5061 for (int i = 0; i < args.length(); i++) { 5059 for (int i = 0; i < args.length(); i++) {
5062 DeleteArray(args[i]); 5060 DeleteArray(args[i]);
5063 } 5061 }
5064 DeleteArray(args.start()); 5062 DeleteArray(args.start());
5065 return NULL; 5063 return NULL;
5066 } 5064 }
5067 Handle<String> source = Handle<String>(String::cast(script->source())); 5065 Handle<String> source = Handle<String>(String::cast(script->source()));
5068 SafeStringInputBuffer input(source.location());
5069 FunctionLiteral* result; 5066 FunctionLiteral* result;
5070 if (is_json) { 5067 if (is_json) {
5071 ASSERT(compile_in_global_context); 5068 ASSERT(compile_in_global_context);
5072 result = parser.ParseJson(source, &input); 5069 result = parser.ParseJson(source);
5073 } else { 5070 } else {
5074 result = parser.ParseProgram(source, &input, compile_in_global_context); 5071 result = parser.ParseProgram(source, compile_in_global_context);
5075 } 5072 }
5076 return result; 5073 return result;
5077 } 5074 }
5078 5075
5079 5076
5080 FunctionLiteral* MakeLazyAST(Handle<Script> script, 5077 FunctionLiteral* MakeLazyAST(Handle<Script> script,
5081 Handle<String> name, 5078 Handle<String> name,
5082 int start_position, 5079 int start_position,
5083 int end_position, 5080 int end_position,
5084 bool is_expression) { 5081 bool is_expression) {
5085 bool allow_natives_syntax_before = always_allow_natives_syntax; 5082 bool allow_natives_syntax_before = always_allow_natives_syntax;
5086 always_allow_natives_syntax = true; 5083 always_allow_natives_syntax = true;
5087 AstBuildingParser parser(script, true, NULL, NULL); // always allow 5084 AstBuildingParser parser(script, true, NULL, NULL); // always allow
5088 always_allow_natives_syntax = allow_natives_syntax_before; 5085 always_allow_natives_syntax = allow_natives_syntax_before;
5089 // Parse the function by pulling the function source from the script source. 5086 // Parse the function by pointing to the function source in the script source.
5090 Handle<String> script_source(String::cast(script->source())); 5087 Handle<String> script_source(String::cast(script->source()));
5091 Handle<String> function_source =
5092 SubString(script_source, start_position, end_position, TENURED);
5093 FunctionLiteral* result = 5088 FunctionLiteral* result =
5094 parser.ParseLazy(function_source, name, start_position, is_expression); 5089 parser.ParseLazy(script_source, name,
5090 start_position, end_position, is_expression);
5095 return result; 5091 return result;
5096 } 5092 }
5097 5093
5098 5094
5099 #undef NEW 5095 #undef NEW
5100 5096
5101 5097
5102 } } // namespace v8::internal 5098 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects.cc ('k') | src/scanner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698