22
33import ast
44import re
5- from typing import TYPE_CHECKING
5+ from typing import TYPE_CHECKING , Any
66
77from dissect .cstruct import compiler
88from dissect .cstruct .exceptions import (
@@ -33,7 +33,7 @@ def parse(self, data: str) -> None:
3333 Args:
3434 data: Data to parse definitions from, usually a string.
3535 """
36- raise NotImplementedError ()
36+ raise NotImplementedError
3737
3838
3939class TokenParser (Parser ):
@@ -119,10 +119,8 @@ def _enum(self, tokens: TokenConsumer) -> None:
119119 val = val .strip ()
120120 if not key :
121121 continue
122- if not val :
123- val = nextval
124- else :
125- val = Expression (self .cstruct , val ).evaluate (values )
122+
123+ val = nextval if not val else Expression (self .cstruct , val ).evaluate (values )
126124
127125 if enumtype == "flag" :
128126 high_bit = val .bit_length () - 1
@@ -243,7 +241,7 @@ def _lookup(self, tokens: TokenConsumer) -> None:
243241 # Dirty trick because the regex expects a ; but we don't want it to be part of the value
244242 m = pattern .match (ltok .value + ";" )
245243 d = ast .literal_eval (m .group (2 ))
246- self .cstruct .lookups [m .group (1 )] = dict ([( self .cstruct .consts [k ], v ) for k , v in d .items ()])
244+ self .cstruct .lookups [m .group (1 )] = { self .cstruct .consts [k ]: v for k , v in d .items ()}
247245
248246 def _parse_field (self , tokens : TokenConsumer ) -> Field :
249247 type_ = None
@@ -279,10 +277,7 @@ def _parse_field_type(self, type_: MetaType, name: str) -> tuple[MetaType, str,
279277
280278 if count_expression is not None :
281279 # Poor mans multi-dimensional array by abusing the eager regex match of count
282- if "][" in count_expression :
283- counts = count_expression .split ("][" )
284- else :
285- counts = [count_expression ]
280+ counts = count_expression .split ("][" ) if "][" in count_expression else [count_expression ]
286281
287282 for count in reversed (counts ):
288283 if count == "" :
@@ -315,8 +310,7 @@ def _names(self, tokens: TokenConsumer) -> list[str]:
315310 if ntoken == self .TOK .NAME :
316311 names .append (ntoken .value .strip ())
317312 elif ntoken == self .TOK .DEFS :
318- for name in ntoken .value .strip ().split ("," ):
319- names .append (name .strip ())
313+ names .extend ([name .strip () for name in ntoken .value .strip ().split ("," )])
320314
321315 return names
322316
@@ -333,8 +327,8 @@ def _replacer(match: re.Match) -> str:
333327 # it means we have captured a non-quoted (real) comment string.
334328 if comment := match .group (2 ):
335329 return "\n " * comment .count ("\n " ) # so we will return empty to remove the comment
336- else : # otherwise, we will return the 1st group
337- return match .group (1 ) # captured quoted-string
330+ # otherwise, we will return the 1st group
331+ return match .group (1 ) # captured quoted-string
338332
339333 return regex .sub (_replacer , string )
340334
@@ -429,10 +423,8 @@ def _enums(self, data: str) -> None:
429423 val = val .strip ()
430424 if not key :
431425 continue
432- if not val :
433- val = nextval
434- else :
435- val = Expression (self .cstruct , val ).evaluate ()
426+
427+ val = nextval if not val else Expression (self .cstruct , val ).evaluate ()
436428
437429 if enumtype == "flag" :
438430 high_bit = val .bit_length () - 1
@@ -535,7 +527,7 @@ def _lookups(self, data: str, consts: dict[str, int]) -> None:
535527
536528 for t in r :
537529 d = ast .literal_eval (t .group (2 ))
538- self .cstruct .lookups [t .group (1 )] = dict ([( self .cstruct .consts [k ], v ) for k , v in d .items ()])
530+ self .cstruct .lookups [t .group (1 )] = { self .cstruct .consts [k ]: v for k , v in d .items ()}
539531
540532 def parse (self , data : str ) -> None :
541533 self ._constants (data )
@@ -545,23 +537,23 @@ def parse(self, data: str) -> None:
545537
546538
547539class Token :
548- __slots__ = ("token " , "value " , "match " )
540+ __slots__ = ("match " , "token " , "value " )
549541
550542 def __init__ (self , token : str , value : str , match : re .Match ):
551543 self .token = token
552544 self .value = value
553545 self .match = match
554546
555- def __eq__ (self , other ) :
547+ def __eq__ (self , other : object ) -> bool :
556548 if isinstance (other , Token ):
557549 other = other .token
558550
559551 return self .token == other
560552
561- def __ne__ (self , other ) :
553+ def __ne__ (self , other : object ) -> bool :
562554 return not self == other
563555
564- def __repr__ (self ):
556+ def __repr__ (self ) -> str :
565557 return f"<Token.{ self .token } value={ self .value !r} >"
566558
567559
@@ -571,7 +563,7 @@ def __init__(self):
571563 self .lookup : dict [str , str ] = {}
572564 self .patterns : dict [str , re .Pattern ] = {}
573565
574- def __getattr__ (self , attr : str ):
566+ def __getattr__ (self , attr : str ) -> str | Any :
575567 try :
576568 return self .lookup [attr ]
577569 except AttributeError :
0 commit comments