@@ -49,7 +49,7 @@ public class PdePreprocessor {
49
49
// stores number of included library headers written
50
50
// we always write one header: Arduino.h
51
51
public int headerCount = 1 ;
52
-
52
+
53
53
// the prototypes that are generated by the preprocessor
54
54
List <String > prototypes ;
55
55
@@ -68,11 +68,11 @@ public class PdePreprocessor {
68
68
/**
69
69
* Setup a new preprocessor.
70
70
*/
71
- public PdePreprocessor () {
71
+ public PdePreprocessor () {
72
72
}
73
73
74
74
/**
75
- * Writes out the head of the c++ code generated for a sketch.
75
+ * Writes out the head of the c++ code generated for a sketch.
76
76
* Called from processing.app.Sketch.
77
77
* @param program the concatenated code from all tabs containing pde-files
78
78
*/
@@ -115,7 +115,7 @@ public int writePrefix(String program)
115
115
116
116
// store # of prototypes so that line number reporting can be adjusted
117
117
prototypeCount = prototypes .size ();
118
-
118
+
119
119
// do this after the program gets re-combobulated
120
120
this .program = program ;
121
121
@@ -164,7 +164,7 @@ static String substituteUnicode(String program) {
164
164
/**
165
165
* preprocesses a pde file and writes out a cpp file into the specified
166
166
* OutputStream
167
- *
167
+ *
168
168
* @param output
169
169
* @throws Exception
170
170
*/
@@ -177,10 +177,10 @@ public void write(OutputStream output) throws Exception {
177
177
// Write the pde program to the cpp file
178
178
protected void writeProgram (PrintStream out , String program , List <String > prototypes ) {
179
179
int prototypeInsertionPoint = firstStatement (program );
180
-
180
+
181
181
out .print (program .substring (0 , prototypeInsertionPoint ));
182
- out .print ("#include \" Arduino.h\" \n " );
183
-
182
+ out .print ("#include \" Arduino.h\" \n " );
183
+
184
184
// print user defined prototypes
185
185
for (int i = 0 ; i < prototypes .size (); i ++) {
186
186
out .print (prototypes .get (i ) + "\n " );
@@ -214,26 +214,26 @@ public List<String> getExtraImports() {
214
214
public int firstStatement (String in ) {
215
215
// whitespace
216
216
String p = "\\ s+" ;
217
-
217
+
218
218
// multi-line and single-line comment
219
219
//p += "|" + "(//\\s*?$)|(/\\*\\s*?\\*/)";
220
220
p += "|(/\\ *[^*]*(?:\\ *(?!/)[^*]*)*\\ */)|(//.*?$)" ;
221
221
222
222
// pre-processor directive
223
223
p += "|(#(?:\\ \\ \\ n|.)*)" ;
224
224
Pattern pattern = Pattern .compile (p , Pattern .MULTILINE );
225
-
225
+
226
226
Matcher matcher = pattern .matcher (in );
227
227
int i = 0 ;
228
228
while (matcher .find ()) {
229
229
if (matcher .start ()!=i )
230
230
break ;
231
231
i = matcher .end ();
232
232
}
233
-
233
+
234
234
return i ;
235
235
}
236
-
236
+
237
237
/**
238
238
* Strips comments, pre-processor directives, single- and double-quoted
239
239
* strings from a string.
@@ -271,7 +271,7 @@ private String collapseBraces(String in) {
271
271
StringBuffer buffer = new StringBuffer ();
272
272
int nesting = 0 ;
273
273
int start = 0 ;
274
-
274
+
275
275
// XXX: need to keep newlines inside braces so we can determine the line
276
276
// number of a prototype
277
277
for (int i = 0 ; i < in .length (); i ++) {
@@ -288,32 +288,32 @@ private String collapseBraces(String in) {
288
288
}
289
289
}
290
290
}
291
-
291
+
292
292
buffer .append (in .substring (start ));
293
-
293
+
294
294
return buffer .toString ();
295
295
}
296
-
296
+
297
297
public ArrayList <String > prototypes (String in ) {
298
298
in = collapseBraces (strip (in ));
299
299
300
300
// XXX: doesn't handle ... varargs
301
301
// XXX: doesn't handle function pointers
302
302
Pattern prototypePattern = Pattern .compile ("[\\ w\\ [\\ ]\\ *]+\\ s+[&\\ [\\ ]\\ *\\ w\\ s]+\\ ([&,\\ [\\ ]\\ *\\ w\\ s]*\\ )(?=\\ s*;)" );
303
303
Pattern functionPattern = Pattern .compile ("[\\ w\\ [\\ ]\\ *]+\\ s+[&\\ [\\ ]\\ *\\ w\\ s]+\\ ([&,\\ [\\ ]\\ *\\ w\\ s]*\\ )(?=\\ s*\\ {)" );
304
-
304
+
305
305
// Find already declared prototypes
306
306
ArrayList <String > prototypeMatches = new ArrayList <String >();
307
307
Matcher prototypeMatcher = prototypePattern .matcher (in );
308
308
while (prototypeMatcher .find ())
309
309
prototypeMatches .add (prototypeMatcher .group (0 ) + ";" );
310
-
310
+
311
311
// Find all functions and generate prototypes for them
312
312
ArrayList <String > functionMatches = new ArrayList <String >();
313
313
Matcher functionMatcher = functionPattern .matcher (in );
314
314
while (functionMatcher .find ())
315
315
functionMatches .add (functionMatcher .group (0 ) + ";" );
316
-
316
+
317
317
// Remove generated prototypes that exactly match ones found in the source file
318
318
for (int functionIndex =functionMatches .size () - 1 ; functionIndex >= 0 ; functionIndex --) {
319
319
for (int prototypeIndex =0 ; prototypeIndex < prototypeMatches .size (); prototypeIndex ++) {
@@ -323,10 +323,29 @@ public ArrayList<String> prototypes(String in) {
323
323
}
324
324
}
325
325
}
326
-
326
+
327
327
return functionMatches ;
328
328
}
329
329
330
+ private boolean isStartOrEndOfString (char p [], int index ) {
331
+ if (p [index ] != '\"' ) {
332
+ return false ;
333
+ }
334
+
335
+ if (index == 0 ) {
336
+ return true ;
337
+ }
338
+
339
+ if (p [index - 1 ] == '\\' ) {
340
+ return false ;
341
+ }
342
+
343
+ if (index - 2 >= 0 && p [index - 2 ] == '\\' ) {
344
+ return true ;
345
+ }
346
+
347
+ return true ;
348
+ }
330
349
331
350
/**
332
351
* Replace all commented portions of a given String as spaces.
@@ -338,7 +357,7 @@ public String scrubComments(String what) {
338
357
int index = 0 ;
339
358
boolean insideString = false ;
340
359
while (index < p .length ) {
341
- if (p [ index ] == '\"' ) {
360
+ if (isStartOrEndOfString ( p , index ) ) {
342
361
insideString = !insideString ;
343
362
}
344
363
// for any double slash comments, ignore until the end of the line
0 commit comments