Coverage Report

Created: 2026-05-23 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tdengine/source/libs/parser/inc/parToken.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15
16
#ifndef TDENGINE_TTOKEN_H
17
#define TDENGINE_TTOKEN_H
18
19
#ifdef __cplusplus
20
extern "C" {
21
#endif
22
23
#include "os.h"
24
25
#include "ttokendef.h"
26
27
#define IS_TRUE_STR(s, n)                                                                \
28
55.3k
  (n == 4 && (*(s) == 't' || *(s) == 'T') && (*((s) + 1) == 'r' || *((s) + 1) == 'R') && \
29
27.6k
   (*((s) + 2) == 'u' || *((s) + 2) == 'U') && (*((s) + 3) == 'e' || *((s) + 3) == 'E'))
30
31
#define IS_FALSE_STR(s, n)                                                                 \
32
20.7k
  (n == 5 && (*(s) == 'f' || *(s) == 'F') && (*((s) + 1) == 'a' || *((s) + 1) == 'A') &&   \
33
20.7k
   (*((s) + 2) == 'l' || *((s) + 2) == 'L') && (*((s) + 3) == 's' || *((s) + 3) == 'S') && \
34
20.7k
   (*((s) + 4) == 'e' || *((s) + 4) == 'E'))
35
36
// used to denote the minimum unite in sql parsing
37
typedef struct SToken {
38
  uint32_t n;
39
  uint32_t type;
40
  char    *z;
41
} SToken;
42
43
/**
44
 * check if it is a number or not
45
 * @param pToken
46
 * @return
47
 */
48
#define isNumber(tk) \
49
  ((tk)->type == TK_NK_INTEGER || (tk)->type == TK_NK_FLOAT || (tk)->type == TK_NK_HEX || (tk)->type == TK_NK_BIN)
50
51
/**
52
 * tokenizer for sql string
53
 * @param z
54
 * @param tokenType
55
 * @param dupQuoteChar duplicated quote char contained in the quoted string
56
 * @return
57
 */
58
uint32_t tGetToken(const char *z, uint32_t *tokenType, char* dupQuoteChar);
59
60
/**
61
 * enhanced tokenizer for sql string.
62
 *
63
 * @param str
64
 * @param i
65
 * @param isPrevOptr
66
 * @return
67
 */
68
SToken tStrGetToken(const char *str, int32_t *i, bool isPrevOptr, bool *pIgnoreComma);
69
70
/**
71
 * check if it is a keyword or not
72
 * @param z
73
 * @param len
74
 * @return
75
 */
76
bool taosIsKeyWordToken(const char *z, int32_t len);
77
78
/**
79
 * check if it is a token or not
80
 * @param   pToken
81
 * @return  token type, if it is not a number, TK_NK_ILLEGAL will return
82
 */
83
0
static FORCE_INLINE int32_t tGetNumericStringType(const SToken *pToken) {
84
0
  const char *z = pToken->z;
85
0
  int32_t     type = TK_NK_ILLEGAL;
86
0
87
0
  uint32_t i = 0;
88
0
  for (; i < pToken->n; ++i) {
89
0
    switch (z[i]) {
90
0
      case '+':
91
0
      case '-': {
92
0
        break;
93
0
      }
94
0
95
0
      case '.': {
96
0
        /*
97
0
         * handle the the float number with out integer part
98
0
         * .123
99
0
         * .123e4
100
0
         */
101
0
        if (!isdigit(z[i + 1])) {
102
0
          return TK_NK_ILLEGAL;
103
0
        }
104
0
105
0
        for (i += 2; isdigit(z[i]); i++) {
106
0
        }
107
0
108
0
        if ((z[i] == 'e' || z[i] == 'E') &&
109
0
            (isdigit(z[i + 1]) || ((z[i + 1] == '+' || z[i + 1] == '-') && isdigit(z[i + 2])))) {
110
0
          i += 2;
111
0
          while (isdigit(z[i])) {
112
0
            i++;
113
0
          }
114
0
        }
115
0
116
0
        type = TK_NK_FLOAT;
117
0
        goto _end;
118
0
      }
119
0
120
0
      case '0': {
121
0
        char next = z[i + 1];
122
0
        if (next == 'b') {  // bin number
123
0
          type = TK_NK_BIN;
124
0
          for (i += 2; (z[i] == '0' || z[i] == '1'); ++i) {
125
0
          }
126
0
127
0
          goto _end;
128
0
        } else if (next == 'x') {  // hex number
129
0
          type = TK_NK_HEX;
130
0
          for (i += 2; isdigit(z[i]) || (z[i] >= 'a' && z[i] <= 'f') || (z[i] >= 'A' && z[i] <= 'F'); ++i) {
131
0
          }
132
0
133
0
          goto _end;
134
0
        }
135
0
      }
136
0
      case '1':
137
0
      case '2':
138
0
      case '3':
139
0
      case '4':
140
0
      case '5':
141
0
      case '6':
142
0
      case '7':
143
0
      case '8':
144
0
      case '9': {
145
0
        type = TK_NK_INTEGER;
146
0
        for (; isdigit(z[i]); i++) {
147
0
        }
148
0
149
0
        int32_t seg = 0;
150
0
        while (z[i] == '.' && isdigit(z[i + 1])) {
151
0
          i += 2;
152
0
153
0
          while (isdigit(z[i])) {
154
0
            i++;
155
0
          }
156
0
157
0
          seg++;
158
0
          type = TK_NK_FLOAT;
159
0
        }
160
0
161
0
        if (seg > 1) {
162
0
          return TK_NK_ILLEGAL;
163
0
        }
164
0
165
0
        if ((z[i] == 'e' || z[i] == 'E') &&
166
0
            (isdigit(z[i + 1]) || ((z[i + 1] == '+' || z[i + 1] == '-') && isdigit(z[i + 2])))) {
167
0
          i += 2;
168
0
          while (isdigit(z[i])) {
169
0
            i++;
170
0
          }
171
0
172
0
          type = TK_NK_FLOAT;
173
0
        }
174
0
175
0
        goto _end;
176
0
      }
177
0
      default:
178
0
        return TK_NK_ILLEGAL;
179
0
    }
180
0
  }
181
0
182
0
_end:
183
0
  return (i < pToken->n) ? TK_NK_ILLEGAL : type;
184
0
}
Unexecuted instantiation: parser.c:tGetNumericStringType
Unexecuted instantiation: parTokenizer.c:tGetNumericStringType
Unexecuted instantiation: parTranslater.c:tGetNumericStringType
Unexecuted instantiation: parUtil.c:tGetNumericStringType
Unexecuted instantiation: parAstParser.c:tGetNumericStringType
Unexecuted instantiation: parAuthenticator.c:tGetNumericStringType
Unexecuted instantiation: parCalcConst.c:tGetNumericStringType
Unexecuted instantiation: parInsertSql.c:tGetNumericStringType
Unexecuted instantiation: parInsertUtil.c:tGetNumericStringType
Unexecuted instantiation: taos_lemon_sql.tab.c:tGetNumericStringType
Unexecuted instantiation: parAstCreater.c:tGetNumericStringType
Unexecuted instantiation: parInsertSml.c:tGetNumericStringType
185
186
int32_t taosInitKeywordsTable();
187
void    taosCleanupKeywordsTable();
188
189
#ifdef __cplusplus
190
}
191
#endif
192
193
#endif  // TDENGINE_TTOKEN_H