parTokenizer.c 19.2 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
hjxilinx 已提交
16
#include "os.h"
X
Xiaoyu Wang 已提交
17
#include "parToken.h"
H
Haojun Liao 已提交
18
#include "thash.h"
H
hjxilinx 已提交
19
#include "taosdef.h"
20
#include "ttokendef.h"
H
hzcheng 已提交
21

S
slguan 已提交
22
// All the keywords of the SQL language are stored in a hash table
H
hzcheng 已提交
23
typedef struct SKeyword {
S
slguan 已提交
24
  const char* name;  // The keyword name
H
Haojun Liao 已提交
25
  uint16_t    type;  // type
S
slguan 已提交
26
  uint8_t     len;   // length
H
hzcheng 已提交
27 28
} SKeyword;

S
slguan 已提交
29
// keywords in sql string
H
hzcheng 已提交
30
static SKeyword keywordTable[] = {
31
    {"ACCOUNT",       TK_ACCOUNT},
32 33 34
    {"ACCOUNTS",      TK_ACCOUNTS},
    {"ADD",           TK_ADD},
    {"AGGREGATE",     TK_AGGREGATE},
35
    {"ALL",           TK_ALL},
36
    {"ALTER",         TK_ALTER},
37
    {"ANALYZE",       TK_ANALYZE},
38
    {"AND",           TK_AND},
39
    {"APPS",          TK_APPS},
40 41 42 43 44 45 46
    {"AS",            TK_AS},
    {"ASC",           TK_ASC},
    {"BETWEEN",       TK_BETWEEN},
    {"BINARY",        TK_BINARY},
    {"BIGINT",        TK_BIGINT},
    {"BLOCKS",        TK_BLOCKS},
    {"BOOL",          TK_BOOL},
47
    {"BUFSIZE",       TK_BUFSIZE},
48 49 50
    {"BY",            TK_BY},
    {"CACHE",         TK_CACHE},
    {"CACHELAST",     TK_CACHELAST},
51
    {"COLUMN",        TK_COLUMN},
52 53
    {"COMMENT",       TK_COMMENT},
    {"COMP",          TK_COMP},
54 55 56 57
    {"COMPACT",       TK_COMPACT},
    {"CONNS",         TK_CONNS},
    {"CONNECTION",    TK_CONNECTION},
    {"CONNECTIONS",   TK_CONNECTIONS},
58 59 60 61
    {"CREATE",        TK_CREATE},
    {"DATABASE",      TK_DATABASE},
    {"DATABASES",     TK_DATABASES},
    {"DAYS",          TK_DAYS},
62
    {"DBS",           TK_DBS},
X
Xiaoyu Wang 已提交
63
    {"DELAY",         TK_DELAY},
64
    {"DESC",          TK_DESC},
65
    {"DESCRIBE",      TK_DESCRIBE},
66
    {"DISTINCT",      TK_DISTINCT},
67 68
    {"DNODE",         TK_DNODE},
    {"DNODES",        TK_DNODES},
69
    {"DOUBLE",        TK_DOUBLE},
70
    {"DROP",          TK_DROP},
71
    {"EXISTS",        TK_EXISTS},
72
    {"EXPLAIN",       TK_EXPLAIN},
X
Xiaoyu Wang 已提交
73
    {"FILE_FACTOR",   TK_FILE_FACTOR},
74 75 76 77
    {"FILL",          TK_FILL},
    {"FLOAT",         TK_FLOAT},
    {"FROM",          TK_FROM},
    {"FSYNC",         TK_FSYNC},
X
Xiaoyu Wang 已提交
78
    {"FUNCTION",      TK_FUNCTION},
X
Xiaoyu Wang 已提交
79
    {"FUNCTIONS",     TK_FUNCTIONS},
80 81 82 83 84
    {"GROUP",         TK_GROUP},
    {"HAVING",        TK_HAVING},
    {"IF",            TK_IF},
    {"IMPORT",        TK_IMPORT},
    {"IN",            TK_IN},
X
Xiaoyu Wang 已提交
85
    {"INDEX",         TK_INDEX},
X
Xiaoyu Wang 已提交
86
    {"INDEXES",       TK_INDEXES},
87 88 89 90 91 92 93 94 95 96
    {"INNER",         TK_INNER},
    {"INT",           TK_INT},
    {"INSERT",        TK_INSERT},
    {"INTEGER",       TK_INTEGER},
    {"INTERVAL",      TK_INTERVAL},
    {"INTO",          TK_INTO},
    {"IS",            TK_IS},
    {"JOIN",          TK_JOIN},
    {"JSON",          TK_JSON},
    {"KEEP",          TK_KEEP},
97 98
    {"KILL",          TK_KILL},
    {"LICENCE",       TK_LICENCE},
99 100 101
    {"LIKE",          TK_LIKE},
    {"LIMIT",         TK_LIMIT},
    {"LINEAR",        TK_LINEAR},
102
    {"LOCAL",         TK_LOCAL},
103 104 105 106
    {"MATCH",         TK_MATCH},
    {"MAXROWS",       TK_MAXROWS},
    {"MINROWS",       TK_MINROWS},
    {"MINUS",         TK_MINUS},
X
Xiaoyu Wang 已提交
107
    {"MNODES",        TK_MNODES},
108
    {"MODIFY",        TK_MODIFY},
X
Xiaoyu Wang 已提交
109
    {"MODULES",       TK_MODULES},
110 111 112 113 114 115 116 117 118 119
    {"NCHAR",         TK_NCHAR},
    {"NMATCH",        TK_NMATCH},
    {"NONE",          TK_NONE},
    {"NOT",           TK_NOT},
    {"NOW",           TK_NOW},
    {"NULL",          TK_NULL},
    {"OFFSET",        TK_OFFSET},
    {"ON",            TK_ON},
    {"OR",            TK_OR},
    {"ORDER",         TK_ORDER},
120
    {"OUTPUTTYPE",    TK_OUTPUTTYPE},
121
    {"PARTITION",     TK_PARTITION},
122 123
    {"PASS",          TK_PASS},
    {"PORT",          TK_PORT},
124
    {"PPS",           TK_PPS},
125 126 127
    {"PRECISION",     TK_PRECISION},
    {"PRIVILEGE",     TK_PRIVILEGE},
    {"PREV",          TK_PREV},
X
Xiaoyu Wang 已提交
128
    {"_QENDTS",        TK_QENDTS},
X
Xiaoyu Wang 已提交
129
    {"QNODE",         TK_QNODE},
X
Xiaoyu Wang 已提交
130
    {"QNODES",        TK_QNODES},
X
Xiaoyu Wang 已提交
131
    {"_QSTARTTS",      TK_QSTARTTS},
132 133
    {"QTIME",         TK_QTIME},
    {"QUERIES",       TK_QUERIES},
134
    {"QUERY",         TK_QUERY},
135
    {"QUORUM",        TK_QUORUM},
136
    {"RATIO",         TK_RATIO},
137
    {"REPLICA",       TK_REPLICA},
138
    {"RESET",         TK_RESET},
X
Xiaoyu Wang 已提交
139 140
    {"RETENTIONS",    TK_RETENTIONS},
    {"ROLLUP",        TK_ROLLUP},
X
Xiaoyu Wang 已提交
141
    {"_ROWTS",         TK_ROWTS},
142
    {"SCORES",        TK_SCORES},
143 144
    {"SELECT",        TK_SELECT},
    {"SESSION",       TK_SESSION},
145
    {"SET",           TK_SET},
146 147 148 149 150 151 152
    {"SHOW",          TK_SHOW},
    {"SINGLE_STABLE", TK_SINGLE_STABLE},
    {"SLIDING",       TK_SLIDING},
    {"SLIMIT",        TK_SLIMIT},
    {"SMA",           TK_SMA},
    {"SMALLINT",      TK_SMALLINT},
    {"SOFFSET",       TK_SOFFSET},
153
    {"STABLE",        TK_STABLE},
154
    {"STABLES",       TK_STABLES},
155
    {"STATE",         TK_STATE},
156
    {"STATE_WINDOW",  TK_STATE_WINDOW},
157 158
    {"STORAGE",       TK_STORAGE},
    {"STREAM",       TK_STREAM},
X
Xiaoyu Wang 已提交
159
    {"STREAMS",       TK_STREAMS},
160
    {"STREAM_MODE",   TK_STREAM_MODE},
161
    {"SYNCDB",        TK_SYNCDB},
162 163
    {"TABLE",         TK_TABLE},
    {"TABLES",        TK_TABLES},
164
    {"TAG",           TK_TAG},
165
    {"TAGS",          TK_TAGS},
166
    {"TBNAME",        TK_TBNAME},
167 168
    {"TIMESTAMP",     TK_TIMESTAMP},
    {"TINYINT",       TK_TINYINT},
169
    {"TOPIC",         TK_TOPIC},
170 171
    {"TOPICS",        TK_TOPICS},
    {"TSERIES",       TK_TSERIES},
172 173 174 175
    {"TTL",           TK_TTL},
    {"UNION",         TK_UNION},
    {"UNSIGNED",      TK_UNSIGNED},
    {"USE",           TK_USE},
176 177
    {"USER",          TK_USER},
    {"USERS",         TK_USERS},
178
    {"USING",         TK_USING},
X
Xiaoyu Wang 已提交
179
    {"VALUE",         TK_VALUE},
180 181
    {"VALUES",        TK_VALUES},
    {"VARCHAR",       TK_VARCHAR},
182
    {"VARIABLES",     TK_VARIABLES},
183
    {"VERBOSE",       TK_VERBOSE},
184
    {"VGROUPS",       TK_VGROUPS},
185
    {"VNODES",        TK_VNODES},
186
    {"WAL",           TK_WAL},
X
Xiaoyu Wang 已提交
187 188
    {"_WDURATION",     TK_WDURATION},
    {"_WENDTS",        TK_WENDTS},
189
    {"WHERE",         TK_WHERE},
X
Xiaoyu Wang 已提交
190
    {"_WSTARTTS",     TK_WSTARTTS},
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    // {"ID",           TK_ID},
    // {"STRING",       TK_STRING},
    // {"EQ",           TK_EQ},
    // {"NE",           TK_NE},
    // {"ISNULL",       TK_ISNULL},
    // {"NOTNULL",      TK_NOTNULL},
    // {"GLOB",         TK_GLOB},
    // {"GT",           TK_GT},
    // {"GE",           TK_GE},
    // {"LT",           TK_LT},
    // {"LE",           TK_LE},
    // {"BITAND",       TK_BITAND},
    // {"BITOR",        TK_BITOR},
    // {"LSHIFT",       TK_LSHIFT},
    // {"RSHIFT",       TK_RSHIFT},
    // {"PLUS",         TK_PLUS},
    // {"DIVIDE",       TK_DIVIDE},
    // {"TIMES",        TK_TIMES},
    // {"STAR",         TK_STAR},
    // {"SLASH",        TK_SLASH},
    // {"REM ",         TK_REM},
    // {"||",           TK_CONCAT},
    // {"UMINUS",       TK_UMINUS},
    // {"UPLUS",        TK_UPLUS},
    // {"BITNOT",       TK_BITNOT},
    // {"GRANTS",       TK_GRANTS},
    // {"DOT",          TK_DOT},
    // {"CTIME",        TK_CTIME},
    // {"LP",           TK_LP},
    // {"RP",           TK_RP},
    // {"COMMA",        TK_COMMA},
    // {"EVERY",        TK_EVERY},
    // {"VARIABLE",     TK_VARIABLE},
    // {"UPDATE",       TK_UPDATE},
    // {"CHANGE",       TK_CHANGE},
    // {"COLON",        TK_COLON},
    // {"ABORT",        TK_ABORT},
    // {"AFTER",        TK_AFTER},
    // {"ATTACH",       TK_ATTACH},
    // {"BEFORE",       TK_BEFORE},
    // {"BEGIN",        TK_BEGIN},
    // {"CASCADE",      TK_CASCADE},
    // {"CLUSTER",      TK_CLUSTER},
    // {"CONFLICT",     TK_CONFLICT},
    // {"COPY",         TK_COPY},
    // {"DEFERRED",     TK_DEFERRED},
    // {"DELIMITERS",   TK_DELIMITERS},
    // {"DETACH",       TK_DETACH},
    // {"EACH",         TK_EACH},
    // {"END",          TK_END},
    // {"FAIL",         TK_FAIL},
    // {"FOR",          TK_FOR},
    // {"IGNORE",       TK_IGNORE},
    // {"IMMEDIATE",    TK_IMMEDIATE},
    // {"INITIALLY",    TK_INITIALLY},
    // {"INSTEAD",      TK_INSTEAD},
    // {"KEY",          TK_KEY},
    // {"OF",           TK_OF},
    // {"RAISE",        TK_RAISE},
    // {"REPLACE",      TK_REPLACE},
    // {"RESTRICT",     TK_RESTRICT},
    // {"ROW",          TK_ROW},
    // {"STATEMENT",    TK_STATEMENT},
    // {"TRIGGER",      TK_TRIGGER},
    // {"VIEW",         TK_VIEW},
    // {"SEMI",         TK_SEMI},
257
    // {"PARTITIONS",   TK_PARTITIONS},
258
    // {"MODE",         TK_MODE},
H
hzcheng 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272
};

static const char isIdChar[] = {
    /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
};

H
Haojun Liao 已提交
273
static void* keywordHashTable = NULL;
H
hzcheng 已提交
274

S
TD-1057  
Shengliang Guan 已提交
275
static void doInitKeywordsTable(void) {
276 277
  int numOfEntries = tListLen(keywordTable);
  
H
Haojun Liao 已提交
278
  keywordHashTable = taosHashInit(numOfEntries, MurmurHash3_32, true, false);
279
  for (int32_t i = 0; i < numOfEntries; i++) {
S
TD-1057  
Shengliang Guan 已提交
280
    keywordTable[i].len = (uint8_t)strlen(keywordTable[i].name);
281
    void* ptr = &keywordTable[i];
H
Haojun Liao 已提交
282
    taosHashPut(keywordHashTable, keywordTable[i].name, keywordTable[i].len, (void*)&ptr, POINTER_BYTES);
H
hzcheng 已提交
283
  }
284 285
}

wafwerar's avatar
wafwerar 已提交
286
static TdThreadOnce keywordsHashTableInit = PTHREAD_ONCE_INIT;
287

288
static int32_t tKeywordCode(const char* z, int n) {
wafwerar's avatar
wafwerar 已提交
289
  taosThreadOnce(&keywordsHashTableInit, doInitKeywordsTable);
H
hjxilinx 已提交
290 291
  
  char key[512] = {0};
292
  if (n > tListLen(key)) { // too long token, can not be any other token type
293
    return TK_NK_ID;
294
  }
H
hjxilinx 已提交
295
  
H
hzcheng 已提交
296 297
  for (int32_t j = 0; j < n; ++j) {
    if (z[j] >= 'a' && z[j] <= 'z') {
298
      key[j] = (char)(z[j] & 0xDF);  // to uppercase and set the null-terminated
H
hzcheng 已提交
299 300 301 302 303
    } else {
      key[j] = z[j];
    }
  }

D
dapan1121 已提交
304
  if (keywordHashTable == NULL) {
305
    return TK_NK_ILLEGAL;
D
dapan1121 已提交
306
  }
H
Haojun Liao 已提交
307

H
Haojun Liao 已提交
308
  SKeyword** pKey = (SKeyword**)taosHashGet(keywordHashTable, key, n);
309
  return (pKey != NULL)? (*pKey)->type:TK_NK_ID;
H
hzcheng 已提交
310 311
}

H
huili 已提交
312
/*
313 314 315
 * Return the length of the token that begins at z[0].
 * Store the token type in *type before returning.
 */
316
uint32_t tGetToken(const char* z, uint32_t* tokenId) {
317
  uint32_t i;
H
hzcheng 已提交
318 319 320 321 322 323 324 325
  switch (*z) {
    case ' ':
    case '\t':
    case '\n':
    case '\f':
    case '\r': {
      for (i = 1; isspace(z[i]); i++) {
      }
326
      *tokenId = TK_NK_SPACE;
H
hzcheng 已提交
327 328 329
      return i;
    }
    case ':': {
330
      *tokenId = TK_NK_COLON;
H
hzcheng 已提交
331 332 333 334 335 336
      return 1;
    }
    case '-': {
      if (z[1] == '-') {
        for (i = 2; z[i] && z[i] != '\n'; i++) {
        }
337
        *tokenId = TK_NK_COMMENT;
H
hzcheng 已提交
338 339
        return i;
      }
X
Xiaoyu Wang 已提交
340
      *tokenId = TK_NK_MINUS;
H
hzcheng 已提交
341 342 343
      return 1;
    }
    case '(': {
344
      *tokenId = TK_NK_LP;
H
hzcheng 已提交
345 346 347
      return 1;
    }
    case ')': {
348
      *tokenId = TK_NK_RP;
H
hzcheng 已提交
349 350 351
      return 1;
    }
    case ';': {
352
      *tokenId = TK_NK_SEMI;
H
hzcheng 已提交
353 354 355
      return 1;
    }
    case '+': {
356
      *tokenId = TK_NK_PLUS;
H
hzcheng 已提交
357 358 359
      return 1;
    }
    case '*': {
360
      *tokenId = TK_NK_STAR;
H
hzcheng 已提交
361 362 363 364
      return 1;
    }
    case '/': {
      if (z[1] != '*' || z[2] == 0) {
365
        *tokenId = TK_NK_SLASH;
H
hzcheng 已提交
366 367 368 369 370
        return 1;
      }
      for (i = 3; z[i] && (z[i] != '/' || z[i - 1] != '*'); i++) {
      }
      if (z[i]) i++;
371
      *tokenId = TK_NK_COMMENT;
H
hzcheng 已提交
372 373 374
      return i;
    }
    case '%': {
375
      *tokenId = TK_NK_REM;
H
hzcheng 已提交
376 377 378
      return 1;
    }
    case '=': {
379
      *tokenId = TK_NK_EQ;
H
hzcheng 已提交
380 381 382 383
      return 1 + (z[1] == '=');
    }
    case '<': {
      if (z[1] == '=') {
384
        *tokenId = TK_NK_LE;
H
hzcheng 已提交
385 386
        return 2;
      } else if (z[1] == '>') {
387
        *tokenId = TK_NK_NE;
H
hzcheng 已提交
388 389
        return 2;
      } else if (z[1] == '<') {
390
        *tokenId = TK_NK_LSHIFT;
H
hzcheng 已提交
391 392
        return 2;
      } else {
393
        *tokenId = TK_NK_LT;
H
hzcheng 已提交
394 395 396 397 398
        return 1;
      }
    }
    case '>': {
      if (z[1] == '=') {
399
        *tokenId = TK_NK_GE;
H
hzcheng 已提交
400 401
        return 2;
      } else if (z[1] == '>') {
402
        *tokenId = TK_NK_RSHIFT;
H
hzcheng 已提交
403 404
        return 2;
      } else {
405
        *tokenId = TK_NK_GT;
H
hzcheng 已提交
406 407 408 409 410
        return 1;
      }
    }
    case '!': {
      if (z[1] != '=') {
411
        *tokenId = TK_NK_ILLEGAL;
H
hzcheng 已提交
412 413
        return 2;
      } else {
414
        *tokenId = TK_NK_NE;
H
hzcheng 已提交
415 416 417 418 419
        return 2;
      }
    }
    case '|': {
      if (z[1] != '|') {
420
        *tokenId = TK_NK_BITOR;
H
hzcheng 已提交
421 422
        return 1;
      } else {
423
        *tokenId = TK_NK_CONCAT;
H
hzcheng 已提交
424 425 426 427
        return 2;
      }
    }
    case ',': {
428
      *tokenId = TK_NK_COMMA;
H
hzcheng 已提交
429 430 431
      return 1;
    }
    case '&': {
432
      *tokenId = TK_NK_BITAND;
H
hzcheng 已提交
433 434 435
      return 1;
    }
    case '~': {
436
      *tokenId = TK_NK_BITNOT;
H
hzcheng 已提交
437 438
      return 1;
    }
S
slguan 已提交
439
    case '?': {
440
      *tokenId = TK_NK_QUESTION;
S
slguan 已提交
441 442
      return 1;
    }
443
    case '`':
H
hzcheng 已提交
444 445
    case '\'':
    case '"': {
S
slguan 已提交
446 447
      int  delim = z[0];
      bool strEnd = false;
H
hzcheng 已提交
448
      for (i = 1; z[i]; i++) {
H
Haojun Liao 已提交
449
        if (z[i] == '\\') {   // ignore the escaped character that follows this backslash
L
[1292]  
lihui 已提交
450 451 452 453
          i++;
          continue;
        }
        
454
        if (z[i] == delim) {
H
hzcheng 已提交
455 456 457
          if (z[i + 1] == delim) {
            i++;
          } else {
H
huili 已提交
458
            strEnd = true;
H
hzcheng 已提交
459 460 461 462
            break;
          }
        }
      }
L
[1292]  
lihui 已提交
463
      
H
hzcheng 已提交
464
      if (z[i]) i++;
H
huili 已提交
465

S
slguan 已提交
466
      if (strEnd) {
467
        *tokenId = (delim == '`')? TK_NK_ID:TK_NK_STRING;
S
slguan 已提交
468 469
        return i;
      }
H
huili 已提交
470

S
slguan 已提交
471
      break;
H
hzcheng 已提交
472 473
    }
    case '.': {
S
slguan 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
      /*
       * handle the the float number with out integer part
       * .123
       * .123e4
       */
      if (isdigit(z[1])) {
        for (i = 2; isdigit(z[i]); i++) {
        }

        if ((z[i] == 'e' || z[i] == 'E') &&
            (isdigit(z[i + 1]) || ((z[i + 1] == '+' || z[i + 1] == '-') && isdigit(z[i + 2])))) {
          i += 2;
          while (isdigit(z[i])) {
            i++;
          }
        }

491
        *tokenId = TK_NK_FLOAT;
S
slguan 已提交
492 493
        return i;
      } else {
494
        *tokenId = TK_NK_DOT;
S
slguan 已提交
495 496 497 498 499 500 501 502
        return 1;
      }
    }

    case '0': {
      char next = z[1];

      if (next == 'b') { // bin number
503
        *tokenId = TK_NK_BIN;
S
slguan 已提交
504 505 506 507 508 509 510 511 512
        for (i = 2; (z[i] == '0' || z[i] == '1'); ++i) {
        }

        if (i == 2) {
          break;
        }

        return i;
      } else if (next == 'x') {  //hex number
513
        *tokenId = TK_NK_HEX;
S
slguan 已提交
514 515 516 517 518 519 520 521 522
        for (i = 2; isdigit(z[i]) || (z[i] >= 'a' && z[i] <= 'f') || (z[i] >= 'A' && z[i] <= 'F'); ++i) {
        }

        if (i == 2) {
          break;
        }

        return i;
      }
H
hzcheng 已提交
523 524 525 526 527 528 529 530 531 532
    }
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9': {
533
      *tokenId = TK_NK_INTEGER;
H
hzcheng 已提交
534 535 536
      for (i = 1; isdigit(z[i]); i++) {
      }

H
Haojun Liao 已提交
537
      /* here is the 1u/1a/2s/3m/9y */
538
      if ((z[i] == 'b' || z[i] == 'u' || z[i] == 'a' || z[i] == 's' || z[i] == 'm' || z[i] == 'h' || z[i] == 'd' || z[i] == 'n' ||
H
Haojun Liao 已提交
539
           z[i] == 'y' || z[i] == 'w' ||
540
           z[i] == 'B' || z[i] == 'U' || z[i] == 'A' || z[i] == 'S' || z[i] == 'M' || z[i] == 'H' || z[i] == 'D' || z[i] == 'N' ||
H
Haojun Liao 已提交
541
           z[i] == 'Y' || z[i] == 'W') &&
H
hjxilinx 已提交
542
          (isIdChar[(uint8_t)z[i + 1]] == 0)) {
543
        *tokenId = TK_NK_VARIABLE;
H
hzcheng 已提交
544 545 546 547 548 549 550 551 552 553
        i += 1;
        return i;
      }

      int32_t seg = 1;
      while (z[i] == '.' && isdigit(z[i + 1])) {
        i += 2;
        while (isdigit(z[i])) {
          i++;
        }
554
        *tokenId = TK_NK_FLOAT;
H
hzcheng 已提交
555 556 557 558
        seg++;
      }

      if (seg == 4) {  // ip address
559
        *tokenId = TK_NK_IPTOKEN;
H
hzcheng 已提交
560 561 562 563 564 565 566 567 568
        return i;
      }

      if ((z[i] == 'e' || z[i] == 'E') &&
          (isdigit(z[i + 1]) || ((z[i + 1] == '+' || z[i + 1] == '-') && isdigit(z[i + 2])))) {
        i += 2;
        while (isdigit(z[i])) {
          i++;
        }
569
        *tokenId = TK_NK_FLOAT;
H
hzcheng 已提交
570 571 572 573 574 575
      }
      return i;
    }
    case '[': {
      for (i = 1; z[i] && z[i - 1] != ']'; i++) {
      }
576
      *tokenId = TK_NK_ID;
H
hzcheng 已提交
577 578 579 580 581 582
      return i;
    }
    case 'T':
    case 't':
    case 'F':
    case 'f': {
H
hjxilinx 已提交
583
      for (i = 1; ((z[i] & 0x80) == 0) && isIdChar[(uint8_t) z[i]]; i++) {
H
hzcheng 已提交
584 585 586
      }

      if ((i == 4 && strncasecmp(z, "true", 4) == 0) || (i == 5 && strncasecmp(z, "false", 5) == 0)) {
587
        *tokenId = TK_NK_BOOL;
H
hzcheng 已提交
588 589 590 591
        return i;
      }
    }
    default: {
H
hjxilinx 已提交
592
      if (((*z & 0x80) != 0) || !isIdChar[(uint8_t) *z]) {
H
hzcheng 已提交
593 594
        break;
      }
H
hjxilinx 已提交
595
      for (i = 1; ((z[i] & 0x80) == 0) && isIdChar[(uint8_t) z[i]]; i++) {
H
hzcheng 已提交
596
      }
597
      *tokenId = tKeywordCode(z, i);
H
hzcheng 已提交
598 599 600 601
      return i;
    }
  }

602
  *tokenId = TK_NK_ILLEGAL;
H
hzcheng 已提交
603 604 605
  return 0;
}

H
Haojun Liao 已提交
606
SToken tscReplaceStrToken(char **str, SToken *token, const char* newToken) {
D
dapan1121 已提交
607
  char *src = *str;
D
dapan1121 已提交
608 609 610
  size_t nsize = strlen(newToken);
  int32_t size = (int32_t)strlen(*str) - token->n + (int32_t)nsize + 1;
  int32_t bsize = (int32_t)((uint64_t)token->z - (uint64_t)src);
H
Haojun Liao 已提交
611
  SToken ntoken;
D
dapan1121 已提交
612

wafwerar's avatar
wafwerar 已提交
613
  *str = taosMemoryCalloc(1, size);
D
dapan1121 已提交
614 615

  strncpy(*str, src, bsize);
H
Haojun Liao 已提交
616
  strcat(*str, newToken);
D
dapan1121 已提交
617 618
  strcat(*str, token->z + token->n);

D
dapan1121 已提交
619
  ntoken.n = (uint32_t)nsize;
D
dapan1121 已提交
620 621
  ntoken.z = *str + bsize;

wafwerar's avatar
wafwerar 已提交
622
  taosMemoryFreeClear(src);
D
dapan1121 已提交
623 624 625 626

  return ntoken;
}

627
SToken tStrGetToken(const char* str, int32_t* i, bool isPrevOptr) {
H
Haojun Liao 已提交
628
  SToken t0 = {0};
S
slguan 已提交
629

H
hzcheng 已提交
630 631
  // here we reach the end of sql string, null-terminated string
  if (str[*i] == 0) {
S
slguan 已提交
632 633
    t0.n = 0;
    return t0;
H
hzcheng 已提交
634 635
  }

636
  // IGNORE TK_NK_SPACE, TK_NK_COMMA, and specified tokens
S
slguan 已提交
637 638 639
  while (1) {
    *i += t0.n;

H
Haojun Liao 已提交
640 641 642 643 644 645
    int32_t numOfComma = 0;
    char t = str[*i];
    while (t == ' ' || t == '\n' || t == '\r' || t == '\t' || t == '\f' || t == ',') {
      if (t == ',' && (++numOfComma > 1)) {  // comma only allowed once
        t0.n = 0;
        return t0;
S
slguan 已提交
646
      }
H
Haojun Liao 已提交
647
    
H
Haojun Liao 已提交
648
      t = str[++(*i)];
S
slguan 已提交
649
    }
H
hzcheng 已提交
650

651
    t0.n = tGetToken(&str[*i], &t0.type);
H
Haojun Liao 已提交
652
    break;
S
slguan 已提交
653

H
Haojun Liao 已提交
654 655
    // not support user specfied ignored symbol list
#if 0
H
Haojun Liao 已提交
656
    bool ignore = false;
S
slguan 已提交
657 658
    for (uint32_t k = 0; k < numOfIgnoreToken; k++) {
      if (t0.type == ignoreTokenTypes[k]) {
H
Haojun Liao 已提交
659
        ignore = true;
S
slguan 已提交
660 661 662 663
        break;
      }
    }

H
Haojun Liao 已提交
664
    if (!ignore) {
S
slguan 已提交
665 666
      break;
    }
H
Haojun Liao 已提交
667
#endif
H
hzcheng 已提交
668 669
  }

670
  if (t0.type == TK_NK_SEMI) {
S
slguan 已提交
671 672 673 674 675 676 677 678 679
    t0.n = 0;
    return t0;
  }

  uint32_t type = 0;
  int32_t  len;

  // support parse the 'db.tbl' format, notes: There should be no space on either side of the dot!
  if ('.' == str[*i + t0.n]) {
680
    len = tGetToken(&str[*i + t0.n + 1], &type);
S
slguan 已提交
681 682

    // only id and string are valid
683
    if ((TK_NK_STRING != t0.type) && (TK_NK_ID != t0.type)) {
684
      t0.type = TK_NK_ILLEGAL;
S
slguan 已提交
685 686 687 688 689 690 691 692 693
      t0.n = 0;

      return t0;
    }

    t0.n += len + 1;

  } else {
    // support parse the -/+number format
X
Xiaoyu Wang 已提交
694
    if ((isPrevOptr) && (t0.type == TK_NK_MINUS || t0.type == TK_NK_PLUS)) {
695
      len = tGetToken(&str[*i + t0.n], &type);
696
      if (type == TK_NK_INTEGER || type == TK_NK_FLOAT) {
S
slguan 已提交
697 698 699
        t0.type = type;
        t0.n += len;
      }
H
hzcheng 已提交
700 701 702
    }
  }

703
  t0.z = (char*) str + (*i);
S
slguan 已提交
704 705 706
  *i += t0.n;

  return t0;
H
hzcheng 已提交
707 708
}

709
bool taosIsKeyWordToken(const char* z, int32_t len) {
710
  return (tKeywordCode((char*)z, len) != TK_NK_ID);
711
}
H
Haojun Liao 已提交
712 713

void taosCleanupKeywordsTable() {
H
Haojun Liao 已提交
714 715 716 717
  void* m = keywordHashTable;
  if (m != NULL && atomic_val_compare_exchange_ptr(&keywordHashTable, m, 0) == m) {
    taosHashCleanup(m);
  }
Y
yihaoDeng 已提交
718
}
719

H
Haojun Liao 已提交
720
SToken taosTokenDup(SToken* pToken, char* buf, int32_t len) {
721 722 723 724 725
  assert(pToken != NULL && buf != NULL && len > pToken->n);
  
  strncpy(buf, pToken->z, pToken->n);
  buf[pToken->n] = 0;

H
Haojun Liao 已提交
726
  SToken token = *pToken;
727 728 729
  token.z = buf;
  return token;
}