Skip to content

Commit

Permalink
more parsing code for quoted strings, fixes #3394
Browse files Browse the repository at this point in the history
  • Loading branch information
benfry committed Jun 26, 2015
1 parent 1e0e434 commit 87f8a3d
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions java/src/processing/mode/java/preproc/PdePreprocessor.java
Expand Up @@ -288,12 +288,20 @@ make sure that it uses numbers (or displayWidth/Height), copy into settings
char[] c = searchArea.toCharArray();
int depth = 0;
int closeBrace = -1;
// boolean literal; // inside a quoted literal?
StringBuilder sb = new StringBuilder();
for (int i = openBrace; i < c.length; i++) {
if (c[i] == '{') {
depth++;
// } else if (c[i] == '\'') {
} else if (c[i] == '\'') {
String quoted = readSingleQuote(c, i);
sb.append(quoted);
i += quoted.length() - 1;

} else if (c[i] == '\"') {
String quoted = readDoubleQuote(c, i);
sb.append(quoted);
i += quoted.length() - 1;

} else if (c[i] == '}') {
depth--;
if (depth == 0) {
Expand Down Expand Up @@ -429,6 +437,51 @@ make sure that it uses numbers (or displayWidth/Height), copy into settings
}


static String readSingleQuote(char[] c, int i) {
StringBuilder sb = new StringBuilder();
try {
sb.append(c[i++]); // add the quote
if (c[i] == '\\') {
sb.append(c[i++]); // add the escape
if (c[i] == 'u') {
// grabs uNNN and the fourth N will be added below
for (int j = 0; j < 4; j++) {
sb.append(c[i++]);
}
}
}
sb.append(c[i++]); // get the char, escapee, or last unicode digit
sb.append(c[i++]); // get the closing quote

} catch (ArrayIndexOutOfBoundsException ignored) {
// this means they have bigger problems with their code
}
return sb.toString();
}


static String readDoubleQuote(char[] c, int i) {
StringBuilder sb = new StringBuilder();
try {
sb.append(c[i++]); // add the quote
while (i < c.length) {
if (c[i] == '\\') {
sb.append(c[i++]); // add the escape
sb.append(c[i++]); // add whatever was escaped
} else if (c[i] == '\"') {
sb.append(c[i++]);
break;
} else {
sb.append(c[i++]);
}
}
} catch (ArrayIndexOutOfBoundsException ignored) {
// this means they have bigger problems with their code
}
return sb.toString();
}


static protected String[] matchMethod(String methodName, String searchArea) {
final String left = "(?:^|\\s|;)";
// doesn't match empty pairs of parens
Expand Down

0 comments on commit 87f8a3d

Please sign in to comment.