Fixed dropping of tree in PdePreprocessor.

As part of #177, make sure we continue to walk the tree even when there are errors.
This commit is contained in:
A Pottinger
2021-07-03 16:49:08 -07:00
parent 54496a0b7e
commit 26c3bf135d
3 changed files with 54 additions and 13 deletions

View File

@@ -255,9 +255,20 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
/**
* Get the result of the last preprocessing.
*
* @param issues The errors (if any) encountered.
* @return The result of the last preprocessing.
*/
public PreprocessorResult getResult() {
return getResult(new ArrayList<>());
}
/**
* Get the result of the last preprocessing with optional error.
*
* @param issues The errors (if any) encountered.
* @return The result of the last preprocessing.
*/
public PreprocessorResult getResult(List<PdePreprocessIssue> issues) {
List<ImportStatement> allImports = new ArrayList<>();
allImports.addAll(coreImports);
@@ -277,7 +288,8 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
allImports,
allEdits,
sketchWidth,
sketchHeight
sketchHeight,
issues
);
}

View File

@@ -208,20 +208,11 @@ public class PdePreprocessor {
));
parser.setBuildParseTree(true);
tree = parser.processingSketch();
if (preprocessIssues.size() > 0) {
return PreprocessorResult.reportPreprocessIssues(preprocessIssues);
}
}
ParseTreeWalker treeWalker = new ParseTreeWalker();
treeWalker.walk(listener, tree);
// Check for issues encountered in walk
if (treeIssues.size() > 0) {
return PreprocessorResult.reportPreprocessIssues(treeIssues);
}
// Return resulting program
String outputProgram = listener.getOutputProgram();
PrintWriter outPrintWriter = new PrintWriter(outWriter);
@@ -229,7 +220,13 @@ public class PdePreprocessor {
foundMain = listener.foundMain();
return listener.getResult();
if (preprocessIssues.size() > 0) {
return listener.getResult(preprocessIssues);
} else if (treeIssues.size() > 0) {
return listener.getResult(treeIssues);
} else {
return listener.getResult();
}
}
/**

View File

@@ -69,8 +69,8 @@ public class PreprocessorResult {
* @param newSketchHeight The height of the sketch in pixels or special value like displayWidth;
*/
public PreprocessorResult(PdePreprocessor.Mode newProgramType, int newHeaderOffset,
String newClassName, List<ImportStatement> newImportStatements, List<TextTransform.Edit> newEdits,
String newSketchWidth, String newSketchHeight) {
String newClassName, List<ImportStatement> newImportStatements,
List<TextTransform.Edit> newEdits, String newSketchWidth, String newSketchHeight) {
if (newClassName == null) {
throw new RuntimeException("Could not find main class");
@@ -87,6 +87,38 @@ public class PreprocessorResult {
sketchHeight = newSketchHeight;
}
/**
* Create a new preprocessing result with errors
*
* @param newProgramType The type of program that has be preprocessed.
* @param newHeaderOffset The offset (in number of chars) from the start of the program at which
* the header finishes.
* @param newClassName The name of the class containing the sketch.
* @param newImportStatements The imports required for the sketch including defaults and core imports.
* @param newEdits The edits made during preprocessing.
* @param newSketchWidth The width of the sketch in pixels or special value like displayWidth;
* @param newSketchHeight The height of the sketch in pixels or special value like displayWidth;
*/
public PreprocessorResult(PdePreprocessor.Mode newProgramType, int newHeaderOffset,
String newClassName, List<ImportStatement> newImportStatements,
List<TextTransform.Edit> newEdits, String newSketchWidth, String newSketchHeight,
List<PdePreprocessIssue> newPreprocessIssues) {
if (newClassName == null) {
throw new RuntimeException("Could not find main class");
}
headerOffset = newHeaderOffset;
className = newClassName;
importStatements = newImportStatements;
programType = newProgramType;
edits = newEdits;
preprocessIssues = newPreprocessIssues;
sketchWidth = newSketchWidth;
sketchHeight = newSketchHeight;
}
/**
* Private constructor allowing creation of result indicating preprocess issues.
*