@@ -81,7 +81,7 @@ def _read(self, stream):
8181
8282 return Instance(self, r, sizes)
8383
84- def add_fields(self, name, type_, offset=None):
84+ def add_fields(self, name, type_, offset=None, commentAttributes={} ):
8585 raise NotImplementedError("Can't add fields to a compiled structure")
8686
8787 def __repr__(self):
@@ -401,8 +401,9 @@ def _structs(self, data):
401401 self .cstruct .addtype (td , st )
402402
403403 def _parse_fields (self , s ):
404+ commentAttributes = {}
404405 fields = re .finditer (
405- r'(?P<type>[^\s]+)\s+(?P<name>[^\s\[:]+)(\s*:\s*(?P<bits>\d+))?(\[(?P<count>[^;\n]*)\])?;' ,
406+ r'(?P<commentBlock>\/\*(\*(?!\/)|[^*])*\*\/)?[ \t\r\n]*(?P< type>[^\s]+)\s+(?P<name>[^\s\[:]+)(\s*:\s*(?P<bits>\d+))?(\[(?P<count>[^;\n]*)\])?;' ,
406407 s ,
407408 )
408409 r = []
@@ -411,6 +412,21 @@ def _parse_fields(self, s):
411412 if d ['type' ].startswith ('//' ):
412413 continue
413414
415+ commentAttributes = {}
416+
417+ #parse the comment header
418+ if d ['commentBlock' ] is not None and d ['commentBlock' ].startswith ('/*' ):
419+ commentfields = re .finditer (
420+ r'@(?P<commentType>[a-zA-Z0-9_\-\/,.]+):[ \t]*(?P<commentVal>[a-zA-Z0-9_\-,.]+)' ,
421+ d ['commentBlock' ],
422+ )
423+ for cf in commentfields :
424+ cd = cf .groupdict ()
425+ try :
426+ commentAttributes [cd ['commentType' ]]= cd ['commentVal' ]
427+ except Exception :
428+ pass
429+
414430 type_ = self .cstruct .resolve (d ['type' ])
415431
416432 d ['name' ] = d ['name' ].replace ('(' , '' ).replace (')' , '' )
@@ -433,7 +449,7 @@ def _parse_fields(self, s):
433449 d ['name' ] = d ['name' ][1 :]
434450 type_ = Pointer (self .cstruct , type_ )
435451
436- field = Field (d ['name' ], type_ , int (d ['bits' ]) if d ['bits' ] else None )
452+ field = Field (d ['name' ], type_ , int (d ['bits' ]) if d ['bits' ] else None , commentAttributes = commentAttributes )
437453 r .append (field )
438454
439455 return r
@@ -747,6 +763,7 @@ def __init__(self, cstruct, name, fields=None):
747763 self .lookup = OrderedDict ()
748764 self .fields = fields if fields else []
749765
766+
750767 for f in self .fields :
751768 self .lookup [f .name ] = f
752769
@@ -860,15 +877,15 @@ def _write(self, stream, data):
860877
861878 return num
862879
863- def add_field (self , name , type_ , offset = None ):
880+ def add_fields (self , name , type_ , offset = None , commentAttributes = {} ):
864881 """Add a field to this structure.
865882
866883 Args:
867884 name: The field name.
868885 type_: The field type.
869886 offset: The field offset.
870887 """
871- field = Field (name , type_ , offset = offset )
888+ field = Field (name , type_ , offset = offset , commentAttributes = commentAttributes )
872889 self .fields .append (field )
873890 self .lookup [name ] = field
874891 self .size = None
@@ -969,14 +986,15 @@ def reset(self):
969986class Field (object ):
970987 """Holds a structure field."""
971988
972- def __init__ (self , name , type_ , bits = None , offset = None ):
989+ def __init__ (self , name , type_ , bits = None , offset = None , commentAttributes = {} ):
973990 self .name = name
974991 self .type = type_
975992 self .bits = bits
976993 self .offset = offset
994+ self .commentAttributes = commentAttributes
977995
978996 def __repr__ (self ):
979- return '<Field {} {}>' .format (self .name , self .type )
997+ return '<Field {} {} {} >' .format (self .name , self .type , self . commentAttributes )
980998
981999
9821000class Array (BaseType ):
0 commit comments