説明を見る。00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "v8.h"
00029
00030 #include "token.h"
00031
00032 namespace v8 { namespace internal {
00033
00034 #ifdef DEBUG
00035 #define T(name, string, precedence) #name,
00036 const char* Token::name_[NUM_TOKENS] = {
00037 TOKEN_LIST(T, T, IGNORE_TOKEN)
00038 };
00039 #undef T
00040 #endif
00041
00042
00043 #define T(name, string, precedence) string,
00044 const char* Token::string_[NUM_TOKENS] = {
00045 TOKEN_LIST(T, T, IGNORE_TOKEN)
00046 };
00047 #undef T
00048
00049
00050 #define T(name, string, precedence) precedence,
00051 int8_t Token::precedence_[NUM_TOKENS] = {
00052 TOKEN_LIST(T, T, IGNORE_TOKEN)
00053 };
00054 #undef T
00055
00056
00057
00058
00059
00060 const unsigned int N = 128;
00061
00062 static uint8_t Hashtable[N];
00063 static bool IsInitialized = false;
00064
00065
00066 static unsigned int Hash(const char* s) {
00067
00068
00069
00070
00071
00072
00073
00074 const unsigned int L = 5;
00075
00076 const unsigned int S = 4;
00077
00078 const unsigned int M = 3;
00079
00080 unsigned int h = 0;
00081 for (unsigned int i = 0; s[i] != '\0' && i < L; i++) {
00082 h += (h << S) + s[i];
00083 }
00084
00085 return h * M % N;
00086 }
00087
00088
00089 Token::Value Token::Lookup(const char* str) {
00090 ASSERT(IsInitialized);
00091 Value k = static_cast<Value>(Hashtable[Hash(str)]);
00092 const char* s = string_[k];
00093 ASSERT(s != NULL || k == IDENTIFIER);
00094 if (s == NULL || strcmp(s, str) == 0) {
00095 return k;
00096 }
00097 return IDENTIFIER;
00098 }
00099
00100
00101 #ifdef DEBUG
00102
00103
00104
00105 static bool IsNull(const char* s) {
00106 return s == NULL;
00107 }
00108 #endif
00109
00110
00111 void Token::Initialize() {
00112 if (IsInitialized) return;
00113
00114
00115 #define T(name, string, precedence) name,
00116 static Value keyword[] = {
00117 TOKEN_LIST(IGNORE_TOKEN, T, IGNORE_TOKEN)
00118 ILLEGAL
00119 };
00120 #undef T
00121
00122
00123
00124
00125 ASSERT(ARRAY_SIZE(keyword) == 25 + 3 + 3 + 1);
00126
00127
00128 ASSERT(NUM_TOKENS <= 256);
00129 for (unsigned int i = 0; i < N; i++) {
00130 Hashtable[i] = IDENTIFIER;
00131 }
00132
00133
00134 int collisions = 0;
00135 for (int i = 0; keyword[i] != ILLEGAL; i++) {
00136 Value k = keyword[i];
00137 unsigned int h = Hash(string_[k]);
00138 if (Hashtable[h] != IDENTIFIER) collisions++;
00139 Hashtable[h] = k;
00140 }
00141
00142 if (collisions > 0) {
00143 PrintF("%d collisions in keyword hashtable\n", collisions);
00144 FATAL("Fix keyword lookup!");
00145 }
00146
00147 IsInitialized = true;
00148
00149
00150 #define T(name, string, precedence) \
00151 ASSERT(IsNull(string) || Lookup(string) == IDENTIFIER);
00152
00153 #define K(name, string, precedence) \
00154 ASSERT(Lookup(string) == name);
00155
00156 TOKEN_LIST(T, K, IGNORE_TOKEN)
00157
00158 #undef K
00159 #undef T
00160 }
00161
00162 } }