forked from reportmill/SnapKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenizer.java
More file actions
355 lines (299 loc) · 9.5 KB
/
Tokenizer.java
File metadata and controls
355 lines (299 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
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
/*
* Copyright (c) 2010, ReportMill Software. All rights reserved.
*/
package snap.parse;
import snap.util.ArrayUtils;
import snap.util.CharSequenceUtils;
import java.util.regex.Matcher;
/**
* A class to extract tokens from a char sequence.
*/
public class Tokenizer {
// An array of regexes
private Regex[] _regexes = new Regex[0];
// The tokenizer input
private CharSequence _input;
// The input length
private int _length;
// The current char index
protected int _charIndex;
// A map of char to matchers
private Regex[][] _charMatchers = new Regex[128][];
// The current token line
protected TokenLine _tokenLine;
// The current token doc
private TokenDoc _tokenDoc;
// Constants for common special token names
public static final String SINGLE_LINE_COMMENT = "SingleLineComment";
public static final String MULTI_LINE_COMMENT = "MultiLineComment";
/**
* Constructor.
*/
public Tokenizer()
{
super();
}
/**
* Returns the array of regexes.
*/
public Regex[] getRegexes() { return _regexes; }
/**
* Sets the array of regexes.
*/
public void setRegexes(Regex[] theRegexes) { _regexes = theRegexes; }
/**
* Sets regexes for pattern rules in given rule.
*/
public void setRegexesForPatternRulesInRule(ParseRule aRule)
{
ParseRule[] patternRules = ParseUtils.getPatternRulesForRule(aRule);
Regex[] regexes = ArrayUtils.map(patternRules, rule -> new Regex(rule.getName(), rule.getPattern()), Regex.class);
setRegexes(regexes);
}
/**
* Returns the current tokenizer input.
*/
public CharSequence getInput() { return _input; }
/**
* Sets the current tokenizer input.
*/
public void setInput(CharSequence anInput)
{
// Set input
_input = anInput;
_length = _input.length();
// Reset char index
setCharIndex(0);
// Reset token doc, line
_tokenDoc = null;
_tokenLine = null;
getTokenLine();
// Reset matchers
for (Regex regex : _regexes)
regex.getMatcher().reset(_input);
}
/**
* CharSequence method.
*/
public final int length() { return _length; }
/**
* CharSequence method.
*/
public final char charAt(int anIndex)
{
return _input.charAt(anIndex);
}
/**
* Returns whether another char is available.
*/
public final boolean hasChar()
{
return _charIndex < length();
}
/**
* Returns whether another given number of chars is available.
*/
public final boolean hasChars(int aVal)
{
return _charIndex + aVal <= length();
}
/**
* Returns the next parse char.
*/
public final char nextChar() { return _input.charAt(_charIndex); }
/**
* Returns the char at the current index plus offset.
*/
public final char nextCharAt(int anOffset) { return _input.charAt(_charIndex + anOffset); }
/**
* Returns whether next chars start with given string.
*/
public boolean nextCharsStartWith(CharSequence startChars)
{
// If not enough chars, return false
int charsLength = startChars.length();
if (!hasChars(charsLength))
return false;
// Iterate over startChars and return false if any don't match nextChars
for (int charIndex = 0; charIndex < charsLength; charIndex++) {
if (startChars.charAt(charIndex) != nextCharAt(charIndex))
return false;
}
// Return true
return true;
}
/**
* Returns the char at the current index plus offset.
*/
public final char eatChar()
{
// Get next char
char eatChar = _input.charAt(_charIndex++);
// If newline, look for Windows sister newline char and eat that too and clear token line
if (eatChar == '\n' || eatChar == '\r') {
if (eatChar == '\r' && hasChar() && nextChar() == '\n')
_charIndex++;
_tokenLine = null;
getTokenLine();
}
// Return
return eatChar;
}
/**
* Advances charIndex by given char count.
*/
public void eatChars(int charCount)
{
for (int i = 0; i < charCount; i++)
eatChar();
}
/**
* Eats the chars till line end.
*/
public void eatCharsTillLineEnd()
{
// Eat chars till line end or text end
while (hasChar() && !CharSequenceUtils.isLineEndChar(nextChar()))
eatChar();
// Eat line end
if (hasChar())
eatChar();
}
/**
* Returns the current parse char location.
*/
public final int getCharIndex() { return _charIndex; }
/**
* Sets the current parse char location.
*/
public void setCharIndex(int aValue)
{
_charIndex = aValue;
}
/**
* Returns the next token.
*/
public ParseToken getNextToken()
{
// Get next special token
ParseToken specialToken = getNextSpecialToken();
if (specialToken != null) {
TokenLine tokenLine = getTokenLine();
tokenLine.addSpecialToken(specialToken);
}
// Get list of matchers for next char
char nextChar = hasChar() ? nextChar() : 0;
Regex[] regexes = nextChar < 128 ? getRegexesForStartChar(nextChar) : getRegexes();
// Iterate over regular expressions to find best match
Regex match = null;
int matchEnd = _charIndex;
for (Regex regex : regexes) {
// Get matcher
Matcher matcher = regex.getMatcher();
matcher.region(_charIndex, _input.length());
// Find pattern
if (matcher.lookingAt()) {
if (match == null || matcher.end() > matchEnd ||
(matcher.end() == matchEnd && regex.getLiteralLength() > match.getLiteralLength())) {
match = regex;
matchEnd = matcher.end();
}
}
}
// If no match, return null
if (match == null) {
// If no more chars, just return null
if (!hasChar())
return null;
// Get next chars and let tokenizerFailed() decide whether to throw, stop or provide alt token
String nextChars = getInput().subSequence(_charIndex, Math.min(_charIndex + 30, length())).toString();
ParseToken nextToken = tokenizerFailed(nextChars);
return nextToken;
}
// Create new token for match
String matchName = match.getName();
String matchPattern = match.getPattern();
ParseToken token = createTokenForProps(matchName, matchPattern, _charIndex, matchEnd);
// Reset end and return
_charIndex = matchEnd;
return token;
}
/**
* Returns list of Regex for a starting char.
*/
private Regex[] getRegexesForStartChar(char aChar)
{
// Get cached regex array for char, just return if found
Regex[] regexesForChar = _charMatchers[aChar];
if (regexesForChar != null)
return regexesForChar;
// If bogus char, just return
if (aChar == 0)
return _charMatchers[aChar] = new Regex[0];
// Get matching Regexes for char
String charStr = Character.toString(aChar);
Regex[] matchingRegexes = ArrayUtils.filter(_regexes, regex -> regex.isMatchingStartChar(aChar, charStr));
return _charMatchers[aChar] = matchingRegexes;
}
/**
* Returns the current token line.
*/
public TokenLine getTokenLine()
{
if (_tokenLine != null) return _tokenLine;
// Create new token line
TokenDoc tokenDoc = getTokenDoc();
TokenLine lastLine = tokenDoc.getLastLine();
int startCharIndex = lastLine != null ? lastLine.getEndCharIndex() : 0;
int endCharIndex = CharSequenceUtils.indexAfterNewlineOrEnd(_input, startCharIndex);
TokenLine tokenLine = tokenDoc.addLineForCharRange(startCharIndex, endCharIndex);
// Set and return
return _tokenLine = tokenLine;
}
/**
* Returns the current token doc.
*/
public TokenDoc getTokenDoc()
{
if (_tokenDoc != null) return _tokenDoc;
return _tokenDoc = new TokenDoc(_input);
}
/**
* Creates a new token for given properties.
*/
public ParseToken createTokenForProps(String aName, String aPattern, int aStart, int anEnd)
{
ParseTokenImpl token = new ParseTokenImpl();
token._tokenLine = getTokenLine();
token._name = aName;
token._pattern = aPattern;
token._startCharIndex = aStart;
token._endCharIndex = anEnd;
return token;
}
/**
* Processes and returns a special token if found.
* This version just skips whitespace.
*/
public ParseToken getNextSpecialToken()
{
skipWhiteSpace();
return null;
}
/**
* Gobble input characters until next non-whitespace or input end.
*/
protected void skipWhiteSpace()
{
while (hasChar() && Character.isWhitespace(nextChar()))
eatChar();
}
/**
* Called when next chars don't conform to any known token pattern.
* Default implementation just throws ParseExcpetion.
*/
protected ParseToken tokenizerFailed(String nextChars)
{
throw new ParseException("Token not found for: " + nextChars);
}
}