fix problem with error message line numbers not coming through

This commit is contained in:
benfry
2008-11-10 07:17:30 +00:00
parent 6d59b763df
commit 3d4123f18f
+62 -66
View File
@@ -32,6 +32,7 @@ import java.io.*;
import java.util.*;
import java.util.zip.*;
import org.eclipse.jdt.core.compiler.batch.BatchCompiler;
import org.eclipse.jdt.core.compiler.CompilationProgress;
@@ -48,15 +49,15 @@ public class Compiler {
* @throws RunnerException Only if there's a problem. Only then.
*/
public boolean compile(Sketch sketch,
String buildPath,
String buildPath,
String primaryClassName) throws RunnerException {
// This will be filled in if anyone gets angry
RunnerException exception = null;
boolean success = false;
String baseCommand[] = new String[] {
"-Xemacs",
//"-noExit", // not necessary for ecj
"-Xemacs",
//"-noExit", // not necessary for ecj
"-source", "1.5",
"-target", "1.5",
"-classpath", sketch.getClassPath(),
@@ -69,9 +70,9 @@ public class Compiler {
// (some files are skipped if they contain no class)
String[] sourceFiles = new String[sketch.getCodeCount()];
int sourceCount = 0;
sourceFiles[sourceCount++] =
sourceFiles[sourceCount++] =
new File(buildPath, primaryClassName + ".java").getAbsolutePath();
for (SketchCode code : sketch.getCode()) {
if (code.isExtension("java")) {
String path = new File(buildPath, code.getFileName()).getAbsolutePath();
@@ -82,7 +83,7 @@ public class Compiler {
System.arraycopy(baseCommand, 0, command, 0, baseCommand.length);
// append each of the files to the command string
System.arraycopy(sourceFiles, 0, command, baseCommand.length, sourceCount);
//PApplet.println(command);
try {
@@ -103,14 +104,10 @@ public class Compiler {
PrintWriter writer = new PrintWriter(internalWriter);
//result = com.sun.tools.javac.Main.compile(command, writer);
CompilationProgress progress = null;
PrintWriter outWriter = new PrintWriter(System.out);
success =
org.eclipse.jdt.core.compiler.batch.BatchCompiler.compile(command,
outWriter,
writer,
progress);
success = BatchCompiler.compile(command, outWriter, writer, progress);
// Close out the stream for good measure
writer.flush();
writer.close();
@@ -123,11 +120,12 @@ public class Compiler {
while ((line = reader.readLine()) != null) {
//System.out.println("got line " + line); // debug
// get first line, which contains file name, line number,
// get first line, which contains file name, line number,
// and at least the first line of the error message
String errorFormat = "([\\w\\d_]+.java):(\\d+):\\s*(.*):\\s*(.*)\\s*";
String[] pieces = PApplet.match(line, errorFormat);
//PApplet.println(pieces);
// if it's something unexpected, die and print the mess to the console
if (pieces == null) {
exception = new RunnerException("Cannot parse error text: " + line);
@@ -135,15 +133,12 @@ public class Compiler {
// Send out the rest of the error message to the console.
System.err.println(line);
while ((line = reader.readLine()) != null) {
// for (int i = lineIndex; i < lineCount; i++) {
// System.err.println(lines[i]);
// }
System.err.println(line);
}
break;
}
// translate the java filename and line number into a un-preprocessed
// translate the java filename and line number into a un-preprocessed
// location inside a source file or tab in the environment.
String dotJavaFilename = pieces[1];
// Line numbers are 1-indexed from javac
@@ -159,43 +154,44 @@ public class Compiler {
if (code.isExtension("java")) {
if (dotJavaFilename.equals(code.getFileName())) {
codeIndex = i;
codeLine = dotJavaLineIndex;
}
}
}
//System.out.println("code index/line are " + codeIndex + " " + codeLine);
//System.out.println("java line number " + dotJavaLineIndex + " from " + dotJavaFilename);
// if it's not a .java file, codeIndex will still be 0
if (codeIndex == 0) { // main class, figure out which tab
for (int i = 1; i < sketch.getCodeCount(); i++) {
//for (int i = 1; i < sketch.getCodeCount(); i++) {
for (int i = 0; i < sketch.getCodeCount(); i++) {
SketchCode code = sketch.getCode(i);
//if (code.flavor == Sketch.PDE) {
if (code.isExtension("pde")) {
if (code.getPreprocOffset() <= dotJavaLineIndex) {
codeIndex = i;
//System.out.println("i'm thinkin file " + i);
codeLine = dotJavaLineIndex - code.getPreprocOffset();
}
}
}
}
//System.out.println("preproc offset is " + sketch.getCode(codeIndex).preprocOffset);
if (codeLine != -1) {
codeLine = dotJavaLineIndex - sketch.getCode(codeIndex).getPreprocOffset();
//if (codeLine != -1) {
//codeLine = dotJavaLineIndex - sketch.getCode(codeIndex).getPreprocOffset();
//}
}
//System.out.println("code line now " + codeLine);
exception = new RunnerException(errorMessage, codeIndex, codeLine, -1, false);
// for a test case once message parsing is implemented,
// for a test case once message parsing is implemented,
// use new Font(...) since that wasn't getting picked up properly.
/*
if (errorMessage.equals("cannot find symbol")) {
handleCannotFindSymbol(reader, exception);
} else if (errorMessage.indexOf("is already defined") != -1) {
reader.readLine(); // repeats the line of code w/ error
int codeColumn = caretColumn(reader.readLine());
exception = new RunnerException(errorMessage,
exception = new RunnerException(errorMessage,
codeIndex, codeLine, codeColumn);
} else if (errorMessage.startsWith("package") &&
@@ -208,18 +204,18 @@ public class Compiler {
exception = new RunnerException(errorMessage);
}
*/
if (errorMessage.startsWith("The import ") &&
if (errorMessage.startsWith("The import ") &&
errorMessage.endsWith("cannot be resolved")) {
// The import poo cannot be resolved
//import poo.shoe.blah.*;
String what = errorMessage.substring("The import ".length());
what = what.substring(0, what.indexOf(' '));
System.err.println("Starting with release 0149, libraries must be " +
System.err.println("Starting with release 0149, libraries must be " +
"installed in a folder named 'libraries' " +
"inside the 'sketchbook' folder.");
exception.setMessage("The package " +
"\u201C" + what + "\u201D" +
" does not exist. " +
exception.setMessage("The package " +
"\u201C" + what + "\u201D" +
" does not exist. " +
"You might be missing a library.");
// Actually create the folder and open it for the user
@@ -233,14 +229,14 @@ public class Compiler {
} else if (errorMessage.endsWith("cannot be resolved to a type")) {
// xxx cannot be resolved to a type
//xxx c;
String what = errorMessage.substring(0, errorMessage.indexOf(' '));
if (what.equals("BFont") ||
what.equals("BGraphics") ||
if (what.equals("BFont") ||
what.equals("BGraphics") ||
what.equals("BImage")) {
handleCrustyCode(exception);
} else {
exception.setMessage("Cannot find a class or type " +
"named \u201C" + what + "\u201D");
@@ -251,21 +247,21 @@ public class Compiler {
//println(xxx);
String what = errorMessage.substring(0, errorMessage.indexOf(' '));
if (what.equals("LINE_LOOP") ||
if (what.equals("LINE_LOOP") ||
what.equals("LINE_STRIP") ||
what.equals("framerate")) {
handleCrustyCode(exception);
} else {
exception.setMessage("Cannot find anything " +
"named \u201C" + what + "\u201D");
}
} else if (errorMessage.startsWith("Duplicate")) {
// "Duplicate nested type xxx"
// "Duplicate local variable xxx"
} else {
String[] parts = null;
@@ -275,7 +271,7 @@ public class Compiler {
//xxx("blah", 34);
// The method xxx(String, int) is undefined for the type PApplet
//PApplet.sub("ding");
String undefined =
String undefined =
"The method (\\S+\\(.*\\)) is undefined for the type (.*)";
parts = PApplet.match(errorMessage, undefined);
if (parts != null) {
@@ -307,8 +303,8 @@ public class Compiler {
return success;
}
/**
* Fire up 'ole javac based on <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javac.html#proginterface">this interface</a>.
*
@@ -444,11 +440,11 @@ public class Compiler {
// e.printStackTrace();
// }
//
// // get first line, which contains file name, line number,
// // get first line, which contains file name, line number,
// // and at least the first line of the error message
// String errorFormat = "([\\w\\d_]+.java):(\\d+):\\s*(.*)\\s*";
// String[] pieces = PApplet.match(line, errorFormat);
//
//
// // if it's something unexpected, die and print the mess to the console
// if (pieces == null) {
// exception = new RunnerException("Cannot parse error text: " + line);
@@ -461,8 +457,8 @@ public class Compiler {
// }
// break;
// }
//
// // translate the java filename and line number into a un-preprocessed
//
// // translate the java filename and line number into a un-preprocessed
// // location inside a source file or tab in the environment.
// String dotJavaFilename = pieces[0];
// // Line numbers are 1-indexed from javac
@@ -497,16 +493,16 @@ public class Compiler {
// //System.out.println("code line now " + codeLine);
// exception = new RunnerException(errorMessage, codeIndex, codeLine, -1, false);
//
// // for a test case once message parsing is implemented,
// // for a test case once message parsing is implemented,
// // use new Font(...) since that wasn't getting picked up properly.
//
//
// if (errorMessage.equals("cannot find symbol")) {
// handleCannotFindSymbol(reader, exception);
//
//
// } else if (errorMessage.indexOf("is already defined") != -1) {
// reader.readLine(); // repeats the line of code w/ error
// int codeColumn = caretColumn(reader.readLine());
// exception = new RunnerException(errorMessage,
// exception = new RunnerException(errorMessage,
// codeIndex, codeLine, codeColumn);
//
// } else if (errorMessage.startsWith("package") &&
@@ -542,8 +538,8 @@ public class Compiler {
// boolean isCompilerMatch(String line, String format) {
// return compilerMatch(line, format) != null;
// }
//
//
//
//
// Object[] compilerMatch(String line, String name) {
// String format = compilerResources.getString(name);
// MessageFormat mf = new MessageFormat(format);
@@ -555,21 +551,21 @@ public class Compiler {
// }
// return matches;
// }
// boolean isCompilerMatch(String line, ParsePosition pos, String format) {
// return compilerMatch(line, pos, format) != null;
// }
//
//
//
//
// Object[] compilerMatch(String line, ParsePosition pos, String name) {
// String format = compilerResources.getString(name);
// MessageFormat mf = new MessageFormat(format);
// Object[] matches = mf.parse(line, pos);
// return matches;
// }
// Tell-tale signs of old code copied and pasted from the web.
// Detect classes BFont, BGraphics, BImage; methods framerate, push;
// and variables LINE_LOOP and LINE_STRIP.
@@ -621,7 +617,7 @@ public class Compiler {
// }
// message += methodParams;
// }
//
//
// String locationClass = "location: class ";
// if (locationLine.startsWith(locationClass) &&
// // don't include the class name when it's a temp class