Skip to content

Commit 16f8de1

Browse files
committed
Fix processing#5165. Wrong tab for missing brace
1 parent 7ada152 commit 16f8de1

File tree

2 files changed

+68
-50
lines changed

2 files changed

+68
-50
lines changed

java/src/processing/mode/java/JavaBuild.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import processing.core.PConstants;
5353
import processing.data.StringList;
5454
import processing.data.XML;
55+
import processing.mode.java.pdex.SourceUtils;
5556
import processing.mode.java.preproc.PdePreprocessor;
5657
import processing.mode.java.preproc.PreprocessorResult;
5758
import processing.mode.java.preproc.SurfaceInfo;
@@ -280,27 +281,30 @@ public String preprocess(File srcFolder,
280281
//System.out.println(java.getAbsolutePath());
281282
// System.out.println(bigCode);
282283

283-
if (msg.contains("expecting RCURLY")) {
284-
//if (msg.equals("expecting RCURLY, found 'null'")) {
285-
// This can be a problem since the error is sometimes listed as a line
286-
// that's actually past the number of lines. For instance, it might
287-
// report "line 15" of a 14 line program. Added code to highlightLine()
288-
// inside Editor to deal with this situation (since that code is also
289-
// useful for other similar situations).
290-
throw new SketchException("Found one too many { characters " +
291-
"without a } to match it.",
292-
errorFile, errorLine, re.getColumn(), false);
293-
}
294-
295-
if (msg.contains("expecting LCURLY")) {
296-
System.err.println(msg);
297-
String suffix = ".";
298-
String[] m = PApplet.match(msg, "found ('.*')");
299-
if (m != null) {
300-
suffix = ", not " + m[1] + ".";
284+
if (msg.contains("expecting RCURLY") || msg.contains("expecting LCURLY")) {
285+
for (int i = 0; i < sketch.getCodeCount(); i++) {
286+
SketchCode sc = sketch.getCode(i);
287+
if (sc.isExtension("pde")) {
288+
String s = sc.getProgram();
289+
int[] braceTest = SourceUtils.checkForMissingBraces(
290+
SourceUtils.scrubCommentsAndStrings(s) + "\n", 0, s.length()+1);
291+
if (braceTest[0] == 0) continue;
292+
293+
// Completely ignoring the errorFile/errorLine given since it's
294+
// likely to be the wrong tab. For the same reason, I'm not showing
295+
// the result of PApplet.match(msg, "found ('.*')") on missing
296+
// LCURLY.
297+
throw new SketchException(braceTest[0] > 0
298+
? "Found one too many { characters without a } to match it."
299+
: "Found one too many } characters without a { to match it.",
300+
i, braceTest[1], braceTest[2], false);
301+
}
301302
}
302-
throw new SketchException("Was expecting a { character" + suffix,
303-
errorFile, errorLine, re.getColumn(), false);
303+
// If we're still here, there's the right brackets, just not in the
304+
// right place. Passing on the original error.
305+
throw new SketchException(
306+
msg.replace("LCURLY", "{").replace("RCURLY", "}"),
307+
errorFile, errorLine, re.getColumn(), false);
304308
}
305309

306310
if (msg.indexOf("expecting RBRACK") != -1) {

java/src/processing/mode/java/pdex/SourceUtils.java

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -333,43 +333,57 @@ static public void scrubCommentsAndStrings(StringBuilder p) {
333333

334334
static public List<JavaProblem> checkForMissingBraces(StringBuilder p, int[] tabStartOffsets) {
335335
List<JavaProblem> problems = new ArrayList<>(0);
336-
tabLoop: for (int tabIndex = 0; tabIndex < tabStartOffsets.length; tabIndex++) {
336+
for (int tabIndex = 0; tabIndex < tabStartOffsets.length; tabIndex++) {
337337
int tabStartOffset = tabStartOffsets[tabIndex];
338338
int tabEndOffset = (tabIndex < tabStartOffsets.length - 1) ?
339339
tabStartOffsets[tabIndex + 1] : p.length();
340-
int depth = 0;
341-
int lineNumber = 0;
342-
for (int i = tabStartOffset; i < tabEndOffset; i++) {
343-
char ch = p.charAt(i);
344-
switch (ch) {
345-
case '{':
346-
depth++;
347-
break;
348-
case '}':
349-
depth--;
350-
break;
351-
case '\n':
352-
lineNumber++;
353-
break;
354-
}
355-
if (depth < 0) {
356-
JavaProblem problem =
357-
new JavaProblem("Found one too many } characters without { to match it.",
358-
JavaProblem.ERROR, tabIndex, lineNumber);
359-
problem.setPDEOffsets(i - tabStartOffset, i - tabStartOffset + 1);
360-
problems.add(problem);
361-
continue tabLoop;
362-
}
363-
}
364-
if (depth > 0) {
340+
int[] braceResult = checkForMissingBraces(p, tabStartOffset, tabEndOffset);
341+
if (braceResult[0] != 0) {
365342
JavaProblem problem =
366-
new JavaProblem("Found one too many { characters without } to match it.",
367-
JavaProblem.ERROR, tabIndex, lineNumber - 1);
368-
problem.setPDEOffsets(tabEndOffset - tabStartOffset - 2, tabEndOffset - tabStartOffset - 1);
369-
problems.add(problem);
343+
new JavaProblem(braceResult[0] < 0
344+
? "Found one too many } characters without { to match it."
345+
: "Found one too many { characters without } to match it.",
346+
JavaProblem.ERROR, tabIndex, braceResult[1]);
347+
problem.setPDEOffsets(braceResult[3], braceResult[3] + 1);
348+
problems.add(problem);
370349
}
371350
}
372351
return problems;
373352
}
374353

354+
355+
/**
356+
* Checks a single code fragment (such as a tab) for non-matching braces.
357+
* Broken out to allow easy use in JavaBuild.
358+
* @param c Program code scrubbed of comments and string literals.
359+
* @param start Start index, inclusive.
360+
* @param end End index, exclusive.
361+
* @return {@code int[4]} Depth at which the loop stopped, followed by the
362+
* line number, column, and string index (within the range) at which
363+
* an error was found, if any.
364+
*/
365+
static public int[] checkForMissingBraces(CharSequence c, int start, int end) {
366+
int depth = 0;
367+
int lineNumber = 0;
368+
int lineStart = start;
369+
for (int i = start; i < end; i++) {
370+
char ch = c.charAt(i);
371+
switch (ch) {
372+
case '{':
373+
depth++;
374+
break;
375+
case '}':
376+
depth--;
377+
break;
378+
case '\n':
379+
lineNumber++;
380+
lineStart = i;
381+
break;
382+
}
383+
if (depth < 0) {
384+
return new int[] {depth, lineNumber, i - lineStart, i - start};
385+
}
386+
}
387+
return new int[] {depth, lineNumber - 1, end - lineStart - 2, end - start - 2};
388+
}
375389
}

0 commit comments

Comments
 (0)