From e923ba7f026deb25d59cf38db1809967248ff44b Mon Sep 17 00:00:00 2001 From: villberg Date: Thu, 2 Oct 2014 11:20:05 +0000 Subject: [PATCH] Improved error reporting refs #5224 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30356 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../src/fi/semantum/sysdyn/solver/Array.java | 2 +- .../fi/semantum/sysdyn/solver/Assignment.java | 49 +++++++++++++++++-- .../sysdyn/solver/ExecutionException.java | 18 +++++++ .../fi/semantum/sysdyn/solver/Function.java | 5 +- .../src/fi/semantum/sysdyn/solver/Model.java | 36 ++++++++++++-- .../src/fi/semantum/sysdyn/solver/Parser.java | 26 +++++----- .../src/fi/semantum/sysdyn/solver/Solver.java | 25 +++++----- .../fi/semantum/sysdyn/solver/Variable.java | 20 ++++++-- .../sysdyn/solver/parser/ModelParser.java | 48 +++++++++--------- .../sysdyn/solver/parser/ModelicaParser.jj | 16 +++--- .../sysdyn/solver/parser/ModelicaParser.jjt | 10 ++-- .../sysdyn/solver/parser/SimpleNode.java | 1 + 12 files changed, 179 insertions(+), 77 deletions(-) create mode 100644 fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ExecutionException.java diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java index 6cdc5b83..cb16d4b9 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java @@ -515,7 +515,7 @@ public class Array implements IExpression { Object element = element(index); // Last position should not be array if(position == subscripts.length-1) { - if(element instanceof Array) throw new IllegalStateException(); + if(element instanceof Array) throw new ExecutionException("Array size does not match subscripts."); if(element instanceof IExpression) { IExpression exp = (IExpression)element; return exp.evaluate(environment); diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Assignment.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Assignment.java index 6607c198..67752379 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Assignment.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Assignment.java @@ -10,16 +10,19 @@ *******************************************************************************/ package fi.semantum.sysdyn.solver; - public class Assignment { - + + private static final boolean VALIDATE = true; + + public IFrame model; public Variable target; public IExpression[] subscripts; public IExpression expression; public boolean assigned = false; public boolean isConstant = false; - public Assignment(Variable target, IExpression[] subscripts, IExpression expression) { + public Assignment(IFrame model, Variable target, IExpression[] subscripts, IExpression expression) { + this.model = model; this.target = target; this.subscripts = subscripts; this.expression = expression; @@ -35,10 +38,46 @@ public class Assignment { IExpression[] subscripts2 = new IExpression[subscripts.length]; for(int i=0;i indices = new ArrayList(); + + public int line; - public Function(String name, IStatement statement) { + public Function(String name, IStatement statement, int line) { this.name = name; this.statement = statement; + this.line = line; } public Object evaluate(IEnvironment environment, int argc) { diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java index 537d238f..ab861d89 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Model.java @@ -40,6 +40,7 @@ class Model implements IFrame { public final Globals globals; + public int line = -1; public String name; public boolean isEnumClass; @@ -82,6 +83,16 @@ class Model implements IFrame { return base; } + private void reportException(Fn fn, ExecutionException e) throws ExecutionException { + + if(fn instanceof Function) { + throw new ExecutionException("function " + name + ": " + (e.line - ((Function)fn).line) + ": " + e.getMessage()); + } else { + throw new ExecutionException("function " + name + ": " + (e.line - ((Function)fn).line) + ": " + e.getMessage()); + } + + } + public Object evaluateFunction(IEnvironment environment, String name, ArgumentList args) { Fn fn = getFunction(name); if(fn == null) { @@ -109,7 +120,15 @@ class Model implements IFrame { fn.setLocals(frame); - return fn.evaluate(frame, args.args.length+1); + try { + + return fn.evaluate(frame, args.args.length+1); + + } catch (ExecutionException e) { + + reportException(fn, e); + + } } else { @@ -157,13 +176,24 @@ class Model implements IFrame { } fn.setLocals(frame); - Object value = fn.evaluate(frame, args.args.length); - return value; + + try { + + return fn.evaluate(frame, args.args.length); + + } catch (ExecutionException e) { + + reportException(fn, e); + + } } } + // This cannot happen - reportException always throws + throw new IllegalStateException(); + } private Array arrayEvaluate(Frame frame, Fn fn, Variable[] parameters, Object[] ps, boolean[] vector, List arrs) { diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java index 6560e2d1..14c73e87 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java @@ -74,12 +74,12 @@ public class Parser { IExpression exp = (IExpression)walk((SimpleNode)n.jjtGetChild(1), indent+2, frame); if(v instanceof Derivative) { Derivative der = (Derivative)v; - Assignment eq = new Assignment(der.variable, der.variable.subscripts, new Derivate(der.variable, der.variable.subscripts, exp)); + Assignment eq = new Assignment(model, der.variable, der.variable.subscripts, new Derivate(der.variable, der.variable.subscripts, exp)); model.derivatives.add(eq); return eq; } else { Variable var = (Variable)v; - Assignment eq = new Assignment(var, var.subscripts, exp); + Assignment eq = new Assignment(model, var, var.subscripts, exp); if(model.initial) model.initials.add(eq); else @@ -338,18 +338,18 @@ public class Parser { return subs; case declaration: if(n.jjtGetNumChildren() == 0) { - return new Declaration(new Variable(frame, n.op, null), null); + return new Declaration(new Variable(frame, n.op, null, n.line), null); } else if(n.jjtGetNumChildren() == 1) { Object o = walk((SimpleNode)n.jjtGetChild(0), indent+2, frame); if(o instanceof IExpression[]) { - return new Declaration(new Variable(frame, n.op, (IExpression[])o), null); + return new Declaration(new Variable(frame, n.op, (IExpression[])o, n.line), null); } else { - return new Declaration(new Variable(frame, n.op, null), o); + return new Declaration(new Variable(frame, n.op, null, n.line), o); } } else if(n.jjtGetNumChildren() == 2) { IExpression[] subscripts2 = (IExpression[])walk((SimpleNode)n.jjtGetChild(0), indent+2, frame); Object modification = walk((SimpleNode)n.jjtGetChild(1), indent+2, frame); - return new Declaration(new Variable(frame, n.op,subscripts2), modification); + return new Declaration(new Variable(frame, n.op,subscripts2, n.line), modification); } else { StringBuilder b = new StringBuilder(); b.append("declaration: "); @@ -365,8 +365,9 @@ public class Parser { SimpleNode child = (SimpleNode)n.jjtGetChild(0); String packagePath = getPackagePath(child); String functionName = packagePath + child.op; + System.err.println("function " + functionName + " at line " + child.line); ArrayList stms2 = new ArrayList(); - Function function = new Function(functionName, new StatementList(stms2)); + Function function = new Function(functionName, new StatementList(stms2), child.line); ArrayList composition = (ArrayList)walk(child, indent+2, function); for(int i=1;i { jjtn000.op = t.image; } string_comment() composition() "end" + LOOKAHEAD(2) t= { jjtn000.op = t.image; jjtn000.line = t.beginLine; } string_comment() composition() "end" | LOOKAHEAD(2) "=" base_prefix() name() ( array_subscripts() )? ( class_modification() )? comment() | LOOKAHEAD(3) "=" "der" "(" name() "," ( "," )* ")" comment() | LOOKAHEAD(3) "=" "enumeration" "(" ( ( enum_list() )? | ":" ) ")" comment()/*@bgen(jjtree)*/ @@ -924,7 +924,7 @@ Parameter declaration() : {/*@bgen(jjtree) declaration */ try { /*@egen*/ // IDENT [ array_subscripts ] [ modification ] - { ret.name = new String(token.image); jjtn000.op = token.image; } + { ret.name = new String(token.image); jjtn000.op = token.image; jjtn000.line = token.beginLine; } ( array_subscripts() )? ( ret.optional = modification() )?/*@bgen(jjtree)*/ { @@ -2643,7 +2643,7 @@ void component_reference() : {/*@bgen(jjtree) component_reference */ try { /*@egen*/ // [ "." ] IDENT [ array_subscripts ] { "." IDENT [ array_subscripts ] } - ( "." )? t= { jjtn000.op = t.image; } ( array_subscripts() )? ( "." ( array_subscripts() )? )*/*@bgen(jjtree)*/ + ( "." )? t= { jjtn000.op = t.image; jjtn000.line = t.beginLine; } ( array_subscripts() )? ( "." ( array_subscripts() )? )*/*@bgen(jjtree)*/ } catch (Throwable jjte000) { if (jjtc000) { jjtree.clearNodeScope(jjtn000); diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/ModelicaParser.jjt b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/ModelicaParser.jjt index b4231401..87103388 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/ModelicaParser.jjt +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/ModelicaParser.jjt @@ -110,7 +110,7 @@ Node stored_definition() #STORED : { /*** Class Definition **********************************************/ -void class_definition() : { +void class_definition() : { Token t; } { // class_definition : // [ encapsulated ] @@ -120,7 +120,7 @@ void class_definition() : { // class_specifier ( "encapsulated" )? ( "partial" )? - ( "class" { jjtThis.op = "class"; } | "model" { jjtThis.op = "model"; } | "record" | "block" | ( "expandable" )? "connector" | "type" | + ( "class" { jjtThis.op = "class"; } | t = "model" { jjtThis.op = "model"; jjtThis.line = t.beginLine; } | "record" | "block" | ( "expandable" )? "connector" | "type" | "package" { jjtThis.op = "package"; } | "function" { jjtThis.op = "function"; } | "operator" | "operator function" | "operator record" ) class_specifier() } @@ -136,7 +136,7 @@ void class_specifier() : { Token t; // | IDENT "=" der "(" name "," IDENT { "," IDENT } ")" comment // | extends IDENT [ class_modification ] string_comment composition // end IDENT - LOOKAHEAD(2) t= { jjtThis.op = t.image; } string_comment() composition() "end" + LOOKAHEAD(2) t= { jjtThis.op = t.image; jjtThis.line = t.beginLine; } string_comment() composition() "end" | LOOKAHEAD(2) "=" base_prefix() name() ( array_subscripts() )? ( class_modification() )? comment() | LOOKAHEAD(3) "=" "der" "(" name() "," ( "," )* ")" comment() | LOOKAHEAD(3) "=" "enumeration" "(" ( ( enum_list() )? | ":" ) ")" comment() @@ -320,7 +320,7 @@ Parameter declaration() : { Token t; Parameter ret = new Parameter(); } { // IDENT [ array_subscripts ] [ modification ] - { ret.name = new String(token.image); jjtThis.op = token.image; } + { ret.name = new String(token.image); jjtThis.op = token.image; jjtThis.line = token.beginLine; } ( array_subscripts() )? ( ret.optional = modification() )? { return ret; } @@ -710,7 +710,7 @@ void component_reference() : { Token t; } { // [ "." ] IDENT [ array_subscripts ] { "." IDENT [ array_subscripts ] } - ( "." )? t= { jjtThis.op = t.image; } ( array_subscripts() )? ( "." ( array_subscripts() )? )* + ( "." )? t= { jjtThis.op = t.image; jjtThis.line = t.beginLine; } ( array_subscripts() )? ( "." ( array_subscripts() )? )* } void function_call_args() : { diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/SimpleNode.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/SimpleNode.java index b399a88f..4a12bf77 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/SimpleNode.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/parser/SimpleNode.java @@ -11,6 +11,7 @@ class SimpleNode implements Node { protected Object value; protected ModelParser parser; + public int line = -1; public String op; public SimpleNode(int i) { -- 2.47.1