1212import enum
1313import os
1414
15- from typing import Optional
15+ from typing import Optional , Union
1616
1717from .decorator import classproperty
1818
2626 os .path .abspath (os .path .dirname (os .path .abspath (__file__ ))), os .path .pardir )
2727)
2828
29+
2930# ----------------------------------------------------------------------
3031# Class enums
3132# ----------------------------------------------------------------------
3233
33-
34- class CLASS :
34+ @ enum . unique
35+ class CLASS ( str , enum . Enum ) :
3536 """ Enums class constants
3637 """
3738 unknown = 'unknown' # 0
@@ -41,35 +42,24 @@ class CLASS:
4142 label = 'label' # 4 Labels
4243 const = 'const' # 5 # constant literal value e.g. 5 or "AB"
4344 sub = 'sub' # 6 # subroutine
44- type_ = 'type' # 7 # type
45-
46- _CLASS_NAMES = {
47- unknown : '(unknown)' ,
48- var : 'var' ,
49- array : 'array' ,
50- function : 'function' ,
51- label : 'label' ,
52- const : 'const' ,
53- sub : 'sub' ,
54- type_ : 'type'
55- }
45+ type = 'type' # 7 # type
5646
5747 @classproperty
5848 def classes (cls ):
5949 return (cls .unknown , cls .var , cls .array , cls .function , cls .sub ,
6050 cls .const , cls .label )
6151
6252 @classmethod
63- def is_valid (cls , class_ ):
53+ def is_valid (cls , class_ : Union [ str , 'CLASS' ] ):
6454 """ Whether the given class is
6555 valid or not.
6656 """
67- return class_ in cls . classes
57+ return class_ in set ( CLASS )
6858
6959 @classmethod
70- def to_string (cls , class_ ):
60+ def to_string (cls , class_ : 'CLASS' ):
7161 assert cls .is_valid (class_ )
72- return cls . _CLASS_NAMES [ class_ ]
62+ return class_ . value
7363
7464
7565class ARRAY :
@@ -179,83 +169,59 @@ def to_type(typename: str) -> Optional['TYPE']:
179169 return None
180170
181171
182- class SCOPE :
172+ @enum .unique
173+ class SCOPE (str , enum .Enum ):
183174 """ Enum scopes
184175 """
185- unknown = None
186176 global_ = 'global'
187177 local = 'local'
188178 parameter = 'parameter'
189179
190- _names = {
191- unknown : 'unknown' ,
192- global_ : 'global' ,
193- local : 'local' ,
194- parameter : 'parameter'
195- }
196-
197- @classmethod
198- def is_valid (cls , scope ):
199- return cls ._names .get (scope , None ) is not None
180+ @staticmethod
181+ def is_valid (scope : Union [str , 'SCOPE' ]):
182+ return scope in set (SCOPE )
200183
201- @classmethod
202- def to_string (cls , scope ):
203- assert cls .is_valid (scope )
204- return cls . _names [ scope ]
184+ @staticmethod
185+ def to_string (scope : 'SCOPE' ):
186+ assert SCOPE .is_valid (scope )
187+ return scope . value
205188
206189
207- class KIND :
190+ @enum .unique
191+ class KIND (str , enum .Enum ):
208192 """ Enum kind
209193 """
210- unknown = None
194+ unknown = 'unknown'
211195 var = 'var'
212196 function = 'function'
213197 sub = 'sub'
214- type_ = 'type'
215-
216- _NAMES = {
217- unknown : '(unknown)' ,
218- var : 'variable' ,
219- function : 'function' ,
220- sub : 'subroutine' ,
221- type_ : 'type'
222- }
198+ type = 'type'
223199
224- @classmethod
225- def is_valid (cls , kind ):
226- return cls . _NAMES . get ( kind , None ) is not None
200+ @staticmethod
201+ def is_valid (kind : Union [ str , 'KIND' ] ):
202+ return kind in set ( KIND )
227203
228- @classmethod
229- def to_string (cls , kind ):
230- assert cls .is_valid (kind )
231- return cls . _NAMES . get ( kind )
204+ @staticmethod
205+ def to_string (kind : 'KIND' ):
206+ assert KIND .is_valid (kind )
207+ return kind . value
232208
233209
234- class CONVENTION :
235- unknown = None
210+ @enum .unique
211+ class CONVENTION (str , enum .Enum ):
212+ unknown = 'unknown'
236213 fastcall = '__fastcall__'
237214 stdcall = '__stdcall__'
238215
239- _NAMES = {
240- unknown : '(unknown)' ,
241- fastcall : '__fastcall__' ,
242- stdcall : '__stdcall__'
243- }
244-
245- @classmethod
246- def is_valid (cls , convention ):
247- return cls ._NAMES .get (convention , None ) is not None
248-
249- @classmethod
250- def to_string (cls , convention ):
251- assert cls .is_valid (convention )
252- return cls ._NAMES [convention ]
216+ @staticmethod
217+ def is_valid (convention : Union [str , 'CONVENTION' ]):
218+ return convention in set (CONVENTION )
253219
220+ @staticmethod
221+ def to_string (convention : 'CONVENTION' ):
222+ assert CONVENTION .is_valid (convention )
223+ return convention .value
254224
255- # ----------------------------------------------------------------------
256- # Identifier Class (variable, function, label, array)
257- # ----------------------------------------------------------------------
258- ID_CLASSES = CLASS .classes
259225
260226# ----------------------------------------------------------------------
261227# Deprecated suffixes for variable names, such as "a$"
0 commit comments