1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-02 13:24:12 +01:00

PrePreprocess.scrubComments doesn't properly work: using RegExp from PrePreprocess.strip. Fixes #817

This commit is contained in:
Federico Fissore 2013-11-11 12:18:42 +01:00
parent 07f8c691b4
commit 05bf2b0be9

View File

@ -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<Pattern> patterns = new ArrayList<Pattern>();
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;
}
}