2
2
3
3
import ast
4
4
import re
5
- from typing import TYPE_CHECKING
5
+ from typing import TYPE_CHECKING , Any
6
6
7
7
from dissect .cstruct import compiler
8
8
from dissect .cstruct .exceptions import (
@@ -33,7 +33,7 @@ def parse(self, data: str) -> None:
33
33
Args:
34
34
data: Data to parse definitions from, usually a string.
35
35
"""
36
- raise NotImplementedError ()
36
+ raise NotImplementedError
37
37
38
38
39
39
class TokenParser (Parser ):
@@ -119,10 +119,8 @@ def _enum(self, tokens: TokenConsumer) -> None:
119
119
val = val .strip ()
120
120
if not key :
121
121
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 )
126
124
127
125
if enumtype == "flag" :
128
126
high_bit = val .bit_length () - 1
@@ -243,7 +241,7 @@ def _lookup(self, tokens: TokenConsumer) -> None:
243
241
# Dirty trick because the regex expects a ; but we don't want it to be part of the value
244
242
m = pattern .match (ltok .value + ";" )
245
243
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 ()}
247
245
248
246
def _parse_field (self , tokens : TokenConsumer ) -> Field :
249
247
type_ = None
@@ -279,10 +277,7 @@ def _parse_field_type(self, type_: MetaType, name: str) -> tuple[MetaType, str,
279
277
280
278
if count_expression is not None :
281
279
# 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 ]
286
281
287
282
for count in reversed (counts ):
288
283
if count == "" :
@@ -315,8 +310,7 @@ def _names(self, tokens: TokenConsumer) -> list[str]:
315
310
if ntoken == self .TOK .NAME :
316
311
names .append (ntoken .value .strip ())
317
312
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 ("," )])
320
314
321
315
return names
322
316
@@ -333,8 +327,8 @@ def _replacer(match: re.Match) -> str:
333
327
# it means we have captured a non-quoted (real) comment string.
334
328
if comment := match .group (2 ):
335
329
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
338
332
339
333
return regex .sub (_replacer , string )
340
334
@@ -429,10 +423,8 @@ def _enums(self, data: str) -> None:
429
423
val = val .strip ()
430
424
if not key :
431
425
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 ()
436
428
437
429
if enumtype == "flag" :
438
430
high_bit = val .bit_length () - 1
@@ -535,7 +527,7 @@ def _lookups(self, data: str, consts: dict[str, int]) -> None:
535
527
536
528
for t in r :
537
529
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 ()}
539
531
540
532
def parse (self , data : str ) -> None :
541
533
self ._constants (data )
@@ -552,13 +544,13 @@ def __init__(self, token: str, value: str, match: re.Match):
552
544
self .value = value
553
545
self .match = match
554
546
555
- def __eq__ (self , other ) :
547
+ def __eq__ (self , other : object ) -> bool :
556
548
if isinstance (other , Token ):
557
549
other = other .token
558
550
559
551
return self .token == other
560
552
561
- def __ne__ (self , other ) :
553
+ def __ne__ (self , other : object ) -> bool :
562
554
return not self == other
563
555
564
556
def __repr__ (self ):
@@ -571,7 +563,7 @@ def __init__(self):
571
563
self .lookup : dict [str , str ] = {}
572
564
self .patterns : dict [str , re .Pattern ] = {}
573
565
574
- def __getattr__ (self , attr : str ):
566
+ def __getattr__ (self , attr : str ) -> str | Any :
575
567
try :
576
568
return self .lookup [attr ]
577
569
except AttributeError :
0 commit comments