3131from .exceptions import CStructException , ParserError
3232
3333if TYPE_CHECKING :
34- from .abstract import AbstractCStruct , AbstractCEnum
34+ from .abstract import AbstractCStruct
3535
36- __all__ = ['parse_struct' , 'parse_struct_def' , 'Tokens' ]
36+ __all__ = ['parse_struct' , 'parse_struct_def' , 'parse_enum_def' , ' Tokens' ]
3737
3838
3939class Tokens (object ):
@@ -48,7 +48,7 @@ def __init__(self, text: str) -> None:
4848 _ , name , value = line .strip ().split (maxsplit = 2 )
4949 DEFINES [name ] = c_eval (value )
5050 except Exception :
51- raise ParserError ("Parsing line {}" . format ( line ) )
51+ raise ParserError (f "Parsing line { line } " )
5252 else :
5353 lines .append (line )
5454 text = " " .join (lines )
@@ -135,9 +135,8 @@ def parse_type(tokens: Tokens, __cls__: Type['AbstractCStruct'], byte_order: Opt
135135 try :
136136 ref = STRUCTS [tail ]
137137 except KeyError :
138- raise ParserError ("Unknow {} {}" . format ( c_type , tail ) )
138+ raise ParserError (f"Unknown ' { c_type } ' ' { tail } '" )
139139 elif c_type .startswith ('enum' ):
140- # from .abstract import AbstractCEnum
141140 from .cenum import CEnum
142141
143142 c_type , tail = c_type .split (' ' , 1 )
@@ -175,8 +174,10 @@ def parse_struct_def(
175174 if not tokens :
176175 return None
177176 kind = tokens .pop ()
177+ if kind == 'enum' :
178+ return parse_enum_def (__def__ , ** kargs )
178179 if kind not in ['struct' , 'union' ]:
179- raise ParserError ("struct or union expected - {}" . format ( kind ) )
180+ raise ParserError ("struct, union, or enum expected - {kind}" )
180181 __is_union__ = kind == 'union'
181182 vtype = tokens .pop ()
182183 if tokens .get () == '{' : # Named nested struct
@@ -185,7 +186,7 @@ def parse_struct_def(
185186 elif vtype == '{' : # Unnamed nested struct
186187 return parse_struct (tokens , __cls__ = __cls__ , __is_union__ = __is_union__ , __byte_order__ = __byte_order__ )
187188 else :
188- raise ParserError ("{ } definition expected". format ( vtype ) )
189+ raise ParserError (f" { vtype } definition expected" )
189190
190191
191192def parse_enum_def (__def__ : Union [str , Tokens ], ** kargs : Any ) -> Optional [Dict [str , Any ]]:
@@ -242,7 +243,7 @@ def parse_enum(__enum__: Union[str, Tokens], **kargs: Any) -> Optional[Dict[str,
242243 break
243244
244245 if len (exp_elems ) == 0 :
245- raise ParserError (f "enum is missing value expression" )
246+ raise ParserError ("enum is missing value expression" )
246247
247248 int_expr = " " .join (exp_elems )
248249 try :
@@ -259,7 +260,12 @@ def parse_enum(__enum__: Union[str, Tokens], **kargs: Any) -> Optional[Dict[str,
259260 if next_token == "}" :
260261 break
261262
262- result = {'__constants__' : constants }
263+ result = {
264+ '__constants__' : constants ,
265+ '__is_struct__' : False ,
266+ '__is_union__' : False ,
267+ '__is_enum__' : True ,
268+ }
263269 return result
264270
265271
@@ -325,7 +331,9 @@ def parse_struct(
325331 '__fields__' : list (fields_types .keys ()),
326332 '__fields_types__' : fields_types ,
327333 '__size__' : size ,
334+ '__is_struct__' : not __is_union__ ,
328335 '__is_union__' : __is_union__ ,
336+ '__is_enum__' : False ,
329337 '__byte_order__' : __byte_order__ ,
330338 '__alignment__' : max_alignment ,
331339 }
0 commit comments