-
-
Notifications
You must be signed in to change notification settings - Fork 402
Expand file tree
/
Copy pathcql2text.cpp
More file actions
289 lines (247 loc) · 9.5 KB
/
cql2text.cpp
File metadata and controls
289 lines (247 loc) · 9.5 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/******************************************************************************
*
* Project: MapServer
* Purpose: CQL2 Text Parser
* Author: Even Rouault <even dot rouault at spatialys dot com>
*
******************************************************************************
* Copyright (C) 2026 Even Rouault <even dot rouault at spatialys dot com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cql2text.h"
#include "cql2textparser.hpp"
#include "mapserver.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <string>
/************************************************************************/
/* CQL2TextParse() */
/************************************************************************/
std::unique_ptr<cql2_expr_node> CQL2TextParse(const char *pszInput,
std::string &osError) {
cql2text_parse_context context;
context.pszInput = pszInput;
context.pszNext = pszInput;
context.pszLastValid = pszInput;
context.nStartToken = CQL2_TOK_VALUE_START;
if (cql2textparse(&context) == 0) {
context.poRoot->RebalanceAndOr();
return std::unique_ptr<cql2_expr_node>(context.poRoot);
} else {
delete context.poRoot;
osError = std::move(context.osError);
return nullptr;
}
}
/************************************************************************/
/* cql2texterror() */
/************************************************************************/
void cql2texterror(cql2text_parse_context *context, const char *msg) {
std::string osMsg("CQL2Text Expression Parsing Error: ");
osMsg += msg;
osMsg += ". Occurred around :\n";
int n = static_cast<int>(context->pszLastValid - context->pszInput);
for (int i = std::max(0, n - 40); i < n + 40 && context->pszInput[i] != '\0';
i++)
osMsg += context->pszInput[i];
osMsg += "\n";
for (int i = 0; i < std::min(n, 40); i++)
osMsg += " ";
osMsg += "^";
context->osError = std::move(osMsg);
}
/************************************************************************/
/* swqlex() */
/* */
/* Read back a token from the input. */
/************************************************************************/
int cql2textlex(cql2_expr_node **ppNode, cql2text_parse_context *context) {
const char *pszInput = context->pszNext;
*ppNode = nullptr;
/* -------------------------------------------------------------------- */
/* Do we have a start symbol to return? */
/* -------------------------------------------------------------------- */
if (context->nStartToken != 0) {
int nRet = context->nStartToken;
context->nStartToken = 0;
return nRet;
}
/* -------------------------------------------------------------------- */
/* Skip white space. */
/* -------------------------------------------------------------------- */
while (*pszInput == ' ' || *pszInput == '\t' || *pszInput == 10 ||
*pszInput == 13)
pszInput++;
context->pszLastValid = pszInput;
if (*pszInput == '\0') {
context->pszNext = pszInput;
return EOF;
}
/* -------------------------------------------------------------------- */
/* Handle string constants. */
/* -------------------------------------------------------------------- */
if (*pszInput == '"' || *pszInput == '\'') {
char chQuote = *pszInput;
bool bFoundEndQuote = false;
int nRet = *pszInput == '"' ? CQL2_TOK_IDENTIFIER : CQL2_TOK_STRING;
pszInput++;
std::string token;
while (*pszInput != '\0') {
if (*pszInput == '\\' && pszInput[1] != 0)
pszInput++;
else if (*pszInput == '\'' && pszInput[1] == chQuote)
pszInput++;
else if (*pszInput == chQuote) {
pszInput++;
bFoundEndQuote = true;
break;
}
token += *(pszInput++);
}
if (!bFoundEndQuote) {
context->osError = "Did not find end-of-string character";
return YYerror;
}
*ppNode = new cql2_expr_node(token);
context->pszNext = pszInput;
return nRet;
}
/* -------------------------------------------------------------------- */
/* Handle numbers. */
/* -------------------------------------------------------------------- */
else if (*pszInput >= '0' && *pszInput <= '9') {
std::string osToken;
osToken += *pszInput;
// collect non-decimal part of number
const char *pszNext = pszInput + 1;
while (*pszNext >= '0' && *pszNext <= '9')
osToken += *(pszNext++);
// collect decimal places.
if (*pszNext == '.') {
osToken += *(pszNext++);
while (*pszNext >= '0' && *pszNext <= '9')
osToken += *(pszNext++);
}
// collect exponent
if (*pszNext == 'e' || *pszNext == 'E') {
osToken += *(pszNext++);
if (*pszNext == '-' || *pszNext == '+')
osToken += *(pszNext++);
while (*pszNext >= '0' && *pszNext <= '9')
osToken += *(pszNext++);
}
context->pszNext = pszNext;
if (strstr(osToken.c_str(), ".") || strstr(osToken.c_str(), "e") ||
strstr(osToken.c_str(), "E")) {
*ppNode = new cql2_expr_node(atof(osToken.c_str()));
return CQL2_TOK_DOUBLE_NUMBER;
} else {
*ppNode = new cql2_expr_node(atoi(osToken.c_str()));
return CQL2_TOK_INTEGER_NUMBER;
}
}
/* -------------------------------------------------------------------- */
/* Handle alpha-numerics. */
/* -------------------------------------------------------------------- */
else if (isalnum(static_cast<unsigned char>(*pszInput))) {
int nReturn = CQL2_TOK_IDENTIFIER;
std::string osToken;
const char *pszNext = pszInput + 1;
osToken += *pszInput;
// collect text characters
while (isalnum(static_cast<unsigned char>(*pszNext)) || *pszNext == '_' ||
static_cast<unsigned char>(*pszNext) > 127)
osToken += *(pszNext++);
context->pszNext = pszNext;
if (osToken == "true") {
*ppNode = new cql2_expr_node(true);
nReturn = CQL2_TOK_BOOLEAN_LITERAL;
} else if (osToken == "false") {
*ppNode = new cql2_expr_node(false);
nReturn = CQL2_TOK_BOOLEAN_LITERAL;
} else if (EQUAL(osToken.c_str(), "OR"))
nReturn = CQL2_TOK_OR;
else if (EQUAL(osToken.c_str(), "AND"))
nReturn = CQL2_TOK_AND;
else if (EQUAL(osToken.c_str(), "NOT"))
nReturn = CQL2_TOK_NOT;
else if (EQUAL(osToken.c_str(), "LIKE"))
nReturn = CQL2_TOK_LIKE;
else if (EQUAL(osToken.c_str(), "IN"))
nReturn = CQL2_TOK_IN;
else if (EQUAL(osToken.c_str(), "BETWEEN"))
nReturn = CQL2_TOK_BETWEEN;
else if (EQUAL(osToken.c_str(), "IS"))
nReturn = CQL2_TOK_IS;
else if (EQUAL(osToken.c_str(), "NULL"))
nReturn = CQL2_TOK_NULL;
else if (EQUAL(osToken.c_str(), "CASEI"))
nReturn = CQL2_TOK_CASEI;
else if (EQUAL(osToken.c_str(), "DIV"))
nReturn = CQL2_TOK_DIV;
else if (EQUAL(osToken.c_str(), "S_INTERSECTS"))
nReturn = CQL2_TOK_S_INTERSECTS;
else if (EQUAL(osToken.c_str(), "S_EQUALS"))
nReturn = CQL2_TOK_S_EQUALS;
else if (EQUAL(osToken.c_str(), "S_DISJOINT"))
nReturn = CQL2_TOK_S_DISJOINT;
else if (EQUAL(osToken.c_str(), "S_TOUCHES"))
nReturn = CQL2_TOK_S_TOUCHES;
else if (EQUAL(osToken.c_str(), "S_WITHIN"))
nReturn = CQL2_TOK_S_WITHIN;
else if (EQUAL(osToken.c_str(), "S_OVERLAPS"))
nReturn = CQL2_TOK_S_OVERLAPS;
else if (EQUAL(osToken.c_str(), "S_CROSSES"))
nReturn = CQL2_TOK_S_CROSSES;
else if (EQUAL(osToken.c_str(), "S_CONTAINS"))
nReturn = CQL2_TOK_S_CONTAINS;
else if (EQUAL(osToken.c_str(), "POINT") ||
EQUAL(osToken.c_str(), "LINESTRING") ||
EQUAL(osToken.c_str(), "POLYGON") ||
EQUAL(osToken.c_str(), "MULTIPOINT") ||
EQUAL(osToken.c_str(), "MULTILINESTRING") ||
EQUAL(osToken.c_str(), "MULTIPOLYGON") ||
EQUAL(osToken.c_str(), "GEOMETRYCOLLECTION")) {
while (*pszNext == ' ')
++pszNext;
if (*pszNext == '(') {
int nLevel = 0;
while (*pszNext) {
osToken += *pszNext;
if (*pszNext == '(') {
++nLevel;
} else if (*pszNext == ')') {
--nLevel;
if (nLevel <= 0)
break;
}
++pszNext;
}
if (nLevel != 0) {
context->osError = "Unbalanced or missing parenthesis in WKT";
return YYerror;
}
context->pszNext = pszNext + 1;
*ppNode = new cql2_expr_node(osToken);
(*ppNode)->m_field_type = CQL2_WKT;
nReturn = CQL2_TOK_WKT;
} else {
*ppNode = new cql2_expr_node(osToken);
nReturn = CQL2_TOK_IDENTIFIER;
}
} else {
*ppNode = new cql2_expr_node(osToken);
nReturn = CQL2_TOK_IDENTIFIER;
}
return nReturn;
}
/* -------------------------------------------------------------------- */
/* Handle special tokens. */
/* -------------------------------------------------------------------- */
else {
context->pszNext = pszInput + 1;
return *pszInput;
}
}