2929from .field import calculate_padding , Kind , FieldType
3030from .c_expr import c_eval
3131from .exceptions import CStructException , ParserError
32+ from .native_types import get_native_type
3233
3334if TYPE_CHECKING :
3435 from .abstract import AbstractCStruct , AbstractCEnum
@@ -58,6 +59,16 @@ def __init__(self, text: str) -> None:
5859 def pop (self ) -> str :
5960 return self .tokens .pop (0 )
6061
62+ def pop_c_type (self ) -> str :
63+ c_type = self .pop ()
64+ if c_type in ['signed' , 'unsigned' ] and len (self ) > 1 :
65+ # short int, long int, or long long
66+ c_type = c_type + " " + self .pop ()
67+ elif c_type in ['short' , 'long' ] and len (self ) > 1 and self .get () in ['int' , 'long' ]:
68+ # short int, long int, or long long
69+ c_type = c_type + " " + self .pop ()
70+ return c_type
71+
6172 def get (self ) -> str :
6273 return self .tokens [0 ]
6374
@@ -203,11 +214,16 @@ def parse_struct_def(
203214 if result :
204215 result ['__cls__' ].parse (result , ** kargs )
205216 name = tokens .pop ()
217+ native_format = None
218+ if tokens .get () == ':' : # enumeration type declaration
219+ tokens .pop () # pop ":"
220+ type_ = get_native_type (tokens .pop_c_type ())
221+ native_format = type_ .native_format
206222 if tokens .get () == '{' : # named enum
207223 tokens .pop () # pop "{"
208- result = parse_enum (tokens , __name__ = name )
224+ result = parse_enum (tokens , __name__ = name , native_format = native_format )
209225 elif name == '{' : # unnamed enum
210- result = parse_enum (tokens )
226+ result = parse_enum (tokens , native_format = native_format )
211227 else :
212228 raise ParserError (f"{ name } definition expected" )
213229
@@ -244,9 +260,14 @@ def parse_enum_def(__def__: Union[str, Tokens], **kargs: Any) -> Optional[Dict[s
244260 raise ParserError (f"enum expected - { kind } " )
245261
246262 name = tokens .pop ()
263+ native_format = None
264+ if tokens .get () == ':' : # enumeration type declaration
265+ tokens .pop () # pop ":"
266+ type_ = get_native_type (tokens .pop_c_type ())
267+ native_format = type_ .native_format
247268 if tokens .get () == '{' : # named enum
248269 tokens .pop () # pop "{"
249- return parse_enum (tokens , __name__ = name )
270+ return parse_enum (tokens , __name__ = name , native_format = native_format )
250271 elif name == '{' : # unnamed enum
251272 return parse_enum (tokens )
252273 else :
@@ -256,6 +277,7 @@ def parse_enum_def(__def__: Union[str, Tokens], **kargs: Any) -> Optional[Dict[s
256277def parse_enum (
257278 __enum__ : Union [str , Tokens ],
258279 __name__ : Optional [str ] = None ,
280+ native_format : Optional [str ] = None ,
259281 ** kargs : Any ,
260282) -> Optional [Dict [str , Any ]]:
261283 """
@@ -264,6 +286,7 @@ def parse_enum(
264286 Args:
265287 __enum__: definition of the enum in C syntax
266288 __name__: enum name
289+ native_format: struct module format
267290
268291 Returns:
269292 dict: the parsed definition
@@ -283,7 +306,6 @@ def parse_enum(
283306 break
284307
285308 name = tokens .pop ()
286-
287309 next_token = tokens .pop ()
288310 if next_token in {"," , "}" }: # enum-constant without explicit value
289311 if len (constants ) == 0 :
@@ -324,6 +346,7 @@ def parse_enum(
324346 '__is_union__' : False ,
325347 '__is_enum__' : True ,
326348 '__name__' : __name__ ,
349+ '__native_format__' : native_format ,
327350 '__cls__' : CEnum ,
328351 }
329352 return result
0 commit comments