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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
package bjc.everge;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.UnaryOperator;
import bjc.everge.ControlledString.Control;
/**
* String pairs for replacements.
*
* @author Ben Culkin
*/
public class ReplPair implements Comparable<ReplPair>, UnaryOperator<String> {
// Line number we read this pair from
private int lno;
// Stage this pair is in
private int stage;
// Status of this pair with regards to doing staging stuff
private StageStatus stat = StageStatus.BOTH;
/**
* The priority for this replacement.
*/
public int priority;
/**
* The name of this replacement.
*
* Defaults to the 'find' string.
*/
public String name;
/**
* The string to look for.
*/
public String find;
/**
* The string to replace it with.
*/
public String replace;
/**
* Create a new blank replacement pair.
*/
public ReplPair() {
this("", "", 1, null);
}
/**
* Create a new replacement pair with a priority of 1.
*
* @param f
* The string to find.
* @param r
* The string to replace.
*/
public ReplPair(String f, String r) {
this(f, r, 1);
}
/**
* Create a new named replacement pair with a priority of 1.
*
* @param f
* The string to find.
* @param r
* The string to replace.
* @param n
* The name of the replacement pair.
*/
public ReplPair(String f, String r, String n) {
this(f, r, 1, n);
}
/**
* Create a new replacement pair with a set priority.
*
* @param f
* The string to find.
* @param r
* The string to replace.
* @param p
* The priority for the replacement.
*/
public ReplPair(String f, String r, int p) {
this(f, r, p, f);
}
/**
* Create a new replacement pair with a set priority and name.
*
* @param f
* The string to find.
* @param r
* The string to replace.
* @param n
* The name of the replacement pair.
* @param p
* The priority for the replacement.
*/
public ReplPair(String f, String r, int p, String n) {
find = f;
replace = r;
name = n;
priority = p;
}
/**
* Read a list of replacement pairs from an input source.
*
* @param scn
* The source to read the replacements from.
* @return
* The list of replacements.
*/
public static List<ReplPair> readList(Scanner scn) {
List<ReplPair> lst = new ArrayList<>();
return readList(lst, scn);
}
/**
* Read a list of replacement pairs from an input source, adding them to
* an existing list.
*
* @param detals
* The list to add the replacements to.
* @param scn
* The source to read the replacements from.
* @return
* The list of replacements.
*/
public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn) {
List<ReplError> errList = new ArrayList<>();
List<ReplPair> rplPar = readList(detals, scn, errList);
if (errList.size() != 0) {
throw new ReplParseException("", errList);
}
return rplPar;
}
/**
* Read a list of replacement pairs from an input source, adding them to
* an existing list.
*
* @param detals
* The list to add the replacements to.
* @param scn
* The source to read the replacements from.
* @param errs
* The list to stick errors in.
* @return
* The list of replacements.
*/
public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn, List<ReplError> errs) {
return readList(detals, scn, errs, new ReplOpts());
}
/**
* Read a list of replacement pairs from an input source, adding them to
* an existing list.
*
* @param detals
* The list to add the replacements to.
* @param scn
* The source to read the replacements from.
* @param errs
* The list to stick errors in.
* @param ropts
* The options to use when reading the pairs.
* @return
* The list of replacements.
*/
public static List<ReplPair> readList(List<ReplPair> detals, Scanner scn,
List<ReplError> errs, ReplOpts ropts) {
IntHolder lno = new IntHolder();
IntHolder pno = new IntHolder();
List<List<ReplPair>> stages = new ArrayList<>();
stages.add(new ArrayList<ReplPair>());
// For every line in the source...
while (scn.hasNextLine()) {
String name = scn.nextLine().trim();
lno.incr();
// If its commented or blank, skip it
if (name.equals("")) continue;
if (name.startsWith("#")) continue;
// Global control. Process it.
if (name.startsWith("|//")) {
readGlobal(name, scn, errs, ropts, lno, pno);
continue;
}
ReplPair rp = new ReplPair();
rp.priority = ropts.defPrior;
rp.stat = ropts.defStatus;
rp.lno = lno.get();
rp.stage = ropts.defStage;
boolean isMulti = ropts.defMulti;
{
String tmpName = readName(name, scn, errs, rp, ropts, lno, pno);
if (tmpName == null) continue;
name = tmpName;
}
rp.find = name;
if (rp.name == null) rp.name = name;
// We started to process the pair, mark it as being
// started
pno.incr();
String body = null;
// Read in the next uncommented line
do {
if (!scn.hasNextLine()) break;
body = scn.nextLine().trim();
lno.incr();
} while (body.startsWith("#"));
if (body == null) {
String msg =
"Ran out of input looking for replacement body for raw name '" + name + "'";
errs.add(new ReplError(lno, pno, msg, null));
break;
}
isMulti = ropts.defMulti;
// Body has attached controls, process them.
if (body.startsWith("//")) {
body = body.substring(2);
String[] bodyBits = StringUtils.escapeSplit("|", "//", body);
if (bodyBits.length < 2) {
String msg = "Did not find control terminator (//) in body where it should be";
errs.add(new ReplError(lno, pno, msg, body));
continue;
}
String contBody = bodyBits[0];
String actBody = bodyBits[1];
// Split out each control
String[] bits = StringUtils.escapeSplit("|", ";", actBody);
for (String bit : bits) {
String bitHead = bit.toUpperCase();
String bitBody = bit;
String[] bots = StringUtils.escapeSplit("|", "/", bit);
if (bots.length > 1) {
bitHead = bots[0].toUpperCase();
bitBody = bots[1];
}
switch (bitHead) {
case "MULTITRUE":
case "MULTIT":
case "MT":
isMulti = true;
break;
case "MULTIFALSE":
case "MULTIF":
case "MF":
isMulti = false;
break;
case "MULTI":
case "M":
isMulti = Boolean.parseBoolean(bitBody);
break;
default:
errs.add(new ReplError(lno, pno, String.format("Invalid control name '%s'", bitHead), body));
break;
}
}
body = actBody;
}
if (isMulti) {
String tmp = readMultiLine(body, scn, ropts, errs, "body", lno);
if (tmp == null) continue;
body = tmp;
}
rp.replace = body;
List<ReplPair> stageList = null;
if (rp.stage == 0 || stages.size() < (rp.stage - 1)) {
stageList = stages.get(rp.stage);
if (stageList == null) {
stageList = new ArrayList<>();
stages.add(rp.stage, stageList);
}
} else {
for (int i = stages.size(); i <= rp.stage; i++) {
stages.add(new ArrayList<>());
}
stageList = stages.get(rp.stage);
}
if (ropts.isTrace) {
ropts.errStream.printf("\t[DEBUG] Stage %d: Added %s\n\t\tContents: %s\n",
rp.stage, rp, stageList);
}
stageList.add(rp);
}
// Special-case one-stage processing.
if (stages.size() == 1) {
if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Executing single-stage bypass\n");
for (ReplPair rp : stages.iterator().next()) {
if (rp.stat == StageStatus.INTERNAL) {
if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Excluding internal RP %s\n", rp);
continue;
}
detals.add(rp);
}
detals.sort(null);
return detals;
}
// Handle stages
List<ReplPair> tmpList = new ArrayList<>();
tmpList.addAll(detals);
if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Stages: %s\n", stages);
int procStg = 0;
for (List<ReplPair> stageList : stages) {
procStg += 1;
List<ReplPair> curStage = new ArrayList<>();
if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Staging stage %d of %d: %s\n",
procStg, stageList.size(), stageList);
for (ReplPair rp : stageList) {
// Process through every pair in the previous
// stages
for (ReplPair curPar : tmpList) {
String tmp = rp.replace.replaceAll(curPar.find, curPar.replace);
if (ropts.isTrace && !rp.replace.equals(tmp)) {
ropts.errStream.printf("\t[DEBUG] Staged '%s' -> '%s'\t%s\n",
rp.replace, tmp, curPar);
}
rp.replace = tmp;
}
// If we're external; add straight to the output
if (rp.stat == StageStatus.EXTERNAL) {
if (ropts.isTrace) {
ropts.errStream.printf("\t[DEBUG] Skipped external for staging: %s\n",
rp);
}
detals.add(rp);
} else {
if (ropts.isTrace) {
ropts.errStream.printf("\t[DEBUG] Added to stage %d: %s\n\t\tContents: %s\n",
procStg, rp, curStage);
}
curStage.add(rp);
}
}
tmpList.addAll(curStage);
tmpList.sort(null);
}
// Copy over to output, excluding internals
for (ReplPair rp : tmpList) {
if (rp.stat == StageStatus.INTERNAL) {
if (ropts.isTrace) ropts.errStream.printf("\t[DEBUG] Excluded internal: %s\n", rp);
continue;
}
detals.add(rp);
}
detals.sort(null);
if (ropts.isTrace) {
ropts.errStream.printf("\t[DEBUG] Final output: %s\n", detals);
}
return detals;
}
private static String readMultiLine(String lead, Scanner src, ReplOpts ropts,
List<ReplError> errs, String typ, IntHolder lno) {
String tmp = lead;
if (ropts.isTrace && tmp.endsWith("\\"))
ropts.errStream.printf("\t[TRACE] Starting multi-line parse for %s '%s'\n", typ, tmp);
boolean didMulti = tmp.endsWith("\\");
while (tmp.endsWith("\\")) {
boolean incNL = tmp.endsWith("|\\");
if (!src.hasNextLine()) break;
String nxt = src.nextLine().trim();
lno.incr();
if (nxt.startsWith("#")) continue;
String nlStr = incNL ? "\n" : "";
if (tmp.endsWith("\\")) {
if (incNL) {
tmp = tmp.substring(0, tmp.length() - 2);
} else {
tmp = tmp.substring(0, tmp.length() - 1);
}
}
tmp = String.format("%s%s%s", tmp, nlStr, nxt);
}
if (ropts.isTrace && didMulti)
ropts.errStream.printf("\t[TRACE] Finished multi-line parse for %s:\n%s\n.\n",
typ, tmp);
return tmp;
}
@Override
public String apply(String inp) {
return inp.replaceAll(find, replace);
}
@Override
public String toString() {
String nameStr = "";
if (!find.equals(name)) nameStr = String.format("(%s)", name);
return String.format("%ss/%s/%s/p(%d)", nameStr, find, replace, priority);
}
@Override
public int compareTo(ReplPair rp) {
if (this.priority == rp.priority) return this.lno - rp.lno;
return rp.priority - this.priority;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (!getClass().equals(o.getClass())) return false;
ReplPair ro = (ReplPair)o;
if (!find.equals(ro.find)) return false;
// lno is not a field we consider for equality
if (!name.equals(ro.name)) return false;
if (priority != ro.priority) return false;
if (!replace.equals(ro.name)) return false;
// stat is not a field we consider for equality
return true;
}
private static String readName(String nam, Scanner scn, List<ReplError> errs,
ReplPair rp, ReplOpts ropts, IntHolder lno, IntHolder pno) {
String name = nam;
boolean isMulti = ropts.defMulti;
// Name has attached controls, process them.
if (name.startsWith("//")) {
name = name.substring(2);
String[] nameBits = StringUtils.escapeSplit("|", "//", name);
if (nameBits.length < 2) {
String msg = "Did not find control terminator (//) in name where it should be";
errs.add(new ReplError(lno, pno, msg, name));
return null;
}
String contName = nameBits[0];
String actName = nameBits[1];
// Split out each control
String[] bits = StringUtils.escapeSplit("|", ";", contName);
for (String bit : bits) {
String bitHead = bit.toUpperCase();
String bitBody = bit;
String[] bots = StringUtils.escapeSplit("|", "/", bit);
if (bots.length > 1) {
bitHead = bots[0].toUpperCase();
bitBody = bots[1];
}
switch (bitHead) {
case "NAME":
case "N":
rp.name = bitBody;
break;
case "PRIORITY":
case "PRIOR":
case "P":
try {
rp.priority = Integer.parseInt(bitBody);
} catch (NumberFormatException nfex) {
String errMsg = String.format("'%s' is not a valid priority (must be an integer)", bitBody);
errs.add(new ReplError(lno, pno, errMsg, name));
}
break;
case "STAGE":
case "S":
try {
int tmpStage = Integer.parseInt(bitBody);
if (tmpStage < 0) {
String errMsg = String.format("'%s' is not a valid stage (must be a positive integer)", bitBody);
errs.add(new ReplError(lno, pno, errMsg, name));
break;
}
rp.stage = tmpStage;
} catch (NumberFormatException nfex) {
String errMsg = String.format("'%s' is not a valid stage (must be a positive integer)", bitBody);
errs.add(new ReplError(lno, pno, errMsg, name));
}
break;
case "MULTITRUE":
case "MULTIT":
case "MT":
isMulti = true;
break;
case "MULTIFALSE":
case "MULTIF":
case "MF":
isMulti = false;
break;
case "MULTI":
case "M":
isMulti = Boolean.parseBoolean(bitBody);
break;
case "INTERNAL":
case "INT":
case "I":
rp.stat = StageStatus.INTERNAL;
break;
case "EXTERNAL":
case "EXT":
case "E":
rp.stat = StageStatus.EXTERNAL;
break;
case "BOTH":
case "B":
rp.stat = StageStatus.BOTH;
break;
default:
{
ReplError erd = new ReplError(lno, pno,
String.format("Unknown control name '%s' for name '%s'",
bitHead, name), name);
errs.add(erd);
}
break;
}
name = actName;
}
// Multi-line name with a trailer
if (isMulti) {
String tmp = readMultiLine(name, scn, ropts, errs, "name", lno);
if (tmp == null) return null;
name = tmp;
}
}
return name;
}
private static void readGlobal(String nam, Scanner scn, List<ReplError> errs,
ReplOpts ropts, IntHolder lno, IntHolder pno) {
String name = nam.substring(3);
// Split out each control
String[] bits = StringUtils.escapeSplit("|", ";", name);
if (ropts.isTrace) {
ropts.errStream.printf("\t[TRACE] Split control bits are: \n");
for (String bit : bits) {
ropts.errStream.printf("%s, ", bit);
}
ropts.errStream.println();
}
for (String bit : bits) {
String bitHead = bit.toUpperCase();
String bitBody = bit;
String[] bots = StringUtils.escapeSplit("|", "/", bit);
if (bots.length > 1) {
bitHead = bots[0];
bitBody = bots[1];
}
switch (bitHead) {
case "PRIORITY":
case "PRIOR":
case "P":
try {
int tmp = Integer.parseInt(bitBody);
ropts.defPrior = tmp;
} catch (NumberFormatException nfex) {
String errMsg = String.format("'%s' is not a valid priority (must be an integer)",
bitBody);
errs.add(new ReplError(lno, pno, errMsg, name));
}
break;
case "STAGE":
case "S":
try {
int tmpStage = Integer.parseInt(bitBody);
if (tmpStage < 0) {
String errMsg = String.format("'%s' is not a valid stage (must be a positive integer)",
bitBody);
errs.add(new ReplError(lno, pno, errMsg, name));
break;
}
ropts.defStage = tmpStage;
} catch (NumberFormatException nfex) {
String errMsg = String.format("'%s' is not a valid stage (must be a positive integer)",
bitBody);
errs.add(new ReplError(lno, pno, errMsg, name));
}
break;
case "MULTITRUE":
case "MULTIT":
case "MT":
ropts.defMulti = true;
break;
case "MULTIFALSE":
case "MULTIF":
case "MF":
ropts.defMulti = false;
break;
case "MULTI":
case "M":
ropts.defMulti = Boolean.parseBoolean(bitBody);
break;
case "INTERNAL":
case "INT":
case "I":
ropts.defStatus = StageStatus.INTERNAL;
break;
case "EXTERNAL":
case "EXT":
case "E":
ropts.defStatus = StageStatus.EXTERNAL;
break;
case "BOTH":
case "B":
ropts.defStatus = StageStatus.BOTH;
break;
case "DEBUGTRUE":
case "DEBUGT":
case "DT":
ropts.isDebug = true;
break;
case "DEBUGFALSE":
case "DEBUGF":
case "DF":
ropts.isDebug = false;
break;
case "DEBUG":
case "D":
ropts.isDebug = Boolean.parseBoolean(bitBody);
break;
case "TRACETRUE":
case "TRACET":
case "TT":
ropts.isTrace = true;
break;
case "TRACEFALSE":
case "TRACEF":
case "TF":
ropts.isTrace = false;
break;
case "TRACE":
case "T":
ropts.isTrace = Boolean.parseBoolean(bitBody);
break;
case "PERFTRUE":
case "PERFT":
case "PRFT":
ropts.isPerf = true;
break;
case "PERFFALSE":
case "PERFF":
case "PRFF":
ropts.isPerf = false;
break;
case "PERF":
case "PRF":
ropts.isPerf = Boolean.parseBoolean(bitBody);
break;
default:
{
String msg = String.format("Invalid global control name '%s'", bitHead);
ReplError err = new ReplError(lno, pno, msg, name);
errs.add(err);
}
break;
}
if (ropts.isTrace)
ropts.errStream.printf("\t[TRACE] Processed global control '%s':'%s'\n",
bitHead, bitBody);
}
return;
}
}
|