-
Notifications
You must be signed in to change notification settings - Fork 778
/
Copy pathJFlexSymbolMatcher.java
468 lines (431 loc) · 16.7 KB
/
JFlexSymbolMatcher.java
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
*/
package org.opensolaris.opengrok.analysis;
import java.util.Set;
import java.util.regex.Pattern;
import org.opensolaris.opengrok.util.StringUtils;
/**
* Represents an abstract base class for subclasses of
* {@link JFlexStateStacker} that can publish as {@link ScanningSymbolMatcher}.
*/
public abstract class JFlexSymbolMatcher extends JFlexStateStacker
implements ScanningSymbolMatcher {
private SymbolMatchedListener symbolListener;
private NonSymbolMatchedListener nonSymbolListener;
private String disjointSpanClassName;
public String normalizeIdentifier(String id) { return id; }
/**
* Associates the specified listener, replacing the former one.
* @param l defined instance
*/
@Override
public void setSymbolMatchedListener(SymbolMatchedListener l) {
if (l == null) {
throw new IllegalArgumentException("`l' is null");
}
symbolListener = l;
}
/**
* Clears any association to a listener.
*/
@Override
public void clearSymbolMatchedListener() {
symbolListener = null;
}
/**
* Associates the specified listener, replacing the former one.
* @param l defined instance
*/
@Override
public void setNonSymbolMatchedListener(NonSymbolMatchedListener l) {
if (l == null) {
throw new IllegalArgumentException("`l' is null");
}
nonSymbolListener = l;
}
/**
* Clears any association to a listener.
*/
@Override
public void clearNonSymbolMatchedListener() {
nonSymbolListener = null;
}
/**
* Gets the class name value from the last call to
* {@link #onDisjointSpanChanged(java.lang.String, int)}.
* @return a defined value or null
*/
protected String getDisjointSpanClassName() {
return disjointSpanClassName;
}
/**
* Raises
* {@link SymbolMatchedListener#symbolMatched(org.opensolaris.opengrok.analysis.SymbolMatchedEvent)}
* for a subscribed listener.
* @param str the symbol string
* @param start the symbol start position
*/
protected void onSymbolMatched(String str, int start) {
SymbolMatchedListener l = symbolListener;
if (l != null) {
SymbolMatchedEvent evt = new SymbolMatchedEvent(this, str, normalizeIdentifier(str), start,
start + str.length());
l.symbolMatched(evt);
}
}
/**
* Raises
* {@link SymbolMatchedListener#sourceCodeSeen(org.opensolaris.opengrok.analysis.SourceCodeSeenEvent)}
* for all subscribed listeners in turn.
* @param start the source code start position
*/
protected void onSourceCodeSeen(int start) {
SymbolMatchedListener l = symbolListener;
if (l != null) {
SourceCodeSeenEvent evt = new SourceCodeSeenEvent(this, start);
l.sourceCodeSeen(evt);
}
}
/**
* Calls {@link #onNonSymbolMatched(java.lang.String, int)} with the
* {@link String#valueOf(char)} {@code c} and {@code start}.
* @param c the text character
* @param start the text start position
*/
protected void onNonSymbolMatched(char c, int start) {
onNonSymbolMatched(String.valueOf(c), start);
}
/**
* Raises
* {@link NonSymbolMatchedListener#nonSymbolMatched(org.opensolaris.opengrok.analysis.TextMatchedEvent)}
* for a subscribed listener.
* @param str the text string
* @param start the text start position
*/
protected void onNonSymbolMatched(String str, int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
TextMatchedEvent evt = new TextMatchedEvent(this, str, start,
start + str.length());
l.nonSymbolMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#nonSymbolMatched(org.opensolaris.opengrok.analysis.TextMatchedEvent)}
* for a subscribed listener.
* @param str the text string
* @param hint the text hint
* @param start the text start position
*/
protected void onNonSymbolMatched(String str, EmphasisHint hint,
int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
TextMatchedEvent evt = new TextMatchedEvent(this, str, hint, start,
start + str.length());
l.nonSymbolMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#keywordMatched(org.opensolaris.opengrok.analysis.TextMatchedEvent)}
* for a subscribed listener.
* @param str the text string
* @param start the text start position
*/
protected void onKeywordMatched(String str, int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
TextMatchedEvent evt = new TextMatchedEvent(this, str, start,
start + str.length());
l.keywordMatched(evt);
}
}
/**
* Calls {@link #setLineNumber(int)} with the sum of
* {@link #getLineNumber()} and the number of LFs in {@code str}, and then
* raises
* {@link NonSymbolMatchedListener#endOfLineMatched(org.opensolaris.opengrok.analysis.TextMatchedEvent)}
* for a subscribed listener.
* @param str the text string
* @param start the text start position
*/
protected void onEndOfLineMatched(String str, int start) {
setLineNumber(getLineNumber() + countLFs(str));
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
TextMatchedEvent evt = new TextMatchedEvent(this, str, start,
start + str.length());
l.endOfLineMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#disjointSpanChanged(org.opensolaris.opengrok.analysis.DisjointSpanChangedEvent)}
* for a subscribed listener.
* @param className the text string
* @param position the text position
*/
protected void onDisjointSpanChanged(String className, int position) {
disjointSpanClassName = className;
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
DisjointSpanChangedEvent evt = new DisjointSpanChangedEvent(this,
className, position);
l.disjointSpanChanged(evt);
}
}
/**
* Calls
* {@link #onUriMatched(java.lang.String, int, java.util.regex.Pattern)}
* with {@code uri}, {@code start}, and {@code null}.
* @param uri the URI string
* @param start the URI start position
*/
protected void onUriMatched(String uri, int start) {
onUriMatched(uri, start, null);
}
/**
* Raises
* {@link NonSymbolMatchedListener#linkageMatched(org.opensolaris.opengrok.analysis.LinkageMatchedEvent)}
* of {@link LinkageType#URI} for a subscribed listener.
* <p>First, the end of {@code uri} is possibly trimmed (with a
* corresponding call to {@link #yypushback(int)}) based on the result
* of {@link StringUtils#countURIEndingPushback(java.lang.String)} and
* optionally
* {@link StringUtils#countPushback(java.lang.String, java.util.regex.Pattern)}
* if {@code collateralCapture} is not null.
* <p>If the pushback count is equal to the length of {@code url}, then it
* is simply written -- and nothing is pushed back -- in order to avoid a
* never-ending {@code yylex()} loop.
*
* @param uri the URI string
* @param start the URI start position
* @param collateralCapture optional pattern to indicate characters which
* may have been captured as valid URI characters but in a particular
* context should mark the start of a pushback
*/
protected void onUriMatched(String uri, int start,
Pattern collateralCapture) {
int n = 0;
int subn;
do {
// An ending-pushback could be present before a collateral capture,
// so detect both in a loop (on a shrinking `url') until no more
// shrinking should occur.
subn = StringUtils.countURIEndingPushback(uri);
int ccn = StringUtils.countPushback(uri, collateralCapture);
if (ccn > subn) subn = ccn;
// Push back if positive, but not if equal to the current length.
if (subn > 0 && subn < uri.length()) {
uri = uri.substring(0, uri.length() - subn);
n += subn;
} else {
subn = 0;
}
} while (subn != 0);
if (n > 0) yypushback(n);
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
LinkageMatchedEvent evt = new LinkageMatchedEvent(this, uri,
LinkageType.URI, start, start + uri.length());
l.linkageMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#linkageMatched(org.opensolaris.opengrok.analysis.LinkageMatchedEvent)}
* of {@link LinkageType#FILELIKE} for a subscribed listener.
* @param str the text string
* @param start the text start position
*/
protected void onFilelikeMatched(String str, int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
LinkageMatchedEvent evt = new LinkageMatchedEvent(this, str,
LinkageType.FILELIKE, start, start + str.length());
l.linkageMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#pathlikeMatched(org.opensolaris.opengrok.analysis.PathlikeMatchedEvent)}
* for a subscribed listener.
* @param str the path text string
* @param sep the path separator
* @param canonicalize a value indicating whether the path should be
* canonicalized
* @param start the text start position
*/
protected void onPathlikeMatched(String str, char sep,
boolean canonicalize, int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
PathlikeMatchedEvent evt = new PathlikeMatchedEvent(this, str,
sep, canonicalize, start, start + str.length());
l.pathlikeMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#linkageMatched(org.opensolaris.opengrok.analysis.LinkageMatchedEvent)}
* of {@link LinkageType#EMAIL} for a subscribed listener.
* @param str the text string
* @param start the text start position
*/
protected void onEmailAddressMatched(String str, int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
LinkageMatchedEvent evt = new LinkageMatchedEvent(this, str,
LinkageType.EMAIL, start, start + str.length());
l.linkageMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#linkageMatched(org.opensolaris.opengrok.analysis.LinkageMatchedEvent)}
* of {@link LinkageType#LABEL} for a subscribed listener.
* @param str the text string (literal capture)
* @param start the text start position
* @param lstr the text link string
*/
protected void onLabelMatched(String str, int start, String lstr) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
LinkageMatchedEvent evt = new LinkageMatchedEvent(this, str,
LinkageType.LABEL, start, start + str.length(), lstr);
l.linkageMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#linkageMatched(org.opensolaris.opengrok.analysis.LinkageMatchedEvent)}
* of {@link LinkageType#LABELDEF} for a subscribed listener.
* @param str the text string (literal capture)
* @param start the text start position
*/
protected void onLabelDefMatched(String str, int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
LinkageMatchedEvent evt = new LinkageMatchedEvent(this, str,
LinkageType.LABELDEF, start, start + str.length());
l.linkageMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#linkageMatched(org.opensolaris.opengrok.analysis.LinkageMatchedEvent)}
* of {@link LinkageType#QUERY} for a subscribed listener.
* @param str the text string
* @param start the text start position
*/
protected void onQueryTermMatched(String str, int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
LinkageMatchedEvent evt = new LinkageMatchedEvent(this, str,
LinkageType.QUERY, start, start + str.length());
l.linkageMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#linkageMatched(org.opensolaris.opengrok.analysis.LinkageMatchedEvent)}
* of {@link LinkageType#REFS} for a subscribed listener.
* @param str the text string
* @param start the text start position
*/
protected void onRefsTermMatched(String str, int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
LinkageMatchedEvent evt = new LinkageMatchedEvent(this, str,
LinkageType.REFS, start, start + str.length());
l.linkageMatched(evt);
}
}
/**
* Raises
* {@link NonSymbolMatchedListener#scopeChanged(org.opensolaris.opengrok.analysis.ScopeChangedEvent)}
* for a subscribed listener.
* @param action the scope change action
* @param str the text string
* @param start the text start position
*/
protected void onScopeChanged(ScopeAction action, String str, int start) {
NonSymbolMatchedListener l = nonSymbolListener;
if (l != null) {
ScopeChangedEvent evt = new ScopeChangedEvent(this, action, str,
start, start + str.length());
l.scopeChanged(evt);
}
}
/**
* Calls
* {@link #onFilteredSymbolMatched(java.lang.String, int, java.util.Set, boolean)}
* with {@code str}, {@code start}, {@code keywords}, and {@code true}.
* @param str the text string
* @param start the text start position
* @param keywords an optional set to search for {@code str} as a member to
* indicate a keyword
* @return true if the {@code str} was not in {@code keywords} or if
* {@code keywords} was null
*/
protected boolean onFilteredSymbolMatched(String str, int start,
Set<String> keywords) {
return onFilteredSymbolMatched(str, start, keywords, true);
}
/**
* Raises {@link #onKeywordMatched(java.lang.String, int)} if
* {@code keywords} is not null and {@code str} is found as a member (in a
* case-sensitive or case-less search per {@code caseSensitive}); otherwise
* raises {@link #onSymbolMatched(java.lang.String, int)}.
* @param str the text string
* @param start the text start position
* @param keywords an optional set to search for {@code str} as a member to
* indicate a keyword
* @param caseSensitive a value indicating if {@code keywords} should be
* searched for {@code str} as-is ({@code true}) or if the lower-case
* equivalent of {@code str} should be used ({@code false}).
* @return true if the {@code str} was not in {@code keywords} or if
* {@code keywords} was null
*/
protected boolean onFilteredSymbolMatched(String str, int start,
Set<String> keywords, boolean caseSensitive) {
if (keywords != null) {
String check = caseSensitive ? str : str.toLowerCase();
if (keywords.contains(check)) {
onKeywordMatched(str, start);
return false;
}
}
onSymbolMatched(str, start);
return true;
}
private static int countLFs(String str) {
int n = 0;
for (int i = 0; i < str.length(); ++i) {
char c = str.charAt(i);
if (c == '\n') ++n;
}
return n;
}
}