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
|
package tlIItools;
import java.util.*;
/** String pairs for replacements.
*
* @author Ben Culkin */
public class ReplPair implements Comparable<ReplPair> {
/** Represents an error encountered parsing ReplPairs
*
* @author Ben Culkin */
public static class ReplError {
/** The line the error occured on. */
public int line;
/** The number of pairs we have processed so far. */
public int numPairs;
/** The text of the line we errored on. */
public String txt;
/** The message of the error. */
public String msg;
/** Create a new ReplPair parse error.
*
* @param lne The line the error occured on.
* @param nPairs The number of pairs processed up to this point.
* @param msg The message detailing the error.
* @param txt The text that caused the error. */
public ReplError(int lne, int nPairs, String msg, String txt) {
line = lne;
numPairs = nPairs;
this.txt = txt;
this.msg = msg;
}
@Override
public String toString() {
String errString;
if (txt == null) errString = "No associated line";
else if (txt.equals("")) errString = "Text of line was empty";
else errString = "Text of line was: " + txt;
return String.format("line %d, pair %d:%s\n\t%s", line, numPairs, msg, errString);
}
}
/** 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) {
return ReplPair.readList(new ArrayList<>(), 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) {
String errString;
if (errList.size() == 0) errString = "An error";
else errString = "Errors";
throw new IllegalArgumentException(String.format(
"%s occured parsing replacement pairs:\n%s",
errString, 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) {
int lno = 0;
int pno = 0;
int defPrior = 1;
List<ReplPair> resList = new ArrayList<>();
// For every line in the source...
while (scn.hasNextLine()) {
String name = scn.nextLine().trim();
lno += 1;
// If its commented or blank, skip it
if (name.equals("")) continue;
if (name.startsWith("#")) continue;
ReplPair rp = new ReplPair();
rp.priority = defPrior;
rp.find = name;
if (rp.name == null) rp.name = name;
// We started to process the pair, mark it as being
// started
pno += 1;
String body = null;
// Read in the next uncommented line
do {
if (!scn.hasNextLine()) {
String msg = "Ran out of input looking for replacement body for raw name " + name;
errs.add(new ReplError(lno, pno, msg, null));
break;
}
body = scn.nextLine().trim();
lno += 1;
} while (body.startsWith("#"));
rp.replace = body;
resList.add(rp);
}
return resList;
}
@Override
public String toString() {
return String.format("s/%s/%s/p%d", find, replace, priority);
}
@Override
public int compareTo(ReplPair rp) {
return this.priority - rp.priority;
}
}
|