@@ -35,7 +35,7 @@ Processing version Copyright (c) 2004-05 Ben Fry and Casey Reas
3535import java .io .*;
3636import java .util .*;
3737
38- import com . oroinc . text .regex .*;
38+ import java . util .regex .*;
3939
4040
4141/**
@@ -49,16 +49,17 @@ public class PdePreprocessor {
4949 // we always write one header: WProgram.h
5050 public int headerCount = 1 ;
5151
52- List prototypes ;
52+ // the prototypes that are generated by the preprocessor
53+ List <String > prototypes ;
5354
5455 // these ones have the .* at the end, since a class name might be at the end
5556 // instead of .* which would make trouble other classes using this can lop
5657 // off the . and anything after it to produce a package name consistently.
57- ArrayList <String > programImports ;
58+ List <String > programImports ;
5859
5960 // imports just from the code folder, treated differently
6061 // than the others, since the imports are auto-generated.
61- ArrayList <String > codeFolderImports ;
62+ List <String > codeFolderImports ;
6263
6364 String indent ;
6465
@@ -79,6 +80,14 @@ public PdePreprocessor() {
7980 indent = new String (indentChars );
8081 }
8182
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+ */
8291 public int writePrefix (String program , String buildPath ,
8392 String sketchName , String codeFolderPackages []) throws FileNotFoundException {
8493 this .buildPath = buildPath ;
@@ -93,7 +102,7 @@ public int writePrefix(String program, String buildPath,
93102 // an OutOfMemoryError or NullPointerException will happen.
94103 // again, not gonna bother tracking this down, but here's a hack.
95104 // http://dev.processing.org/bugs/show_bug.cgi?id=16
96- String scrubbed = Sketch .scrubComments (program );
105+ Sketch .scrubComments (program );
97106 // If there are errors, an exception is thrown and this fxn exits.
98107
99108 if (Preferences .getBoolean ("preproc.substitute_unicode" )) {
@@ -117,14 +126,7 @@ public int writePrefix(String program, String buildPath,
117126// }
118127// }
119128
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 );
128130
129131 // store # of prototypes so that line number reporting can be adjusted
130132 prototypeCount = prototypes .size ();
@@ -193,7 +195,7 @@ public String write() throws java.lang.Exception {
193195 }
194196
195197 // 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 ) {
197199 int prototypeInsertionPoint = firstStatement (program );
198200
199201 out .print (program .substring (0 , prototypeInsertionPoint ));
@@ -216,7 +218,7 @@ protected void writeProgram(PrintStream out, String program, List prototypes) {
216218 protected void writeFooter (PrintStream out ) throws java .lang .Exception {}
217219
218220
219- public ArrayList <String > getExtraImports () {
221+ public List <String > getExtraImports () {
220222 return programImports ;
221223 }
222224
@@ -229,31 +231,23 @@ public ArrayList<String> getExtraImports() {
229231 * or a pre-processor directive.
230232 */
231233 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+" ;
236236
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 );
252244
245+ Matcher matcher = pattern .matcher (in );
253246 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 ();
257251 }
258252
259253 return i ;
@@ -265,31 +259,24 @@ public int firstStatement(String in) {
265259 * @param in the String to strip
266260 * @return the stripped String
267261 */
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 += "|(\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )" ;
291269
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 (" " );
293280 }
294281
295282 /**
@@ -324,21 +311,17 @@ private String collapseBraces(String in) {
324311 return buffer .toString ();
325312 }
326313
327- public List prototypes (String in ) throws MalformedPatternException {
314+ public ArrayList < String > prototypes (String in ) {
328315 in = collapseBraces (strip (in ));
329316
330- PatternMatcherInput input = new PatternMatcherInput (in );
331- PatternCompiler compiler = new Perl5Compiler ();
332- PatternMatcher matcher = new Perl5Matcher ();
333317 // XXX: doesn't handle ... varargs
334318 // 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*\\ {)" );
338320
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 ) + ";" );
342325
343326 return matches ;
344327 }
0 commit comments