-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tokenizer.h
153 lines (145 loc) · 2.61 KB
/
Tokenizer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef Token
#define Token
/* xxxx xxxx 0:1
x0: string:number
x1: data:keyword
x2: 1 operator
x4: 1 open list
x5: 1 open array
x6: 1 close last opened
*/
#define Token_Number 0X00
#define Token_String 0X01
#define Token_Keyword 0X02
#define Token_Operator 0X04
#define Token_List 0X10
#define Token_Array 0X20
#define Token_Close 0X40
struct Token_list
{
char *token;
unsigned char type;
Token_list *next = nullptr;
};
void Add_Token(Token_list *(&q), char str[], int s, int i, unsigned char t){
Token_list *p = new Token_list;
p->token = new char[i - s + 1];
for (int j = s; j<i; j++){
p->token[j-s] = str[j];
}
p->token[i - s] = '\0';
p->type = t;
if (q != nullptr)
{
q->next = p;
}
q = p;
}
Token_list* Tokenizer(char* query)
{
char d = '0';
bool is_number = false;
unsigned char type = 0x00;
Token_list *Tokens = nullptr, *p = nullptr;
int s, i;
for (i = 0, s = 0; query[i] != '\0'; i++)
{
if (d != '0'){
if (query[i] == '\\'){
continue;
}
if (d == query[i]){
Add_Token(p, query, s, i, Token_String | type);
s = i + 1;
d = '0';
type = 0x00;
}
}
else{
switch (query[i]){
case '(':
type |= Token_List;
s = i + 1;
continue;
case '[':
type |= Token_Array;
s = i + 1;
continue;
case ')':
case ']':
type |= Token_Close;
s = i + 1;
continue;
case '\'':
case '"':
case '`':
d = query[i];
s = i + 1;
continue;
case ' ':
case '\n':
if (s == i){
s = i + 1;
continue;
}
if (is_number){
type |= Token_Number;
}
else{
type |= Token_Keyword;
}
Add_Token(p, query, s, i, type);
s = i + 1;
type = 0x00;
break;
case '=':
case '>':
case '<':
case '!':
case '+':
case '-':
case '*':
case '/':
case '%':
case ',':
if (s < i){
if (is_number){
type |= Token_Number;
}
else{
type |= Token_Keyword;
}
Add_Token(p, query, s, i, type);
type = 0x00;
}
Add_Token(p, query, i, i+1, Token_Operator);
s = i + 1;
break;
default:
is_number = (
(query[i] > 47 && query[i] < 58)
&&
(is_number || s == i)
)
||
(query[i] == 45 && s == i)
||
(s < i && is_number && query[i] == 46);
continue;
}
}
if (Tokens == nullptr && p != nullptr)
{
Tokens = p;
}
}
if (is_number){
type |= Token_Number;
}
else{
type |= Token_Keyword;
}
Add_Token(p, query, s, i, type);
return Tokens;
}
#endif