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-expression-rewriter.h" | 10 #include "src/ast/ast-expression-rewriter.h" |
(...skipping 1745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1756 Expression* Parser::RewriteDoExpression(Block* body, int pos, bool* ok) { | 1756 Expression* Parser::RewriteDoExpression(Block* body, int pos, bool* ok) { |
1757 Variable* result = NewTemporary(ast_value_factory()->dot_result_string()); | 1757 Variable* result = NewTemporary(ast_value_factory()->dot_result_string()); |
1758 DoExpression* expr = factory()->NewDoExpression(body, result, pos); | 1758 DoExpression* expr = factory()->NewDoExpression(body, result, pos); |
1759 if (!Rewriter::Rewrite(this, GetClosureScope(), expr, ast_value_factory())) { | 1759 if (!Rewriter::Rewrite(this, GetClosureScope(), expr, ast_value_factory())) { |
1760 *ok = false; | 1760 *ok = false; |
1761 return nullptr; | 1761 return nullptr; |
1762 } | 1762 } |
1763 return expr; | 1763 return expr; |
1764 } | 1764 } |
1765 | 1765 |
1766 Statement* Parser::ParseFunctionDeclaration(bool* ok) { | 1766 Statement* Parser::RewriteSwitchStatement(Expression* tag, |
1767 Consume(Token::FUNCTION); | 1767 SwitchStatement* switch_statement, |
1768 int pos = position(); | 1768 ZoneList<CaseClause*>* cases, |
1769 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; | 1769 Scope* scope) { |
1770 if (Check(Token::MUL)) { | |
1771 flags |= ParseFunctionFlags::kIsGenerator; | |
1772 if (allow_harmony_restrictive_declarations()) { | |
1773 ReportMessageAt(scanner()->location(), | |
1774 MessageTemplate::kGeneratorInLegacyContext); | |
1775 *ok = false; | |
1776 return nullptr; | |
1777 } | |
1778 } | |
1779 | |
1780 return ParseHoistableDeclaration(pos, flags, nullptr, false, CHECK_OK); | |
1781 } | |
1782 | |
1783 CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) { | |
1784 // CaseClause :: | |
1785 // 'case' Expression ':' StatementList | |
1786 // 'default' ':' StatementList | |
1787 | |
1788 Expression* label = NULL; // NULL expression indicates default case | |
1789 if (peek() == Token::CASE) { | |
1790 Expect(Token::CASE, CHECK_OK); | |
1791 label = ParseExpression(true, CHECK_OK); | |
1792 } else { | |
1793 Expect(Token::DEFAULT, CHECK_OK); | |
1794 if (*default_seen_ptr) { | |
1795 ReportMessage(MessageTemplate::kMultipleDefaultsInSwitch); | |
1796 *ok = false; | |
1797 return NULL; | |
1798 } | |
1799 *default_seen_ptr = true; | |
1800 } | |
1801 Expect(Token::COLON, CHECK_OK); | |
1802 int pos = position(); | |
1803 ZoneList<Statement*>* statements = | |
1804 new(zone()) ZoneList<Statement*>(5, zone()); | |
1805 Statement* stat = NULL; | |
1806 while (peek() != Token::CASE && | |
1807 peek() != Token::DEFAULT && | |
1808 peek() != Token::RBRACE) { | |
1809 stat = ParseStatementListItem(CHECK_OK); | |
1810 statements->Add(stat, zone()); | |
1811 } | |
1812 return factory()->NewCaseClause(label, statements, pos); | |
1813 } | |
1814 | |
1815 | |
1816 Statement* Parser::ParseSwitchStatement(ZoneList<const AstRawString*>* labels, | |
1817 bool* ok) { | |
1818 // SwitchStatement :: | |
1819 // 'switch' '(' Expression ')' '{' CaseClause* '}' | |
1820 // In order to get the CaseClauses to execute in their own lexical scope, | 1770 // In order to get the CaseClauses to execute in their own lexical scope, |
1821 // but without requiring downstream code to have special scope handling | 1771 // but without requiring downstream code to have special scope handling |
1822 // code for switch statements, desugar into blocks as follows: | 1772 // code for switch statements, desugar into blocks as follows: |
1823 // { // To group the statements--harmless to evaluate Expression in scope | 1773 // { // To group the statements--harmless to evaluate Expression in scope |
1824 // .tag_variable = Expression; | 1774 // .tag_variable = Expression; |
1825 // { // To give CaseClauses a scope | 1775 // { // To give CaseClauses a scope |
1826 // switch (.tag_variable) { CaseClause* } | 1776 // switch (.tag_variable) { CaseClause* } |
1827 // } | 1777 // } |
1828 // } | 1778 // } |
1829 | 1779 |
1830 Block* switch_block = factory()->NewBlock(NULL, 2, false, kNoSourcePosition); | 1780 BlockT switch_block = factory()->NewBlock(NULL, 2, false, kNoSourcePosition); |
adamk
2016/09/09 17:59:12
s/BlockT/Block*/
nickie
2016/09/10 18:13:11
Done.
| |
1831 int switch_pos = peek_position(); | |
1832 | |
1833 Expect(Token::SWITCH, CHECK_OK); | |
1834 Expect(Token::LPAREN, CHECK_OK); | |
1835 Expression* tag = ParseExpression(true, CHECK_OK); | |
1836 Expect(Token::RPAREN, CHECK_OK); | |
1837 | 1781 |
1838 Variable* tag_variable = | 1782 Variable* tag_variable = |
1839 NewTemporary(ast_value_factory()->dot_switch_tag_string()); | 1783 NewTemporary(ast_value_factory()->dot_switch_tag_string()); |
1840 Assignment* tag_assign = factory()->NewAssignment( | 1784 Assignment* tag_assign = factory()->NewAssignment( |
1841 Token::ASSIGN, factory()->NewVariableProxy(tag_variable), tag, | 1785 Token::ASSIGN, factory()->NewVariableProxy(tag_variable), tag, |
1842 tag->position()); | 1786 tag->position()); |
1843 Statement* tag_statement = | 1787 Statement* tag_statement = |
1844 factory()->NewExpressionStatement(tag_assign, kNoSourcePosition); | 1788 factory()->NewExpressionStatement(tag_assign, kNoSourcePosition); |
1845 switch_block->statements()->Add(tag_statement, zone()); | 1789 switch_block->statements()->Add(tag_statement, zone()); |
1846 | 1790 |
1847 // make statement: undefined; | 1791 // make statement: undefined; |
1848 // This is needed so the tag isn't returned as the value, in case the switch | 1792 // This is needed so the tag isn't returned as the value, in case the switch |
1849 // statements don't have a value. | 1793 // statements don't have a value. |
1850 switch_block->statements()->Add( | 1794 switch_block->statements()->Add( |
1851 factory()->NewExpressionStatement( | 1795 factory()->NewExpressionStatement( |
1852 factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition), | 1796 factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition), |
1853 zone()); | 1797 zone()); |
1854 | 1798 |
1855 Block* cases_block = factory()->NewBlock(NULL, 1, false, kNoSourcePosition); | 1799 ExpressionT tag_read = factory()->NewVariableProxy(tag_variable); |
adamk
2016/09/09 17:59:12
and here
nickie
2016/09/10 18:13:11
Done.
| |
1856 | 1800 switch_statement->Initialize(tag_read, cases); |
1857 SwitchStatement* switch_statement = | 1801 BlockT cases_block = factory()->NewBlock(NULL, 1, false, kNoSourcePosition); |
adamk
2016/09/09 17:59:12
and here
nickie
2016/09/10 18:13:11
Done.
| |
1858 factory()->NewSwitchStatement(labels, switch_pos); | 1802 cases_block->statements()->Add(switch_statement, zone()); |
1859 | 1803 cases_block->set_scope(scope); |
1860 { | |
1861 BlockState cases_block_state(&scope_state_); | |
1862 cases_block_state.set_start_position(scanner()->location().beg_pos); | |
1863 cases_block_state.SetNonlinear(); | |
1864 ParserTarget target(this, switch_statement); | |
1865 | |
1866 Expression* tag_read = factory()->NewVariableProxy(tag_variable); | |
1867 | |
1868 bool default_seen = false; | |
1869 ZoneList<CaseClause*>* cases = | |
1870 new (zone()) ZoneList<CaseClause*>(4, zone()); | |
1871 Expect(Token::LBRACE, CHECK_OK); | |
1872 while (peek() != Token::RBRACE) { | |
1873 CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK); | |
1874 cases->Add(clause, zone()); | |
1875 } | |
1876 switch_statement->Initialize(tag_read, cases); | |
1877 cases_block->statements()->Add(switch_statement, zone()); | |
1878 Expect(Token::RBRACE, CHECK_OK); | |
1879 | |
1880 cases_block_state.set_end_position(scanner()->location().end_pos); | |
1881 cases_block->set_scope(cases_block_state.FinalizedBlockScope()); | |
1882 } | |
1883 | |
1884 switch_block->statements()->Add(cases_block, zone()); | 1804 switch_block->statements()->Add(cases_block, zone()); |
1885 | |
1886 return switch_block; | 1805 return switch_block; |
1887 } | 1806 } |
1888 | 1807 |
1808 Statement* Parser::ParseFunctionDeclaration(bool* ok) { | |
1809 Consume(Token::FUNCTION); | |
1810 int pos = position(); | |
1811 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; | |
1812 if (Check(Token::MUL)) { | |
1813 flags |= ParseFunctionFlags::kIsGenerator; | |
1814 if (allow_harmony_restrictive_declarations()) { | |
1815 ReportMessageAt(scanner()->location(), | |
1816 MessageTemplate::kGeneratorInLegacyContext); | |
1817 *ok = false; | |
1818 return nullptr; | |
1819 } | |
1820 } | |
1821 | |
1822 return ParseHoistableDeclaration(pos, flags, nullptr, false, CHECK_OK); | |
1823 } | |
1824 | |
1889 TryStatement* Parser::ParseTryStatement(bool* ok) { | 1825 TryStatement* Parser::ParseTryStatement(bool* ok) { |
1890 // TryStatement :: | 1826 // TryStatement :: |
1891 // 'try' Block Catch | 1827 // 'try' Block Catch |
1892 // 'try' Block Finally | 1828 // 'try' Block Finally |
1893 // 'try' Block Catch Finally | 1829 // 'try' Block Catch Finally |
1894 // | 1830 // |
1895 // Catch :: | 1831 // Catch :: |
1896 // 'catch' '(' Identifier ')' Block | 1832 // 'catch' '(' Identifier ')' Block |
1897 // | 1833 // |
1898 // Finally :: | 1834 // Finally :: |
(...skipping 3808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5707 node->Print(Isolate::Current()); | 5643 node->Print(Isolate::Current()); |
5708 } | 5644 } |
5709 #endif // DEBUG | 5645 #endif // DEBUG |
5710 | 5646 |
5711 #undef CHECK_OK | 5647 #undef CHECK_OK |
5712 #undef CHECK_OK_VOID | 5648 #undef CHECK_OK_VOID |
5713 #undef CHECK_FAILED | 5649 #undef CHECK_FAILED |
5714 | 5650 |
5715 } // namespace internal | 5651 } // namespace internal |
5716 } // namespace v8 | 5652 } // namespace v8 |
OLD | NEW |