forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceUtils.java
More file actions
325 lines (258 loc) · 9.88 KB
/
SourceUtils.java
File metadata and controls
325 lines (258 loc) · 9.88 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
package processing.mode.java.pdex;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import processing.mode.java.pdex.TextTransform.Edit;
import processing.mode.java.preproc.PdePreprocessor;
public class SourceUtils {
public static final Pattern IMPORT_REGEX =
Pattern.compile("(?:^|;)\\s*(import\\s+(?:(static)\\s+)?((?:\\w+\\s*\\.)*)\\s*(\\S+)\\s*;)",
Pattern.MULTILINE | Pattern.DOTALL);
public static final Pattern IMPORT_REGEX_NO_KEYWORD =
Pattern.compile("^\\s*((?:(static)\\s+)?((?:\\w+\\s*\\.)*)\\s*(\\S+))",
Pattern.MULTILINE | Pattern.DOTALL);
public static List<ImportStatement> parseProgramImports(CharSequence source) {
List<ImportStatement> result = new ArrayList<>();
Matcher matcher = IMPORT_REGEX.matcher(source);
while (matcher.find()) {
ImportStatement is = ImportStatement.parse(matcher.toMatchResult());
result.add(is);
}
return result;
}
public static List<Edit> parseProgramImports(CharSequence source,
List<ImportStatement> outImports) {
List<Edit> result = new ArrayList<>();
Matcher matcher = IMPORT_REGEX.matcher(source);
while (matcher.find()) {
ImportStatement is = ImportStatement.parse(matcher.toMatchResult());
outImports.add(is);
int idx = matcher.start(1);
int len = matcher.end(1) - idx;
// Remove the import from the main program
// Substitute with white spaces
result.add(Edit.move(idx, len, 0));
result.add(Edit.insert(0, "\n"));
}
return result;
}
public static final Pattern TYPE_CONSTRUCTOR_REGEX =
Pattern.compile("(?:^|\\W)(int|char|float|boolean|byte)(?:\\s*\\()",
Pattern.MULTILINE);
public static List<Edit> replaceTypeConstructors(CharSequence source) {
List<Edit> result = new ArrayList<>();
Matcher matcher = TYPE_CONSTRUCTOR_REGEX.matcher(source);
while (matcher.find()) {
String match = matcher.group(1);
int offset = matcher.start(1);
int length = match.length();
result.add(Edit.insert(offset, "PApplet."));
String replace = "parse"
+ Character.toUpperCase(match.charAt(0)) + match.substring(1);
result.add(Edit.replace(offset, length, replace));
}
return result;
}
public static final Pattern HEX_LITERAL_REGEX =
Pattern.compile("(?:^|\\W)(#[A-Fa-f0-9]{6})(?:\\W|$)");
public static List<Edit> replaceHexLiterals(CharSequence source) {
// Find all #[webcolor] and replace with 0xff[webcolor]
// Should be 6 digits only.
List<Edit> result = new ArrayList<>();
Matcher matcher = HEX_LITERAL_REGEX.matcher(source);
while (matcher.find()) {
int offset = matcher.start(1);
result.add(Edit.replace(offset, 1, "0xff"));
}
return result;
}
public static List<Edit> insertImports(List<ImportStatement> imports) {
List<Edit> result = new ArrayList<>();
for (ImportStatement imp : imports) {
result.add(Edit.insert(0, imp.getFullSourceLine() + "\n"));
}
return result;
}
public static List<Edit> wrapSketch(PdePreprocessor.Mode mode, String className, int sourceLength) {
List<Edit> edits = new ArrayList<>();
StringBuilder b = new StringBuilder();
// Header
if (mode != PdePreprocessor.Mode.JAVA) {
b.append("\npublic class ").append(className).append(" extends PApplet {\n");
if (mode == PdePreprocessor.Mode.STATIC) {
b.append("public void setup() {\n");
}
}
edits.add(Edit.insert(0, b.toString()));
// Reset builder
b.setLength(0);
// Footer
if (mode != PdePreprocessor.Mode.JAVA) {
if (mode == PdePreprocessor.Mode.STATIC) {
// no noLoop() here so it does not tell you
// "can't invoke noLoop() on obj" when you type "obj."
b.append("\n}");
}
b.append("\n}\n");
}
edits.add(Edit.insert(sourceLength, b.toString()));
return edits;
}
public static List<Edit> addPublicToTopLevelMethods(CompilationUnit cu) {
List<Edit> edits = new ArrayList<>();
// Add public modifier to top level methods
for (Object node : cu.types()) {
if (node instanceof TypeDeclaration) {
TypeDeclaration type = (TypeDeclaration) node;
for (MethodDeclaration method : type.getMethods()) {
if (method.modifiers().isEmpty() && !method.isConstructor()) {
edits.add(Edit.insert(method.getStartPosition(), "public "));
}
}
}
}
return edits;
}
// Verifies that whole input String is floating point literal. Can't be used for searching.
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-DecimalFloatingPointLiteral
public static final Pattern FLOATING_POINT_LITERAL_VERIFIER;
static {
final String DIGITS = "(?:[0-9]|[0-9][0-9_]*[0-9])";
final String EXPONENT_PART = "(?:[eE][+-]?" + DIGITS + ")";
FLOATING_POINT_LITERAL_VERIFIER = Pattern.compile(
"(?:^" + DIGITS + "\\." + DIGITS + "?" + EXPONENT_PART + "?[fFdD]?$)|" +
"(?:^\\." + DIGITS + EXPONENT_PART + "?[fFdD]?$)|" +
"(?:^" + DIGITS + EXPONENT_PART + "[fFdD]?$)|" +
"(?:^" + DIGITS + EXPONENT_PART + "?[fFdD]$)");
}
public static List<Edit> replaceColorAndFixFloats(CompilationUnit cu) {
final List<Edit> edits = new ArrayList<>();
// Walk the tree, replace "color" with "int" and add 'f' to floats
cu.accept(new ASTVisitor() {
@Override
public boolean visit(SimpleType node) {
if ("color".equals(node.getName().toString())) {
edits.add(Edit.replace(node.getStartPosition(), node.getLength(), "int"));
}
return super.visit(node);
}
@Override
public boolean visit(NumberLiteral node) {
String s = node.getToken().toLowerCase();
if (FLOATING_POINT_LITERAL_VERIFIER.matcher(s).matches() && !s.endsWith("f") && !s.endsWith("d")) {
edits.add(Edit.insert(node.getStartPosition() + node.getLength(), "f"));
}
return super.visit(node);
}
});
return edits;
}
public static final Pattern COLOR_TYPE_REGEX =
Pattern.compile("(?:^|^\\p{javaJavaIdentifierPart})(color)\\s(?!\\s*\\()",
Pattern.MULTILINE | Pattern.UNICODE_CHARACTER_CLASS);
public static List<Edit> replaceColorRegex(CharSequence source) {
final List<Edit> edits = new ArrayList<>();
Matcher matcher = COLOR_TYPE_REGEX.matcher(source);
while (matcher.find()) {
int offset = matcher.start(1);
edits.add(Edit.replace(offset, 5, "int"));
}
return edits;
}
public static final Pattern NUMBER_LITERAL_REGEX =
Pattern.compile("[-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?");
public static List<Edit> fixFloatsRegex(CharSequence source) {
final List<Edit> edits = new ArrayList<>();
Matcher matcher = NUMBER_LITERAL_REGEX.matcher(source);
while (matcher.find()) {
int offset = matcher.start();
int end = matcher.end();
String group = matcher.group().toLowerCase();
boolean isFloatingPoint = group.contains(".") || group.contains("e");
boolean hasSuffix = end < source.length() &&
Character.toLowerCase(source.charAt(end)) != 'f' &&
Character.toLowerCase(source.charAt(end)) != 'd';
if (isFloatingPoint && !hasSuffix) {
edits.add(Edit.insert(offset, "f"));
}
}
return edits;
}
static public String scrubCommentsAndStrings(String p) {
StringBuilder sb = new StringBuilder(p);
scrubCommentsAndStrings(sb);
return sb.toString();
}
static public void scrubCommentsAndStrings(StringBuilder p) {
final int length = p.length();
final int OUT = 0;
final int IN_BLOCK_COMMENT = 1;
final int IN_EOL_COMMENT = 2;
final int IN_STRING_LITERAL = 3;
final int IN_CHAR_LITERAL = 4;
int blockStart = -1;
int prevState = OUT;
int state = OUT;
for (int i = 0; i <= length; i++) {
char ch = (i < length) ? p.charAt(i) : 0;
char pch = (i == 0) ? 0 : p.charAt(i-1);
switch (state) {
case OUT:
switch (ch) {
case '\'': state = IN_CHAR_LITERAL; break;
case '"': state = IN_STRING_LITERAL; break;
case '*': if (pch == '/') state = IN_BLOCK_COMMENT; break;
case '/': if (pch == '/') state = IN_EOL_COMMENT; break;
}
break;
case IN_BLOCK_COMMENT:
if (pch == '*' && ch == '/' && (i - blockStart) > 1) {
state = OUT;
}
break;
case IN_EOL_COMMENT:
if (ch == '\r' || ch == '\n') {
state = OUT;
}
break;
case IN_STRING_LITERAL:
if ((pch != '\\' && ch == '"') || ch == '\r' || ch == '\n') {
state = OUT;
}
break;
case IN_CHAR_LITERAL:
if ((pch != '\\' && ch == '\'') || ch == '\r' || ch == '\n') {
state = OUT;
}
break;
}
// Terminate ongoing block at last char
if (i == length) {
state = OUT;
}
// Handle state changes
if (state != prevState) {
if (state != OUT) {
// Entering block
blockStart = i + 1;
} else {
// Exiting block
int blockEnd = i;
if (prevState == IN_BLOCK_COMMENT && i < length) blockEnd--; // preserve star in '*/'
for (int j = blockStart; j < blockEnd; j++) {
char c = p.charAt(j);
if (c != '\n' && c != '\r') p.setCharAt(j, ' ');
}
}
}
prevState = state;
}
}
}