Skip to content

Commit 0da1bf5

Browse files
committed
cleanup OMTypedParser
* function names * PEP8 renames in pyparsing (setParseAction() => set_parse_action()) * long lines
1 parent bb0d3f2 commit 0da1bf5

3 files changed

Lines changed: 45 additions & 25 deletions

File tree

OMPython/OMCSession.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import zmq
5555

5656
# TODO: replace this with the new parser
57-
from OMPython.OMTypedParser import parseString as om_parser_typed
57+
from OMPython.OMTypedParser import om_parser_typed
5858
from OMPython.OMParser import om_parser_basic
5959

6060
# define logger using the current module name as ID

OMPython/OMTypedParser.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
__status__ = "Prototype"
3232
__maintainer__ = "https://openmodelica.org"
3333

34+
from typing import Any
35+
3436
from pyparsing import (
3537
Combine,
3638
Dict,
@@ -52,45 +54,45 @@
5254
)
5355

5456

55-
def convertNumbers(s, loc, toks):
57+
def convert_numbers(s, loc, toks):
5658
n = toks[0]
5759
try:
5860
return int(n)
5961
except ValueError:
6062
return float(n)
6163

6264

63-
def convertString2(s, s2):
65+
def convert_string2(s, s2):
6466
tmp = s2[0].replace("\\\"", "\"")
6567
tmp = tmp.replace("\"", "\\\"")
6668
tmp = tmp.replace("\'", "\\'")
6769
tmp = tmp.replace("\f", "\\f")
6870
tmp = tmp.replace("\n", "\\n")
6971
tmp = tmp.replace("\r", "\\r")
7072
tmp = tmp.replace("\t", "\\t")
71-
return "'"+tmp+"'"
73+
return "'" + tmp + "'"
7274

7375

74-
def convertString(s, s2):
76+
def convert_string(s, s2):
7577
return s2[0].replace("\\\"", '"')
7678

7779

78-
def convertDict(d):
80+
def convert_dict(d):
7981
return dict(d[0])
8082

8183

82-
def convertTuple(t):
84+
def convert_tuple(t):
8385
return tuple(t[0])
8486

8587

86-
def evaluateExpression(s, loc, toks):
88+
def evaluate_expression(s, loc, toks):
8789
# Convert the tokens (ParseResults) into a string expression
8890
flat_list = [item for sublist in toks[0] for item in sublist]
8991
expr = "".join(flat_list)
9092
try:
9193
# Evaluate the expression safely
9294
return eval(expr)
93-
except Exception:
95+
except (SyntaxError, NameError):
9496
return expr
9597

9698

@@ -102,42 +104,60 @@ def evaluateExpression(s, loc, toks):
102104
(Word("*/", exact=1), 2, opAssoc.LEFT),
103105
(Word("+-", exact=1), 2, opAssoc.LEFT),
104106
],
105-
).setParseAction(evaluateExpression)
107+
).set_parse_action(evaluate_expression)
106108

107109
omcRecord = Forward()
108110
omcValue = Forward()
109111

110112
# pyparsing's replace_with (and thus replaceWith) has incorrect type
111113
# annotation: https://github.com/pyparsing/pyparsing/issues/602
112-
TRUE = Keyword("true").setParseAction(replaceWith(True)) # type: ignore
113-
FALSE = Keyword("false").setParseAction(replaceWith(False)) # type: ignore
114-
NONE = (Keyword("NONE") + Suppress("(") + Suppress(")")).setParseAction(replaceWith(None)) # type: ignore
114+
TRUE = Keyword("true").set_parse_action(replaceWith(True)) # type: ignore
115+
FALSE = Keyword("false").set_parse_action(replaceWith(False)) # type: ignore
116+
NONE = (Keyword("NONE") + Suppress("(") + Suppress(")")).set_parse_action(replaceWith(None)) # type: ignore
115117
SOME = (Suppress(Keyword("SOME")) + Suppress("(") + omcValue + Suppress(")"))
116118

117-
omcString = QuotedString(quoteChar='"', escChar='\\', multiline=True).setParseAction(convertString)
119+
omcString = QuotedString(quoteChar='"', escChar='\\', multiline=True).set_parse_action(convert_string)
118120
omcNumber = Combine(Optional('-') + ('0' | Word('123456789', nums)) +
119121
Optional('.' + Word(nums)) +
120122
Optional(Word('eE', exact=1) + Word(nums + '+-', nums)))
121123

122124
# ident = Word(alphas + "_", alphanums + "_") | Combine("'" + Word(alphanums + "!#$%&()*+,-./:;<>=?@[]^{}|~ ") + "'")
123-
ident = Word(alphas + "_", alphanums + "_") | QuotedString(quoteChar='\'', escChar='\\').setParseAction(convertString2)
125+
ident = (Word(alphas + "_", alphanums + "_")
126+
| QuotedString(quoteChar='\'', escChar='\\').set_parse_action(convert_string2))
124127
fqident = Forward()
125128
fqident << ((ident + "." + fqident) | ident)
126129
omcValues = delimitedList(omcValue)
127-
omcTuple = Group(Suppress('(') + Optional(omcValues) + Suppress(')')).setParseAction(convertTuple)
128-
omcArray = Group(Suppress('{') + Optional(omcValues) + Suppress('}')).setParseAction(convertTuple)
129-
omcArraySpecialTypes = Group(Suppress('{') + delimitedList(arrayDimension) + Suppress('}')).setParseAction(convertTuple)
130-
omcValue << (omcString | omcNumber | omcRecord | omcArray | omcArraySpecialTypes | omcTuple | SOME | TRUE | FALSE | NONE | Combine(fqident))
130+
omcTuple = Group(Suppress('(') + Optional(omcValues) + Suppress(')')).set_parse_action(convert_tuple)
131+
omcArray = Group(Suppress('{') + Optional(omcValues) + Suppress('}')).set_parse_action(convert_tuple)
132+
omcArraySpecialTypes = Group(Suppress('{')
133+
+ delimitedList(arrayDimension)
134+
+ Suppress('}')).set_parse_action(convert_tuple)
135+
omcValue << (omcString
136+
| omcNumber
137+
| omcRecord
138+
| omcArray
139+
| omcArraySpecialTypes
140+
| omcTuple
141+
| SOME
142+
| TRUE
143+
| FALSE
144+
| NONE
145+
| Combine(fqident))
131146
recordMember = delimitedList(Group(ident + Suppress('=') + omcValue))
132-
omcRecord << Group(Suppress('record') + Suppress(fqident) + Dict(recordMember) + Suppress('end') + Suppress(fqident) + Suppress(';')).setParseAction(convertDict)
147+
omcRecord << Group(Suppress('record')
148+
+ Suppress(fqident)
149+
+ Dict(recordMember)
150+
+ Suppress('end')
151+
+ Suppress(fqident)
152+
+ Suppress(';')).set_parse_action(convert_dict)
133153

134154
omcGrammar = Optional(omcValue) + StringEnd()
135155

136-
omcNumber.setParseAction(convertNumbers)
156+
omcNumber.set_parse_action(convert_numbers)
137157

138158

139-
def parseString(string):
140-
res = omcGrammar.parseString(string)
159+
def om_parser_typed(string) -> Any:
160+
res = omcGrammar.parse_string(string)
141161
if len(res) == 0:
142-
return
162+
return None
143163
return res[0]

tests/test_typedParser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from OMPython import OMTypedParser
22

3-
typeCheck = OMTypedParser.parseString
3+
typeCheck = OMTypedParser.om_parser_typed
44

55

66
def test_newline_behaviour():

0 commit comments

Comments
 (0)