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 1735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1746 } | 1746 } |
1747 if (is_generator()) { | 1747 if (is_generator()) { |
1748 return_value = impl()->BuildIteratorResult(return_value, true); | 1748 return_value = impl()->BuildIteratorResult(return_value, true); |
1749 } else if (is_async_function()) { | 1749 } else if (is_async_function()) { |
1750 return_value = | 1750 return_value = |
1751 impl()->BuildResolvePromise(return_value, return_value->position()); | 1751 impl()->BuildResolvePromise(return_value, return_value->position()); |
1752 } | 1752 } |
1753 return return_value; | 1753 return return_value; |
1754 } | 1754 } |
1755 | 1755 |
| 1756 Expression* Parser::RewriteDoExpression(Block* body, int pos, bool* ok) { |
| 1757 Variable* result = NewTemporary(ast_value_factory()->dot_result_string()); |
| 1758 DoExpression* expr = factory()->NewDoExpression(body, result, pos); |
| 1759 if (!Rewriter::Rewrite(this, GetClosureScope(), expr, ast_value_factory())) { |
| 1760 *ok = false; |
| 1761 return nullptr; |
| 1762 } |
| 1763 return expr; |
| 1764 } |
| 1765 |
1756 Statement* Parser::ParseFunctionDeclaration(bool* ok) { | 1766 Statement* Parser::ParseFunctionDeclaration(bool* ok) { |
1757 Consume(Token::FUNCTION); | 1767 Consume(Token::FUNCTION); |
1758 int pos = position(); | 1768 int pos = position(); |
1759 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; | 1769 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; |
1760 if (Check(Token::MUL)) { | 1770 if (Check(Token::MUL)) { |
1761 flags |= ParseFunctionFlags::kIsGenerator; | 1771 flags |= ParseFunctionFlags::kIsGenerator; |
1762 if (allow_harmony_restrictive_declarations()) { | 1772 if (allow_harmony_restrictive_declarations()) { |
1763 ReportMessageAt(scanner()->location(), | 1773 ReportMessageAt(scanner()->location(), |
1764 MessageTemplate::kGeneratorInLegacyContext); | 1774 MessageTemplate::kGeneratorInLegacyContext); |
1765 *ok = false; | 1775 *ok = false; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1869 | 1879 |
1870 cases_block_state.set_end_position(scanner()->location().end_pos); | 1880 cases_block_state.set_end_position(scanner()->location().end_pos); |
1871 cases_block->set_scope(cases_block_state.FinalizedBlockScope()); | 1881 cases_block->set_scope(cases_block_state.FinalizedBlockScope()); |
1872 } | 1882 } |
1873 | 1883 |
1874 switch_block->statements()->Add(cases_block, zone()); | 1884 switch_block->statements()->Add(cases_block, zone()); |
1875 | 1885 |
1876 return switch_block; | 1886 return switch_block; |
1877 } | 1887 } |
1878 | 1888 |
1879 | |
1880 Statement* Parser::ParseThrowStatement(bool* ok) { | |
1881 // ThrowStatement :: | |
1882 // 'throw' Expression ';' | |
1883 | |
1884 Expect(Token::THROW, CHECK_OK); | |
1885 int pos = position(); | |
1886 if (scanner()->HasAnyLineTerminatorBeforeNext()) { | |
1887 ReportMessage(MessageTemplate::kNewlineAfterThrow); | |
1888 *ok = false; | |
1889 return NULL; | |
1890 } | |
1891 Expression* exception = ParseExpression(true, CHECK_OK); | |
1892 ExpectSemicolon(CHECK_OK); | |
1893 | |
1894 return factory()->NewExpressionStatement( | |
1895 factory()->NewThrow(exception, pos), pos); | |
1896 } | |
1897 | |
1898 | |
1899 TryStatement* Parser::ParseTryStatement(bool* ok) { | 1889 TryStatement* Parser::ParseTryStatement(bool* ok) { |
1900 // TryStatement :: | 1890 // TryStatement :: |
1901 // 'try' Block Catch | 1891 // 'try' Block Catch |
1902 // 'try' Block Finally | 1892 // 'try' Block Finally |
1903 // 'try' Block Catch Finally | 1893 // 'try' Block Catch Finally |
1904 // | 1894 // |
1905 // Catch :: | 1895 // Catch :: |
1906 // 'catch' '(' Identifier ')' Block | 1896 // 'catch' '(' Identifier ')' Block |
1907 // | 1897 // |
1908 // Finally :: | 1898 // Finally :: |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2082 return NULL; | 2072 return NULL; |
2083 } | 2073 } |
2084 DCHECK(finally_block != NULL); | 2074 DCHECK(finally_block != NULL); |
2085 result = factory()->NewTryFinallyStatement(try_block, finally_block, pos); | 2075 result = factory()->NewTryFinallyStatement(try_block, finally_block, pos); |
2086 } | 2076 } |
2087 | 2077 |
2088 return result; | 2078 return result; |
2089 } | 2079 } |
2090 | 2080 |
2091 | 2081 |
2092 DoWhileStatement* Parser::ParseDoWhileStatement( | |
2093 ZoneList<const AstRawString*>* labels, bool* ok) { | |
2094 // DoStatement :: | |
2095 // 'do' Statement 'while' '(' Expression ')' ';' | |
2096 | |
2097 DoWhileStatement* loop = | |
2098 factory()->NewDoWhileStatement(labels, peek_position()); | |
2099 ParserTarget target(this, loop); | |
2100 | |
2101 Expect(Token::DO, CHECK_OK); | |
2102 Statement* body = ParseScopedStatement(NULL, true, CHECK_OK); | |
2103 Expect(Token::WHILE, CHECK_OK); | |
2104 Expect(Token::LPAREN, CHECK_OK); | |
2105 | |
2106 Expression* cond = ParseExpression(true, CHECK_OK); | |
2107 Expect(Token::RPAREN, CHECK_OK); | |
2108 | |
2109 // Allow do-statements to be terminated with and without | |
2110 // semi-colons. This allows code such as 'do;while(0)return' to | |
2111 // parse, which would not be the case if we had used the | |
2112 // ExpectSemicolon() functionality here. | |
2113 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON); | |
2114 | |
2115 if (loop != NULL) loop->Initialize(cond, body); | |
2116 return loop; | |
2117 } | |
2118 | |
2119 | |
2120 WhileStatement* Parser::ParseWhileStatement( | |
2121 ZoneList<const AstRawString*>* labels, bool* ok) { | |
2122 // WhileStatement :: | |
2123 // 'while' '(' Expression ')' Statement | |
2124 | |
2125 WhileStatement* loop = factory()->NewWhileStatement(labels, peek_position()); | |
2126 ParserTarget target(this, loop); | |
2127 | |
2128 Expect(Token::WHILE, CHECK_OK); | |
2129 Expect(Token::LPAREN, CHECK_OK); | |
2130 Expression* cond = ParseExpression(true, CHECK_OK); | |
2131 Expect(Token::RPAREN, CHECK_OK); | |
2132 Statement* body = ParseScopedStatement(NULL, true, CHECK_OK); | |
2133 | |
2134 if (loop != NULL) loop->Initialize(cond, body); | |
2135 return loop; | |
2136 } | |
2137 | |
2138 | |
2139 // !%_IsJSReceiver(result = iterator.next()) && | 2082 // !%_IsJSReceiver(result = iterator.next()) && |
2140 // %ThrowIteratorResultNotAnObject(result) | 2083 // %ThrowIteratorResultNotAnObject(result) |
2141 Expression* Parser::BuildIteratorNextResult(Expression* iterator, | 2084 Expression* Parser::BuildIteratorNextResult(Expression* iterator, |
2142 Variable* result, int pos) { | 2085 Variable* result, int pos) { |
2143 Expression* next_literal = factory()->NewStringLiteral( | 2086 Expression* next_literal = factory()->NewStringLiteral( |
2144 ast_value_factory()->next_string(), kNoSourcePosition); | 2087 ast_value_factory()->next_string(), kNoSourcePosition); |
2145 Expression* next_property = | 2088 Expression* next_property = |
2146 factory()->NewProperty(iterator, next_literal, kNoSourcePosition); | 2089 factory()->NewProperty(iterator, next_literal, kNoSourcePosition); |
2147 ZoneList<Expression*>* next_arguments = | 2090 ZoneList<Expression*>* next_arguments = |
2148 new (zone()) ZoneList<Expression*>(0, zone()); | 2091 new (zone()) ZoneList<Expression*>(0, zone()); |
(...skipping 854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3003 | 2946 |
3004 return_value = BuildResolvePromise(return_value, return_value->position()); | 2947 return_value = BuildResolvePromise(return_value, return_value->position()); |
3005 block->statements()->Add( | 2948 block->statements()->Add( |
3006 factory()->NewReturnStatement(return_value, return_value->position()), | 2949 factory()->NewReturnStatement(return_value, return_value->position()), |
3007 zone()); | 2950 zone()); |
3008 block = BuildRejectPromiseOnException(block, CHECK_OK_VOID); | 2951 block = BuildRejectPromiseOnException(block, CHECK_OK_VOID); |
3009 body->Add(block, zone()); | 2952 body->Add(block, zone()); |
3010 scope->set_end_position(scanner()->location().end_pos); | 2953 scope->set_end_position(scanner()->location().end_pos); |
3011 } | 2954 } |
3012 | 2955 |
3013 DoExpression* Parser::ParseDoExpression(bool* ok) { | |
3014 // AssignmentExpression :: | |
3015 // do '{' StatementList '}' | |
3016 int pos = peek_position(); | |
3017 | |
3018 Expect(Token::DO, CHECK_OK); | |
3019 Variable* result = NewTemporary(ast_value_factory()->dot_result_string()); | |
3020 Block* block = ParseBlock(nullptr, CHECK_OK); | |
3021 DoExpression* expr = factory()->NewDoExpression(block, result, pos); | |
3022 if (!Rewriter::Rewrite(this, GetClosureScope(), expr, ast_value_factory())) { | |
3023 *ok = false; | |
3024 return nullptr; | |
3025 } | |
3026 return expr; | |
3027 } | |
3028 | |
3029 void Parser::ParseArrowFunctionFormalParameterList( | 2956 void Parser::ParseArrowFunctionFormalParameterList( |
3030 ParserFormalParameters* parameters, Expression* expr, | 2957 ParserFormalParameters* parameters, Expression* expr, |
3031 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, | 2958 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, |
3032 const Scope::Snapshot& scope_snapshot, bool* ok) { | 2959 const Scope::Snapshot& scope_snapshot, bool* ok) { |
3033 if (expr->IsEmptyParentheses()) return; | 2960 if (expr->IsEmptyParentheses()) return; |
3034 | 2961 |
3035 ParseArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos, | 2962 ParseArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos, |
3036 CHECK_OK_VOID); | 2963 CHECK_OK_VOID); |
3037 | 2964 |
3038 scope_snapshot.Reparent(parameters->scope); | 2965 scope_snapshot.Reparent(parameters->scope); |
(...skipping 2741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5780 node->Print(Isolate::Current()); | 5707 node->Print(Isolate::Current()); |
5781 } | 5708 } |
5782 #endif // DEBUG | 5709 #endif // DEBUG |
5783 | 5710 |
5784 #undef CHECK_OK | 5711 #undef CHECK_OK |
5785 #undef CHECK_OK_VOID | 5712 #undef CHECK_OK_VOID |
5786 #undef CHECK_FAILED | 5713 #undef CHECK_FAILED |
5787 | 5714 |
5788 } // namespace internal | 5715 } // namespace internal |
5789 } // namespace v8 | 5716 } // namespace v8 |
OLD | NEW |