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

Side by Side Diff: src/parser.cc

Issue 902093002: Add basic compilation support for modules (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add TODO Created 5 years, 10 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
« no previous file with comments | « src/parser.h ('k') | test/cctest/cctest.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 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 936 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 AstNodeFactory function_factory(ast_value_factory()); 947 AstNodeFactory function_factory(ast_value_factory());
948 FunctionState function_state(&function_state_, &scope_, *scope, 948 FunctionState function_state(&function_state_, &scope_, *scope,
949 &function_factory); 949 &function_factory);
950 950
951 scope_->SetLanguageMode(info->language_mode()); 951 scope_->SetLanguageMode(info->language_mode());
952 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); 952 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone());
953 bool ok = true; 953 bool ok = true;
954 int beg_pos = scanner()->location().beg_pos; 954 int beg_pos = scanner()->location().beg_pos;
955 if (info->is_module()) { 955 if (info->is_module()) {
956 DCHECK(allow_harmony_modules()); 956 DCHECK(allow_harmony_modules());
957 Module* module = ParseModule(&ok); 957 Statement* stmt = ParseModule(&ok);
958 if (ok) { 958 if (ok) {
959 // TODO(adamk): Do something with returned Module 959 body->Add(stmt, zone());
960 CHECK(module);
961 body->Add(factory()->NewEmptyStatement(RelocInfo::kNoPosition), zone());
962 } 960 }
963 } else { 961 } else {
964 ParseStatementList(body, Token::EOS, info->is_eval(), eval_scope, &ok); 962 ParseStatementList(body, Token::EOS, info->is_eval(), eval_scope, &ok);
965 } 963 }
966 964
967 if (ok && is_strict(language_mode())) { 965 if (ok && is_strict(language_mode())) {
968 CheckStrictOctalLiteral(beg_pos, scanner()->location().end_pos, &ok); 966 CheckStrictOctalLiteral(beg_pos, scanner()->location().end_pos, &ok);
969 } 967 }
970 968
971 if (ok && allow_harmony_scoping() && is_strict(language_mode())) { 969 if (ok && allow_harmony_scoping() && is_strict(language_mode())) {
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
1247 case Token::IMPORT: 1245 case Token::IMPORT:
1248 return ParseImportDeclaration(ok); 1246 return ParseImportDeclaration(ok);
1249 case Token::EXPORT: 1247 case Token::EXPORT:
1250 return ParseExportDeclaration(ok); 1248 return ParseExportDeclaration(ok);
1251 default: 1249 default:
1252 return ParseStatementListItem(ok); 1250 return ParseStatementListItem(ok);
1253 } 1251 }
1254 } 1252 }
1255 1253
1256 1254
1257 Module* Parser::ParseModule(bool* ok) { 1255 Statement* Parser::ParseModule(bool* ok) {
1258 // (Ecma 262 6th Edition, 15.2): 1256 // (Ecma 262 6th Edition, 15.2):
1259 // Module : 1257 // Module :
1260 // ModuleBody? 1258 // ModuleBody?
1261 // 1259 //
1262 // ModuleBody : 1260 // ModuleBody :
1263 // ModuleItem* 1261 // ModuleItem*
1264 1262
1265 int pos = peek_position();
1266 // Construct block expecting 16 statements.
1267 Block* body = factory()->NewBlock(NULL, 16, false, RelocInfo::kNoPosition); 1263 Block* body = factory()->NewBlock(NULL, 16, false, RelocInfo::kNoPosition);
1268 #ifdef DEBUG
1269 if (FLAG_print_interface_details) PrintF("# Literal ");
1270 #endif
1271 Scope* scope = NewScope(scope_, MODULE_SCOPE); 1264 Scope* scope = NewScope(scope_, MODULE_SCOPE);
1272
1273 scope->set_start_position(scanner()->location().beg_pos); 1265 scope->set_start_position(scanner()->location().beg_pos);
1274 scope->SetLanguageMode( 1266 scope->SetLanguageMode(
1275 static_cast<LanguageMode>(scope->language_mode() | STRICT_BIT)); 1267 static_cast<LanguageMode>(scope->language_mode() | STRICT_BIT));
1276 1268
1277 { 1269 {
1278 BlockState block_state(&scope_, scope); 1270 BlockState block_state(&scope_, scope);
1279 Target target(&this->target_stack_, body);
1280 1271
1281 while (peek() != Token::EOS) { 1272 while (peek() != Token::EOS) {
1282 Statement* stat = ParseModuleItem(CHECK_OK); 1273 Statement* stat = ParseModuleItem(CHECK_OK);
1283 if (stat && !stat->IsEmpty()) { 1274 if (stat && !stat->IsEmpty()) {
1284 body->AddStatement(stat, zone()); 1275 body->AddStatement(stat, zone());
1285 } 1276 }
1286 } 1277 }
1287 } 1278 }
1288 1279
1289 scope->set_end_position(scanner()->location().end_pos); 1280 scope->set_end_position(scanner()->location().end_pos);
1290 body->set_scope(scope); 1281 body->set_scope(scope);
1291 1282
1292 // Check that all exports are bound. 1283 // Check that all exports are bound.
1293 Interface* interface = scope->interface(); 1284 Interface* interface = scope->interface();
1294 for (Interface::Iterator it = interface->iterator(); 1285 for (Interface::Iterator it = interface->iterator();
1295 !it.done(); it.Advance()) { 1286 !it.done(); it.Advance()) {
1296 if (scope->LookupLocal(it.name()) == NULL) { 1287 if (scope->LookupLocal(it.name()) == NULL) {
1297 ParserTraits::ReportMessage("module_export_undefined", it.name()); 1288 ParserTraits::ReportMessage("module_export_undefined", it.name());
1298 *ok = false; 1289 *ok = false;
1299 return NULL; 1290 return NULL;
1300 } 1291 }
1301 } 1292 }
1302 1293
1303 interface->MakeModule(ok); 1294 interface->MakeModule(ok);
1304 DCHECK(*ok); 1295 DCHECK(*ok);
1305 interface->Freeze(ok); 1296 interface->Freeze(ok);
1306 DCHECK(*ok); 1297 DCHECK(*ok);
1307 return factory()->NewModuleLiteral(body, interface, pos); 1298 return body;
1308 } 1299 }
1309 1300
1310 1301
1311 Module* Parser::ParseModuleSpecifier(bool* ok) { 1302 Literal* Parser::ParseModuleSpecifier(bool* ok) {
1312 // Module: 1303 // ModuleSpecifier :
1313 // String 1304 // StringLiteral
1314 1305
1315 int pos = peek_position(); 1306 int pos = peek_position();
1316 Expect(Token::STRING, CHECK_OK); 1307 Expect(Token::STRING, CHECK_OK);
1317 const AstRawString* symbol = GetSymbol(scanner()); 1308 return factory()->NewStringLiteral(GetSymbol(scanner()), pos);
1318
1319 // TODO(ES6): Request JS resource from environment...
1320
1321 #ifdef DEBUG
1322 if (FLAG_print_interface_details) PrintF("# Url ");
1323 #endif
1324
1325 // Create an empty literal as long as the feature isn't finished.
1326 USE(symbol);
1327 Scope* scope = NewScope(scope_, MODULE_SCOPE);
1328 Block* body = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
1329 body->set_scope(scope);
1330 Interface* interface = scope->interface();
1331 Module* result = factory()->NewModuleLiteral(body, interface, pos);
1332 interface->Freeze(ok);
1333 DCHECK(*ok);
1334 interface->Unify(scope->interface(), zone(), ok);
1335 DCHECK(*ok);
1336 return result;
1337 } 1309 }
1338 1310
1339 1311
1340 void* Parser::ParseExportClause(ZoneList<const AstRawString*>* names, 1312 void* Parser::ParseExportClause(ZoneList<const AstRawString*>* names,
1341 Scanner::Location* reserved_loc, bool* ok) { 1313 Scanner::Location* reserved_loc, bool* ok) {
1342 // ExportClause : 1314 // ExportClause :
1343 // '{' '}' 1315 // '{' '}'
1344 // '{' ExportsList '}' 1316 // '{' ExportsList '}'
1345 // '{' ExportsList ',' '}' 1317 // '{' ExportsList ',' '}'
1346 // 1318 //
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1479 break; 1451 break;
1480 1452
1481 default: 1453 default:
1482 *ok = false; 1454 *ok = false;
1483 ReportUnexpectedToken(scanner()->current_token()); 1455 ReportUnexpectedToken(scanner()->current_token());
1484 return NULL; 1456 return NULL;
1485 } 1457 }
1486 } 1458 }
1487 1459
1488 ExpectContextualKeyword(CStrVector("from"), CHECK_OK); 1460 ExpectContextualKeyword(CStrVector("from"), CHECK_OK);
1489 Module* module = ParseModuleSpecifier(CHECK_OK); 1461 Literal* module = ParseModuleSpecifier(CHECK_OK);
1490 USE(module); 1462 USE(module);
1491 1463
1492 ExpectSemicolon(CHECK_OK); 1464 ExpectSemicolon(CHECK_OK);
1493 1465
1494 if (module_instance_binding != NULL) { 1466 if (module_instance_binding != NULL) {
1495 // TODO(ES6): Bind name to the Module Instance Object of module. 1467 // TODO(ES6): Bind name to the Module Instance Object of module.
1496 } 1468 }
1497 1469
1498 if (imported_default_binding != NULL) { 1470 if (imported_default_binding != NULL) {
1499 // TODO(ES6): Add an appropriate declaration. 1471 // TODO(ES6): Add an appropriate declaration.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1555 ZoneList<const AstRawString*> names(1, zone()); 1527 ZoneList<const AstRawString*> names(1, zone());
1556 bool is_export_from = false; 1528 bool is_export_from = false;
1557 switch (peek()) { 1529 switch (peek()) {
1558 case Token::DEFAULT: 1530 case Token::DEFAULT:
1559 Consume(Token::DEFAULT); 1531 Consume(Token::DEFAULT);
1560 return ParseExportDefault(ok); 1532 return ParseExportDefault(ok);
1561 1533
1562 case Token::MUL: { 1534 case Token::MUL: {
1563 Consume(Token::MUL); 1535 Consume(Token::MUL);
1564 ExpectContextualKeyword(CStrVector("from"), CHECK_OK); 1536 ExpectContextualKeyword(CStrVector("from"), CHECK_OK);
1565 Module* module = ParseModuleSpecifier(CHECK_OK); 1537 Literal* module = ParseModuleSpecifier(CHECK_OK);
1566 ExpectSemicolon(CHECK_OK); 1538 ExpectSemicolon(CHECK_OK);
1567 // TODO(ES6): Do something with the return value 1539 // TODO(ES6): Do something with the return value
1568 // of ParseModuleSpecifier. 1540 // of ParseModuleSpecifier.
1569 USE(module); 1541 USE(module);
1570 is_export_from = true; 1542 is_export_from = true;
1571 result = factory()->NewEmptyStatement(pos); 1543 result = factory()->NewEmptyStatement(pos);
1572 break; 1544 break;
1573 } 1545 }
1574 1546
1575 case Token::LBRACE: { 1547 case Token::LBRACE: {
1576 // There are two cases here: 1548 // There are two cases here:
1577 // 1549 //
1578 // 'export' ExportClause ';' 1550 // 'export' ExportClause ';'
1579 // and 1551 // and
1580 // 'export' ExportClause FromClause ';' 1552 // 'export' ExportClause FromClause ';'
1581 // 1553 //
1582 // In the first case, the exported identifiers in ExportClause must 1554 // In the first case, the exported identifiers in ExportClause must
1583 // not be reserved words, while in the latter they may be. We 1555 // not be reserved words, while in the latter they may be. We
1584 // pass in a location that gets filled with the first reserved word 1556 // pass in a location that gets filled with the first reserved word
1585 // encountered, and then throw a SyntaxError if we are in the 1557 // encountered, and then throw a SyntaxError if we are in the
1586 // non-FromClause case. 1558 // non-FromClause case.
1587 Scanner::Location reserved_loc = Scanner::Location::invalid(); 1559 Scanner::Location reserved_loc = Scanner::Location::invalid();
1588 ParseExportClause(&names, &reserved_loc, CHECK_OK); 1560 ParseExportClause(&names, &reserved_loc, CHECK_OK);
1589 if (CheckContextualKeyword(CStrVector("from"))) { 1561 if (CheckContextualKeyword(CStrVector("from"))) {
1590 Module* module = ParseModuleSpecifier(CHECK_OK); 1562 Literal* module = ParseModuleSpecifier(CHECK_OK);
1591 // TODO(ES6): Do something with the return value 1563 // TODO(ES6): Do something with the return value
1592 // of ParseModuleSpecifier. 1564 // of ParseModuleSpecifier.
1593 USE(module); 1565 USE(module);
1594 is_export_from = true; 1566 is_export_from = true;
1595 } else if (reserved_loc.IsValid()) { 1567 } else if (reserved_loc.IsValid()) {
1596 // No FromClause, so reserved words are invalid in ExportClause. 1568 // No FromClause, so reserved words are invalid in ExportClause.
1597 *ok = false; 1569 *ok = false;
1598 ReportMessageAt(reserved_loc, "unexpected_reserved"); 1570 ReportMessageAt(reserved_loc, "unexpected_reserved");
1599 return NULL; 1571 return NULL;
1600 } 1572 }
(...skipping 3828 matching lines...) Expand 10 before | Expand all | Expand 10 after
5429 } else { 5401 } else {
5430 const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data()); 5402 const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data());
5431 running_hash = StringHasher::ComputeRunningHash(running_hash, data, 5403 running_hash = StringHasher::ComputeRunningHash(running_hash, data,
5432 raw_string->length()); 5404 raw_string->length());
5433 } 5405 }
5434 } 5406 }
5435 5407
5436 return running_hash; 5408 return running_hash;
5437 } 5409 }
5438 } } // namespace v8::internal 5410 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | test/cctest/cctest.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698