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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned))); | 170 reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned))); |
171 } | 171 } |
172 | 172 |
173 // Read strings written by ParserRecorder::WriteString. | 173 // Read strings written by ParserRecorder::WriteString. |
174 static const char* ReadString(unsigned* start, int* chars); | 174 static const char* ReadString(unsigned* start, int* chars); |
175 | 175 |
176 friend class ScriptData; | 176 friend class ScriptData; |
177 }; | 177 }; |
178 | 178 |
179 | 179 |
| 180 class ParserLog BASE_EMBEDDED { |
| 181 public: |
| 182 virtual ~ParserLog() { } |
| 183 |
| 184 // Records the occurrence of a function. |
| 185 virtual void LogFunction(int start, int end, int literals, int properties) {} |
| 186 // Records the occurrence of a symbol in the source. The vector holds the |
| 187 // UTF-8 encoded symbol content. |
| 188 virtual void LogSymbol(int start, Vector<const char> symbol) {} |
| 189 // Records the occurrence of a symbol in the source. The symbol pointer |
| 190 // points to the UTF-8 encoded symbol content. |
| 191 virtual void LogSymbol(int start, const char* symbol, int length) {} |
| 192 // Return the current position in the function entry log. |
| 193 virtual int function_position() { return 0; } |
| 194 // Return the current position in the symbol entry log. |
| 195 // Notice: Functions and symbols are currently logged separately. |
| 196 virtual int symbol_position() { return 0; } |
| 197 // Return the number of distinct symbols logged. |
| 198 virtual int symbol_ids() { return 0; } |
| 199 // Pauses recording. The Log-functions above will do nothing during pausing. |
| 200 // Pauses can be nested. |
| 201 virtual void PauseRecording() {} |
| 202 // Ends a recording pause. |
| 203 virtual void ResumeRecording() {} |
| 204 // Extracts a representation of the logged data that can be used by |
| 205 // ScriptData. |
| 206 virtual Vector<unsigned> ExtractData() { |
| 207 return Vector<unsigned>(); |
| 208 }; |
| 209 }; |
| 210 |
| 211 |
| 212 // Record only functions. |
| 213 class PartialParserRecorder: public ParserLog { |
| 214 public: |
| 215 PartialParserRecorder(); |
| 216 |
| 217 virtual void LogFunction(int start, int end, int literals, int properties) { |
| 218 function_store_.Add(start); |
| 219 function_store_.Add(end); |
| 220 function_store_.Add(literals); |
| 221 function_store_.Add(properties); |
| 222 } |
| 223 |
| 224 // Logs an error message and marks the log as containing an error. |
| 225 // Further logging will be ignored, and ExtractData will return a vector |
| 226 // representing the error only. |
| 227 void LogMessage(int start, |
| 228 int end, |
| 229 const char* message, |
| 230 const char* argument_opt) { |
| 231 Scanner::Location location(start, end); |
| 232 Vector<const char*> arguments; |
| 233 if (argument_opt != NULL) { |
| 234 arguments = Vector<const char*>(&argument_opt, 1); |
| 235 } |
| 236 this->LogMessage(location, message, arguments); |
| 237 } |
| 238 |
| 239 virtual int function_position() { return function_store_.size(); } |
| 240 |
| 241 virtual void LogMessage(Scanner::Location loc, |
| 242 const char* message, |
| 243 Vector<const char*> args); |
| 244 |
| 245 virtual Vector<unsigned> ExtractData(); |
| 246 |
| 247 virtual void PauseRecording() { |
| 248 pause_count_++; |
| 249 is_recording_ = false; |
| 250 } |
| 251 |
| 252 virtual void ResumeRecording() { |
| 253 ASSERT(pause_count_ > 0); |
| 254 if (--pause_count_ == 0) is_recording_ = !has_error(); |
| 255 } |
| 256 |
| 257 protected: |
| 258 bool has_error() { |
| 259 return static_cast<bool>(preamble_[ScriptDataImpl::kHasErrorOffset]); |
| 260 } |
| 261 |
| 262 bool is_recording() { |
| 263 return is_recording_; |
| 264 } |
| 265 |
| 266 void WriteString(Vector<const char> str); |
| 267 |
| 268 Collector<unsigned> function_store_; |
| 269 unsigned preamble_[ScriptDataImpl::kHeaderSize]; |
| 270 bool is_recording_; |
| 271 int pause_count_; |
| 272 |
| 273 #ifdef DEBUG |
| 274 int prev_start_; |
| 275 #endif |
| 276 }; |
| 277 |
| 278 |
| 279 // Record both functions and symbols. |
| 280 class CompleteParserRecorder: public PartialParserRecorder { |
| 281 public: |
| 282 CompleteParserRecorder(); |
| 283 |
| 284 virtual void LogSymbol(int start, Vector<const char> literal); |
| 285 |
| 286 virtual void LogSymbol(int start, const char* symbol, int length) { |
| 287 LogSymbol(start, Vector<const char>(symbol, length)); |
| 288 } |
| 289 |
| 290 virtual Vector<unsigned> ExtractData(); |
| 291 |
| 292 virtual int symbol_position() { return symbol_store_.size(); } |
| 293 virtual int symbol_ids() { return symbol_id_; } |
| 294 |
| 295 private: |
| 296 static int vector_hash(Vector<const char> string) { |
| 297 int hash = 0; |
| 298 for (int i = 0; i < string.length(); i++) { |
| 299 int c = string[i]; |
| 300 hash += c; |
| 301 hash += (hash << 10); |
| 302 hash ^= (hash >> 6); |
| 303 } |
| 304 return hash; |
| 305 } |
| 306 |
| 307 static bool vector_compare(void* a, void* b) { |
| 308 Vector<const char>* string1 = reinterpret_cast<Vector<const char>* >(a); |
| 309 Vector<const char>* string2 = reinterpret_cast<Vector<const char>* >(b); |
| 310 int length = string1->length(); |
| 311 if (string2->length() != length) return false; |
| 312 return memcmp(string1->start(), string2->start(), length) == 0; |
| 313 } |
| 314 |
| 315 // Write a non-negative number to the symbol store. |
| 316 void WriteNumber(int number); |
| 317 |
| 318 Collector<byte> symbol_store_; |
| 319 Collector<Vector<const char> > symbol_entries_; |
| 320 HashMap symbol_table_; |
| 321 int symbol_id_; |
| 322 }; |
| 323 |
| 324 |
| 325 |
180 class ParserApi { | 326 class ParserApi { |
181 public: | 327 public: |
182 // Parses the source code represented by the compilation info and sets its | 328 // Parses the source code represented by the compilation info and sets its |
183 // function literal. Returns false (and deallocates any allocated AST | 329 // function literal. Returns false (and deallocates any allocated AST |
184 // nodes) if parsing failed. | 330 // nodes) if parsing failed. |
185 static bool Parse(CompilationInfo* info); | 331 static bool Parse(CompilationInfo* info); |
186 | 332 |
187 // Generic preparser generating full preparse data. | 333 // Generic preparser generating full preparse data. |
188 static ScriptDataImpl* PreParse(Handle<String> source, | 334 static ScriptDataImpl* PreParse(Handle<String> source, |
189 unibrow::CharacterStream* stream, | 335 unibrow::CharacterStream* stream, |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 }; | 573 }; |
428 | 574 |
429 | 575 |
430 class Parser { | 576 class Parser { |
431 public: | 577 public: |
432 Parser(Handle<Script> script, bool allow_natives_syntax, | 578 Parser(Handle<Script> script, bool allow_natives_syntax, |
433 v8::Extension* extension, ParserMode is_pre_parsing, | 579 v8::Extension* extension, ParserMode is_pre_parsing, |
434 ParserFactory* factory, ParserLog* log, ScriptDataImpl* pre_data); | 580 ParserFactory* factory, ParserLog* log, ScriptDataImpl* pre_data); |
435 virtual ~Parser() { } | 581 virtual ~Parser() { } |
436 | 582 |
437 // Pre-parse the program from the character stream; returns true on | |
438 // success, false if a stack-overflow happened during parsing. | |
439 bool PreParseProgram(Handle<String> source, unibrow::CharacterStream* stream); | |
440 | |
441 void ReportMessage(const char* message, Vector<const char*> args); | 583 void ReportMessage(const char* message, Vector<const char*> args); |
442 virtual void ReportMessageAt(Scanner::Location loc, | 584 virtual void ReportMessageAt(Scanner::Location loc, |
443 const char* message, | 585 const char* message, |
444 Vector<const char*> args) = 0; | 586 Vector<const char*> args) = 0; |
445 | 587 |
446 | 588 |
447 // Returns NULL if parsing failed. | 589 // Returns NULL if parsing failed. |
448 FunctionLiteral* ParseProgram(Handle<String> source, | 590 FunctionLiteral* ParseProgram(Handle<String> source, |
449 bool in_global_context); | 591 bool in_global_context); |
450 FunctionLiteral* ParseLazy(Handle<SharedFunctionInfo> info); | 592 FunctionLiteral* ParseLazy(Handle<SharedFunctionInfo> info); |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 // return a null handle. Primarily for readability. | 854 // return a null handle. Primarily for readability. |
713 Handle<Object> ReportUnexpectedToken() { return Handle<Object>::null(); } | 855 Handle<Object> ReportUnexpectedToken() { return Handle<Object>::null(); } |
714 // Converts the currently parsed literal to a JavaScript String. | 856 // Converts the currently parsed literal to a JavaScript String. |
715 Handle<String> GetString(); | 857 Handle<String> GetString(); |
716 | 858 |
717 Scanner scanner_; | 859 Scanner scanner_; |
718 }; | 860 }; |
719 } } // namespace v8::internal | 861 } } // namespace v8::internal |
720 | 862 |
721 #endif // V8_PARSER_H_ | 863 #endif // V8_PARSER_H_ |
OLD | NEW |