Skip to content

Commit 95eced6

Browse files
committed
Merge remote-tracking branch 'arduino/ide-1.5.x' into ide-1.5.x
2 parents 853be57 + e1579af commit 95eced6

File tree

4 files changed

+54
-64
lines changed

4 files changed

+54
-64
lines changed

app/src/processing/app/preproc/PdePreprocessor.java

+27-59
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public int writePrefix(String program)
8787
// an OutOfMemoryError or NullPointerException will happen.
8888
// again, not gonna bother tracking this down, but here's a hack.
8989
// http://dev.processing.org/bugs/show_bug.cgi?id=16
90-
scrubComments(program);
90+
program = scrubComments(program);
9191
// If there are errors, an exception is thrown and this fxn exits.
9292

9393
if (Preferences.getBoolean("preproc.substitute_unicode")) {
@@ -242,26 +242,27 @@ public int firstStatement(String in) {
242242
*/
243243
public String strip(String in) {
244244
// XXX: doesn't properly handle special single-quoted characters
245+
List<Pattern> patterns = new ArrayList<Pattern>();
245246
// single-quoted character
246-
String p = "('.')";
247-
248-
p += "|('\\\\\"')";
249-
250-
// double-quoted string
251-
p += "|(\"(?:[^\"\\\\]|\\\\.)*\")";
252-
247+
patterns.add(Pattern.compile("('.')", Pattern.MULTILINE));
253248
// single and multi-line comment
254-
//p += "|" + "(//\\s*?$)|(/\\*\\s*?\\*/)";
255-
p += "|(//.*?$)|(/\\*[^*]*(?:\\*(?!/)[^*]*)*\\*/)";
256-
249+
patterns.add(Pattern.compile("('\\\\\"')", Pattern.MULTILINE));
250+
patterns.add(Pattern.compile("(//.*?$)", Pattern.MULTILINE));
251+
patterns.add(Pattern.compile("(/\\*[^*]*(?:\\*(?!/)[^*]*)*\\*/)", Pattern.MULTILINE));
257252
// pre-processor directive
258-
p += "|" + "(^\\s*#.*?$)";
253+
patterns.add(Pattern.compile("(^\\s*#.*?$)", Pattern.MULTILINE));
254+
// double-quoted string
255+
patterns.add(Pattern.compile("(\"(?:[^\"\\\\]|\\\\.)*\")", Pattern.MULTILINE));
259256

260-
Pattern pattern = Pattern.compile(p, Pattern.MULTILINE);
261-
Matcher matcher = pattern.matcher(in);
262-
return matcher.replaceAll(" ");
257+
String code = in;
258+
for (Pattern p : patterns) {
259+
Matcher matcher = p.matcher(code);
260+
code = matcher.replaceAll(" ");
261+
}
262+
263+
return code;
263264
}
264-
265+
265266
/**
266267
* Removes the contents of all top-level curly brace pairs {}.
267268
* @param in the String to collapse
@@ -333,49 +334,16 @@ public ArrayList<String> prototypes(String in) {
333334
* Utility function used here and in the preprocessor.
334335
*/
335336
static public String scrubComments(String what) {
336-
char p[] = what.toCharArray();
337-
338-
int index = 0;
339-
while (index < p.length) {
340-
// for any double slash comments, ignore until the end of the line
341-
if ((p[index] == '/') &&
342-
(index < p.length - 1) &&
343-
(p[index+1] == '/')) {
344-
p[index++] = ' ';
345-
p[index++] = ' ';
346-
while ((index < p.length) &&
347-
(p[index] != '\n')) {
348-
p[index++] = ' ';
349-
}
350-
351-
// check to see if this is the start of a new multiline comment.
352-
// if it is, then make sure it's actually terminated somewhere.
353-
} else if ((p[index] == '/') &&
354-
(index < p.length - 1) &&
355-
(p[index+1] == '*')) {
356-
p[index++] = ' ';
357-
p[index++] = ' ';
358-
boolean endOfRainbow = false;
359-
while (index < p.length - 1) {
360-
if ((p[index] == '*') && (p[index+1] == '/')) {
361-
p[index++] = ' ';
362-
p[index++] = ' ';
363-
endOfRainbow = true;
364-
break;
365-
366-
} else {
367-
// continue blanking this area
368-
p[index++] = ' ';
369-
}
370-
}
371-
if (!endOfRainbow) {
372-
throw new RuntimeException(_("Missing the */ from the end of a " +
373-
"/* comment */"));
374-
}
375-
} else { // any old character, move along
376-
index++;
377-
}
337+
List<Pattern> patterns = new ArrayList<Pattern>();
338+
patterns.add(Pattern.compile("('\\\\\"')", Pattern.MULTILINE));
339+
patterns.add(Pattern.compile("(//.*?$)", Pattern.MULTILINE));
340+
patterns.add(Pattern.compile("(/\\*[^*]*(?:\\*(?!/)[^*]*)*\\*/)", Pattern.MULTILINE));
341+
342+
String result = what;
343+
for (Pattern p : patterns) {
344+
result = p.matcher(result).replaceAll("");
378345
}
379-
return new String(p);
346+
347+
return result;
380348
}
381349
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <CapacitiveSensorDue.h>
2+
/*
3+
#include <WiFi.h>
4+
*/
5+
CapacitiveSensorDue cs_13_8 = CapacitiveSensorDue(13,8);
6+
void setup()
7+
{
8+
Serial.begin(9600);
9+
}
10+
void loop()
11+
{
12+
long total1 = cs_13_8.read(30);
13+
Serial.println(total1);
14+
delay(100);
15+
}

app/test/processing/app/preproc/PdePreprocessorTest.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ public void testSourceWithQuoteAndDoubleQuotesEscapedAndFinalQuoteShouldNotRaise
1616
String actualOutput = new PdePreprocessor().strip(s);
1717
String expectedOutput = FileUtils.readFileToString(new File(PdePreprocessorTest.class.getResource("RemoteCallLogger_v1e0.stripped.ino").getFile()));
1818

19-
assertEquals(actualOutput, expectedOutput);
19+
assertEquals(expectedOutput, actualOutput);
2020
}
21-
}
21+
22+
@Test
23+
public void testIncludeInsideMultilineComment() throws Exception {
24+
String s = FileUtils.readFileToString(new File(PdePreprocessorTest.class.getResource("IncludeBetweenMultilineComment.ino").getFile()));
25+
26+
PdePreprocessor pdePreprocessor = new PdePreprocessor();
27+
pdePreprocessor.writePrefix(s);
28+
assertEquals(1, pdePreprocessor.getExtraImports().size());
29+
assertEquals("CapacitiveSensorDue.h", pdePreprocessor.getExtraImports().get(0));
30+
}
31+
}

app/test/processing/app/preproc/RemoteCallLogger_v1e0.stripped.ino

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11

22

3-
4-
5-
63

74

85

0 commit comments

Comments
 (0)