From 12446a25d9c0c256bfc4ceb1534222cb9e3d70d6 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Wed, 6 Nov 2013 18:32:09 +0100 Subject: [PATCH 1/4] Test preproc --- .../app/preproc/PdePreprocessor.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/app/src/processing/app/preproc/PdePreprocessor.java b/app/src/processing/app/preproc/PdePreprocessor.java index c5794de1e..0f58c0649 100644 --- a/app/src/processing/app/preproc/PdePreprocessor.java +++ b/app/src/processing/app/preproc/PdePreprocessor.java @@ -243,23 +243,21 @@ public class PdePreprocessor { public String strip(String in) { // XXX: doesn't properly handle special single-quoted characters // single-quoted character - String p = "('.')"; + Pattern[] patterns = new Pattern[6]; + patterns[5] = Pattern.compile("(\"(?:[^\"\\\\]|\\\\.)*\")", Pattern.MULTILINE); + patterns[4] = Pattern.compile("(^\\s*#.*?$)", Pattern.MULTILINE); + patterns[3] = Pattern.compile("(/\\*[^*]*(?:\\*(?!/)[^*]*)*\\*/)", Pattern.MULTILINE); + patterns[2] = Pattern.compile("(//.*?$)", Pattern.MULTILINE); + patterns[1] = Pattern.compile("('\\\\\"')", Pattern.MULTILINE); + patterns[0] = Pattern.compile("('.')", Pattern.MULTILINE); - p += "|('\\\\\"')"; + String code = in; + for (Pattern p : patterns) { + Matcher matcher = p.matcher(code); + code = matcher.replaceAll(" "); + } - // double-quoted string - p += "|(\"(?:[^\"\\\\]|\\\\.)*\")"; - - // single and multi-line comment - //p += "|" + "(//\\s*?$)|(/\\*\\s*?\\*/)"; - p += "|(//.*?$)|(/\\*[^*]*(?:\\*(?!/)[^*]*)*\\*/)"; - - // pre-processor directive - p += "|" + "(^\\s*#.*?$)"; - - Pattern pattern = Pattern.compile(p, Pattern.MULTILINE); - Matcher matcher = pattern.matcher(in); - return matcher.replaceAll(" "); + return code; } /** From 07f8c691b4cf9242c2f1c45ad1bc130da9437a23 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 11 Nov 2013 12:05:59 +0100 Subject: [PATCH 2/4] PdePreprocessor.scubComments result used before looking for libraries. Fixes #1293 --- .../app/preproc/PdePreprocessor.java | 21 +++++++++++-------- .../IncludeBetweenMultilineComment.ino | 15 +++++++++++++ .../app/preproc/PdePreprocessorTest.java | 12 ++++++++++- 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 app/test/processing/app/preproc/IncludeBetweenMultilineComment.ino diff --git a/app/src/processing/app/preproc/PdePreprocessor.java b/app/src/processing/app/preproc/PdePreprocessor.java index 0f58c0649..e6af0e8df 100644 --- a/app/src/processing/app/preproc/PdePreprocessor.java +++ b/app/src/processing/app/preproc/PdePreprocessor.java @@ -87,7 +87,7 @@ public class PdePreprocessor { // an OutOfMemoryError or NullPointerException will happen. // again, not gonna bother tracking this down, but here's a hack. // http://dev.processing.org/bugs/show_bug.cgi?id=16 - scrubComments(program); + program = scrubComments(program); // If there are errors, an exception is thrown and this fxn exits. if (Preferences.getBoolean("preproc.substitute_unicode")) { @@ -242,14 +242,17 @@ public class PdePreprocessor { */ public String strip(String in) { // XXX: doesn't properly handle special single-quoted characters + List patterns = new ArrayList(); // single-quoted character - Pattern[] patterns = new Pattern[6]; - patterns[5] = Pattern.compile("(\"(?:[^\"\\\\]|\\\\.)*\")", Pattern.MULTILINE); - patterns[4] = Pattern.compile("(^\\s*#.*?$)", Pattern.MULTILINE); - patterns[3] = Pattern.compile("(/\\*[^*]*(?:\\*(?!/)[^*]*)*\\*/)", Pattern.MULTILINE); - patterns[2] = Pattern.compile("(//.*?$)", Pattern.MULTILINE); - patterns[1] = Pattern.compile("('\\\\\"')", Pattern.MULTILINE); - patterns[0] = Pattern.compile("('.')", Pattern.MULTILINE); + patterns.add(Pattern.compile("('.')", Pattern.MULTILINE)); + // single and multi-line comment + patterns.add(Pattern.compile("('\\\\\"')", Pattern.MULTILINE)); + patterns.add(Pattern.compile("(//.*?$)", Pattern.MULTILINE)); + patterns.add(Pattern.compile("(/\\*[^*]*(?:\\*(?!/)[^*]*)*\\*/)", Pattern.MULTILINE)); + // pre-processor directive + patterns.add(Pattern.compile("(^\\s*#.*?$)", Pattern.MULTILINE)); + // double-quoted string + patterns.add(Pattern.compile("(\"(?:[^\"\\\\]|\\\\.)*\")", Pattern.MULTILINE)); String code = in; for (Pattern p : patterns) { @@ -259,7 +262,7 @@ public class PdePreprocessor { return code; } - + /** * Removes the contents of all top-level curly brace pairs {}. * @param in the String to collapse diff --git a/app/test/processing/app/preproc/IncludeBetweenMultilineComment.ino b/app/test/processing/app/preproc/IncludeBetweenMultilineComment.ino new file mode 100644 index 000000000..1c22729a8 --- /dev/null +++ b/app/test/processing/app/preproc/IncludeBetweenMultilineComment.ino @@ -0,0 +1,15 @@ +#include +/* +#include +*/ +CapacitiveSensorDue cs_13_8 = CapacitiveSensorDue(13,8); +void setup() +{ + Serial.begin(9600); +} +void loop() +{ + long total1 = cs_13_8.read(30); + Serial.println(total1); + delay(100); +} \ No newline at end of file diff --git a/app/test/processing/app/preproc/PdePreprocessorTest.java b/app/test/processing/app/preproc/PdePreprocessorTest.java index 446f96209..aad17e744 100644 --- a/app/test/processing/app/preproc/PdePreprocessorTest.java +++ b/app/test/processing/app/preproc/PdePreprocessorTest.java @@ -18,4 +18,14 @@ public class PdePreprocessorTest { assertEquals(actualOutput, expectedOutput); } -} + + @Test + public void testIncludeInsideMultilineComment() throws Exception { + String s = FileUtils.readFileToString(new File(PdePreprocessorTest.class.getResource("IncludeBetweenMultilineComment.ino").getFile())); + + PdePreprocessor pdePreprocessor = new PdePreprocessor(); + pdePreprocessor.writePrefix(s); + assertEquals(1, pdePreprocessor.getExtraImports().size()); + assertEquals("CapacitiveSensorDue.h", pdePreprocessor.getExtraImports().get(0)); + } +} \ No newline at end of file From 05bf2b0be90bbd94271cee599047006a2c48a4f7 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 11 Nov 2013 12:18:42 +0100 Subject: [PATCH 3/4] PrePreprocess.scrubComments doesn't properly work: using RegExp from PrePreprocess.strip. Fixes #817 --- .../app/preproc/PdePreprocessor.java | 51 ++++--------------- 1 file changed, 9 insertions(+), 42 deletions(-) diff --git a/app/src/processing/app/preproc/PdePreprocessor.java b/app/src/processing/app/preproc/PdePreprocessor.java index e6af0e8df..27a28b455 100644 --- a/app/src/processing/app/preproc/PdePreprocessor.java +++ b/app/src/processing/app/preproc/PdePreprocessor.java @@ -334,49 +334,16 @@ public class PdePreprocessor { * Utility function used here and in the preprocessor. */ static public String scrubComments(String what) { - char p[] = what.toCharArray(); + List patterns = new ArrayList(); + patterns.add(Pattern.compile("('\\\\\"')", Pattern.MULTILINE)); + patterns.add(Pattern.compile("(//.*?$)", Pattern.MULTILINE)); + patterns.add(Pattern.compile("(/\\*[^*]*(?:\\*(?!/)[^*]*)*\\*/)", Pattern.MULTILINE)); - int index = 0; - while (index < p.length) { - // for any double slash comments, ignore until the end of the line - if ((p[index] == '/') && - (index < p.length - 1) && - (p[index+1] == '/')) { - p[index++] = ' '; - p[index++] = ' '; - while ((index < p.length) && - (p[index] != '\n')) { - p[index++] = ' '; - } - - // check to see if this is the start of a new multiline comment. - // if it is, then make sure it's actually terminated somewhere. - } else if ((p[index] == '/') && - (index < p.length - 1) && - (p[index+1] == '*')) { - p[index++] = ' '; - p[index++] = ' '; - boolean endOfRainbow = false; - while (index < p.length - 1) { - if ((p[index] == '*') && (p[index+1] == '/')) { - p[index++] = ' '; - p[index++] = ' '; - endOfRainbow = true; - break; - - } else { - // continue blanking this area - p[index++] = ' '; - } - } - if (!endOfRainbow) { - throw new RuntimeException(_("Missing the */ from the end of a " + - "/* comment */")); - } - } else { // any old character, move along - index++; - } + String result = what; + for (Pattern p : patterns) { + result = p.matcher(result).replaceAll(""); } - return new String(p); + + return result; } } From e1579af56538a875faf7838e37d032e2668ef256 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 11 Nov 2013 12:24:59 +0100 Subject: [PATCH 4/4] PdePreprocessor: different patterns order leads to a slightly different result. Updating test --- app/test/processing/app/preproc/PdePreprocessorTest.java | 2 +- .../processing/app/preproc/RemoteCallLogger_v1e0.stripped.ino | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/test/processing/app/preproc/PdePreprocessorTest.java b/app/test/processing/app/preproc/PdePreprocessorTest.java index aad17e744..1532a9eaf 100644 --- a/app/test/processing/app/preproc/PdePreprocessorTest.java +++ b/app/test/processing/app/preproc/PdePreprocessorTest.java @@ -16,7 +16,7 @@ public class PdePreprocessorTest { String actualOutput = new PdePreprocessor().strip(s); String expectedOutput = FileUtils.readFileToString(new File(PdePreprocessorTest.class.getResource("RemoteCallLogger_v1e0.stripped.ino").getFile())); - assertEquals(actualOutput, expectedOutput); + assertEquals(expectedOutput, actualOutput); } @Test diff --git a/app/test/processing/app/preproc/RemoteCallLogger_v1e0.stripped.ino b/app/test/processing/app/preproc/RemoteCallLogger_v1e0.stripped.ino index 706544402..8e00fdd3f 100644 --- a/app/test/processing/app/preproc/RemoteCallLogger_v1e0.stripped.ino +++ b/app/test/processing/app/preproc/RemoteCallLogger_v1e0.stripped.ino @@ -1,8 +1,5 @@ - - -