forked from dashjoin/jsonata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenizer.java
More file actions
366 lines (348 loc) · 12.9 KB
/
Tokenizer.java
File metadata and controls
366 lines (348 loc) · 12.9 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* jsonata-java is the JSONata Java reference port
*
* Copyright Dashjoin GmbH. https://dashjoin.com
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Derived from Javascript code under this license:
/**
* © Copyright IBM Corp. 2016, 2018 All Rights Reserved
* Project name: JSONata
* This project is licensed under the MIT License, see LICENSE
*/
package com.dashjoin.jsonata;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tokenizer { // = function (path) {
static HashMap<String, Integer> operators = new HashMap<String, Integer>() {{
put(".", 75);
put("[", 80);
put("]", 0);
put("{", 70);
put("}", 0);
put("(", 80);
put(")", 0);
put(",", 0);
put("@", 80);
put("#", 80);
put(";", 80);
put(":", 80);
put("?", 20);
put("+", 50);
put("-", 50);
put("*", 60);
put("/", 60);
put("%", 60);
put("|", 20);
put("=", 40);
put("<", 40);
put(">", 40);
put("^", 40);
put("**", 60);
put("..", 20);
put(":=", 10);
put("!=", 40);
put("<=", 40);
put(">=", 40);
put("~>", 40);
put("?:", 40);
put("??", 40);
put("and", 30);
put("or", 25);
put("in", 40);
put("&", 50);
put("!", 0);
put("~", 0);
}};
static HashMap<String, String> escapes = new HashMap<String, String>() {{
// JSON string escape sequences - see json.org
put("\"", "\"");
put("\\", "\\");
put("/", "/");
put("b", "\b");
put("f", "\f");
put("n", "\n");
put("r", "\r");
put("t", "\t");
}};
// Tokenizer (lexer) - invoked by the parser to return one token at a time
String path;
int position = 0;
int length; // = path.length;
Tokenizer(String path) {
this.path = path;
length = path.length();
}
public static class Token {
String type;
Object value;
int position;
//
String id;
}
Token create(String type, Object value) {
Token t = new Token();
t.type = type; t.value = value; t.position = position;
return t;
}
boolean isClosingSlash(int position) {
if (path.charAt(position) == '/' && depth == 0) {
int backslashCount = 0;
while (path.charAt(position - (backslashCount + 1)) == '\\') {
backslashCount++;
}
if (backslashCount % 2 == 0) {
return true;
}
}
return false;
}
int depth;
Pattern scanRegex() {
// the prefix '/' will have been previously scanned. Find the end of the regex.
// search for closing '/' ignoring any that are escaped, or within brackets
int start = position;
//int depth = 0;
String pattern;
String flags;
while (position < length) {
char currentChar = path.charAt(position);
if (isClosingSlash(position)) {
// end of regex found
pattern = path.substring(start, position);
if (pattern.equals("")) {
throw new JException("S0301", position);
}
position++;
if (position < length) {
currentChar = path.charAt(position);
} else {
currentChar = 0;
}
// flags
start = position;
while (currentChar == 'i' || currentChar == 'm') {
position++;
if (position < length) {
currentChar = path.charAt(position);
} else {
currentChar = 0;
}
}
flags = path.substring(start, position) + 'g';
// Convert flags to Java Pattern flags
int _flags = 0;
if (flags.contains("i"))
_flags |= Pattern.CASE_INSENSITIVE;
if (flags.contains("m"))
_flags |= Pattern.MULTILINE;
return Pattern.compile(pattern, _flags); // Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
}
if ((currentChar == '(' || currentChar == '[' || currentChar == '{') && path.charAt(position - 1) != '\\') {
depth++;
}
if ((currentChar == ')' || currentChar == ']' || currentChar == '}') && path.charAt(position - 1) != '\\') {
depth--;
}
position++;
}
throw new JException("S0302", position);
};
Token next(boolean prefix) {
if (position >= length) return null;
char currentChar = path.charAt(position);
// skip whitespace
while (position < length && " \t\n\r".indexOf(currentChar) > -1) { // Uli: removed \v as Java doesn't support it
position++;
if (position >= length) return null; // Uli: JS relies on charAt returns null
currentChar = path.charAt(position);
}
// skip comments
if (currentChar == '/' && path.charAt(position + 1) == '*') {
var commentStart = position;
position += 2;
currentChar = path.charAt(position);
while (!(currentChar == '*' && path.charAt(position + 1) == '/')) {
currentChar = path.charAt(++position);
if (position >= length) {
// no closing tag
throw new JException("S0106", commentStart);
}
}
position += 2;
currentChar = path.charAt(position);
return next(prefix); // need this to swallow any following whitespace
}
// test for regex
if (prefix != true && currentChar == '/') {
position++;
return create("regex", scanRegex());
}
// handle double-char operators
boolean haveMore = position < path.length()-1; // Java: position+1 is valid
if (currentChar == '.' && haveMore && path.charAt(position + 1) == '.') {
// double-dot .. range operator
position += 2;
return create("operator", "..");
}
if (currentChar == ':' && haveMore && path.charAt(position + 1) == '=') {
// := assignment
position += 2;
return create("operator", ":=");
}
if (currentChar == '!' && haveMore && path.charAt(position + 1) == '=') {
// !=
position += 2;
return create("operator", "!=");
}
if (currentChar == '>' && haveMore && path.charAt(position + 1) == '=') {
// >=
position += 2;
return create("operator", ">=");
}
if (currentChar == '<' && haveMore && path.charAt(position + 1) == '=') {
// <=
position += 2;
return create("operator", "<=");
}
if (currentChar == '*' && haveMore && path.charAt(position + 1) == '*') {
// ** descendant wildcard
position += 2;
return create("operator", "**");
}
if (currentChar == '~' && haveMore && path.charAt(position + 1) == '>') {
// ~> chain function
position += 2;
return create("operator", "~>");
}
if (currentChar == '?' && haveMore && path.charAt(position + 1) == ':') {
// ?: default / elvis operator
position += 2;
return create("operator", "?:");
}
if (currentChar == '?' && haveMore && path.charAt(position + 1) == '?') {
// ?? coalescing operator
position += 2;
return create("operator", "??");
}
// test for single char operators
if (operators.get(""+currentChar)!=null) {
position++;
return create("operator", currentChar);
}
// test for string literals
if (currentChar == '"' || currentChar == '\'') {
char quoteType = currentChar;
// double quoted string literal - find end of string
position++;
var qstr = "";
while (position < length) {
currentChar = path.charAt(position);
if (currentChar == '\\') { // escape sequence
position++;
if (position < path.length()) currentChar = path.charAt(position); else throw new JException("S0103", position, "");
if (escapes.get(""+currentChar)!=null) {
qstr += escapes.get(""+currentChar);
} else if (currentChar == 'u') {
// u should be followed by 4 hex digits
String octets = position+5 < path.length() ? path.substring(position + 1, (position + 1) + 4) : "";
if (octets.matches("^[0-9a-fA-F]+$")) { // /^[0-9a-fA-F]+$/.test(octets)) {
int codepoint = Integer.parseInt(octets, 16);
qstr += Character.toString((char) codepoint);
position += 4;
} else {
throw new JException("S0104", position);
}
} else {
// illegal escape sequence
throw new JException("S0103", position, currentChar);
}
} else if (currentChar == quoteType) {
position++;
return create("string", qstr);
} else {
qstr += currentChar;
}
position++;
}
throw new JException("S0101", position);
}
// test for numbers
Pattern numregex = Pattern.compile("^-?(0|([1-9][0-9]*))(\\.[0-9]+)?([Ee][-+]?[0-9]+)?");
Matcher match = numregex.matcher(path.substring(position));
if (match.find()) {
double num = Double.parseDouble(match.group(0));
if (!Double.isNaN(num) && Double.isFinite(num)) {
position += match.group(0).length();
// If the number is integral, use long as type
return create("number", Utils.convertNumber(num));
} else {
throw new JException("S0102", position); //, match.group[0]);
}
}
// test for quoted names (backticks)
String name;
if (currentChar == '`') {
// scan for closing quote
position++;
int end = path.indexOf('`', position);
if (end != -1) {
name = path.substring(position, end);
position = end + 1;
return create("name", name);
}
position = length;
throw new JException("S0105", position);
}
// test for names
int i = position;
char ch;
while (true) {
//if (i>=length) return null; // Uli: JS relies on charAt returns null
ch = i<length ? path.charAt(i) : 0;
if (i == length || " \t\n\r".indexOf(ch) > -1 || operators.containsKey(""+ch)) { // Uli: removed \v
if (path.charAt(position) == '$') {
// variable reference
String _name = path.substring(position + 1, i);
position = i;
return create("variable", _name);
} else {
String _name = path.substring(position, i);
position = i;
switch (_name) {
case "or":
case "in":
case "and":
return create("operator", _name);
case "true":
return create("value", true);
case "false":
return create("value", false);
case "null":
return create("value", null);
default:
if (position == length && _name.equals("")) {
// whitespace at end of input
return null;
}
return create("name", _name);
}
}
} else {
i++;
}
}
}
}