@@ -35,7 +35,7 @@ Processing version Copyright (c) 2004-05 Ben Fry and Casey Reas
35
35
import java .io .*;
36
36
import java .util .*;
37
37
38
- import com . oroinc . text .regex .*;
38
+ import java . util .regex .*;
39
39
40
40
41
41
/**
@@ -49,16 +49,17 @@ public class PdePreprocessor {
49
49
// we always write one header: WProgram.h
50
50
public int headerCount = 1 ;
51
51
52
- List prototypes ;
52
+ // the prototypes that are generated by the preprocessor
53
+ List <String > prototypes ;
53
54
54
55
// these ones have the .* at the end, since a class name might be at the end
55
56
// instead of .* which would make trouble other classes using this can lop
56
57
// off the . and anything after it to produce a package name consistently.
57
- ArrayList <String > programImports ;
58
+ List <String > programImports ;
58
59
59
60
// imports just from the code folder, treated differently
60
61
// than the others, since the imports are auto-generated.
61
- ArrayList <String > codeFolderImports ;
62
+ List <String > codeFolderImports ;
62
63
63
64
String indent ;
64
65
@@ -79,6 +80,14 @@ public PdePreprocessor() {
79
80
indent = new String (indentChars );
80
81
}
81
82
83
+ /**
84
+ * Writes out the head of the c++ code generated for a sketch.
85
+ * Called from processing.app.Sketch.
86
+ * @param program the concatenated code from all tabs containing pde-files
87
+ * @param buildPath the path into which the processed pde-code is to be written
88
+ * @param name the name of the sketch
89
+ * @param codeFolderPackages unused param (leftover from processing)
90
+ */
82
91
public int writePrefix (String program , String buildPath ,
83
92
String sketchName , String codeFolderPackages []) throws FileNotFoundException {
84
93
this .buildPath = buildPath ;
@@ -93,7 +102,7 @@ public int writePrefix(String program, String buildPath,
93
102
// an OutOfMemoryError or NullPointerException will happen.
94
103
// again, not gonna bother tracking this down, but here's a hack.
95
104
// http://dev.processing.org/bugs/show_bug.cgi?id=16
96
- String scrubbed = Sketch .scrubComments (program );
105
+ Sketch .scrubComments (program );
97
106
// If there are errors, an exception is thrown and this fxn exits.
98
107
99
108
if (Preferences .getBoolean ("preproc.substitute_unicode" )) {
@@ -117,14 +126,7 @@ public int writePrefix(String program, String buildPath,
117
126
// }
118
127
// }
119
128
120
- prototypes = new ArrayList ();
121
-
122
- try {
123
- prototypes = prototypes (program );
124
- } catch (MalformedPatternException e ) {
125
- System .out .println ("Internal error while pre-processing; " +
126
- "not generating function prototypes.\n \n " + e );
127
- }
129
+ prototypes = prototypes (program );
128
130
129
131
// store # of prototypes so that line number reporting can be adjusted
130
132
prototypeCount = prototypes .size ();
@@ -193,7 +195,7 @@ public String write() throws java.lang.Exception {
193
195
}
194
196
195
197
// Write the pde program to the cpp file
196
- protected void writeProgram (PrintStream out , String program , List prototypes ) {
198
+ protected void writeProgram (PrintStream out , String program , List < String > prototypes ) {
197
199
int prototypeInsertionPoint = firstStatement (program );
198
200
199
201
out .print (program .substring (0 , prototypeInsertionPoint ));
@@ -216,7 +218,7 @@ protected void writeProgram(PrintStream out, String program, List prototypes) {
216
218
protected void writeFooter (PrintStream out ) throws java .lang .Exception {}
217
219
218
220
219
- public ArrayList <String > getExtraImports () {
221
+ public List <String > getExtraImports () {
220
222
return programImports ;
221
223
}
222
224
@@ -229,31 +231,23 @@ public ArrayList<String> getExtraImports() {
229
231
* or a pre-processor directive.
230
232
*/
231
233
public int firstStatement (String in ) {
232
- PatternMatcherInput input = new PatternMatcherInput (in );
233
- PatternCompiler compiler = new Perl5Compiler ();
234
- PatternMatcher matcher = new Perl5Matcher ();
235
- Pattern pattern = null ;
234
+ // whitespace
235
+ String p = "\\ s+" ;
236
236
237
- try {
238
- pattern = compiler .compile (
239
- // XXX: doesn't properly handle special single-quoted characters
240
- // whitespace
241
- "\\ s+" + "|" +
242
- // multi-line comment
243
- "(/\\ *[^*]*(?:\\ *(?!/)[^*]*)*\\ */)" + "|" +
244
- // single-line comment
245
- "(//.*?$)" + "|" +
246
- // pre-processor directive
247
- "(#(?:\\ \\ \\ n|.)*)" ,
248
- Perl5Compiler .MULTILINE_MASK );
249
- } catch (MalformedPatternException e ) {
250
- throw new RuntimeException ("Internal error in firstStatement()" , e );
251
- }
237
+ // multi-line and single-line comment
238
+ //p += "|" + "(//\\s*?$)|(/\\*\\s*?\\*/)";
239
+ p += "|(/\\ *[^*]*(?:\\ *(?!/)[^*]*)*\\ */)|(//.*?$)" ;
240
+
241
+ // pre-processor directive
242
+ p += "|(#(?:\\ \\ \\ n|.)*)" ;
243
+ Pattern pattern = Pattern .compile (p , Pattern .MULTILINE );
252
244
245
+ Matcher matcher = pattern .matcher (in );
253
246
int i = 0 ;
254
- while (matcher .matchesPrefix (input , pattern )) {
255
- i = matcher .getMatch ().endOffset (0 );
256
- input .setCurrentOffset (i );
247
+ while (matcher .find ()) {
248
+ if (matcher .start ()!=i )
249
+ break ;
250
+ i = matcher .end ();
257
251
}
258
252
259
253
return i ;
@@ -265,31 +259,24 @@ public int firstStatement(String in) {
265
259
* @param in the String to strip
266
260
* @return the stripped String
267
261
*/
268
- public String strip (String in ) throws MalformedPatternException {
269
- PatternCompiler compiler = new Perl5Compiler ();
270
- PatternMatcher matcher = new Perl5Matcher ();
271
- Pattern pattern = compiler .compile (
272
- // XXX: doesn't properly handle special single-quoted characters
273
- // single-quoted character
274
- "('.')" + "|" +
275
- // double-quoted string
276
- "(\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )" + "|" +
277
- // multi-line comment
278
- "(/\\ *[^*]*(?:\\ *(?!/)[^*]*)*\\ */)" + "|" +
279
- // single-line comment
280
- "(//.*?$)" + "|" +
281
- // pre-processor directive
282
- "(^\\ s*#.*?$)" ,
283
- Perl5Compiler .MULTILINE_MASK );
284
-
285
- while (matcher .contains (in , pattern )) {
286
- MatchResult result = matcher .getMatch ();
287
- // XXX: should preserve newlines in the result so that line numbers of
288
- // the stripped string correspond to those in the original source.
289
- in = in .substring (0 , result .beginOffset (0 )) + " " + in .substring (result .endOffset (0 ));
290
- }
262
+ public String strip (String in ) {
263
+ // XXX: doesn't properly handle special single-quoted characters
264
+ // single-quoted character
265
+ String p = "('.')" ;
266
+
267
+ // double-quoted string
268
+ p += "|(\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )" ;
291
269
292
- return in ;
270
+ // single and multi-line comment
271
+ //p += "|" + "(//\\s*?$)|(/\\*\\s*?\\*/)";
272
+ p += "|(//.*?$)|(/\\ *[^*]*(?:\\ *(?!/)[^*]*)*\\ */)" ;
273
+
274
+ // pre-processor directive
275
+ p += "|" + "(^\\ s*#.*?$)" ;
276
+
277
+ Pattern pattern = Pattern .compile (p , Pattern .MULTILINE );
278
+ Matcher matcher = pattern .matcher (in );
279
+ return matcher .replaceAll (" " );
293
280
}
294
281
295
282
/**
@@ -324,21 +311,17 @@ private String collapseBraces(String in) {
324
311
return buffer .toString ();
325
312
}
326
313
327
- public List prototypes (String in ) throws MalformedPatternException {
314
+ public ArrayList < String > prototypes (String in ) {
328
315
in = collapseBraces (strip (in ));
329
316
330
- PatternMatcherInput input = new PatternMatcherInput (in );
331
- PatternCompiler compiler = new Perl5Compiler ();
332
- PatternMatcher matcher = new Perl5Matcher ();
333
317
// XXX: doesn't handle ... varargs
334
318
// XXX: doesn't handle function pointers
335
- Pattern pattern = compiler .compile (
336
- "[\\ w\\ [\\ ]\\ *]+\\ s+[\\ [\\ ]\\ *\\ w\\ s]+\\ ([,\\ [\\ ]\\ *\\ w\\ s]*\\ )(?=\\ s*\\ {)" );
337
- List matches = new ArrayList ();
319
+ Pattern pattern = Pattern .compile ("[\\ w\\ [\\ ]\\ *]+\\ s+[&\\ [\\ ]\\ *\\ w\\ s]+\\ ([&,\\ [\\ ]\\ *\\ w\\ s]*\\ )(?=\\ s*\\ {)" );
338
320
339
- while (matcher .contains (input , pattern )) {
340
- matches .add (matcher .getMatch ().group (0 ) + ";" );
341
- }
321
+ ArrayList <String > matches = new ArrayList <String >();
322
+ Matcher matcher = pattern .matcher (in );
323
+ while (matcher .find ())
324
+ matches .add (matcher .group (0 ) + ";" );
342
325
343
326
return matches ;
344
327
}
0 commit comments