Skip to content

Commit 6e0b61a

Browse files
authored
allow ident's in array dimension (#234)
1 parent 317b944 commit 6e0b61a

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

OMPython/OMTypedParser.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,12 @@ def evaluateExpression(s, loc, toks):
9797
try:
9898
# Evaluate the expression safely
9999
return eval(expr)
100-
except Exception as e:
101-
print(f"Error evaluating expression: {expr}")
102-
return None
100+
except NameError:
101+
return expr
103102

104103
# Number parsing (supports arithmetic expressions in dimensions) (e.g., {1 + 1, 1})
105104
arrayDimension = infixNotation(
106-
Word(nums),
105+
Word(alphas + "_", alphanums + "_") | Word(nums),
107106
[
108107
(Word("+-", exact=1), 1, opAssoc.RIGHT),
109108
(Word("*/", exact=1), 2, opAssoc.LEFT),

tests/test_ArrayDimension.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ def test_ArrayDimension(self):
2626
result = omc.sendExpression("getComponents(A)")
2727
assert result[0][-1] == (6,7), f"array dimension does not match the expected value. Got: {result[0][-1]}, Expected: {(6, 7)}"
2828

29+
omc.sendExpression("loadString(\"model A Integer y = 5; Integer x[y+1,1+9]; end A;\")")
30+
omc.sendExpression("getErrorString()")
31+
32+
result = omc.sendExpression("getComponents(A)")
33+
assert result[-1][-1] == ('y + 1', 10), f"array dimension does not match the expected value. Got: {result[-1][-1]}, Expected: {('y + 1', 10)}"
34+
2935
omc.__del__()
3036
shutil.rmtree(tempdir, ignore_errors= True)
3137

0 commit comments

Comments
 (0)