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 { ...@@ -45,7 +45,7 @@ public class ControlFacade {
exceptionHandler = new ExceptionHandler(fileHandlerInteractor, guiFacade); exceptionHandler = new ExceptionHandler(fileHandlerInteractor, guiFacade);
} }
boolean isInDebugMode() { public boolean isInDebugMode() {
return isInDebugMode; return isInDebugMode;
} }
...@@ -59,13 +59,15 @@ public class ControlFacade { ...@@ -59,13 +59,15 @@ public class ControlFacade {
private void ensureInDebugMode() { private void ensureInDebugMode() {
if (!isInDebugMode()) { if (!isInDebugMode()) {
throw new IllegalStateException(); //TODO: German translation
throw new IllegalStateException("This action is not allowed in edit mode.");
} }
} }
private void ensureNotInDebugMode() { private void ensureNotInDebugMode() {
if (isInDebugMode()) { 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 { ...@@ -100,7 +102,8 @@ public class ControlFacade {
} catch (DIbuggerLogicException exception) { } catch (DIbuggerLogicException exception) {
exceptionHandler.handle(exception); exceptionHandler.handle(exception);
} catch (IllegalStateException e){ } catch (IllegalStateException e){
exceptionHandler.handle(e);
//e.printStackTrace();
} }
} }
...@@ -117,7 +120,8 @@ public class ControlFacade { ...@@ -117,7 +120,8 @@ public class ControlFacade {
} catch (DIbuggerLogicException exception) { } catch (DIbuggerLogicException exception) {
exceptionHandler.handle(exception); exceptionHandler.handle(exception);
} catch (IllegalStateException e){ } catch (IllegalStateException e){
exceptionHandler.handle(e);
//e.printStackTrace();
} }
} }
...@@ -133,7 +137,8 @@ public class ControlFacade { ...@@ -133,7 +137,8 @@ public class ControlFacade {
ensureInDebugMode(); ensureInDebugMode();
debugLogicController.singleStep(programNameId); debugLogicController.singleStep(programNameId);
} catch (IllegalStateException e){ } catch (IllegalStateException e){
exceptionHandler.handle(e);
//e.printStackTrace();
} }
} }
...@@ -342,14 +347,17 @@ public class ControlFacade { ...@@ -342,14 +347,17 @@ public class ControlFacade {
* @see FileHandlerInteractor#loadConfigurationFile(File) * @see FileHandlerInteractor#loadConfigurationFile(File)
*/ */
public void loadConfiguration(File configurationFile) { public void loadConfiguration(File configurationFile) {
ensureNotInDebugMode();
try { try {
ensureNotInDebugMode();
fileHandlerInteractor.loadConfigurationFile(configurationFile); fileHandlerInteractor.loadConfigurationFile(configurationFile);
} catch (FileHandlerException exception) { } catch (FileHandlerException exception) {
exceptionHandler.handle(exception); exceptionHandler.handle(exception);
} catch (DIbuggerLogicException e) { } catch (DIbuggerLogicException e) {
exceptionHandler.handle(e); exceptionHandler.handle(e);
} catch (Exception e) {
exceptionHandler.handle(e);
} }
} }
...@@ -374,8 +382,15 @@ public class ControlFacade { ...@@ -374,8 +382,15 @@ public class ControlFacade {
* @see FileHandlerInteractor#loadProgramText(File) * @see FileHandlerInteractor#loadProgramText(File)
*/ */
public String loadProgramText(File file) { public String loadProgramText(File file) {
ensureNotInDebugMode(); try {
return fileHandlerInteractor.loadProgramText(file); ensureNotInDebugMode();
return fileHandlerInteractor.loadProgramText(file);
}
catch(Exception e) {
exceptionHandler.handle(e);
return "";
}
} }
/** /**
......
...@@ -61,6 +61,7 @@ public class ExceptionHandler implements Observer { ...@@ -61,6 +61,7 @@ public class ExceptionHandler implements Observer {
} }
errorMessage += "</html>"; errorMessage += "</html>";
guiFacade.showError(errorMessage); guiFacade.showError(errorMessage);
//exception.printStackTrace();
} }
/** /**
...@@ -73,12 +74,18 @@ public class ExceptionHandler implements Observer { ...@@ -73,12 +74,18 @@ public class ExceptionHandler implements Observer {
String exceptionId = exception.getID(); String exceptionId = exception.getID();
String errorMessage = languageFile.getTranslation(exceptionId); String errorMessage = languageFile.getTranslation(exceptionId);
guiFacade.showError(errorMessage); guiFacade.showError(errorMessage);
exception.printStackTrace();
} }
public void setLanguageFile(LanguageFile languageFile) { public void setLanguageFile(LanguageFile languageFile) {
Objects.requireNonNull(languageFile); Objects.requireNonNull(languageFile);
this.languageFile = languageFile; this.languageFile = languageFile;
} }
public void handle(Exception exception) {
//TODO: Provide German translation for this exception message.
guiFacade.showErrorAndContinueDebugging(exception.getMessage());
}
@Override @Override
public void update(Observable observable, Object argument) { public void update(Observable observable, Object argument) {
......
...@@ -6,6 +6,7 @@ import java.util.List; ...@@ -6,6 +6,7 @@ import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.Map; import java.util.Map;
import dibugger.debuglogic.antlrparser.ActuallyHelpfulSyntaxException;
import dibugger.debuglogic.exceptions.DIbuggerLogicException; import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.SyntaxException; import dibugger.debuglogic.exceptions.SyntaxException;
import dibugger.debuglogic.interpreter.ConditionalBreakpoint; import dibugger.debuglogic.interpreter.ConditionalBreakpoint;
...@@ -86,9 +87,13 @@ public class DebugControl { ...@@ -86,9 +87,13 @@ public class DebugControl {
generationController.setMaxIterations(maxIterations); generationController.setMaxIterations(maxIterations);
for (int i = 0; i < programs.size(); ++i) { for (int i = 0; i < programs.size(); ++i) {
ProgramInput pi = programs.get(i); ProgramInput pi = programs.get(i);
ListIterator<TraceState> generatedTrace;
list_traceIterator try {
.add(generationController.generateTrace(pi.getText(), pi.getInputValues(), pi.getProgramID())); 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()); list_programReturnValue.add(generationController.getReturnValue());
if (list_stepSize.size() <= i) { if (list_stepSize.size() <= i) {
......
...@@ -9,7 +9,7 @@ import java.util.List; ...@@ -9,7 +9,7 @@ import java.util.List;
* @author scheler * @author scheler
* *
*/ */
public abstract class DIbuggerLogicException extends Exception { public class DIbuggerLogicException extends Exception {
/** /**
* Default serialVersionUID. * Default serialVersionUID.
...@@ -29,10 +29,24 @@ public abstract class DIbuggerLogicException extends Exception { ...@@ -29,10 +29,24 @@ public abstract class DIbuggerLogicException extends Exception {
* in the language file * in the language file
*/ */
public DIbuggerLogicException(int occurrence, String id) { public DIbuggerLogicException(int occurrence, String id) {
this.occurrences = new ArrayList<String>(); 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.occurrences.add("" + occurrence);
this.id = id; this.id = id;
} }
/** /**
* Getter for the identifier of an exception. * Getter for the identifier of an exception.
...@@ -61,4 +75,6 @@ public abstract class DIbuggerLogicException extends Exception { ...@@ -61,4 +75,6 @@ public abstract class DIbuggerLogicException extends Exception {
public void addOccurence(String occurrence) { public void addOccurence(String occurrence) {
this.occurrences.add(occurrence); this.occurrences.add(occurrence);
} }
} }
...@@ -6,7 +6,7 @@ package dibugger.debuglogic.exceptions; ...@@ -6,7 +6,7 @@ package dibugger.debuglogic.exceptions;
* @author scheler * @author scheler
* *
*/ */
public class VariableNotFoundException extends DIbuggerLogicException { public class IdentifierNotFoundException extends DIbuggerLogicException {
/** /**
* Default serialVersionUID. * Default serialVersionUID.
...@@ -19,8 +19,8 @@ public class VariableNotFoundException extends DIbuggerLogicException { ...@@ -19,8 +19,8 @@ public class VariableNotFoundException extends DIbuggerLogicException {
* @param occurrence * @param occurrence
* the line of occurrence of the exception * the line of occurrence of the exception
*/ */
public VariableNotFoundException(int occurrence) { public IdentifierNotFoundException(int occurrence, String id) {
super(occurrence, "dl_exc_variable_not_found"); super(occurrence, "dl_exc_variable_not_found", "Unknown identifier in: "+id);
} }
} }
...@@ -3,7 +3,7 @@ package dibugger.debuglogic.interpreter; ...@@ -3,7 +3,7 @@ package dibugger.debuglogic.interpreter;
import java.util.List; import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException; 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 * Represents an arrayaccess based on the arrays of multiple programs
...@@ -100,12 +100,12 @@ public final class ArrayAccessRelationalTerm extends Term { ...@@ -100,12 +100,12 @@ public final class ArrayAccessRelationalTerm extends Term {
} }
} }
} }
throw new VariableNotFoundException(-1); throw new IdentifierNotFoundException(-1, identifier);
} }
@Override @Override
public TermValue evaluate(Scope currentScope) throws VariableNotFoundException { public TermValue evaluate(Scope currentScope) throws IdentifierNotFoundException {
throw new VariableNotFoundException(-1); throw new IdentifierNotFoundException(-1, identifier);
} }
private void splitId(String identifier) { private void splitId(String identifier) {
......
...@@ -3,7 +3,7 @@ package dibugger.debuglogic.interpreter; ...@@ -3,7 +3,7 @@ package dibugger.debuglogic.interpreter;
import java.util.List; import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException; 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. * Represents an arrayaccess based on the arrays of a single program.
...@@ -68,8 +68,8 @@ public final class ArrayAccessTerm extends Term { ...@@ -68,8 +68,8 @@ public final class ArrayAccessTerm extends Term {
} }
@Override @Override
public TermValue evaluate(List<TraceState> states) throws VariableNotFoundException { public TermValue evaluate(List<TraceState> states) throws IdentifierNotFoundException {
throw new VariableNotFoundException(-1); throw new IdentifierNotFoundException(-1, identifier);
} }
@Override @Override
......
...@@ -4,7 +4,7 @@ import java.util.ArrayList; ...@@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException; import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException; import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
import dibugger.debuglogic.exceptions.WrongArgumentException; import dibugger.debuglogic.exceptions.WrongArgumentException;
import dibugger.debuglogic.exceptions.WrongTypeArgumentException; import dibugger.debuglogic.exceptions.WrongTypeArgumentException;
import dibugger.debuglogic.exceptions.WrongTypeAssignmentException; import dibugger.debuglogic.exceptions.WrongTypeAssignmentException;
...@@ -62,7 +62,7 @@ public class ArrayElementAssignment extends Command { ...@@ -62,7 +62,7 @@ public class ArrayElementAssignment extends Command {
// check if variable exists // check if variable exists
if (type == null) { if (type == null) {
throw new VariableNotFoundException(this.linenumber); throw new IdentifierNotFoundException(this.linenumber, identifier);
} }
// check type // check type
...@@ -100,11 +100,11 @@ public class ArrayElementAssignment extends Command { ...@@ -100,11 +100,11 @@ public class ArrayElementAssignment extends Command {
if (k < innerInnerArray.length) { if (k < innerInnerArray.length) {
innerInnerArray[k] = val; innerInnerArray[k] = val;
} else } else
throw new VariableNotFoundException(this.linenumber); throw new IdentifierNotFoundException(this.linenumber, identifier);
} else } else
throw new VariableNotFoundException(this.linenumber); throw new IdentifierNotFoundException(this.linenumber, identifier);
} else } else
throw new VariableNotFoundException(this.linenumber); throw new IdentifierNotFoundException(this.linenumber, identifier);
// save the state and return it // save the state and return it
List<TraceState> traceStateList = new ArrayList<TraceState>(); List<TraceState> traceStateList = new ArrayList<TraceState>();
......
...@@ -4,7 +4,7 @@ import java.util.ArrayList; ...@@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException; import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException; import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
import dibugger.debuglogic.exceptions.WrongTypeAssignmentException; import dibugger.debuglogic.exceptions.WrongTypeAssignmentException;
/** /**
...@@ -30,7 +30,7 @@ public class Assignment extends Command { ...@@ -30,7 +30,7 @@ public class Assignment extends Command {
Type type = scope.getTypeOf(identifier); Type type = scope.getTypeOf(identifier);
// check if variable exists // check if variable exists
if (type == null) if (type == null)
throw new VariableNotFoundException(this.linenumber); throw new IdentifierNotFoundException(this.linenumber, identifier);
// check type // check type
if (type != value.getType()) { if (type != value.getType()) {
......
...@@ -4,7 +4,7 @@ import java.util.ArrayList; ...@@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException; import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException; import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
import dibugger.debuglogic.exceptions.WrongTypeAssignmentException; import dibugger.debuglogic.exceptions.WrongTypeAssignmentException;
/** /**
...@@ -29,7 +29,7 @@ public class CallingAssignment extends Command { ...@@ -29,7 +29,7 @@ public class CallingAssignment extends Command {
Type type = scope.getTypeOf(identifier); Type type = scope.getTypeOf(identifier);
// check if variable exists // check if variable exists
if (type == null) if (type == null)
throw new VariableNotFoundException(this.linenumber); throw new IdentifierNotFoundException(this.linenumber, identifier);
// run the function // run the function
List<TraceState> traceStateList = new ArrayList<TraceState>(); List<TraceState> traceStateList = new ArrayList<TraceState>();
......
...@@ -19,7 +19,7 @@ import dibugger.debuglogic.exceptions.ConditionalBreakpointSyntaxException; ...@@ -19,7 +19,7 @@ import dibugger.debuglogic.exceptions.ConditionalBreakpointSyntaxException;
import dibugger.debuglogic.exceptions.DIbuggerLogicException; import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.IncompatibleTypeException; import dibugger.debuglogic.exceptions.IncompatibleTypeException;
import dibugger.debuglogic.exceptions.SyntaxException; import dibugger.debuglogic.exceptions.SyntaxException;
import dibugger.debuglogic.exceptions.VariableNotFoundException; import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
/** /**
* @author wagner * @author wagner
......
...@@ -5,7 +5,7 @@ import java.util.List; ...@@ -5,7 +5,7 @@ import java.util.List;
import dibugger.debuglogic.exceptions.AlreadyDeclaredException; import dibugger.debuglogic.exceptions.AlreadyDeclaredException;
import dibugger.debuglogic.exceptions.DIbuggerLogicException; import dibugger.debuglogic.exceptions.DIbuggerLogicException;
import dibugger.debuglogic.exceptions.VariableNotFoundException; import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
/** /**
* Command to declare a variable. * Command to declare a variable.
......
...@@ -76,6 +76,9 @@ public class GenerationController { ...@@ -76,6 +76,9 @@ public class GenerationController {
ParseTree mainTree; ParseTree mainTree;
// create parsetree // create parsetree
try { try {
if(programText == null || programText.equals("")) {
throw new ActuallyHelpfulSyntaxException("Program is empty.");
}
CharStream stream = CharStreams.fromString(programText); CharStream stream = CharStreams.fromString(programText);
WlangLexer lexer = new WlangLexer(stream); WlangLexer lexer = new WlangLexer(stream);
// Setting our own error listener. // Setting our own error listener.
......
...@@ -2,7 +2,7 @@ package dibugger.debuglogic.interpreter; ...@@ -2,7 +2,7 @@ package dibugger.debuglogic.interpreter;
import java.util.List; import java.util.List;
import dibugger.debuglogic.exceptions.VariableNotFoundException; import dibugger.debuglogic.exceptions.IdentifierNotFoundException;
/** /**
* Represents a variableaccess based on the values of multiple programs. * Represents a variableaccess based on the values of multiple programs.
...@@ -29,7 +29,7 @@ public final class VariableRelationalTerm extends Term { ...@@ -29,7 +29,7 @@ public final class VariableRelationalTerm extends Term {
} }
@Override @Override
public TermValue evaluate(List<TraceState> states) throws VariableNotFoundException { public TermValue evaluate(List<TraceState> states) throws IdentifierNotFoundException {
// find the state the variable belongs to // find the state the variable belongs to
for (TraceState state : states) { for (TraceState state : states) {
if (state.getProgramId() != null && state.getProgramId().equals(this.programId)) { if (state.getProgramId() != null && state.getProgramId().equals(this.programId)) {
...@@ -38,12 +38,12 @@ public final class VariableRelationalTerm extends Term { ...@@ -38,12 +38,12 @@ public final class VariableRelationalTerm extends Term {
return value; return value;
} }
} }
throw new VariableNotFoundException(-1); throw new IdentifierNotFoundException(-1, identifier);
} }
@Override @Override
public TermValue evaluate(Scope currentScope) throws VariableNotFoundException { public TermValue evaluate(Scope currentScope) throws IdentifierNotFoundException {
throw new VariableNotFoundException(-1); throw new IdentifierNotFoundException(-1, identifier);
} }
} }
...@@ -3,7 +3,7 @@ package dibugger.debuglogic.interpreter; ...@@ -3,7 +3,7 @@ package dibugger.debuglogic.interpreter;
import java.util.List; import java.util.List;
import dibugger.debuglogic.exceptions.DIbuggerLogicException; 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. * Represents a variableaccess based on the values of a single program.
...@@ -19,15 +19,15 @@ public final class VariableTerm extends Term { ...@@ -19,15 +19,15 @@ public final class VariableTerm extends Term {
} }
@Override @Override
public TermValue evaluate(List<TraceState> states) throws VariableNotFoundException { public TermValue evaluate(List<TraceState> states) throws IdentifierNotFoundException {
throw new VariableNotFoundException(-1); throw new IdentifierNotFoundException(-1, identifier);
} }
@Override @Override
public TermValue evaluate(Scope currentScope) throws DIbuggerLogicException { public TermValue evaluate(Scope currentScope) throws DIbuggerLogicException {
TermValue value = currentScope.getValueOf(identifier); TermValue value = currentScope.getValueOf(identifier);