- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathTokens.cpp
233 lines (192 loc) · 4.25 KB
/
Tokens.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
/*
* PROGRAM: String parser
* MODULE: Tokens.cpp
* DESCRIPTION: Enhanced variant of strtok()
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Alex Peshkov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2015 Alex Peshkov <peshkoff at mail.ru>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include"firebird.h"
#include"../common/Tokens.h"
#include"../common/StatusArg.h"
namespace {
const Firebird::Tokens::Comment sqlComments[3] = {
{ "/*", "*/", false },
{ "--", "\n", true },
{ NULL, NULL, false }
};
constchar* sqlSpaces = "\t\r\n";
constchar* sqlSeps = "!\"#%&'()*+,-./:;<=>?@[\\]^`{|}~";
constchar* sqlQuotes = "\"'";
} // anonymous namespace
namespaceFirebird {
Tokens::Tokens()
: tokens(getPool()),
str(getPool()),
wsps(sqlSpaces),
qs(sqlQuotes),
comms(sqlComments),
seps(sqlSeps)
{
}
voidTokens::parse(FB_SIZE_T length, constchar* toParse)
{
tokens.clear();
if (!length)
length = strlen(toParse);
str.assign(toParse, length);
char inStr = '\0';
Tok* inToken = NULL;
FB_SIZE_T startp = 0;
FB_SIZE_T origin = 0;
FB_SIZE_T p = 0;
while (p < str.length())
{
if (comms && !inStr)
{
bool foundComment = false;
for (const Comment* comm = comms; comm->start; ++comm)
{
if (strncmp(comm->start, &str[p], strlen(comm->start)) == 0)
{
FB_SIZE_T p2 = p + strlen(comm->start);
p2 = str.find(comm->stop, p2);
if (p2 == str.npos)
{
if (!comm->endOnEol)
error("Missing close comment for %s", comm->start);
p2 = str.length();
}
else
p2 += strlen(comm->stop);
str.erase(p, p2 - p);
origin += (p2 - p);
foundComment = true;
break;
}
}
if (foundComment)
continue;
}
char c = str[p];
if (inStr)
{
if (c == inStr)
{
++p;
++origin;
if (p >= str.length() || str[p] != inStr)
{
inStr = '\0';
inToken->length = p - startp;
inToken = NULL;
continue;
}
// double quote - continue processing string
}
++p;
++origin;
continue;
}
if (wsps && strchr(wsps, c))
{
if (inToken)
{
inToken->length = p - startp;
inToken = NULL;
}
++p;
++origin;
continue;
}
bool quote = qs && strchr(qs, c);
if (quote)
{
if (inToken)
{
inToken->length = p - startp;
inToken = NULL;
}
// start string
inStr = c;
}
if ((!quote) && seps && strchr(seps, c))
{
// close current token
if (inToken)
{
inToken->length = p - startp;
inToken = NULL;
}
// and create new one for one symbol
inToken = createToken(p, origin);
inToken->length = 1;
inToken = NULL;
}
elseif (!inToken)
{
// start token
startp = p;
inToken = createToken(p, origin);
}
// done with char
++p;
++origin;
}
if (inStr)
error("Missing close quote <%c>", inStr);
if (inToken)
inToken->length = p - startp;
//#define DEBUG_TOKENS
#ifdef DEBUG_TOKENS
for (unsigned dbg = 0; dbg < getCount(); ++dbg)
printf("%2d %.*s\n", dbg, tokens[dbg].length, tokens[dbg].text);
#endif
}
Tokens::Tok* Tokens::createToken(FB_SIZE_T p, FB_SIZE_T origin)
{
tokens.grow(tokens.getCount() + 1);
Tok* tok = &tokens[tokens.getCount() - 1];
tok->text = &str[p];
tok->origin = origin;
return tok;
}
voidTokens::error(constchar* fmt, ...)
{
string buffer;
va_list params;
va_start(params, fmt);
buffer.vprintf(fmt, params);
va_end(params);
(Arg::Gds(isc_tokens_parse) << Arg::Gds(isc_random) << buffer).raise();
}
string Tokens::Tok::stripped() const
{
string rc;
char q = text[0];
for (FB_SIZE_T i = 1; i < length - 1; ++i)
{
if (text[i] == q)
++i;
rc += text[i];
}
return rc;
}
} // namespace Firebird