OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 class PreParser { | 112 class PreParser { |
113 public: | 113 public: |
114 enum PreParseResult { | 114 enum PreParseResult { |
115 kPreParseStackOverflow, | 115 kPreParseStackOverflow, |
116 kPreParseSuccess | 116 kPreParseSuccess |
117 }; | 117 }; |
118 | 118 |
119 | 119 |
120 PreParser(i::Scanner* scanner, | 120 PreParser(i::Scanner* scanner, |
121 i::ParserRecorder* log, | 121 i::ParserRecorder* log, |
122 uintptr_t stack_limit, | 122 uintptr_t stack_limit) |
123 bool allow_lazy, | |
124 bool allow_natives_syntax, | |
125 bool allow_modules, | |
126 bool allow_generators) | |
127 : scanner_(scanner), | 123 : scanner_(scanner), |
128 log_(log), | 124 log_(log), |
129 scope_(NULL), | 125 scope_(NULL), |
130 stack_limit_(stack_limit), | 126 stack_limit_(stack_limit), |
131 strict_mode_violation_location_(i::Scanner::Location::invalid()), | 127 strict_mode_violation_location_(i::Scanner::Location::invalid()), |
132 strict_mode_violation_type_(NULL), | 128 strict_mode_violation_type_(NULL), |
133 stack_overflow_(false), | 129 stack_overflow_(false), |
134 allow_lazy_(allow_lazy), | 130 allow_lazy_(false), |
135 allow_modules_(allow_modules), | 131 allow_natives_syntax_(false), |
136 allow_natives_syntax_(allow_natives_syntax), | 132 allow_generators_(false), |
137 allow_generators_(allow_generators), | 133 parenthesized_function_(false) { } |
138 parenthesized_function_(false), | |
139 harmony_scoping_(scanner->HarmonyScoping()) { } | |
140 | 134 |
141 ~PreParser() {} | 135 ~PreParser() {} |
142 | 136 |
| 137 bool allow_natives_syntax() const { return allow_natives_syntax_; } |
| 138 bool allow_lazy() const { return allow_lazy_; } |
| 139 bool allow_modules() const { return scanner_->HarmonyModules(); } |
| 140 bool allow_harmony_scoping() const { return scanner_->HarmonyScoping(); } |
| 141 bool allow_generators() const { return allow_generators_; } |
| 142 |
| 143 void set_allow_natives_syntax(bool allow) { allow_natives_syntax_ = allow; } |
| 144 void set_allow_lazy(bool allow) { allow_lazy_ = allow; } |
| 145 void set_allow_modules(bool allow) { scanner_->SetHarmonyModules(allow); } |
| 146 void set_allow_harmony_scoping(bool allow) { |
| 147 scanner_->SetHarmonyScoping(allow); |
| 148 } |
| 149 void set_allow_generators(bool allow) { allow_generators_ = allow; } |
| 150 |
143 // Pre-parse the program from the character stream; returns true on | 151 // Pre-parse the program from the character stream; returns true on |
144 // success (even if parsing failed, the pre-parse data successfully | 152 // success (even if parsing failed, the pre-parse data successfully |
145 // captured the syntax error), and false if a stack-overflow happened | 153 // captured the syntax error), and false if a stack-overflow happened |
146 // during parsing. | 154 // during parsing. |
147 static PreParseResult PreParseProgram(i::Scanner* scanner, | 155 PreParseResult PreParseProgram() { |
148 i::ParserRecorder* log, | 156 Scope top_scope(&scope_, kTopLevelScope); |
149 int flags, | 157 bool ok = true; |
150 uintptr_t stack_limit) { | 158 int start_position = scanner_->peek_location().beg_pos; |
151 bool allow_lazy = (flags & i::kAllowLazy) != 0; | 159 ParseSourceElements(i::Token::EOS, &ok); |
152 bool allow_natives_syntax = (flags & i::kAllowNativesSyntax) != 0; | 160 if (stack_overflow_) return kPreParseStackOverflow; |
153 bool allow_modules = (flags & i::kAllowModules) != 0; | 161 if (!ok) { |
154 bool allow_generators = (flags & i::kAllowGenerators) != 0; | 162 ReportUnexpectedToken(scanner_->current_token()); |
155 return PreParser(scanner, log, stack_limit, allow_lazy, | 163 } else if (!scope_->is_classic_mode()) { |
156 allow_natives_syntax, allow_modules, | 164 CheckOctalLiteral(start_position, scanner_->location().end_pos, &ok); |
157 allow_generators).PreParse(); | 165 } |
| 166 return kPreParseSuccess; |
158 } | 167 } |
159 | 168 |
160 // Parses a single function literal, from the opening parentheses before | 169 // Parses a single function literal, from the opening parentheses before |
161 // parameters to the closing brace after the body. | 170 // parameters to the closing brace after the body. |
162 // Returns a FunctionEntry describing the body of the function in enough | 171 // Returns a FunctionEntry describing the body of the function in enough |
163 // detail that it can be lazily compiled. | 172 // detail that it can be lazily compiled. |
164 // The scanner is expected to have matched the "function" or "function*" | 173 // The scanner is expected to have matched the "function" or "function*" |
165 // keyword and parameters, and have consumed the initial '{'. | 174 // keyword and parameters, and have consumed the initial '{'. |
166 // At return, unless an error occurred, the scanner is positioned before the | 175 // At return, unless an error occurred, the scanner is positioned before the |
167 // the final '}'. | 176 // the final '}'. |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 Scope** const variable_; | 516 Scope** const variable_; |
508 Scope* const prev_; | 517 Scope* const prev_; |
509 const ScopeType type_; | 518 const ScopeType type_; |
510 int materialized_literal_count_; | 519 int materialized_literal_count_; |
511 int expected_properties_; | 520 int expected_properties_; |
512 int with_nesting_count_; | 521 int with_nesting_count_; |
513 i::LanguageMode language_mode_; | 522 i::LanguageMode language_mode_; |
514 bool is_generator_; | 523 bool is_generator_; |
515 }; | 524 }; |
516 | 525 |
517 // Preparse the program. Only called in PreParseProgram after creating | |
518 // the instance. | |
519 PreParseResult PreParse() { | |
520 Scope top_scope(&scope_, kTopLevelScope); | |
521 bool ok = true; | |
522 int start_position = scanner_->peek_location().beg_pos; | |
523 ParseSourceElements(i::Token::EOS, &ok); | |
524 if (stack_overflow_) return kPreParseStackOverflow; | |
525 if (!ok) { | |
526 ReportUnexpectedToken(scanner_->current_token()); | |
527 } else if (!scope_->is_classic_mode()) { | |
528 CheckOctalLiteral(start_position, scanner_->location().end_pos, &ok); | |
529 } | |
530 return kPreParseSuccess; | |
531 } | |
532 | |
533 // Report syntax error | 526 // Report syntax error |
534 void ReportUnexpectedToken(i::Token::Value token); | 527 void ReportUnexpectedToken(i::Token::Value token); |
535 void ReportMessageAt(i::Scanner::Location location, | 528 void ReportMessageAt(i::Scanner::Location location, |
536 const char* type, | 529 const char* type, |
537 const char* name_opt) { | 530 const char* name_opt) { |
538 log_->LogMessage(location.beg_pos, location.end_pos, type, name_opt); | 531 log_->LogMessage(location.beg_pos, location.end_pos, type, name_opt); |
539 } | 532 } |
540 void ReportMessageAt(int start_pos, | 533 void ReportMessageAt(int start_pos, |
541 int end_pos, | 534 int end_pos, |
542 const char* type, | 535 const char* type, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 bool* ok); | 669 bool* ok); |
677 | 670 |
678 i::Scanner* scanner_; | 671 i::Scanner* scanner_; |
679 i::ParserRecorder* log_; | 672 i::ParserRecorder* log_; |
680 Scope* scope_; | 673 Scope* scope_; |
681 uintptr_t stack_limit_; | 674 uintptr_t stack_limit_; |
682 i::Scanner::Location strict_mode_violation_location_; | 675 i::Scanner::Location strict_mode_violation_location_; |
683 const char* strict_mode_violation_type_; | 676 const char* strict_mode_violation_type_; |
684 bool stack_overflow_; | 677 bool stack_overflow_; |
685 bool allow_lazy_; | 678 bool allow_lazy_; |
686 bool allow_modules_; | |
687 bool allow_natives_syntax_; | 679 bool allow_natives_syntax_; |
688 bool allow_generators_; | 680 bool allow_generators_; |
689 bool parenthesized_function_; | 681 bool parenthesized_function_; |
690 bool harmony_scoping_; | |
691 }; | 682 }; |
692 } } // v8::preparser | 683 } } // v8::preparser |
693 | 684 |
694 #endif // V8_PREPARSER_H | 685 #endif // V8_PREPARSER_H |
OLD | NEW |