@@ -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 java . util .regex .*;
38
+ import com . oroinc . text .regex .*;
39
39
40
40
41
41
/**
@@ -49,17 +49,16 @@ public class PdePreprocessor {
49
49
// we always write one header: WProgram.h
50
50
public int headerCount = 1 ;
51
51
52
- // the prototypes that are generated by the preprocessor
53
- List <String > prototypes ;
52
+ List prototypes ;
54
53
55
54
// these ones have the .* at the end, since a class name might be at the end
56
55
// instead of .* which would make trouble other classes using this can lop
57
56
// off the . and anything after it to produce a package name consistently.
58
- List <String > programImports ;
57
+ ArrayList <String > programImports ;
59
58
60
59
// imports just from the code folder, treated differently
61
60
// than the others, since the imports are auto-generated.
62
- List <String > codeFolderImports ;
61
+ ArrayList <String > codeFolderImports ;
63
62
64
63
String indent ;
65
64
@@ -80,14 +79,6 @@ public PdePreprocessor() {
80
79
indent = new String (indentChars );
81
80
}
82
81
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
- */
91
82
public int writePrefix (String program , String buildPath ,
92
83
String sketchName , String codeFolderPackages []) throws FileNotFoundException {
93
84
this .buildPath = buildPath ;
@@ -102,7 +93,7 @@ public int writePrefix(String program, String buildPath,
102
93
// an OutOfMemoryError or NullPointerException will happen.
103
94
// again, not gonna bother tracking this down, but here's a hack.
104
95
// http://dev.processing.org/bugs/show_bug.cgi?id=16
105
- Sketch .scrubComments (program );
96
+ String scrubbed = Sketch .scrubComments (program );
106
97
// If there are errors, an exception is thrown and this fxn exits.
107
98
108
99
if (Preferences .getBoolean ("preproc.substitute_unicode" )) {
@@ -126,7 +117,14 @@ public int writePrefix(String program, String buildPath,
126
117
// }
127
118
// }
128
119
129
- prototypes = prototypes (program );
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
+ }
130
128
131
129
// store # of prototypes so that line number reporting can be adjusted
132
130
prototypeCount = prototypes .size ();
@@ -195,7 +193,7 @@ public String write() throws java.lang.Exception {
195
193
}
196
194
197
195
// Write the pde program to the cpp file
198
- protected void writeProgram (PrintStream out , String program , List < String > prototypes ) {
196
+ protected void writeProgram (PrintStream out , String program , List prototypes ) {
199
197
int prototypeInsertionPoint = firstStatement (program );
200
198
201
199
out .print (program .substring (0 , prototypeInsertionPoint ));
@@ -218,7 +216,7 @@ protected void writeProgram(PrintStream out, String program, List<String> protot
218
216
protected void writeFooter (PrintStream out ) throws java .lang .Exception {}
219
217
220
218
221
- public List <String > getExtraImports () {
219
+ public ArrayList <String > getExtraImports () {
222
220
return programImports ;
223
221
}
224
222
@@ -231,23 +229,31 @@ public List<String> getExtraImports() {
231
229
* or a pre-processor directive.
232
230
*/
233
231
public int firstStatement (String in ) {
234
- // whitespace
235
- String p = "\\ s+" ;
232
+ PatternMatcherInput input = new PatternMatcherInput (in );
233
+ PatternCompiler compiler = new Perl5Compiler ();
234
+ PatternMatcher matcher = new Perl5Matcher ();
235
+ Pattern pattern = null ;
236
236
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 );
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
+ }
244
252
245
- Matcher matcher = pattern .matcher (in );
246
253
int i = 0 ;
247
- while (matcher .find ()) {
248
- if (matcher .start ()!=i )
249
- break ;
250
- i = matcher .end ();
254
+ while (matcher .matchesPrefix (input , pattern )) {
255
+ i = matcher .getMatch ().endOffset (0 );
256
+ input .setCurrentOffset (i );
251
257
}
252
258
253
259
return i ;
@@ -259,24 +265,31 @@ public int firstStatement(String in) {
259
265
* @param in the String to strip
260
266
* @return the stripped String
261
267
*/
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 += "|(\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )" ;
269
-
270
- // single and multi-line comment
271
- //p += "|" + "(//\\s*?$)|(/\\*\\s*?\\*/)";
272
- p += "|(//.*?$)|(/\\ *[^*]*(?:\\ *(?!/)[^*]*)*\\ */)" ;
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
+ }
273
291
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 (" " );
292
+ return in ;
280
293
}
281
294
282
295
/**
@@ -311,17 +324,21 @@ private String collapseBraces(String in) {
311
324
return buffer .toString ();
312
325
}
313
326
314
- public ArrayList < String > prototypes (String in ) {
327
+ public List prototypes (String in ) throws MalformedPatternException {
315
328
in = collapseBraces (strip (in ));
316
329
330
+ PatternMatcherInput input = new PatternMatcherInput (in );
331
+ PatternCompiler compiler = new Perl5Compiler ();
332
+ PatternMatcher matcher = new Perl5Matcher ();
317
333
// XXX: doesn't handle ... varargs
318
334
// XXX: doesn't handle function pointers
319
- Pattern pattern = Pattern .compile ("[\\ w\\ [\\ ]\\ *]+\\ s+[&\\ [\\ ]\\ *\\ w\\ s]+\\ ([&,\\ [\\ ]\\ *\\ w\\ s]*\\ )(?=\\ s*\\ {)" );
335
+ Pattern pattern = compiler .compile (
336
+ "[\\ w\\ [\\ ]\\ *]+\\ s+[\\ [\\ ]\\ *\\ w\\ s]+\\ ([,\\ [\\ ]\\ *\\ w\\ s]*\\ )(?=\\ s*\\ {)" );
337
+ List matches = new ArrayList ();
320
338
321
- ArrayList <String > matches = new ArrayList <String >();
322
- Matcher matcher = pattern .matcher (in );
323
- while (matcher .find ())
324
- matches .add (matcher .group (0 ) + ";" );
339
+ while (matcher .contains (input , pattern )) {
340
+ matches .add (matcher .getMatch ().group (0 ) + ";" );
341
+ }
325
342
326
343
return matches ;
327
344
}
0 commit comments