OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 24 matching lines...) Expand all Loading... |
35 #include "token.h" | 35 #include "token.h" |
36 #include "scanner.h" | 36 #include "scanner.h" |
37 #include "parser.h" | 37 #include "parser.h" |
38 #include "utils.h" | 38 #include "utils.h" |
39 #include "execution.h" | 39 #include "execution.h" |
40 #include "preparser.h" | 40 #include "preparser.h" |
41 #include "cctest.h" | 41 #include "cctest.h" |
42 | 42 |
43 namespace i = ::v8::internal; | 43 namespace i = ::v8::internal; |
44 | 44 |
45 TEST(KeywordMatcher) { | 45 TEST(ScanKeywords) { |
46 struct KeywordToken { | 46 struct KeywordToken { |
47 const char* keyword; | 47 const char* keyword; |
48 i::Token::Value token; | 48 i::Token::Value token; |
49 }; | 49 }; |
50 | 50 |
51 static const KeywordToken keywords[] = { | 51 static const KeywordToken keywords[] = { |
52 #define KEYWORD(t, s, d) { s, i::Token::t }, | 52 #define KEYWORD(t, s, d) { s, i::Token::t }, |
53 #define IGNORE(t, s, d) /* */ | 53 TOKEN_LIST(IGNORE_TOKEN, KEYWORD) |
54 TOKEN_LIST(IGNORE, KEYWORD, IGNORE) | |
55 #undef KEYWORD | 54 #undef KEYWORD |
56 { NULL, i::Token::IDENTIFIER } | 55 { NULL, i::Token::IDENTIFIER } |
57 }; | 56 }; |
58 | 57 |
59 static const char* future_keywords[] = { | |
60 #define FUTURE(t, s, d) s, | |
61 TOKEN_LIST(IGNORE, IGNORE, FUTURE) | |
62 #undef FUTURE | |
63 #undef IGNORE | |
64 NULL | |
65 }; | |
66 | |
67 KeywordToken key_token; | 58 KeywordToken key_token; |
| 59 i::UnicodeCache unicode_cache; |
| 60 i::byte buffer[32]; |
68 for (int i = 0; (key_token = keywords[i]).keyword != NULL; i++) { | 61 for (int i = 0; (key_token = keywords[i]).keyword != NULL; i++) { |
69 i::KeywordMatcher matcher; | 62 const i::byte* keyword = |
70 const char* keyword = key_token.keyword; | 63 reinterpret_cast<const i::byte*>(key_token.keyword); |
71 int length = i::StrLength(keyword); | 64 int length = i::StrLength(key_token.keyword); |
72 for (int j = 0; j < length; j++) { | 65 CHECK(static_cast<int>(sizeof(buffer)) >= length); |
73 if (key_token.token == i::Token::INSTANCEOF && j == 2) { | 66 { |
74 // "in" is a prefix of "instanceof". It's the only keyword | 67 i::Utf8ToUC16CharacterStream stream(keyword, length); |
75 // that is a prefix of another. | 68 i::JavaScriptScanner scanner(&unicode_cache); |
76 CHECK_EQ(i::Token::IN, matcher.token()); | 69 scanner.Initialize(&stream); |
77 } else { | 70 CHECK_EQ(key_token.token, scanner.Next()); |
78 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); | 71 CHECK_EQ(i::Token::EOS, scanner.Next()); |
79 } | |
80 matcher.AddChar(keyword[j]); | |
81 } | 72 } |
82 CHECK_EQ(key_token.token, matcher.token()); | 73 // Removing characters will make keyword matching fail. |
83 // Adding more characters will make keyword matching fail. | 74 { |
84 matcher.AddChar('z'); | 75 i::Utf8ToUC16CharacterStream stream(keyword, length - 1); |
85 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); | 76 i::JavaScriptScanner scanner(&unicode_cache); |
86 // Adding a keyword later will not make it match again. | 77 scanner.Initialize(&stream); |
87 matcher.AddChar('i'); | 78 CHECK_EQ(i::Token::IDENTIFIER, scanner.Next()); |
88 matcher.AddChar('f'); | 79 CHECK_EQ(i::Token::EOS, scanner.Next()); |
89 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); | 80 } |
| 81 // Adding characters will make keyword matching fail. |
| 82 static const char chars_to_append[] = { 'z', '0', '_' }; |
| 83 for (int j = 0; j < static_cast<int>(ARRAY_SIZE(chars_to_append)); ++j) { |
| 84 memmove(buffer, keyword, length); |
| 85 buffer[length] = chars_to_append[j]; |
| 86 i::Utf8ToUC16CharacterStream stream(buffer, length + 1); |
| 87 i::JavaScriptScanner scanner(&unicode_cache); |
| 88 scanner.Initialize(&stream); |
| 89 CHECK_EQ(i::Token::IDENTIFIER, scanner.Next()); |
| 90 CHECK_EQ(i::Token::EOS, scanner.Next()); |
| 91 } |
| 92 // Replacing characters will make keyword matching fail. |
| 93 { |
| 94 memmove(buffer, keyword, length); |
| 95 buffer[length - 1] = '_'; |
| 96 i::Utf8ToUC16CharacterStream stream(buffer, length); |
| 97 i::JavaScriptScanner scanner(&unicode_cache); |
| 98 scanner.Initialize(&stream); |
| 99 CHECK_EQ(i::Token::IDENTIFIER, scanner.Next()); |
| 100 CHECK_EQ(i::Token::EOS, scanner.Next()); |
| 101 } |
90 } | 102 } |
91 | |
92 // Future keywords are not recognized. | |
93 const char* future_keyword; | |
94 for (int i = 0; (future_keyword = future_keywords[i]) != NULL; i++) { | |
95 i::KeywordMatcher matcher; | |
96 int length = i::StrLength(future_keyword); | |
97 for (int j = 0; j < length; j++) { | |
98 matcher.AddChar(future_keyword[j]); | |
99 } | |
100 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); | |
101 } | |
102 | |
103 // Zero isn't ignored at first. | |
104 i::KeywordMatcher bad_start; | |
105 bad_start.AddChar(0); | |
106 CHECK_EQ(i::Token::IDENTIFIER, bad_start.token()); | |
107 bad_start.AddChar('i'); | |
108 bad_start.AddChar('f'); | |
109 CHECK_EQ(i::Token::IDENTIFIER, bad_start.token()); | |
110 | |
111 // Zero isn't ignored at end. | |
112 i::KeywordMatcher bad_end; | |
113 bad_end.AddChar('i'); | |
114 bad_end.AddChar('f'); | |
115 CHECK_EQ(i::Token::IF, bad_end.token()); | |
116 bad_end.AddChar(0); | |
117 CHECK_EQ(i::Token::IDENTIFIER, bad_end.token()); | |
118 | |
119 // Case isn't ignored. | |
120 i::KeywordMatcher bad_case; | |
121 bad_case.AddChar('i'); | |
122 bad_case.AddChar('F'); | |
123 CHECK_EQ(i::Token::IDENTIFIER, bad_case.token()); | |
124 | |
125 // If we mark it as failure, continuing won't help. | |
126 i::KeywordMatcher full_stop; | |
127 full_stop.AddChar('i'); | |
128 CHECK_EQ(i::Token::IDENTIFIER, full_stop.token()); | |
129 full_stop.Fail(); | |
130 CHECK_EQ(i::Token::IDENTIFIER, full_stop.token()); | |
131 full_stop.AddChar('f'); | |
132 CHECK_EQ(i::Token::IDENTIFIER, full_stop.token()); | |
133 } | 103 } |
134 | 104 |
135 | 105 |
136 TEST(ScanHTMLEndComments) { | 106 TEST(ScanHTMLEndComments) { |
137 v8::V8::Initialize(); | 107 v8::V8::Initialize(); |
138 | 108 |
139 // Regression test. See: | 109 // Regression test. See: |
140 // http://code.google.com/p/chromium/issues/detail?id=53548 | 110 // http://code.google.com/p/chromium/issues/detail?id=53548 |
141 // Tests that --> is correctly interpreted as comment-to-end-of-line if there | 111 // Tests that --> is correctly interpreted as comment-to-end-of-line if there |
142 // is only whitespace before it on the line (with comments considered as | 112 // is only whitespace before it on the line (with comments considered as |
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 TestScanRegExp("/[\\u12]/flipperwald", "[\\u12]"); | 699 TestScanRegExp("/[\\u12]/flipperwald", "[\\u12]"); |
730 TestScanRegExp("/[\\u123]/flipperwald", "[\\u123]"); | 700 TestScanRegExp("/[\\u123]/flipperwald", "[\\u123]"); |
731 // Escaped ']'s wont end the character class. | 701 // Escaped ']'s wont end the character class. |
732 TestScanRegExp("/[\\]/]/flipperwald", "[\\]/]"); | 702 TestScanRegExp("/[\\]/]/flipperwald", "[\\]/]"); |
733 // Escaped slashes are not terminating. | 703 // Escaped slashes are not terminating. |
734 TestScanRegExp("/\\//flipperwald", "\\/"); | 704 TestScanRegExp("/\\//flipperwald", "\\/"); |
735 // Starting with '=' works too. | 705 // Starting with '=' works too. |
736 TestScanRegExp("/=/", "="); | 706 TestScanRegExp("/=/", "="); |
737 TestScanRegExp("/=?/", "=?"); | 707 TestScanRegExp("/=?/", "=?"); |
738 } | 708 } |
OLD | NEW |