OLD | NEW |
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/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/ast/ast.h" | 10 #include "src/ast/ast.h" |
(...skipping 1505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1516 parsing_module_)) { | 1516 parsing_module_)) { |
1517 *ok = false; | 1517 *ok = false; |
1518 ReportMessage(MessageTemplate::kUnexpectedReserved); | 1518 ReportMessage(MessageTemplate::kUnexpectedReserved); |
1519 return nullptr; | 1519 return nullptr; |
1520 } else if (IsEvalOrArguments(local_name)) { | 1520 } else if (IsEvalOrArguments(local_name)) { |
1521 *ok = false; | 1521 *ok = false; |
1522 ReportMessage(MessageTemplate::kStrictEvalArguments); | 1522 ReportMessage(MessageTemplate::kStrictEvalArguments); |
1523 return nullptr; | 1523 return nullptr; |
1524 } | 1524 } |
1525 | 1525 |
1526 DeclareImport(local_name, position(), CHECK_OK); | 1526 DeclareConstVariable(local_name, kNeedsInitialization, position(), |
| 1527 CHECK_OK); |
1527 | 1528 |
1528 NamedImport* import = new (zone()) NamedImport( | 1529 NamedImport* import = new (zone()) NamedImport( |
1529 import_name, local_name, scanner()->location()); | 1530 import_name, local_name, scanner()->location()); |
1530 result->Add(import, zone()); | 1531 result->Add(import, zone()); |
1531 | 1532 |
1532 if (peek() == Token::RBRACE) break; | 1533 if (peek() == Token::RBRACE) break; |
1533 Expect(Token::COMMA, CHECK_OK); | 1534 Expect(Token::COMMA, CHECK_OK); |
1534 } | 1535 } |
1535 | 1536 |
1536 Expect(Token::RBRACE, CHECK_OK); | 1537 Expect(Token::RBRACE, CHECK_OK); |
(...skipping 29 matching lines...) Expand all Loading... |
1566 return; | 1567 return; |
1567 } | 1568 } |
1568 | 1569 |
1569 // Parse ImportedDefaultBinding if present. | 1570 // Parse ImportedDefaultBinding if present. |
1570 const AstRawString* import_default_binding = nullptr; | 1571 const AstRawString* import_default_binding = nullptr; |
1571 Scanner::Location import_default_binding_loc; | 1572 Scanner::Location import_default_binding_loc; |
1572 if (tok != Token::MUL && tok != Token::LBRACE) { | 1573 if (tok != Token::MUL && tok != Token::LBRACE) { |
1573 import_default_binding = | 1574 import_default_binding = |
1574 ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID); | 1575 ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID); |
1575 import_default_binding_loc = scanner()->location(); | 1576 import_default_binding_loc = scanner()->location(); |
1576 DeclareImport(import_default_binding, pos, CHECK_OK_VOID); | 1577 DeclareConstVariable(import_default_binding, kNeedsInitialization, pos, |
| 1578 CHECK_OK_VOID); |
1577 } | 1579 } |
1578 | 1580 |
1579 // Parse NameSpaceImport or NamedImports if present. | 1581 // Parse NameSpaceImport or NamedImports if present. |
1580 const AstRawString* module_namespace_binding = nullptr; | 1582 const AstRawString* module_namespace_binding = nullptr; |
1581 Scanner::Location module_namespace_binding_loc; | 1583 Scanner::Location module_namespace_binding_loc; |
1582 const ZoneList<const NamedImport*>* named_imports = nullptr; | 1584 const ZoneList<const NamedImport*>* named_imports = nullptr; |
1583 if (import_default_binding == nullptr || Check(Token::COMMA)) { | 1585 if (import_default_binding == nullptr || Check(Token::COMMA)) { |
1584 switch (peek()) { | 1586 switch (peek()) { |
1585 case Token::MUL: { | 1587 case Token::MUL: { |
1586 Consume(Token::MUL); | 1588 Consume(Token::MUL); |
1587 ExpectContextualKeyword(CStrVector("as"), CHECK_OK_VOID); | 1589 ExpectContextualKeyword(CStrVector("as"), CHECK_OK_VOID); |
1588 module_namespace_binding = | 1590 module_namespace_binding = |
1589 ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID); | 1591 ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID); |
1590 module_namespace_binding_loc = scanner()->location(); | 1592 module_namespace_binding_loc = scanner()->location(); |
1591 DeclareImport(module_namespace_binding, pos, CHECK_OK_VOID); | 1593 DeclareConstVariable(module_namespace_binding, kCreatedInitialized, pos, |
| 1594 CHECK_OK_VOID); |
1592 break; | 1595 break; |
1593 } | 1596 } |
1594 | 1597 |
1595 case Token::LBRACE: | 1598 case Token::LBRACE: |
1596 named_imports = ParseNamedImports(pos, CHECK_OK_VOID); | 1599 named_imports = ParseNamedImports(pos, CHECK_OK_VOID); |
1597 break; | 1600 break; |
1598 | 1601 |
1599 default: | 1602 default: |
1600 *ok = false; | 1603 *ok = false; |
1601 ReportUnexpectedToken(scanner()->current_token()); | 1604 ReportUnexpectedToken(scanner()->current_token()); |
1602 return; | 1605 return; |
1603 } | 1606 } |
1604 } | 1607 } |
1605 | 1608 |
1606 ExpectContextualKeyword(CStrVector("from"), CHECK_OK_VOID); | 1609 ExpectContextualKeyword(CStrVector("from"), CHECK_OK_VOID); |
1607 const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK_VOID); | 1610 const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK_VOID); |
1608 ExpectSemicolon(CHECK_OK_VOID); | 1611 ExpectSemicolon(CHECK_OK_VOID); |
1609 | 1612 |
1610 // Now that we have all the information, we can make the appropriate | 1613 // Now that we have all the information, we can make the appropriate |
1611 // declarations. | 1614 // declarations. |
1612 | 1615 |
1613 // TODO(neis): Would prefer to call DeclareImport below rather than above and | 1616 // TODO(neis): Would prefer to call DeclareConstVariable for each case below |
1614 // in ParseNamedImports, but then a possible error message would point to the | 1617 // rather than above and in ParseNamedImports, but then a possible error |
1615 // wrong location. Maybe have a DeclareAt version of Declare that takes a | 1618 // message would point to the wrong location. Maybe have a DeclareAt version |
1616 // location? | 1619 // of Declare that takes a location? |
1617 | 1620 |
1618 if (module_namespace_binding != nullptr) { | 1621 if (module_namespace_binding != nullptr) { |
1619 module()->AddStarImport(module_namespace_binding, module_specifier, | 1622 module()->AddStarImport(module_namespace_binding, module_specifier, |
1620 module_namespace_binding_loc, zone()); | 1623 module_namespace_binding_loc, zone()); |
1621 // DeclareImport(module_namespace_binding, pos, CHECK_OK_VOID); | |
1622 } | 1624 } |
1623 | 1625 |
1624 if (import_default_binding != nullptr) { | 1626 if (import_default_binding != nullptr) { |
1625 module()->AddImport(ast_value_factory()->default_string(), | 1627 module()->AddImport(ast_value_factory()->default_string(), |
1626 import_default_binding, module_specifier, | 1628 import_default_binding, module_specifier, |
1627 import_default_binding_loc, zone()); | 1629 import_default_binding_loc, zone()); |
1628 // DeclareImport(import_default_binding, pos, CHECK_OK_VOID); | |
1629 } | 1630 } |
1630 | 1631 |
1631 if (named_imports != nullptr) { | 1632 if (named_imports != nullptr) { |
1632 if (named_imports->length() == 0) { | 1633 if (named_imports->length() == 0) { |
1633 module()->AddEmptyImport(module_specifier, scanner()->location(), zone()); | 1634 module()->AddEmptyImport(module_specifier, scanner()->location(), zone()); |
1634 } else { | 1635 } else { |
1635 for (int i = 0; i < named_imports->length(); ++i) { | 1636 for (int i = 0; i < named_imports->length(); ++i) { |
1636 const NamedImport* import = named_imports->at(i); | 1637 const NamedImport* import = named_imports->at(i); |
1637 module()->AddImport(import->import_name, import->local_name, | 1638 module()->AddImport(import->import_name, import->local_name, |
1638 module_specifier, import->location, zone()); | 1639 module_specifier, import->location, zone()); |
1639 // DeclareImport(import->local_name, pos, CHECK_OK_VOID); | |
1640 } | 1640 } |
1641 } | 1641 } |
1642 } | 1642 } |
1643 } | 1643 } |
1644 | 1644 |
1645 | 1645 |
1646 Statement* Parser::ParseExportDefault(bool* ok) { | 1646 Statement* Parser::ParseExportDefault(bool* ok) { |
1647 // Supports the following productions, starting after the 'default' token: | 1647 // Supports the following productions, starting after the 'default' token: |
1648 // 'export' 'default' HoistableDeclaration | 1648 // 'export' 'default' HoistableDeclaration |
1649 // 'export' 'default' ClassDeclaration | 1649 // 'export' 'default' ClassDeclaration |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1967 // scope. | 1967 // scope. |
1968 // Let/const variables are always added to the immediately enclosing scope. | 1968 // Let/const variables are always added to the immediately enclosing scope. |
1969 Scope* scope = IsLexicalVariableMode(mode) | 1969 Scope* scope = IsLexicalVariableMode(mode) |
1970 ? this->scope() | 1970 ? this->scope() |
1971 : this->scope()->GetDeclarationScope(); | 1971 : this->scope()->GetDeclarationScope(); |
1972 return scope->NewUnresolved(factory(), name, Variable::NORMAL, | 1972 return scope->NewUnresolved(factory(), name, Variable::NORMAL, |
1973 scanner()->location().beg_pos, | 1973 scanner()->location().beg_pos, |
1974 scanner()->location().end_pos); | 1974 scanner()->location().end_pos); |
1975 } | 1975 } |
1976 | 1976 |
1977 | 1977 void Parser::DeclareConstVariable(const AstRawString* name, |
1978 void Parser::DeclareImport(const AstRawString* local_name, int pos, bool* ok) { | 1978 InitializationFlag init, int pos, bool* ok) { |
1979 DCHECK_NOT_NULL(local_name); | 1979 DCHECK_NOT_NULL(name); |
1980 VariableProxy* proxy = NewUnresolved(local_name, CONST); | 1980 VariableProxy* proxy = NewUnresolved(name, CONST); |
1981 Declaration* declaration = | 1981 Declaration* declaration = |
1982 factory()->NewVariableDeclaration(proxy, CONST, scope(), pos); | 1982 factory()->NewVariableDeclaration(proxy, CONST, scope(), init, pos); |
1983 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK_VOID); | 1983 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK_VOID); |
1984 } | 1984 } |
1985 | 1985 |
1986 | 1986 |
1987 Variable* Parser::Declare(Declaration* declaration, | 1987 Variable* Parser::Declare(Declaration* declaration, |
1988 DeclarationDescriptor::Kind declaration_kind, | 1988 DeclarationDescriptor::Kind declaration_kind, |
1989 bool resolve, bool* ok, Scope* scope) { | 1989 bool resolve, bool* ok, Scope* scope) { |
1990 VariableProxy* proxy = declaration->proxy(); | 1990 VariableProxy* proxy = declaration->proxy(); |
1991 DCHECK(proxy->raw_name() != NULL); | 1991 DCHECK(proxy->raw_name() != NULL); |
1992 const AstRawString* name = proxy->raw_name(); | 1992 const AstRawString* name = proxy->raw_name(); |
(...skipping 5172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7165 node->Print(Isolate::Current()); | 7165 node->Print(Isolate::Current()); |
7166 } | 7166 } |
7167 #endif // DEBUG | 7167 #endif // DEBUG |
7168 | 7168 |
7169 #undef CHECK_OK | 7169 #undef CHECK_OK |
7170 #undef CHECK_OK_VOID | 7170 #undef CHECK_OK_VOID |
7171 #undef CHECK_FAILED | 7171 #undef CHECK_FAILED |
7172 | 7172 |
7173 } // namespace internal | 7173 } // namespace internal |
7174 } // namespace v8 | 7174 } // namespace v8 |
OLD | NEW |