3131__status__ = "Prototype"
3232__maintainer__ = "https://openmodelica.org"
3333
34+ from typing import Any
35+
3436from pyparsing import (
3537 Combine ,
3638 Dict ,
5254)
5355
5456
55- def convertNumbers (s , l , 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
107109omcRecord = Forward ()
108110omcValue = 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
115117SOME = (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 )
118120omcNumber = 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 ))
124127fqident = Forward ()
125128fqident << ((ident + "." + fqident ) | ident )
126129omcValues = 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 ))
131146recordMember = 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
134154omcGrammar = 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 ]
0 commit comments