Skip to content

Commit 42c250e

Browse files
authored
Don't strip parentheses in getModelInstance (#13760)
- Make stripping parentheses (tuples) in `AbsynUtil.stripCommentExpressions` optional, and disable it for `getModelInstance`. Fixes #13755
1 parent a15e503 commit 42c250e

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

OMCompiler/Compiler/FrontEnd/AbsynUtil.mo

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,17 +2533,22 @@ algorithm
25332533
end getString;
25342534

25352535
public function stripCommentExpressions
2536+
"Strips comment expressions from the given expression. If onlyComments is
2537+
false it also strips parentheses (represented by tuples)."
25362538
input output Absyn.Exp exp;
2539+
input Boolean onlyComments = false;
25372540
algorithm
2538-
(exp,_) := traverseExp(exp, stripCommentExpressionsHelper, true);
2541+
(exp,_) := traverseExp(exp, stripCommentExpressionsHelper, onlyComments);
25392542
end stripCommentExpressions;
25402543

2541-
protected function stripCommentExpressionsHelper<T>
2544+
protected function stripCommentExpressionsHelper
25422545
input output Absyn.Exp exp;
2543-
input output T extra;
2546+
input output Boolean onlyComments;
2547+
protected
2548+
Absyn.Exp e;
25442549
algorithm
25452550
exp := match exp
2546-
case Absyn.TUPLE({exp}) then exp;
2551+
case Absyn.TUPLE({e}) guard not onlyComments then e;
25472552
case Absyn.EXPRESSIONCOMMENT() then exp.exp;
25482553
else exp;
25492554
end match;

OMCompiler/Compiler/Script/NFApi.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ algorithm
18441844
then
18451845
json;
18461846

1847-
else JSON.makeString(Dump.printExpStr(AbsynUtil.stripCommentExpressions(exp)));
1847+
else JSON.makeString(Dump.printExpStr(AbsynUtil.stripCommentExpressions(exp, true)));
18481848
end match;
18491849
end dumpJSONAbsynExpression;
18501850

@@ -2133,7 +2133,7 @@ algorithm
21332133
end if;
21342134

21352135
if isSome(mod.binding) then
2136-
binding_json := JSON.makeString(Dump.printExpStr(AbsynUtil.stripCommentExpressions(Util.getOption(mod.binding))));
2136+
binding_json := JSON.makeString(Dump.printExpStr(AbsynUtil.stripCommentExpressions(Util.getOption(mod.binding), true)));
21372137

21382138
if JSON.isNull(json) then
21392139
json := binding_json;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// name: GetModelInstanceBinding11
2+
// keywords:
3+
// status: correct
4+
// cflags: -d=newInst
5+
//
6+
//
7+
8+
loadString("
9+
model M
10+
Real x = (1 - 2) / (3);
11+
end M;
12+
");
13+
14+
getModelInstance(M, prettyPrint = true);
15+
getErrorString();
16+
17+
// Result:
18+
// true
19+
// "{
20+
// \"name\": \"M\",
21+
// \"restriction\": \"model\",
22+
// \"elements\": [
23+
// {
24+
// \"$kind\": \"component\",
25+
// \"name\": \"x\",
26+
// \"type\": \"Real\",
27+
// \"modifiers\": \"(1 - 2)/(3)\",
28+
// \"value\": {
29+
// \"binding\": {
30+
// \"$kind\": \"binary_op\",
31+
// \"lhs\": {
32+
// \"$kind\": \"binary_op\",
33+
// \"lhs\": 1,
34+
// \"op\": \"-\",
35+
// \"rhs\": 2
36+
// },
37+
// \"op\": \"/\",
38+
// \"rhs\": 3
39+
// }
40+
// }
41+
// }
42+
// ],
43+
// \"source\": {
44+
// \"filename\": \"<interactive>\",
45+
// \"lineStart\": 2,
46+
// \"columnStart\": 3,
47+
// \"lineEnd\": 4,
48+
// \"columnEnd\": 8
49+
// }
50+
// }"
51+
// ""
52+
// endResult

testsuite/openmodelica/instance-API/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ GetModelInstanceBinding7.mos \
2828
GetModelInstanceBinding8.mos \
2929
GetModelInstanceBinding9.mos \
3030
GetModelInstanceBinding10.mos \
31+
GetModelInstanceBinding11.mos \
3132
GetModelInstanceBreak1.mos \
3233
GetModelInstanceChoices1.mos \
3334
GetModelInstanceChoices2.mos \

0 commit comments

Comments
 (0)