Skip to content

Commit 4a9580d

Browse files
committed
Merge branch 'only-exists' of gitlab.ewi.tudelft.nl:cse2000-software-project/2023-2024/cluster-e/04b/rune-dsl-interpreter into only-exists
2 parents 8de5d5e + 137f048 commit 4a9580d

25 files changed

+541
-138
lines changed

rosetta-interpreter/src/main/java/com/regnosys/rosetta/interpreternew/RosettaInterpreterVisitor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
import com.regnosys.rosetta.rosetta.expression.RosettaStringLiteral;
3232
import com.regnosys.rosetta.rosetta.expression.RosettaSymbolReference;
3333
import com.regnosys.rosetta.rosetta.expression.SumOperation;
34-
34+
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEmptyError;
3535
import org.eclipse.emf.ecore.impl.MinimalEObjectImpl;
36-
3736
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
3837
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterLogicalOperationInterpreter;
3938
import com.regnosys.rosetta.interpreternew.visitors.RosettaInterpreterOnlyExistsInterpreter;
@@ -89,7 +88,7 @@ public RosettaInterpreterValue interp(RosettaIntLiteral exp,
8988
public RosettaInterpreterValue interp(RosettaPatternLiteral exp,
9089
RosettaInterpreterBaseEnvironment env) {
9190
return new RosettaInterpreterErrorValue(
92-
new RosettaInterpreterError("Pattern literals are not supported"));
91+
new RosettaInterpreterEmptyError("Pattern literals are not supported"));
9392
}
9493

9594
@Override
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.regnosys.rosetta.interpreternew.values;
2+
3+
import java.util.Objects;
4+
5+
import org.eclipse.emf.ecore.impl.MinimalEObjectImpl;
6+
7+
import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterBaseError;
8+
9+
public class RosettaInterpreterEmptyError extends MinimalEObjectImpl implements RosettaInterpreterBaseError {
10+
11+
private String errorMessage;
12+
13+
public RosettaInterpreterEmptyError(String message) {
14+
this.errorMessage = message;
15+
}
16+
17+
public RosettaInterpreterEmptyError() {
18+
this.errorMessage = "";
19+
}
20+
21+
@Override
22+
public String getMessage() {
23+
return errorMessage;
24+
}
25+
26+
@Override
27+
public void setMessage(String value) {
28+
this.errorMessage = value;
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return Objects.hash(errorMessage);
34+
}
35+
36+
@Override
37+
public boolean equals(Object obj) {
38+
if (this == obj) {
39+
return true;
40+
}
41+
if (obj == null) {
42+
return false;
43+
}
44+
if (getClass() != obj.getClass()) {
45+
return false;
46+
}
47+
RosettaInterpreterEmptyError other = (RosettaInterpreterEmptyError) obj;
48+
return Objects.equals(errorMessage, other.errorMessage);
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return errorMessage;
54+
}
55+
}

rosetta-interpreter/src/main/java/com/regnosys/rosetta/interpreternew/values/RosettaInterpreterEnvironment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public RosettaInterpreterValue findValue(String name) {
4141
}
4242
else {
4343
return new RosettaInterpreterErrorValue(
44-
new RosettaInterpreterError(
44+
new RosettaInterpreterEmptyError(
4545
name
4646
+ " does not exist in the environment"));
4747
}

rosetta-interpreter/src/main/java/com/regnosys/rosetta/interpreternew/values/RosettaInterpreterError.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22

33
import java.util.Objects;
44

5+
import org.eclipse.emf.ecore.EObject;
6+
import org.eclipse.xtext.nodemodel.INode;
7+
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
58
import org.eclipse.emf.ecore.impl.MinimalEObjectImpl;
69

710
import com.regnosys.rosetta.rosetta.interpreter.RosettaInterpreterBaseError;
811

9-
public class RosettaInterpreterError extends MinimalEObjectImpl implements RosettaInterpreterBaseError {
10-
private String errorMessage;
11-
12+
public class RosettaInterpreterError extends MinimalEObjectImpl implements RosettaInterpreterBaseError {
13+
private String errorMessage;
14+
private EObject associatedObject;
15+
1216
public RosettaInterpreterError(String errorMessage) {
1317
super();
1418
this.errorMessage = errorMessage;
1519
}
1620

17-
public String getError() { return errorMessage; }
18-
19-
@Override
20-
public String toString() {
21-
return "RosettaInterpreterError [errorMessage=" + errorMessage + "]";
22-
}
23-
2421
@Override
2522
public int hashCode() {
2623
return Objects.hash(errorMessage);
@@ -40,6 +37,40 @@ public boolean equals(Object obj) {
4037
RosettaInterpreterError other = (RosettaInterpreterError) obj;
4138
return Objects.equals(errorMessage, other.errorMessage);
4239
}
40+
41+
public RosettaInterpreterError(String errorMessage, EObject obj) {
42+
this.associatedObject = obj;
43+
this.errorMessage = errorMessage;
44+
}
45+
46+
public String getError() { return errorMessage; }
47+
48+
public EObject getEobject() { return associatedObject; }
49+
50+
/**
51+
* Gives a parsed error message associated with this error.
52+
* Gets the INode associated in order to provide details of where the erorr ocurred.
53+
*
54+
* @return Error message with code information
55+
*/
56+
public String properErrorMessage() {
57+
if (associatedObject == null) {
58+
return errorMessage;
59+
}
60+
61+
INode node = NodeModelUtils.findActualNodeFor(associatedObject);
62+
int startLine = node.getStartLine();
63+
int offset = node.getOffset();
64+
String text = node.getText().trim();
65+
String message = "Error at line " + startLine + ", position " + offset + ": "
66+
+ "\"" + text + "\". " + errorMessage;
67+
return message;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return properErrorMessage();
73+
}
4374

4475
@Override
4576
public String getMessage() {

rosetta-interpreter/src/main/java/com/regnosys/rosetta/interpreternew/values/RosettaInterpreterErrorValue.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ public boolean equals(Object obj) {
145145
return false;
146146
}
147147
RosettaInterpreterErrorValue other = (RosettaInterpreterErrorValue) obj;
148-
return Objects.equals(errors, other.errors);
148+
return ((List<RosettaInterpreterBaseError>)errors)
149+
.equals(((List<RosettaInterpreterBaseError>)other.errors));
149150
}
150151

151152
@Override

rosetta-interpreter/src/main/java/com/regnosys/rosetta/interpreternew/visitors/RosettaInterpreterComparisonOperationInterpreter.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Arrays;
55
import java.util.List;
66

7+
import org.eclipse.emf.ecore.EObject;
8+
79
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterBaseValue;
810
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterBooleanValue;
911
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
@@ -46,7 +48,7 @@ public RosettaInterpreterBaseValue interp(ModifiableBinaryOperation expr,
4648
if (!comparisonOperators.contains(expr.getOperator())) {
4749
return new RosettaInterpreterErrorValue(
4850
new RosettaInterpreterError(
49-
"operator not suppported"));
51+
"operator not suppported", expr));
5052
}
5153
RosettaExpression left = expr.getLeft();
5254
RosettaExpression right = expr.getRight();
@@ -70,30 +72,28 @@ else if (RosettaInterpreterErrorValue.errorsExist(rightValue)) {
7072
//check cardinality operation
7173
switch (expr.getCardMod()) {
7274
case ANY:
73-
return compareAny(leftValue, rightValue, expr.getOperator());
75+
return compareAny(leftValue, rightValue, expr.getOperator(), expr);
7476

7577
case ALL:
76-
return compareAll(leftValue, rightValue, expr.getOperator());
77-
78+
return compareAll(leftValue, rightValue, expr.getOperator(), expr);
7879
default: //case NONE
7980
//normally compare left and right side.
8081
boolean result = checkComparableTypes(leftValue,
8182
rightValue,
8283
expr.getOperator());
83-
return new RosettaInterpreterBooleanValue(result);
84-
84+
return new RosettaInterpreterBooleanValue(result);
8585
}
8686
}
8787

8888
private RosettaInterpreterBaseValue compareAny(RosettaInterpreterValue leftValue,
8989
RosettaInterpreterValue rightValue,
90-
String operator) {
90+
String operator, EObject associatedObject) {
9191
//list vs list case:
9292
if (leftValue instanceof RosettaInterpreterListValue
9393
&& rightValue instanceof RosettaInterpreterListValue) {
9494
return new RosettaInterpreterErrorValue(
9595
new RosettaInterpreterError(
96-
"cannot compare two lists"));
96+
"cannot compare two lists", associatedObject));
9797
}
9898

9999
//list vs element case:
@@ -121,18 +121,18 @@ else if (leftValue instanceof RosettaInterpreterListValue) {
121121
return new RosettaInterpreterErrorValue(
122122
new RosettaInterpreterError(
123123
"cannot use \"ANY\" keyword "
124-
+ "to compare two elements"));
124+
+ "to compare two elements", associatedObject));
125125
}
126126

127127
private RosettaInterpreterBaseValue compareAll(RosettaInterpreterValue leftValue,
128128
RosettaInterpreterValue rightValue,
129-
String operator) {
129+
String operator, EObject associatedObject) {
130130
//list vs list case:
131131
if (leftValue instanceof RosettaInterpreterListValue
132132
&& rightValue instanceof RosettaInterpreterListValue) {
133133
return new RosettaInterpreterErrorValue(
134134
new RosettaInterpreterError(
135-
"cannot compare two lists"));
135+
"cannot compare two lists", associatedObject));
136136
}
137137

138138
//list vs element case:
@@ -160,7 +160,7 @@ else if (leftValue instanceof RosettaInterpreterListValue) {
160160
return new RosettaInterpreterErrorValue(
161161
new RosettaInterpreterError(
162162
"cannot use \"ALL\" keyword "
163-
+ "to compare two elements"));
163+
+ "to compare two elements", associatedObject));
164164
}
165165

166166
private boolean checkComparableTypes(RosettaInterpreterValue leftValue,

rosetta-interpreter/src/main/java/com/regnosys/rosetta/interpreternew/visitors/RosettaInterpreterListOperationsInterpreter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ public RosettaInterpreterValue interp(JoinOperation exp,
115115
if (!allStrings) {
116116
return new RosettaInterpreterErrorValue(
117117
new RosettaInterpreterError("The list of values for a join "
118-
+ "operation must be a list of strings"));
118+
+ "operation must be a list of strings", exp));
119119
}
120120
if (!(delimVal instanceof RosettaInterpreterStringValue)) {
121121
return new RosettaInterpreterErrorValue(
122122
new RosettaInterpreterError("The delimiter for a join"
123-
+ " operation must be a string"));
123+
+ " operation must be a string", exp));
124124
}
125125

126126
if (RosettaInterpreterBaseValue.valueStream(stringsVal)

rosetta-interpreter/src/main/java/com/regnosys/rosetta/interpreternew/visitors/RosettaInterpreterListOperatorInterpreter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public RosettaInterpreterValue interp(FirstOperation exp, RosettaInterpreterEnvi
123123
if (count == 0L) {
124124
// List is empty
125125
return new RosettaInterpreterErrorValue(
126-
new RosettaInterpreterError("List is empty"));
126+
new RosettaInterpreterError("List is empty", exp));
127127
} else {
128128
// List has at least one element
129129
return RosettaInterpreterBaseValue.valueStream(interpretedArgument)
@@ -151,14 +151,14 @@ public RosettaInterpreterValue interp(RosettaOnlyElement exp, RosettaInterpreter
151151
if (count == 0L) {
152152
// List is empty
153153
return new RosettaInterpreterErrorValue(
154-
new RosettaInterpreterError("List is empty"));
154+
new RosettaInterpreterError("List is empty", exp));
155155
} else if (count == 1L) {
156156
// List has one element
157157
return RosettaInterpreterBaseValue.valueStream(interpretedArgument)
158158
.collect(Collectors.toList()).get(0);
159159
} else {
160160
return new RosettaInterpreterErrorValue(
161-
new RosettaInterpreterError("List contains more than one element"));
161+
new RosettaInterpreterError("List contains more than one element", exp));
162162
}
163163
}
164164

@@ -183,7 +183,7 @@ public RosettaInterpreterValue interp(LastOperation exp, RosettaInterpreterEnvir
183183
if (count == 0L) {
184184
// List is empty
185185
return new RosettaInterpreterErrorValue(
186-
new RosettaInterpreterError("List is empty"));
186+
new RosettaInterpreterError("List is empty", exp));
187187
} else {
188188
// List has at least one element
189189
return RosettaInterpreterBaseValue.valueStream(interpretedArgument)
@@ -259,7 +259,7 @@ public RosettaInterpreterValue interp(SumOperation exp, RosettaInterpreterEnviro
259259
if (RosettaInterpreterBaseValue.toValueList(val).size() < 1) {
260260
return new RosettaInterpreterErrorValue(
261261
new RosettaInterpreterError("Cannot take sum"
262-
+ " of empty list"));
262+
+ " of empty list", exp));
263263
}
264264

265265
List<RosettaInterpreterValue> values =
@@ -272,7 +272,7 @@ public RosettaInterpreterValue interp(SumOperation exp, RosettaInterpreterEnviro
272272
if (!(v instanceof RosettaInterpreterNumberValue)) {
273273
return new RosettaInterpreterErrorValue(
274274
new RosettaInterpreterError("Cannot take sum"
275-
+ "of non-number value"));
275+
+ "of non-number value", exp));
276276
}
277277
}
278278

rosetta-interpreter/src/main/java/com/regnosys/rosetta/interpreternew/visitors/RosettaInterpreterLogicalOperationInterpreter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.List;
44

5+
import org.eclipse.emf.ecore.EObject;
6+
57
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterBaseValue;
68
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterBooleanValue;
79
import com.regnosys.rosetta.interpreternew.values.RosettaInterpreterEnvironment;
@@ -41,9 +43,9 @@ public RosettaInterpreterBaseValue interp(LogicalOperation expr,
4143
} else {
4244
// Check for errors in the left or right side of the binary operation
4345
RosettaInterpreterErrorValue leftErrors =
44-
checkForErrors(leftInterpreted, "Leftside");
46+
checkForErrors(leftInterpreted, "Leftside", expr);
4547
RosettaInterpreterErrorValue rightErrors =
46-
checkForErrors(rightInterpreted, "Rightside");
48+
checkForErrors(rightInterpreted, "Rightside", expr);
4749

4850
return RosettaInterpreterErrorValue.merge(List.of(leftErrors, rightErrors));
4951
}
@@ -57,7 +59,7 @@ public RosettaInterpreterBaseValue interp(LogicalOperation expr,
5759
return new RosettaInterpreterErrorValue(
5860
new RosettaInterpreterError(
5961
"Logical Operation: Wrong operator "
60-
+ "- only 'and' / 'or' supported"));
62+
+ "- only 'and' / 'or' supported", null));
6163
}
6264
}
6365

@@ -73,7 +75,8 @@ public RosettaInterpreterBaseValue interp(LogicalOperation expr,
7375
* if the interpretedValue does not cause an error
7476
*/
7577
private RosettaInterpreterErrorValue checkForErrors(
76-
RosettaInterpreterValue interpretedValue, String side) {
78+
RosettaInterpreterValue interpretedValue, String side,
79+
EObject associatedObject) {
7780
if (interpretedValue instanceof RosettaInterpreterBooleanValue) {
7881
// No errors found.
7982
// I return an error value without any errors in its list,
@@ -88,7 +91,7 @@ private RosettaInterpreterErrorValue checkForErrors(
8891
return new RosettaInterpreterErrorValue(
8992
new RosettaInterpreterError(
9093
"Logical Operation: " + side
91-
+ " is not of type Boolean"));
94+
+ " is not of type Boolean", associatedObject));
9295
}
9396
}
9497
}

0 commit comments

Comments
 (0)