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 #ifndef V8_PARSING_PARSER_BASE_H | 5 #ifndef V8_PARSING_PARSER_BASE_H |
6 #define V8_PARSING_PARSER_BASE_H | 6 #define V8_PARSING_PARSER_BASE_H |
7 | 7 |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/scopes.h" | 9 #include "src/ast/scopes.h" |
10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 ++arity; | 76 ++arity; |
77 } | 77 } |
78 | 78 |
79 DeclarationScope* scope; | 79 DeclarationScope* scope; |
80 bool has_rest = false; | 80 bool has_rest = false; |
81 bool is_simple = true; | 81 bool is_simple = true; |
82 int function_length = 0; | 82 int function_length = 0; |
83 int arity = 0; | 83 int arity = 0; |
84 }; | 84 }; |
85 | 85 |
| 86 // Stack-allocated scope to collect source ranges from the parser. |
| 87 class SourceRangeScope final { |
| 88 public: |
| 89 enum PositionKind { |
| 90 POSITION, |
| 91 PEEK_POS, |
| 92 }; |
| 93 |
| 94 SourceRangeScope(Scanner* scanner, SourceRange* range, |
| 95 PositionKind pre_kind = PEEK_POS, |
| 96 PositionKind post_kind = POSITION) |
| 97 : scanner_(scanner), range_(range), post_kind_(post_kind) { |
| 98 range_->start = GetPosition(pre_kind); |
| 99 } |
| 100 |
| 101 ~SourceRangeScope() { range_->end = GetPosition(post_kind_); } |
| 102 |
| 103 private: |
| 104 int32_t GetPosition(PositionKind kind) { |
| 105 switch (post_kind_) { |
| 106 case POSITION: |
| 107 return scanner_->location().beg_pos; |
| 108 case PEEK_POS: |
| 109 return scanner_->peek_location().beg_pos; |
| 110 default: |
| 111 UNREACHABLE(); |
| 112 } |
| 113 } |
| 114 |
| 115 Scanner* scanner_; |
| 116 SourceRange* range_; |
| 117 PositionKind post_kind_; |
| 118 |
| 119 DISALLOW_IMPLICIT_CONSTRUCTORS(SourceRangeScope); |
| 120 }; |
86 | 121 |
87 // ---------------------------------------------------------------------------- | 122 // ---------------------------------------------------------------------------- |
88 // The CHECK_OK macro is a convenient macro to enforce error | 123 // The CHECK_OK macro is a convenient macro to enforce error |
89 // handling for functions that may fail (by returning !*ok). | 124 // handling for functions that may fail (by returning !*ok). |
90 // | 125 // |
91 // CAUTION: This macro appends extra statements after a call, | 126 // CAUTION: This macro appends extra statements after a call, |
92 // thus it must never be used where only a single statement | 127 // thus it must never be used where only a single statement |
93 // is correct (e.g. an if statement branch w/o braces)! | 128 // is correct (e.g. an if statement branch w/o braces)! |
94 | 129 |
95 #define CHECK_OK_CUSTOM(x, ...) ok); \ | 130 #define CHECK_OK_CUSTOM(x, ...) ok); \ |
(...skipping 5042 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5138 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseIfStatement( | 5173 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseIfStatement( |
5139 ZoneList<const AstRawString*>* labels, bool* ok) { | 5174 ZoneList<const AstRawString*>* labels, bool* ok) { |
5140 // IfStatement :: | 5175 // IfStatement :: |
5141 // 'if' '(' Expression ')' Statement ('else' Statement)? | 5176 // 'if' '(' Expression ')' Statement ('else' Statement)? |
5142 | 5177 |
5143 int pos = peek_position(); | 5178 int pos = peek_position(); |
5144 Expect(Token::IF, CHECK_OK); | 5179 Expect(Token::IF, CHECK_OK); |
5145 Expect(Token::LPAREN, CHECK_OK); | 5180 Expect(Token::LPAREN, CHECK_OK); |
5146 ExpressionT condition = ParseExpression(true, CHECK_OK); | 5181 ExpressionT condition = ParseExpression(true, CHECK_OK); |
5147 Expect(Token::RPAREN, CHECK_OK); | 5182 Expect(Token::RPAREN, CHECK_OK); |
5148 StatementT then_statement = ParseScopedStatement(labels, CHECK_OK); | 5183 |
| 5184 SourceRange then_range, else_range; |
| 5185 StatementT then_statement = impl()->NullStatement(); |
| 5186 { |
| 5187 SourceRangeScope range_scope(scanner(), &then_range); |
| 5188 then_statement = ParseScopedStatement(labels, CHECK_OK); |
| 5189 } |
| 5190 |
5149 StatementT else_statement = impl()->NullStatement(); | 5191 StatementT else_statement = impl()->NullStatement(); |
5150 if (Check(Token::ELSE)) { | 5192 if (Check(Token::ELSE)) { |
| 5193 SourceRangeScope range_scope(scanner(), &else_range); |
5151 else_statement = ParseScopedStatement(labels, CHECK_OK); | 5194 else_statement = ParseScopedStatement(labels, CHECK_OK); |
5152 } else { | 5195 } else { |
5153 else_statement = factory()->NewEmptyStatement(kNoSourcePosition); | 5196 else_statement = factory()->NewEmptyStatement(kNoSourcePosition); |
5154 } | 5197 } |
5155 return factory()->NewIfStatement(condition, then_statement, else_statement, | 5198 return factory()->NewIfStatement(condition, then_statement, else_statement, |
5156 pos); | 5199 pos, then_range, else_range); |
5157 } | 5200 } |
5158 | 5201 |
5159 template <typename Impl> | 5202 template <typename Impl> |
5160 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseContinueStatement( | 5203 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseContinueStatement( |
5161 bool* ok) { | 5204 bool* ok) { |
5162 // ContinueStatement :: | 5205 // ContinueStatement :: |
5163 // 'continue' Identifier? ';' | 5206 // 'continue' Identifier? ';' |
5164 | 5207 |
5165 int pos = peek_position(); | 5208 int pos = peek_position(); |
5166 Expect(Token::CONTINUE, CHECK_OK); | 5209 Expect(Token::CONTINUE, CHECK_OK); |
(...skipping 841 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6008 } | 6051 } |
6009 | 6052 |
6010 #undef CHECK_OK | 6053 #undef CHECK_OK |
6011 #undef CHECK_OK_CUSTOM | 6054 #undef CHECK_OK_CUSTOM |
6012 #undef CHECK_OK_VOID | 6055 #undef CHECK_OK_VOID |
6013 | 6056 |
6014 } // namespace internal | 6057 } // namespace internal |
6015 } // namespace v8 | 6058 } // namespace v8 |
6016 | 6059 |
6017 #endif // V8_PARSING_PARSER_BASE_H | 6060 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |