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 #include "scanner-base.h" |
| 31 |
| 32 namespace v8 { |
| 33 namespace internal { |
| 34 |
| 35 // ---------------------------------------------------------------------------- |
| 36 // Keyword Matcher |
| 37 |
| 38 KeywordMatcher::FirstState KeywordMatcher::first_states_[] = { |
| 39 { "break", KEYWORD_PREFIX, Token::BREAK }, |
| 40 { NULL, C, Token::ILLEGAL }, |
| 41 { NULL, D, Token::ILLEGAL }, |
| 42 { "else", KEYWORD_PREFIX, Token::ELSE }, |
| 43 { NULL, F, Token::ILLEGAL }, |
| 44 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 45 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 46 { NULL, I, Token::ILLEGAL }, |
| 47 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 48 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 49 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 50 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 51 { NULL, N, Token::ILLEGAL }, |
| 52 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 53 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 54 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 55 { "return", KEYWORD_PREFIX, Token::RETURN }, |
| 56 { "switch", KEYWORD_PREFIX, Token::SWITCH }, |
| 57 { NULL, T, Token::ILLEGAL }, |
| 58 { NULL, UNMATCHABLE, Token::ILLEGAL }, |
| 59 { NULL, V, Token::ILLEGAL }, |
| 60 { NULL, W, Token::ILLEGAL } |
| 61 }; |
| 62 |
| 63 |
| 64 void KeywordMatcher::Step(unibrow::uchar input) { |
| 65 switch (state_) { |
| 66 case INITIAL: { |
| 67 // matching the first character is the only state with significant fanout. |
| 68 // Match only lower-case letters in range 'b'..'w'. |
| 69 unsigned int offset = input - kFirstCharRangeMin; |
| 70 if (offset < kFirstCharRangeLength) { |
| 71 state_ = first_states_[offset].state; |
| 72 if (state_ == KEYWORD_PREFIX) { |
| 73 keyword_ = first_states_[offset].keyword; |
| 74 counter_ = 1; |
| 75 keyword_token_ = first_states_[offset].token; |
| 76 } |
| 77 return; |
| 78 } |
| 79 break; |
| 80 } |
| 81 case KEYWORD_PREFIX: |
| 82 if (static_cast<unibrow::uchar>(keyword_[counter_]) == input) { |
| 83 counter_++; |
| 84 if (keyword_[counter_] == '\0') { |
| 85 state_ = KEYWORD_MATCHED; |
| 86 token_ = keyword_token_; |
| 87 } |
| 88 return; |
| 89 } |
| 90 break; |
| 91 case KEYWORD_MATCHED: |
| 92 token_ = Token::IDENTIFIER; |
| 93 break; |
| 94 case C: |
| 95 if (MatchState(input, 'a', CA)) return; |
| 96 if (MatchState(input, 'o', CO)) return; |
| 97 break; |
| 98 case CA: |
| 99 if (MatchKeywordStart(input, "case", 2, Token::CASE)) return; |
| 100 if (MatchKeywordStart(input, "catch", 2, Token::CATCH)) return; |
| 101 break; |
| 102 case CO: |
| 103 if (MatchState(input, 'n', CON)) return; |
| 104 break; |
| 105 case CON: |
| 106 if (MatchKeywordStart(input, "const", 3, Token::CONST)) return; |
| 107 if (MatchKeywordStart(input, "continue", 3, Token::CONTINUE)) return; |
| 108 break; |
| 109 case D: |
| 110 if (MatchState(input, 'e', DE)) return; |
| 111 if (MatchKeyword(input, 'o', KEYWORD_MATCHED, Token::DO)) return; |
| 112 break; |
| 113 case DE: |
| 114 if (MatchKeywordStart(input, "debugger", 2, Token::DEBUGGER)) return; |
| 115 if (MatchKeywordStart(input, "default", 2, Token::DEFAULT)) return; |
| 116 if (MatchKeywordStart(input, "delete", 2, Token::DELETE)) return; |
| 117 break; |
| 118 case F: |
| 119 if (MatchKeywordStart(input, "false", 1, Token::FALSE_LITERAL)) return; |
| 120 if (MatchKeywordStart(input, "finally", 1, Token::FINALLY)) return; |
| 121 if (MatchKeywordStart(input, "for", 1, Token::FOR)) return; |
| 122 if (MatchKeywordStart(input, "function", 1, Token::FUNCTION)) return; |
| 123 break; |
| 124 case I: |
| 125 if (MatchKeyword(input, 'f', KEYWORD_MATCHED, Token::IF)) return; |
| 126 if (MatchKeyword(input, 'n', IN, Token::IN)) return; |
| 127 break; |
| 128 case IN: |
| 129 token_ = Token::IDENTIFIER; |
| 130 if (MatchKeywordStart(input, "instanceof", 2, Token::INSTANCEOF)) { |
| 131 return; |
| 132 } |
| 133 break; |
| 134 case N: |
| 135 if (MatchKeywordStart(input, "native", 1, Token::NATIVE)) return; |
| 136 if (MatchKeywordStart(input, "new", 1, Token::NEW)) return; |
| 137 if (MatchKeywordStart(input, "null", 1, Token::NULL_LITERAL)) return; |
| 138 break; |
| 139 case T: |
| 140 if (MatchState(input, 'h', TH)) return; |
| 141 if (MatchState(input, 'r', TR)) return; |
| 142 if (MatchKeywordStart(input, "typeof", 1, Token::TYPEOF)) return; |
| 143 break; |
| 144 case TH: |
| 145 if (MatchKeywordStart(input, "this", 2, Token::THIS)) return; |
| 146 if (MatchKeywordStart(input, "throw", 2, Token::THROW)) return; |
| 147 break; |
| 148 case TR: |
| 149 if (MatchKeywordStart(input, "true", 2, Token::TRUE_LITERAL)) return; |
| 150 if (MatchKeyword(input, 'y', KEYWORD_MATCHED, Token::TRY)) return; |
| 151 break; |
| 152 case V: |
| 153 if (MatchKeywordStart(input, "var", 1, Token::VAR)) return; |
| 154 if (MatchKeywordStart(input, "void", 1, Token::VOID)) return; |
| 155 break; |
| 156 case W: |
| 157 if (MatchKeywordStart(input, "while", 1, Token::WHILE)) return; |
| 158 if (MatchKeywordStart(input, "with", 1, Token::WITH)) return; |
| 159 break; |
| 160 case UNMATCHABLE: |
| 161 break; |
| 162 } |
| 163 // On fallthrough, it's a failure. |
| 164 state_ = UNMATCHABLE; |
| 165 } |
| 166 |
| 167 } } // namespace v8::internal |
OLD | NEW |