Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 // Features shared by parsing and pre-parsing scanners. | |
| 29 | |
| 30 #ifndef V8_SCANNER_BASE_H_ | |
| 31 #define V8_SCANNER_BASE_H_ | |
| 32 | |
| 33 #include "token.h" | |
| 34 #include "unicode.h" | |
| 35 | |
| 36 namespace v8 { | |
| 37 namespace internal { | |
| 38 | |
| 39 class KeywordMatcher { | |
| 40 // Incrementally recognize keywords. | |
| 41 // | |
| 42 // Recognized keywords: | |
| 43 // break case catch const* continue debugger* default delete do else | |
| 44 // finally false for function if in instanceof native* new null | |
| 45 // return switch this throw true try typeof var void while with | |
| 46 // | |
| 47 // *: Actually "future reserved keywords". These are the only ones we | |
| 48 // recognized, the remaining are allowed as identifiers. | |
| 49 public: | |
| 50 KeywordMatcher() | |
| 51 : state_(INITIAL), | |
| 52 token_(Token::IDENTIFIER), | |
| 53 keyword_(NULL), | |
| 54 counter_(0), | |
| 55 keyword_token_(Token::ILLEGAL) {} | |
| 56 | |
| 57 Token::Value token() { return token_; } | |
| 58 | |
| 59 inline void AddChar(unibrow::uchar input) { | |
| 60 if (state_ != UNMATCHABLE) { | |
| 61 Step(input); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 void Fail() { | |
| 66 token_ = Token::IDENTIFIER; | |
| 67 state_ = UNMATCHABLE; | |
| 68 } | |
| 69 | |
| 70 private: | |
| 71 enum State { | |
| 72 UNMATCHABLE, | |
| 73 INITIAL, | |
| 74 KEYWORD_PREFIX, | |
| 75 KEYWORD_MATCHED, | |
| 76 C, | |
| 77 CA, | |
| 78 CO, | |
| 79 CON, | |
| 80 D, | |
| 81 DE, | |
| 82 F, | |
| 83 I, | |
| 84 IN, | |
| 85 N, | |
| 86 T, | |
| 87 TH, | |
| 88 TR, | |
| 89 V, | |
| 90 W | |
| 91 }; | |
| 92 | |
| 93 struct FirstState { | |
| 94 const char* keyword; | |
| 95 State state; | |
| 96 Token::Value token; | |
| 97 }; | |
| 98 | |
| 99 // Range of possible first characters of a keyword. | |
| 100 static const unsigned int kFirstCharRangeMin = 'b'; | |
| 101 static const unsigned int kFirstCharRangeMax = 'w'; | |
| 102 static const unsigned int kFirstCharRangeLength = | |
| 103 kFirstCharRangeMax - kFirstCharRangeMin + 1; | |
| 104 // State map for first keyword character range. | |
| 105 static FirstState first_states_[kFirstCharRangeLength]; | |
| 106 | |
| 107 // If input equals keyword's character at position, continue matching keyword | |
| 108 // from that position. | |
| 109 inline bool MatchKeywordStart(unibrow::uchar input, | |
| 110 const char* keyword, | |
| 111 int position, | |
| 112 Token::Value token_if_match) { | |
| 113 if (input == static_cast<unibrow::uchar>(keyword[position])) { | |
| 114 state_ = KEYWORD_PREFIX; | |
| 115 this->keyword_ = keyword; | |
| 116 this->counter_ = position + 1; | |
| 117 this->keyword_token_ = token_if_match; | |
| 118 return true; | |
| 119 } | |
| 120 return false; | |
| 121 } | |
| 122 | |
| 123 // If input equals match character, transition to new state and return true. | |
| 124 inline bool MatchState(unibrow::uchar input, char match, State new_state) { | |
| 125 if (input == static_cast<unibrow::uchar>(match)) { | |
| 126 state_ = new_state; | |
| 127 return true; | |
| 128 } | |
| 129 return false; | |
| 130 } | |
| 131 | |
| 132 inline bool MatchKeyword(unibrow::uchar input, | |
| 133 char match, | |
| 134 State new_state, | |
| 135 Token::Value keyword_token) { | |
| 136 if (input != static_cast<unibrow::uchar>(match)) { | |
| 137 return false; | |
| 138 } | |
| 139 state_ = new_state; | |
| 140 token_ = keyword_token; | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 void Step(unibrow::uchar input); | |
| 145 | |
| 146 // Current state. | |
| 147 State state_; | |
| 148 // Token for currently added characters. | |
| 149 Token::Value token_; | |
| 150 | |
| 151 // Matching a specific keyword string (there is only one possible valid | |
| 152 // keyword with the current prefix). | |
| 153 const char* keyword_; | |
| 154 int counter_; | |
| 155 Token::Value keyword_token_; | |
| 156 }; | |
| 157 | |
| 158 | |
| 159 | |
|
Mads Ager (chromium)
2010/11/05 13:29:27
Excessive spacing.
| |
| 160 | |
| 161 | |
| 162 | |
| 163 } } // namespace v8::internal | |
| 164 | |
| 165 #endif // V8_SCANNER_BASE_H_ | |
| OLD | NEW |