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

Side by Side Diff: src/parser.cc

Issue 948303004: Re-introduce ImportDeclaration to the parser (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Merged to trunk so I can run tryjobs Created 5 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
« no previous file with comments | « src/parser.h ('k') | src/prettyprinter.cc » ('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 1262 matching lines...) Expand 10 before | Expand all | Expand 10 after
1273 *ok = false; 1273 *ok = false;
1274 return NULL; 1274 return NULL;
1275 } 1275 }
1276 } 1276 }
1277 1277
1278 scope_->module()->Freeze(); 1278 scope_->module()->Freeze();
1279 return NULL; 1279 return NULL;
1280 } 1280 }
1281 1281
1282 1282
1283 Literal* Parser::ParseModuleSpecifier(bool* ok) { 1283 const AstRawString* Parser::ParseModuleSpecifier(bool* ok) {
1284 // ModuleSpecifier : 1284 // ModuleSpecifier :
1285 // StringLiteral 1285 // StringLiteral
1286 1286
1287 int pos = peek_position();
1288 Expect(Token::STRING, CHECK_OK); 1287 Expect(Token::STRING, CHECK_OK);
1289 return factory()->NewStringLiteral(GetSymbol(scanner()), pos); 1288 return GetSymbol(scanner());
1290 } 1289 }
1291 1290
1292 1291
1293 void* Parser::ParseExportClause(ZoneList<const AstRawString*>* export_names, 1292 void* Parser::ParseExportClause(ZoneList<const AstRawString*>* export_names,
1294 ZoneList<Scanner::Location>* export_locations, 1293 ZoneList<Scanner::Location>* export_locations,
1295 ZoneList<const AstRawString*>* local_names, 1294 ZoneList<const AstRawString*>* local_names,
1296 Scanner::Location* reserved_loc, bool* ok) { 1295 Scanner::Location* reserved_loc, bool* ok) {
1297 // ExportClause : 1296 // ExportClause :
1298 // '{' '}' 1297 // '{' '}'
1299 // '{' ExportsList '}' 1298 // '{' ExportsList '}'
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1331 if (peek() == Token::RBRACE) break; 1330 if (peek() == Token::RBRACE) break;
1332 Expect(Token::COMMA, CHECK_OK); 1331 Expect(Token::COMMA, CHECK_OK);
1333 } 1332 }
1334 1333
1335 Expect(Token::RBRACE, CHECK_OK); 1334 Expect(Token::RBRACE, CHECK_OK);
1336 1335
1337 return 0; 1336 return 0;
1338 } 1337 }
1339 1338
1340 1339
1341 void* Parser::ParseNamedImports(ZoneList<const AstRawString*>* import_names, 1340 ZoneList<ImportDeclaration*>* Parser::ParseNamedImports(int pos, bool* ok) {
1342 ZoneList<const AstRawString*>* local_names,
1343 bool* ok) {
1344 // NamedImports : 1341 // NamedImports :
1345 // '{' '}' 1342 // '{' '}'
1346 // '{' ImportsList '}' 1343 // '{' ImportsList '}'
1347 // '{' ImportsList ',' '}' 1344 // '{' ImportsList ',' '}'
1348 // 1345 //
1349 // ImportsList : 1346 // ImportsList :
1350 // ImportSpecifier 1347 // ImportSpecifier
1351 // ImportsList ',' ImportSpecifier 1348 // ImportsList ',' ImportSpecifier
1352 // 1349 //
1353 // ImportSpecifier : 1350 // ImportSpecifier :
1354 // BindingIdentifier 1351 // BindingIdentifier
1355 // IdentifierName 'as' BindingIdentifier 1352 // IdentifierName 'as' BindingIdentifier
1356 1353
1357 Expect(Token::LBRACE, CHECK_OK); 1354 Expect(Token::LBRACE, CHECK_OK);
1358 1355
1356 ZoneList<ImportDeclaration*>* result =
1357 new (zone()) ZoneList<ImportDeclaration*>(1, zone());
1359 while (peek() != Token::RBRACE) { 1358 while (peek() != Token::RBRACE) {
1360 const AstRawString* import_name = ParseIdentifierName(CHECK_OK); 1359 const AstRawString* import_name = ParseIdentifierName(CHECK_OK);
1361 const AstRawString* local_name = import_name; 1360 const AstRawString* local_name = import_name;
1362 // In the presence of 'as', the left-side of the 'as' can 1361 // In the presence of 'as', the left-side of the 'as' can
1363 // be any IdentifierName. But without 'as', it must be a valid 1362 // be any IdentifierName. But without 'as', it must be a valid
1364 // BindingIdentifier. 1363 // BindingIdentifier.
1365 if (CheckContextualKeyword(CStrVector("as"))) { 1364 if (CheckContextualKeyword(CStrVector("as"))) {
1366 local_name = ParseIdentifierName(CHECK_OK); 1365 local_name = ParseIdentifierName(CHECK_OK);
1367 } 1366 }
1368 if (!Token::IsIdentifier(scanner()->current_token(), STRICT, false)) { 1367 if (!Token::IsIdentifier(scanner()->current_token(), STRICT, false)) {
1369 *ok = false; 1368 *ok = false;
1370 ReportMessage("unexpected_reserved"); 1369 ReportMessage("unexpected_reserved");
1371 return NULL; 1370 return NULL;
1372 } else if (IsEvalOrArguments(local_name)) { 1371 } else if (IsEvalOrArguments(local_name)) {
1373 *ok = false; 1372 *ok = false;
1374 ReportMessage("strict_eval_arguments"); 1373 ReportMessage("strict_eval_arguments");
1375 return NULL; 1374 return NULL;
1376 } 1375 }
1377 import_names->Add(import_name, zone()); 1376 VariableProxy* proxy = NewUnresolved(local_name, IMPORT);
1378 local_names->Add(local_name, zone()); 1377 ImportDeclaration* declaration =
1378 factory()->NewImportDeclaration(proxy, import_name, NULL, scope_, pos);
1379 Declare(declaration, true, CHECK_OK);
1380 result->Add(declaration, zone());
1379 if (peek() == Token::RBRACE) break; 1381 if (peek() == Token::RBRACE) break;
1380 Expect(Token::COMMA, CHECK_OK); 1382 Expect(Token::COMMA, CHECK_OK);
1381 } 1383 }
1382 1384
1383 Expect(Token::RBRACE, CHECK_OK); 1385 Expect(Token::RBRACE, CHECK_OK);
1384 1386
1385 return NULL; 1387 return result;
1386 } 1388 }
1387 1389
1388 1390
1389 Statement* Parser::ParseImportDeclaration(bool* ok) { 1391 Statement* Parser::ParseImportDeclaration(bool* ok) {
1390 // ImportDeclaration : 1392 // ImportDeclaration :
1391 // 'import' ImportClause 'from' ModuleSpecifier ';' 1393 // 'import' ImportClause 'from' ModuleSpecifier ';'
1392 // 'import' ModuleSpecifier ';' 1394 // 'import' ModuleSpecifier ';'
1393 // 1395 //
1394 // ImportClause : 1396 // ImportClause :
1395 // NameSpaceImport 1397 // NameSpaceImport
1396 // NamedImports 1398 // NamedImports
1397 // ImportedDefaultBinding 1399 // ImportedDefaultBinding
1398 // ImportedDefaultBinding ',' NameSpaceImport 1400 // ImportedDefaultBinding ',' NameSpaceImport
1399 // ImportedDefaultBinding ',' NamedImports 1401 // ImportedDefaultBinding ',' NamedImports
1400 // 1402 //
1401 // NameSpaceImport : 1403 // NameSpaceImport :
1402 // '*' 'as' ImportedBinding 1404 // '*' 'as' ImportedBinding
1403 1405
1404 int pos = peek_position(); 1406 int pos = peek_position();
1405 Expect(Token::IMPORT, CHECK_OK); 1407 Expect(Token::IMPORT, CHECK_OK);
1406 1408
1407 Token::Value tok = peek(); 1409 Token::Value tok = peek();
1408 1410
1409 // 'import' ModuleSpecifier ';' 1411 // 'import' ModuleSpecifier ';'
1410 if (tok == Token::STRING) { 1412 if (tok == Token::STRING) {
1411 Literal* module_specifier = ParseModuleSpecifier(CHECK_OK); 1413 const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK);
1412 ExpectSemicolon(CHECK_OK); 1414 ExpectSemicolon(CHECK_OK);
1413 // TODO(ES6): Add module to the requested modules of scope_->module(). 1415 // TODO(ES6): Add module to the requested modules of scope_->module().
1414 USE(module_specifier); 1416 USE(module_specifier);
1415 return factory()->NewEmptyStatement(pos); 1417 return factory()->NewEmptyStatement(pos);
1416 } 1418 }
1417 1419
1418 // Parse ImportedDefaultBinding if present. 1420 // Parse ImportedDefaultBinding if present.
1419 const AstRawString* imported_default_binding = NULL; 1421 const AstRawString* imported_default_binding = NULL;
1420 if (tok != Token::MUL && tok != Token::LBRACE) { 1422 if (tok != Token::MUL && tok != Token::LBRACE) {
1421 imported_default_binding = 1423 imported_default_binding =
1422 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK); 1424 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
1425 // TODO(ES6): Add an appropriate declaration.
1423 } 1426 }
1424 1427
1425 const AstRawString* module_instance_binding = NULL; 1428 const AstRawString* module_instance_binding = NULL;
1426 ZoneList<const AstRawString*> local_names(1, zone()); 1429 ZoneList<ImportDeclaration*>* named_declarations = NULL;
1427 ZoneList<const AstRawString*> import_names(1, zone());
1428 if (imported_default_binding == NULL || Check(Token::COMMA)) { 1430 if (imported_default_binding == NULL || Check(Token::COMMA)) {
1429 switch (peek()) { 1431 switch (peek()) {
1430 case Token::MUL: { 1432 case Token::MUL: {
1431 Consume(Token::MUL); 1433 Consume(Token::MUL);
1432 ExpectContextualKeyword(CStrVector("as"), CHECK_OK); 1434 ExpectContextualKeyword(CStrVector("as"), CHECK_OK);
1433 module_instance_binding = 1435 module_instance_binding =
1434 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK); 1436 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
1437 // TODO(ES6): Add an appropriate declaration.
1435 break; 1438 break;
1436 } 1439 }
1437 1440
1438 case Token::LBRACE: 1441 case Token::LBRACE:
1439 ParseNamedImports(&import_names, &local_names, CHECK_OK); 1442 named_declarations = ParseNamedImports(pos, CHECK_OK);
1440 break; 1443 break;
1441 1444
1442 default: 1445 default:
1443 *ok = false; 1446 *ok = false;
1444 ReportUnexpectedToken(scanner()->current_token()); 1447 ReportUnexpectedToken(scanner()->current_token());
1445 return NULL; 1448 return NULL;
1446 } 1449 }
1447 } 1450 }
1448 1451
1449 ExpectContextualKeyword(CStrVector("from"), CHECK_OK); 1452 ExpectContextualKeyword(CStrVector("from"), CHECK_OK);
1450 Literal* module = ParseModuleSpecifier(CHECK_OK); 1453 const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK);
1451 USE(module);
1452
1453 ExpectSemicolon(CHECK_OK); 1454 ExpectSemicolon(CHECK_OK);
1454 1455
1455 if (module_instance_binding != NULL) { 1456 if (module_instance_binding != NULL) {
1456 // TODO(ES6): Bind name to the Module Instance Object of module. 1457 // TODO(ES6): Set the module specifier for the module namespace binding.
1457 } 1458 }
1458 1459
1459 if (imported_default_binding != NULL) { 1460 if (imported_default_binding != NULL) {
1460 // TODO(ES6): Add an appropriate declaration. 1461 // TODO(ES6): Set the module specifier for the default binding.
1461 } 1462 }
1462 1463
1463 const int length = import_names.length(); 1464 if (named_declarations != NULL) {
1464 DCHECK_EQ(length, local_names.length()); 1465 for (int i = 0; i < named_declarations->length(); ++i) {
1465 for (int i = 0; i < length; ++i) { 1466 named_declarations->at(i)->set_module_specifier(module_specifier);
1466 // TODO(ES6): Add an appropriate declaration for each name 1467 }
1467 } 1468 }
1468 1469
1469 return factory()->NewEmptyStatement(pos); 1470 return factory()->NewEmptyStatement(pos);
1470 } 1471 }
1471 1472
1472 1473
1473 Statement* Parser::ParseExportDefault(bool* ok) { 1474 Statement* Parser::ParseExportDefault(bool* ok) {
1474 // Supports the following productions, starting after the 'default' token: 1475 // Supports the following productions, starting after the 'default' token:
1475 // 'export' 'default' FunctionDeclaration 1476 // 'export' 'default' FunctionDeclaration
1476 // 'export' 'default' ClassDeclaration 1477 // 'export' 'default' ClassDeclaration
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1533 1534
1534 Statement* result = NULL; 1535 Statement* result = NULL;
1535 ZoneList<const AstRawString*> names(1, zone()); 1536 ZoneList<const AstRawString*> names(1, zone());
1536 switch (peek()) { 1537 switch (peek()) {
1537 case Token::DEFAULT: 1538 case Token::DEFAULT:
1538 return ParseExportDefault(ok); 1539 return ParseExportDefault(ok);
1539 1540
1540 case Token::MUL: { 1541 case Token::MUL: {
1541 Consume(Token::MUL); 1542 Consume(Token::MUL);
1542 ExpectContextualKeyword(CStrVector("from"), CHECK_OK); 1543 ExpectContextualKeyword(CStrVector("from"), CHECK_OK);
1543 Literal* module = ParseModuleSpecifier(CHECK_OK); 1544 const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK);
1544 ExpectSemicolon(CHECK_OK); 1545 ExpectSemicolon(CHECK_OK);
1545 // TODO(ES6): scope_->module()->AddStarExport(...) 1546 // TODO(ES6): scope_->module()->AddStarExport(...)
1546 USE(module); 1547 USE(module_specifier);
1547 return factory()->NewEmptyStatement(pos); 1548 return factory()->NewEmptyStatement(pos);
1548 } 1549 }
1549 1550
1550 case Token::LBRACE: { 1551 case Token::LBRACE: {
1551 // There are two cases here: 1552 // There are two cases here:
1552 // 1553 //
1553 // 'export' ExportClause ';' 1554 // 'export' ExportClause ';'
1554 // and 1555 // and
1555 // 'export' ExportClause FromClause ';' 1556 // 'export' ExportClause FromClause ';'
1556 // 1557 //
1557 // In the first case, the exported identifiers in ExportClause must 1558 // In the first case, the exported identifiers in ExportClause must
1558 // not be reserved words, while in the latter they may be. We 1559 // not be reserved words, while in the latter they may be. We
1559 // pass in a location that gets filled with the first reserved word 1560 // pass in a location that gets filled with the first reserved word
1560 // encountered, and then throw a SyntaxError if we are in the 1561 // encountered, and then throw a SyntaxError if we are in the
1561 // non-FromClause case. 1562 // non-FromClause case.
1562 Scanner::Location reserved_loc = Scanner::Location::invalid(); 1563 Scanner::Location reserved_loc = Scanner::Location::invalid();
1563 ZoneList<const AstRawString*> export_names(1, zone()); 1564 ZoneList<const AstRawString*> export_names(1, zone());
1564 ZoneList<Scanner::Location> export_locations(1, zone()); 1565 ZoneList<Scanner::Location> export_locations(1, zone());
1565 ZoneList<const AstRawString*> local_names(1, zone()); 1566 ZoneList<const AstRawString*> local_names(1, zone());
1566 ParseExportClause(&export_names, &export_locations, &local_names, 1567 ParseExportClause(&export_names, &export_locations, &local_names,
1567 &reserved_loc, CHECK_OK); 1568 &reserved_loc, CHECK_OK);
1568 Literal* indirect_export_module_specifier = NULL; 1569 const AstRawString* indirect_export_module_specifier = NULL;
1569 if (CheckContextualKeyword(CStrVector("from"))) { 1570 if (CheckContextualKeyword(CStrVector("from"))) {
1570 indirect_export_module_specifier = ParseModuleSpecifier(CHECK_OK); 1571 indirect_export_module_specifier = ParseModuleSpecifier(CHECK_OK);
1571 } else if (reserved_loc.IsValid()) { 1572 } else if (reserved_loc.IsValid()) {
1572 // No FromClause, so reserved words are invalid in ExportClause. 1573 // No FromClause, so reserved words are invalid in ExportClause.
1573 *ok = false; 1574 *ok = false;
1574 ReportMessageAt(reserved_loc, "unexpected_reserved"); 1575 ReportMessageAt(reserved_loc, "unexpected_reserved");
1575 return NULL; 1576 return NULL;
1576 } 1577 }
1577 ExpectSemicolon(CHECK_OK); 1578 ExpectSemicolon(CHECK_OK);
1578 const int length = export_names.length(); 1579 const int length = export_names.length();
(...skipping 3840 matching lines...) Expand 10 before | Expand all | Expand 10 after
5419 } else { 5420 } else {
5420 const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data()); 5421 const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data());
5421 running_hash = StringHasher::ComputeRunningHash(running_hash, data, 5422 running_hash = StringHasher::ComputeRunningHash(running_hash, data,
5422 raw_string->length()); 5423 raw_string->length());
5423 } 5424 }
5424 } 5425 }
5425 5426
5426 return running_hash; 5427 return running_hash;
5427 } 5428 }
5428 } } // namespace v8::internal 5429 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/prettyprinter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698