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_PREPARSER_H | 5 #ifndef V8_PARSING_PREPARSER_H |
6 #define V8_PARSING_PREPARSER_H | 6 #define V8_PARSING_PREPARSER_H |
7 | 7 |
8 #include "src/ast/scopes.h" | 8 #include "src/ast/scopes.h" |
9 #include "src/bailout-reason.h" | 9 #include "src/bailout-reason.h" |
10 #include "src/hashmap.h" | 10 #include "src/hashmap.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 } | 48 } |
49 static PreParserIdentifier Yield() { | 49 static PreParserIdentifier Yield() { |
50 return PreParserIdentifier(kYieldIdentifier); | 50 return PreParserIdentifier(kYieldIdentifier); |
51 } | 51 } |
52 static PreParserIdentifier Prototype() { | 52 static PreParserIdentifier Prototype() { |
53 return PreParserIdentifier(kPrototypeIdentifier); | 53 return PreParserIdentifier(kPrototypeIdentifier); |
54 } | 54 } |
55 static PreParserIdentifier Constructor() { | 55 static PreParserIdentifier Constructor() { |
56 return PreParserIdentifier(kConstructorIdentifier); | 56 return PreParserIdentifier(kConstructorIdentifier); |
57 } | 57 } |
| 58 static PreParserIdentifier Enum() { |
| 59 return PreParserIdentifier(kEnumIdentifier); |
| 60 } |
| 61 static PreParserIdentifier Await() { |
| 62 return PreParserIdentifier(kAwaitIdentifier); |
| 63 } |
58 bool IsEval() const { return type_ == kEvalIdentifier; } | 64 bool IsEval() const { return type_ == kEvalIdentifier; } |
59 bool IsArguments() const { return type_ == kArgumentsIdentifier; } | 65 bool IsArguments() const { return type_ == kArgumentsIdentifier; } |
60 bool IsEvalOrArguments() const { return IsEval() || IsArguments(); } | 66 bool IsEvalOrArguments() const { return IsEval() || IsArguments(); } |
61 bool IsUndefined() const { return type_ == kUndefinedIdentifier; } | 67 bool IsUndefined() const { return type_ == kUndefinedIdentifier; } |
62 bool IsLet() const { return type_ == kLetIdentifier; } | 68 bool IsLet() const { return type_ == kLetIdentifier; } |
63 bool IsStatic() const { return type_ == kStaticIdentifier; } | 69 bool IsStatic() const { return type_ == kStaticIdentifier; } |
64 bool IsYield() const { return type_ == kYieldIdentifier; } | 70 bool IsYield() const { return type_ == kYieldIdentifier; } |
65 bool IsPrototype() const { return type_ == kPrototypeIdentifier; } | 71 bool IsPrototype() const { return type_ == kPrototypeIdentifier; } |
66 bool IsConstructor() const { return type_ == kConstructorIdentifier; } | 72 bool IsConstructor() const { return type_ == kConstructorIdentifier; } |
67 bool IsFutureReserved() const { return type_ == kFutureReservedIdentifier; } | 73 bool IsEnum() const { return type_ == kEnumIdentifier; } |
| 74 bool IsAwait() const { return type_ == kAwaitIdentifier; } |
68 bool IsFutureStrictReserved() const { | 75 bool IsFutureStrictReserved() const { |
69 return type_ == kFutureStrictReservedIdentifier || | 76 return type_ == kFutureStrictReservedIdentifier || |
70 type_ == kLetIdentifier || type_ == kStaticIdentifier || | 77 type_ == kLetIdentifier || type_ == kStaticIdentifier || |
71 type_ == kYieldIdentifier; | 78 type_ == kYieldIdentifier; |
72 } | 79 } |
73 | 80 |
74 // Allow identifier->name()[->length()] to work. The preparser | 81 // Allow identifier->name()[->length()] to work. The preparser |
75 // does not need the actual positions/lengths of the identifiers. | 82 // does not need the actual positions/lengths of the identifiers. |
76 const PreParserIdentifier* operator->() const { return this; } | 83 const PreParserIdentifier* operator->() const { return this; } |
77 const PreParserIdentifier raw_name() const { return *this; } | 84 const PreParserIdentifier raw_name() const { return *this; } |
78 | 85 |
79 int position() const { return 0; } | 86 int position() const { return 0; } |
80 int length() const { return 0; } | 87 int length() const { return 0; } |
81 | 88 |
82 private: | 89 private: |
83 enum Type { | 90 enum Type { |
84 kUnknownIdentifier, | 91 kUnknownIdentifier, |
85 kFutureReservedIdentifier, | 92 kFutureReservedIdentifier, |
86 kFutureStrictReservedIdentifier, | 93 kFutureStrictReservedIdentifier, |
87 kLetIdentifier, | 94 kLetIdentifier, |
88 kStaticIdentifier, | 95 kStaticIdentifier, |
89 kYieldIdentifier, | 96 kYieldIdentifier, |
90 kEvalIdentifier, | 97 kEvalIdentifier, |
91 kArgumentsIdentifier, | 98 kArgumentsIdentifier, |
92 kUndefinedIdentifier, | 99 kUndefinedIdentifier, |
93 kPrototypeIdentifier, | 100 kPrototypeIdentifier, |
94 kConstructorIdentifier | 101 kConstructorIdentifier, |
| 102 kEnumIdentifier, |
| 103 kAwaitIdentifier |
95 }; | 104 }; |
96 | 105 |
97 explicit PreParserIdentifier(Type type) : type_(type) {} | 106 explicit PreParserIdentifier(Type type) : type_(type) {} |
98 Type type_; | 107 Type type_; |
99 | 108 |
100 friend class PreParserExpression; | 109 friend class PreParserExpression; |
101 }; | 110 }; |
102 | 111 |
103 | 112 |
104 class PreParserExpression { | 113 class PreParserExpression { |
(...skipping 862 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
967 : ParserBase<PreParserTraits>(zone, scanner, stack_limit, NULL, | 976 : ParserBase<PreParserTraits>(zone, scanner, stack_limit, NULL, |
968 ast_value_factory, log, this), | 977 ast_value_factory, log, this), |
969 use_counts_(nullptr) {} | 978 use_counts_(nullptr) {} |
970 | 979 |
971 // Pre-parse the program from the character stream; returns true on | 980 // Pre-parse the program from the character stream; returns true on |
972 // success (even if parsing failed, the pre-parse data successfully | 981 // success (even if parsing failed, the pre-parse data successfully |
973 // captured the syntax error), and false if a stack-overflow happened | 982 // captured the syntax error), and false if a stack-overflow happened |
974 // during parsing. | 983 // during parsing. |
975 PreParseResult PreParseProgram(int* materialized_literals = 0, | 984 PreParseResult PreParseProgram(int* materialized_literals = 0, |
976 bool is_module = false) { | 985 bool is_module = false) { |
977 Scope* scope = NewScope(scope_, is_module ? MODULE_SCOPE : SCRIPT_SCOPE); | 986 Scope* scope = NewScope(scope_, SCRIPT_SCOPE); |
| 987 |
| 988 // ModuleDeclarationInstantiation for Source Text Module Records creates a |
| 989 // new Module Environment Record whose outer lexical environment record is |
| 990 // the global scope. |
| 991 if (is_module) { |
| 992 scope = NewScope(scope, MODULE_SCOPE); |
| 993 } |
| 994 |
978 PreParserFactory factory(NULL); | 995 PreParserFactory factory(NULL); |
979 FunctionState top_scope(&function_state_, &scope_, scope, kNormalFunction, | 996 FunctionState top_scope(&function_state_, &scope_, scope, kNormalFunction, |
980 &factory); | 997 &factory); |
981 bool ok = true; | 998 bool ok = true; |
982 int start_position = scanner()->peek_location().beg_pos; | 999 int start_position = scanner()->peek_location().beg_pos; |
| 1000 parsing_module_ = is_module; |
983 ParseStatementList(Token::EOS, &ok); | 1001 ParseStatementList(Token::EOS, &ok); |
984 if (stack_overflow()) return kPreParseStackOverflow; | 1002 if (stack_overflow()) return kPreParseStackOverflow; |
985 if (!ok) { | 1003 if (!ok) { |
986 ReportUnexpectedToken(scanner()->current_token()); | 1004 ReportUnexpectedToken(scanner()->current_token()); |
987 } else if (is_strict(scope_->language_mode())) { | 1005 } else if (is_strict(scope_->language_mode())) { |
988 CheckStrictOctalLiteral(start_position, scanner()->location().end_pos, | 1006 CheckStrictOctalLiteral(start_position, scanner()->location().end_pos, |
989 &ok); | 1007 &ok); |
990 } | 1008 } |
991 if (materialized_literals) { | 1009 if (materialized_literals) { |
992 *materialized_literals = function_state_->materialized_literal_count(); | 1010 *materialized_literals = function_state_->materialized_literal_count(); |
993 } | 1011 } |
994 return kPreParseSuccess; | 1012 return kPreParseSuccess; |
995 } | 1013 } |
996 | 1014 |
997 // Parses a single function literal, from the opening parentheses before | 1015 // Parses a single function literal, from the opening parentheses before |
998 // parameters to the closing brace after the body. | 1016 // parameters to the closing brace after the body. |
999 // Returns a FunctionEntry describing the body of the function in enough | 1017 // Returns a FunctionEntry describing the body of the function in enough |
1000 // detail that it can be lazily compiled. | 1018 // detail that it can be lazily compiled. |
1001 // The scanner is expected to have matched the "function" or "function*" | 1019 // The scanner is expected to have matched the "function" or "function*" |
1002 // keyword and parameters, and have consumed the initial '{'. | 1020 // keyword and parameters, and have consumed the initial '{'. |
1003 // At return, unless an error occurred, the scanner is positioned before the | 1021 // At return, unless an error occurred, the scanner is positioned before the |
1004 // the final '}'. | 1022 // the final '}'. |
1005 PreParseResult PreParseLazyFunction( | 1023 PreParseResult PreParseLazyFunction(LanguageMode language_mode, |
1006 LanguageMode language_mode, FunctionKind kind, bool has_simple_parameters, | 1024 FunctionKind kind, |
1007 ParserRecorder* log, Scanner::BookmarkScope* bookmark, int* use_counts); | 1025 bool has_simple_parameters, |
| 1026 bool parsing_module, ParserRecorder* log, |
| 1027 Scanner::BookmarkScope* bookmark, |
| 1028 int* use_counts); |
1008 | 1029 |
1009 private: | 1030 private: |
1010 friend class PreParserTraits; | 1031 friend class PreParserTraits; |
1011 | 1032 |
1012 static const int kLazyParseTrialLimit = 200; | 1033 static const int kLazyParseTrialLimit = 200; |
1013 | 1034 |
1014 // These types form an algebra over syntactic categories that is just | 1035 // These types form an algebra over syntactic categories that is just |
1015 // rich enough to let us recognize and propagate the constructs that | 1036 // rich enough to let us recognize and propagate the constructs that |
1016 // are either being counted in the preparser data, or is important | 1037 // are either being counted in the preparser data, or is important |
1017 // to throw the correct syntax error exceptions. | 1038 // to throw the correct syntax error exceptions. |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1176 const PreParserFormalParameters& parameters, FunctionKind kind, | 1197 const PreParserFormalParameters& parameters, FunctionKind kind, |
1177 FunctionLiteral::FunctionType function_type, bool* ok) { | 1198 FunctionLiteral::FunctionType function_type, bool* ok) { |
1178 return pre_parser_->ParseEagerFunctionBody(function_name, pos, parameters, | 1199 return pre_parser_->ParseEagerFunctionBody(function_name, pos, parameters, |
1179 kind, function_type, ok); | 1200 kind, function_type, ok); |
1180 } | 1201 } |
1181 | 1202 |
1182 } // namespace internal | 1203 } // namespace internal |
1183 } // namespace v8 | 1204 } // namespace v8 |
1184 | 1205 |
1185 #endif // V8_PARSING_PREPARSER_H | 1206 #endif // V8_PARSING_PREPARSER_H |
OLD | NEW |