Commit 9107d8de authored by Mihai Herda's avatar Mihai Herda
Browse files

Worked on exception system.

Now it shows which program caused a syntax exception.

Now it also shows the variable identifier that is unknown.

Special handling of empty programs.

If failed to enter debug mode because of syntax exception, it will no
longer try to continue the debugging process, thus throwing
IllegalStateExceptions for every program.
parent 556a64b3
......@@ -45,7 +45,7 @@ public class ControlFacade {
exceptionHandler = new ExceptionHandler(fileHandlerInteractor, guiFacade);
}
boolean isInDebugMode() {
public boolean isInDebugMode() {
return isInDebugMode;
}
......@@ -59,13 +59,15 @@ public class ControlFacade {
private void ensureInDebugMode() {
if (!isInDebugMode()) {
throw new IllegalStateException();
//TODO: German translation
throw new IllegalStateException("This action is not allowed in edit mode.");
}
}
private void ensureNotInDebugMode() {
if (isInDebugMode()) {
throw new IllegalStateException();
//TODO: German translation
throw new IllegalStateException("This action is not allowed in debug mode.");
}
}
......@@ -100,7 +102,8 @@ public class ControlFacade {
} catch (DIbuggerLogicException exception) {
exceptionHandler.handle(exception);
} catch (IllegalStateException e){
exceptionHandler.handle(e);
//e.printStackTrace();
}
}
......@@ -117,7 +120,8 @@ public class ControlFacade {
} catch (DIbuggerLogicException exception) {
exceptionHandler.handle(exception);
} catch (IllegalStateException e){
exceptionHandler.handle(e);
//e.printStackTrace();
}
}
......@@ -133,7 +137,8 @@ public class ControlFacade {
ensureInDebugMode();
debugLogicController.singleStep(programNameId);
} catch (IllegalStateException e){
exceptionHandler.handle(e);
//e.printStackTrace();
}
}
......@@ -342,14 +347,17 @@ public class ControlFacade {
* @see FileHandlerInteractor#loadConfigurationFile(File)
*/
public void loadConfiguration(File configurationFile) {
ensureNotInDebugMode();
try {
ensureNotInDebugMode();
fileHandlerInteractor.loadConfigurationFile(configurationFile);
} catch (FileHandlerException exception) {
exceptionHandler.handle(exception);
} catch (DIbuggerLogicException e) {
exceptionHandler.handle(e);
} catch (Exception e) {
exceptionHandler.handle(e);
}
}
......@@ -374,9 +382,16 @@ public class ControlFacade {
* @see FileHandlerInteractor#loadProgramText(File)
*/
public String loadProgramText(File file) {
try {
ensureNotInDebugMode();
return fileHandlerInteractor.loadProgramText(file);
}
catch(Exception e) {
exceptionHandler.handle(e);
return "";
}
}
/**
* Returns a list containing all languages available for use by this'
......
......@@ -61,6 +61,7 @@ public class ExceptionHandler implements Observer {
}
errorMessage += "</html>";
guiFacade.showError(errorMessage);
//exception.printStackTrace();
}
/**
......@@ -73,6 +74,7 @@ public class ExceptionHandler implements Observer {
String exceptionId = exception.getID();
String errorMessage = languageFile.getTranslation(exceptionId);
guiFacade.showError(errorMessage);
exception.printStackTrace();
}
public void setLanguageFile(LanguageFile languageFile) {
......@@ -80,6 +82,11 @@ public class ExceptionHandler implements Observer {
this.languageFile = languageFile;
}
public void handle(Exception exception) {
//TODO: Provide German translation for this exception message.
guiFacade.showErrorAndContinueDebugging(exception.getMessage());
}
@Override
public void update(Observable observable, Object argument) {
if (observable instanceof FileHandlerInteractor) {
......
......@@ -6,6 +6,7 @@ import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import dibugger.debuglogic.antlrparser.ActuallyHelpfulSyntaxException;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.SyntaxException;
import dibugger.debuglogic.interpreter.ConditionalBreakpoint;
......@@ -86,9 +87,13 @@ public class DebugControl {
generationController.setMaxIterations(maxIterations);
for (int i = 0; i < programs.size(); ++i) {
ProgramInput pi = programs.get(i);
list_traceIterator
.add(generationController.generateTrace(pi.getText(), pi.getInputValues(), pi.getProgramID()));
ListIterator<TraceState> generatedTrace;
try {
generatedTrace = generationController.generateTrace(pi.getText(), pi.getInputValues(), pi.getProgramID());
}catch(DIbuggerLogicException e) {
throw new DIbuggerLogicException(e,"Program "+pi.getProgramID()+": "+ e.getMessage());
}
list_traceIterator.add(generatedTrace);
list_programReturnValue.add(generationController.getReturnValue());
if (list_stepSize.size() <= i) {
......
......@@ -9,7 +9,7 @@ import java.util.List;
* @author scheler
*
*/
public abstract class DIbuggerLogicException extends Exception {
public class DIbuggerLogicException extends Exception {
/**
* Default serialVersionUID.
......@@ -29,11 +29,25 @@ public abstract class DIbuggerLogicException extends Exception {
* in the language file
*/
public DIbuggerLogicException(int occurrence, String id) {
this(occurrence, id, "");
}
public DIbuggerLogicException(DIbuggerLogicException e, String message) {
super(message);
this.occurrences = new ArrayList<String>();
this.occurrences.addAll(e.occurrences);
this.id=e.id;
}
public DIbuggerLogicException(int occurrence, String id, String message) {
super(message);
this.occurrences = new ArrayList<String>();
this.occurrences.add("" + occurrence);
this.id = id;
}
/**
* Getter for the identifier of an exception.
*
......@@ -61,4 +75,6 @@ public abstract class DIbuggerLogicException extends Exception {
public void addOccurence(String occurrence) {
this.occurrences.add(occurrence);
}
}
......@@ -6,7 +6,7 @@ package dibugger.debuglogic.exceptions;
* @author scheler
*
*/
public class VariableNotFoundException extends DIbuggerLogicException {
public class IdentifierNotFoundException extends DIbuggerLogicException {
/**
* Default serialVersionUID.
......@@ -19,8 +19,8 @@ public class VariableNotFoundException extends DIbuggerLogicException {
* @param occurrence
* the line of occurrence of the exception
*/
public VariableNotFoundException(int occurrence) {
super(occurrence, "dl_exc_variable_not_found");
public IdentifierNotFoundException(int occurrence, String id) {
super(occurrence, "dl_exc_variable_not_found", "Unknown identifier in: "+id);
}
}
......@@ -3,7 +3,7 @@ package dibugger.debuglogic.interpreter;
import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
/**
* Represents an arrayaccess based on the arrays of multiple programs
......@@ -100,12 +100,12 @@ public final class ArrayAccessRelationalTerm extends Term {
}
}
}
throw new VariableNotFoundException(-1);
throw new IdentifierNotFoundException(-1, identifier);
}
@Override
public TermValue evaluate(Scope currentScope) throws VariableNotFoundException {
throw new VariableNotFoundException(-1);
public TermValue evaluate(Scope currentScope) throws IdentifierNotFoundException {
throw new IdentifierNotFoundException(-1, identifier);
}
private void splitId(String identifier) {
......
......@@ -3,7 +3,7 @@ package dibugger.debuglogic.interpreter;
import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
/**
* Represents an arrayaccess based on the arrays of a single program.
......@@ -68,8 +68,8 @@ public final class ArrayAccessTerm extends Term {
}
@Override
public TermValue evaluate(List<TraceState> states) throws VariableNotFoundException {
throw new VariableNotFoundException(-1);
public TermValue evaluate(List<TraceState> states) throws IdentifierNotFoundException {
throw new IdentifierNotFoundException(-1, identifier);
}
@Override
......
......@@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
import dibugger.debuglogic.exceptions.WrongArgumentException;
import dibugger.debuglogic.exceptions.WrongTypeArgumentException;
import dibugger.debuglogic.exceptions.WrongTypeAssignmentException;
......@@ -62,7 +62,7 @@ public class ArrayElementAssignment extends Command {
// check if variable exists
if (type == null) {
throw new VariableNotFoundException(this.linenumber);
throw new IdentifierNotFoundException(this.linenumber, identifier);
}
// check type
......@@ -100,11 +100,11 @@ public class ArrayElementAssignment extends Command {
if (k < innerInnerArray.length) {
innerInnerArray[k] = val;
} else
throw new VariableNotFoundException(this.linenumber);
throw new IdentifierNotFoundException(this.linenumber, identifier);
} else
throw new VariableNotFoundException(this.linenumber);
throw new IdentifierNotFoundException(this.linenumber, identifier);
} else
throw new VariableNotFoundException(this.linenumber);
throw new IdentifierNotFoundException(this.linenumber, identifier);
// save the state and return it
List<TraceState> traceStateList = new ArrayList<TraceState>();
......
......@@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
import dibugger.debuglogic.exceptions.WrongTypeAssignmentException;
/**
......@@ -30,7 +30,7 @@ public class Assignment extends Command {
Type type = scope.getTypeOf(identifier);
// check if variable exists
if (type == null)
throw new VariableNotFoundException(this.linenumber);
throw new IdentifierNotFoundException(this.linenumber, identifier);
// check type
if (type != value.getType()) {
......
......@@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
import dibugger.debuglogic.exceptions.WrongTypeAssignmentException;
/**
......@@ -29,7 +29,7 @@ public class CallingAssignment extends Command {
Type type = scope.getTypeOf(identifier);
// check if variable exists
if (type == null)
throw new VariableNotFoundException(this.linenumber);
throw new IdentifierNotFoundException(this.linenumber, identifier);
// run the function
List<TraceState> traceStateList = new ArrayList<TraceState>();
......
......@@ -19,7 +19,7 @@ import dibugger.debuglogic.exceptions.ConditionalBreakpointSyntaxException;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.IncompatibleTypeException;
import dibugger.debuglogic.exceptions.SyntaxException;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
/**
* @author wagner
......
......@@ -5,7 +5,7 @@ import java.util.List;
import dibugger.debuglogic.exceptions.AlreadyDeclaredException;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
/**
* Command to declare a variable.
......
......@@ -76,6 +76,9 @@ public class GenerationController {
ParseTree mainTree;
// create parsetree
try {
if(programText == null || programText.equals("")) {
throw new ActuallyHelpfulSyntaxException("Program is empty.");
}
CharStream stream = CharStreams.fromString(programText);
WlangLexer lexer = new WlangLexer(stream);
// Setting our own error listener.
......
......@@ -2,7 +2,7 @@ package dibugger.debuglogic.interpreter;
import java.util.List;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
/**
* Represents a variableaccess based on the values of multiple programs.
......@@ -29,7 +29,7 @@ public final class VariableRelationalTerm extends Term {
}
@Override
public TermValue evaluate(List<TraceState> states) throws VariableNotFoundException {
public TermValue evaluate(List<TraceState> states) throws IdentifierNotFoundException {
// find the state the variable belongs to
for (TraceState state : states) {
if (state.getProgramId() != null && state.getProgramId().equals(this.programId)) {
......@@ -38,12 +38,12 @@ public final class VariableRelationalTerm extends Term {
return value;
}
}
throw new VariableNotFoundException(-1);
throw new IdentifierNotFoundException(-1, identifier);
}
@Override
public TermValue evaluate(Scope currentScope) throws VariableNotFoundException {
throw new VariableNotFoundException(-1);
public TermValue evaluate(Scope currentScope) throws IdentifierNotFoundException {
throw new IdentifierNotFoundException(-1, identifier);
}
}
......@@ -3,7 +3,7 @@ package dibugger.debuglogic.interpreter;
import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
/**
* Represents a variableaccess based on the values of a single program.
......@@ -19,15 +19,15 @@ public final class VariableTerm extends Term {
}
@Override
public TermValue evaluate(List<TraceState> states) throws VariableNotFoundException {
throw new VariableNotFoundException(-1);
public TermValue evaluate(List<TraceState> states) throws IdentifierNotFoundException {
throw new IdentifierNotFoundException(-1, identifier);
}
@Override
public TermValue evaluate(Scope currentScope) throws DIbuggerLogicException {
TermValue value = currentScope.getValueOf(identifier);
if (value == null)
throw new VariableNotFoundException(-1);
throw new IdentifierNotFoundException(-1, identifier);
return value;
}
......
......@@ -14,7 +14,7 @@ import dibugger.debuglogic.antlrparser.WlangLexer;
import dibugger.debuglogic.antlrparser.WlangParser;
import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.SyntaxException;
import dibugger.debuglogic.exceptions.VariableNotFoundException;
import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
import dibugger.debuglogic.exceptions.WatchExpressionSyntaxException;
/**
......
......@@ -95,7 +95,17 @@ public class GUIFacade implements Observer {
* error message
*/
public void showError(String s) {
new ErrorPopUp(s, mainInterface);
new ErrorPopUp(s, mainInterface, true);
}
/**
* Shows an ErrorPopUp
*
* @param s
* error message
*/
public void showErrorAndContinueDebugging(String s) {
new ErrorPopUp(s, mainInterface, false);
}
/**
......
......@@ -214,7 +214,7 @@ public class MainInterface extends JFrame {
newProgram.setText(ADD_PROGRAM);
newProgram.addActionListener(actionEvent -> {
if (programPanels.size() >= 26) {
new ErrorPopUp(TOO_MANY_PROGRAM_PANELS, this);
new ErrorPopUp(TOO_MANY_PROGRAM_PANELS, this, true);
} else {
String nextId = calcNextProgramId();
ProgramPanel newPanel = new ProgramPanel(nextId, this);
......@@ -662,6 +662,14 @@ public class MainInterface extends JFrame {
p.startDebug();
}
controlFacade.startDebug();
/*
* If starting debug mode failed, because of some error,
* do not perform single steps in programs, as this will
* cause other errors.
*/
if(!controlFacade.isInDebugMode()) {
return;
}
for (ProgramPanel p : programPanels.values()) {
p.singleStep();
}
......
......@@ -23,7 +23,7 @@ public class ErrorPopUp extends DIbuggerPopUp {
* @param mainInterface
* MainInterface on which the PopUp is called
*/
public ErrorPopUp(String message, MainInterface mainInterface) {
public ErrorPopUp(String message, MainInterface mainInterface, boolean stopDebugging) {
this.mainInterface = mainInterface;
this.setSize(400, 200);
this.setResizable(true);
......@@ -41,7 +41,9 @@ public class ErrorPopUp extends DIbuggerPopUp {
JButton ok = new JButton("OK");
ok.addActionListener(actionEvent -> {
if(stopDebugging) {
CommandPanel.getCommandPanel(this.mainInterface).stopDebug();
}
dispose();
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment