OLD | NEW |
(Empty) | |
| 1 // Copyright 2006-2009 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 #include <stdlib.h> |
| 29 |
| 30 #include "v8.h" |
| 31 |
| 32 #include "token.h" |
| 33 #include "scanner.h" |
| 34 |
| 35 #include "cctest.h" |
| 36 |
| 37 namespace i = ::v8::internal; |
| 38 |
| 39 TEST(KeywordMatcher) { |
| 40 struct KeywordToken { |
| 41 const char* keyword; |
| 42 i::Token::Value token; |
| 43 }; |
| 44 |
| 45 static const KeywordToken keywords[] = { |
| 46 #define KEYWORD(t, s, d) { s, i::Token::t }, |
| 47 #define IGNORE(t, s, d) /* */ |
| 48 TOKEN_LIST(IGNORE, KEYWORD, IGNORE) |
| 49 #undef KEYWORD |
| 50 { NULL, i::Token::IDENTIFIER } |
| 51 }; |
| 52 |
| 53 static const char* future_keywords[] = { |
| 54 #define FUTURE(t, s, d) s, |
| 55 TOKEN_LIST(IGNORE, IGNORE, FUTURE) |
| 56 #undef FUTURE |
| 57 #undef IGNORE |
| 58 NULL |
| 59 }; |
| 60 |
| 61 KeywordToken key_token; |
| 62 for (int i = 0; (key_token = keywords[i]).keyword != NULL; i++) { |
| 63 i::KeywordMatcher matcher; |
| 64 const char* keyword = key_token.keyword; |
| 65 int length = strlen(keyword); |
| 66 for (int j = 0; j < length; j++) { |
| 67 if (key_token.token == i::Token::INSTANCEOF && j == 2) { |
| 68 // "in" is a prefix of "instanceof". It's the only keyword |
| 69 // that is a prefix of another. |
| 70 CHECK_EQ(i::Token::IN, matcher.token()); |
| 71 } else { |
| 72 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); |
| 73 } |
| 74 matcher.AddChar(keyword[j]); |
| 75 } |
| 76 CHECK_EQ(key_token.token, matcher.token()); |
| 77 // Adding more characters will make keyword matching fail. |
| 78 matcher.AddChar('z'); |
| 79 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); |
| 80 // Adding a keyword later will not make it match again. |
| 81 matcher.AddChar('i'); |
| 82 matcher.AddChar('f'); |
| 83 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); |
| 84 } |
| 85 |
| 86 // Future keywords are not recognized. |
| 87 const char* future_keyword; |
| 88 for (int i = 0; (future_keyword = future_keywords[i]) != NULL; i++) { |
| 89 i::KeywordMatcher matcher; |
| 90 int length = strlen(future_keyword); |
| 91 for (int j = 0; j < length; j++) { |
| 92 matcher.AddChar(future_keyword[j]); |
| 93 } |
| 94 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); |
| 95 } |
| 96 |
| 97 // Zero isn't ignored at first. |
| 98 i::KeywordMatcher bad_start; |
| 99 bad_start.AddChar(0); |
| 100 CHECK_EQ(i::Token::IDENTIFIER, bad_start.token()); |
| 101 bad_start.AddChar('i'); |
| 102 bad_start.AddChar('f'); |
| 103 CHECK_EQ(i::Token::IDENTIFIER, bad_start.token()); |
| 104 |
| 105 // Zero isn't ignored at end. |
| 106 i::KeywordMatcher bad_end; |
| 107 bad_end.AddChar('i'); |
| 108 bad_end.AddChar('f'); |
| 109 CHECK_EQ(i::Token::IF, bad_end.token()); |
| 110 bad_end.AddChar(0); |
| 111 CHECK_EQ(i::Token::IDENTIFIER, bad_end.token()); |
| 112 |
| 113 // Case isn't ignored. |
| 114 i::KeywordMatcher bad_case; |
| 115 bad_case.AddChar('i'); |
| 116 bad_case.AddChar('F'); |
| 117 CHECK_EQ(i::Token::IDENTIFIER, bad_case.token()); |
| 118 |
| 119 // If we mark it as failure, continuing won't help. |
| 120 i::KeywordMatcher full_stop; |
| 121 full_stop.AddChar('i'); |
| 122 CHECK_EQ(i::Token::IDENTIFIER, full_stop.token()); |
| 123 full_stop.Fail(); |
| 124 CHECK_EQ(i::Token::IDENTIFIER, full_stop.token()); |
| 125 full_stop.AddChar('f'); |
| 126 CHECK_EQ(i::Token::IDENTIFIER, full_stop.token()); |
| 127 } |
| 128 |
OLD | NEW |