OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 28 matching lines...) Expand all Loading... |
39 // and collects some information about the program along the way. | 39 // and collects some information about the program along the way. |
40 // The grammar check is only performed in order to understand the program | 40 // The grammar check is only performed in order to understand the program |
41 // sufficiently to deduce some information about it, that can be used | 41 // sufficiently to deduce some information about it, that can be used |
42 // to speed up later parsing. Finding errors is not the goal of pre-parsing, | 42 // to speed up later parsing. Finding errors is not the goal of pre-parsing, |
43 // rather it is to speed up properly written and correct programs. | 43 // rather it is to speed up properly written and correct programs. |
44 // That means that contextual checks (like a label being declared where | 44 // That means that contextual checks (like a label being declared where |
45 // it is used) are generally omitted. | 45 // it is used) are generally omitted. |
46 | 46 |
47 namespace i = v8::internal; | 47 namespace i = v8::internal; |
48 | 48 |
49 enum StatementType { | |
50 kUnknownStatement | |
51 }; | |
52 | |
53 enum ExpressionType { | |
54 kUnknownExpression, | |
55 kIdentifierExpression, // Used to detect labels. | |
56 kThisExpression, | |
57 kThisPropertyExpression | |
58 }; | |
59 | |
60 enum IdentifierType { | |
61 kUnknownIdentifier | |
62 }; | |
63 | |
64 enum SourceElementTypes { | |
65 kUnknownSourceElements | |
66 }; | |
67 | |
68 | |
69 typedef int SourceElements; | |
70 typedef int Expression; | |
71 typedef int Statement; | |
72 typedef int Identifier; | |
73 typedef int Arguments; | |
74 | |
75 | |
76 class PreParser { | 49 class PreParser { |
77 public: | 50 public: |
78 PreParser() : scope_(NULL), allow_lazy_(true) { } | 51 enum PreParseResult { |
| 52 kPreParseStackOverflow, |
| 53 kPreParseSuccess |
| 54 }; |
| 55 |
79 ~PreParser() { } | 56 ~PreParser() { } |
80 | 57 |
81 // Pre-parse the program from the character stream; returns true on | 58 // Pre-parse the program from the character stream; returns true on |
82 // success (even if parsing failed, the pre-parse data successfully | 59 // success (even if parsing failed, the pre-parse data successfully |
83 // captured the syntax error), and false if a stack-overflow happened | 60 // captured the syntax error), and false if a stack-overflow happened |
84 // during parsing. | 61 // during parsing. |
85 bool PreParseProgram(i::JavaScriptScanner* scanner, | 62 static PreParseResult PreParseProgram(i::JavaScriptScanner* scanner, |
86 i::ParserRecorder* log, | 63 i::ParserRecorder* log, |
87 bool allow_lazy) { | 64 bool allow_lazy, |
88 allow_lazy_ = allow_lazy; | 65 uintptr_t stack_limit) { |
89 scanner_ = scanner; | 66 return PreParser(scanner, log, stack_limit, allow_lazy).PreParse(); |
90 log_ = log; | |
91 Scope top_scope(&scope_, kTopLevelScope); | |
92 bool ok = true; | |
93 ParseSourceElements(i::Token::EOS, &ok); | |
94 bool stack_overflow = scanner_->stack_overflow(); | |
95 if (!ok && !stack_overflow) { | |
96 ReportUnexpectedToken(scanner_->current_token()); | |
97 } | |
98 return !stack_overflow; | |
99 } | 67 } |
100 | 68 |
101 private: | 69 private: |
102 enum ScopeType { | 70 enum ScopeType { |
103 kTopLevelScope, | 71 kTopLevelScope, |
104 kFunctionScope | 72 kFunctionScope |
105 }; | 73 }; |
106 | 74 |
| 75 // Types that allow us to recognize simple this-property assignments. |
| 76 // A simple this-property assignment is a statement on the form |
| 77 // "this.propertyName = {primitive constant or function parameter name);" |
| 78 // where propertyName isn't "__proto__". |
| 79 // The result is only relevant if the function body contains only |
| 80 // simple this-property assignments. |
| 81 |
| 82 enum StatementType { |
| 83 kUnknownStatement |
| 84 }; |
| 85 |
| 86 enum ExpressionType { |
| 87 kUnknownExpression, |
| 88 kIdentifierExpression, // Used to detect labels. |
| 89 kThisExpression, |
| 90 kThisPropertyExpression |
| 91 }; |
| 92 |
| 93 enum IdentifierType { |
| 94 kUnknownIdentifier |
| 95 }; |
| 96 |
| 97 enum SourceElementTypes { |
| 98 kUnknownSourceElements |
| 99 }; |
| 100 |
| 101 typedef int SourceElements; |
| 102 typedef int Expression; |
| 103 typedef int Statement; |
| 104 typedef int Identifier; |
| 105 typedef int Arguments; |
| 106 |
107 class Scope { | 107 class Scope { |
108 public: | 108 public: |
109 Scope(Scope** variable, ScopeType type) | 109 Scope(Scope** variable, ScopeType type) |
110 : variable_(variable), | 110 : variable_(variable), |
111 prev_(*variable), | 111 prev_(*variable), |
112 type_(type), | 112 type_(type), |
113 materialized_literal_count_(0), | 113 materialized_literal_count_(0), |
114 expected_properties_(0), | 114 expected_properties_(0), |
115 with_nesting_count_(0) { | 115 with_nesting_count_(0) { |
116 *variable = this; | 116 *variable = this; |
(...skipping 10 matching lines...) Expand all Loading... |
127 | 127 |
128 private: | 128 private: |
129 Scope** const variable_; | 129 Scope** const variable_; |
130 Scope* const prev_; | 130 Scope* const prev_; |
131 const ScopeType type_; | 131 const ScopeType type_; |
132 int materialized_literal_count_; | 132 int materialized_literal_count_; |
133 int expected_properties_; | 133 int expected_properties_; |
134 int with_nesting_count_; | 134 int with_nesting_count_; |
135 }; | 135 }; |
136 | 136 |
137 // Types that allow us to recognize simple this-property assignments. | 137 // Private constructor only used in PreParseProgram. |
138 // A simple this-property assignment is a statement on the form | 138 PreParser(i::JavaScriptScanner* scanner, |
139 // "this.propertyName = {primitive constant or function parameter name);" | 139 i::ParserRecorder* log, |
140 // where propertyName isn't "__proto__". | 140 uintptr_t stack_limit, |
141 // The result is only relevant if the function body contains only | 141 bool allow_lazy) |
142 // simple this-property assignments. | 142 : scanner_(scanner), |
| 143 log_(log), |
| 144 scope_(NULL), |
| 145 stack_limit_(stack_limit), |
| 146 stack_overflow_(false), |
| 147 allow_lazy_(true) { } |
| 148 |
| 149 // Preparse the program. Only called in PreParseProgram after creating |
| 150 // the instance. |
| 151 PreParseResult PreParse() { |
| 152 Scope top_scope(&scope_, kTopLevelScope); |
| 153 bool ok = true; |
| 154 ParseSourceElements(i::Token::EOS, &ok); |
| 155 if (stack_overflow_) return kPreParseStackOverflow; |
| 156 if (!ok) { |
| 157 ReportUnexpectedToken(scanner_->current_token()); |
| 158 } |
| 159 return kPreParseSuccess; |
| 160 } |
143 | 161 |
144 // Report syntax error | 162 // Report syntax error |
145 void ReportUnexpectedToken(i::Token::Value token); | 163 void ReportUnexpectedToken(i::Token::Value token); |
146 void ReportMessageAt(int start_pos, | 164 void ReportMessageAt(int start_pos, |
147 int end_pos, | 165 int end_pos, |
148 const char* type, | 166 const char* type, |
149 const char* name_opt) { | 167 const char* name_opt) { |
150 log_->LogMessage(start_pos, end_pos, type, name_opt); | 168 log_->LogMessage(start_pos, end_pos, type, name_opt); |
151 } | 169 } |
152 | 170 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 Expression ParseFunctionLiteral(bool* ok); | 213 Expression ParseFunctionLiteral(bool* ok); |
196 | 214 |
197 Identifier ParseIdentifier(bool* ok); | 215 Identifier ParseIdentifier(bool* ok); |
198 Identifier ParseIdentifierName(bool* ok); | 216 Identifier ParseIdentifierName(bool* ok); |
199 Identifier ParseIdentifierOrGetOrSet(bool* is_get, bool* is_set, bool* ok); | 217 Identifier ParseIdentifierOrGetOrSet(bool* is_get, bool* is_set, bool* ok); |
200 | 218 |
201 Identifier GetIdentifierSymbol(); | 219 Identifier GetIdentifierSymbol(); |
202 unsigned int HexDigitValue(char digit); | 220 unsigned int HexDigitValue(char digit); |
203 Expression GetStringSymbol(); | 221 Expression GetStringSymbol(); |
204 | 222 |
205 | 223 i::Token::Value peek() { |
206 i::Token::Value peek() { return scanner_->peek(); } | 224 if (stack_overflow_) return i::Token::ILLEGAL; |
207 i::Token::Value Next() { | 225 return scanner_->peek(); |
208 i::Token::Value next = scanner_->Next(); | |
209 return next; | |
210 } | 226 } |
211 | 227 |
212 void Consume(i::Token::Value token) { | 228 i::Token::Value Next() { |
213 Next(); | 229 if (stack_overflow_) return i::Token::ILLEGAL; |
| 230 { |
| 231 int marker; |
| 232 if (reinterpret_cast<uintptr_t>(&marker) < stack_limit_) { |
| 233 // Further calls to peek/Next will return illegal token. |
| 234 // The current one will still be returned. It might already |
| 235 // have been seen using peek. |
| 236 stack_overflow_ = true; |
| 237 } |
| 238 } |
| 239 return scanner_->Next(); |
214 } | 240 } |
215 | 241 |
| 242 void Consume(i::Token::Value token) { Next(); } |
| 243 |
216 void Expect(i::Token::Value token, bool* ok) { | 244 void Expect(i::Token::Value token, bool* ok) { |
217 if (Next() != token) { | 245 if (Next() != token) { |
218 *ok = false; | 246 *ok = false; |
219 } | 247 } |
220 } | 248 } |
221 | 249 |
222 bool Check(i::Token::Value token) { | 250 bool Check(i::Token::Value token) { |
223 i::Token::Value next = peek(); | 251 i::Token::Value next = peek(); |
224 if (next == token) { | 252 if (next == token) { |
225 Consume(next); | 253 Consume(next); |
226 return true; | 254 return true; |
227 } | 255 } |
228 return false; | 256 return false; |
229 } | 257 } |
230 void ExpectSemicolon(bool* ok); | 258 void ExpectSemicolon(bool* ok); |
231 | 259 |
232 static int Precedence(i::Token::Value tok, bool accept_IN); | 260 static int Precedence(i::Token::Value tok, bool accept_IN); |
233 | 261 |
234 i::JavaScriptScanner* scanner_; | 262 i::JavaScriptScanner* scanner_; |
235 i::ParserRecorder* log_; | 263 i::ParserRecorder* log_; |
236 Scope* scope_; | 264 Scope* scope_; |
| 265 uintptr_t stack_limit_; |
| 266 bool stack_overflow_; |
237 bool allow_lazy_; | 267 bool allow_lazy_; |
238 }; | 268 }; |
239 } } // v8::preparser | 269 } } // v8::preparser |
240 | 270 |
241 #endif // V8_PREPARSER_H | 271 #endif // V8_PREPARSER_H |
OLD | NEW |