Skip to content

Commit b6299af

Browse files
committed
Process review feedback
1 parent 44f86af commit b6299af

5 files changed

Lines changed: 25 additions & 15 deletions

File tree

dissect/cstruct/cstruct.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,7 @@ def load(self, definition: str, deftype: int | None = None, **kwargs) -> cstruct
253253
Definitions can be parsed using different parsers. Currently, there's
254254
only one supported parser - DEF_CSTYLE. Parsers can add types and
255255
modify this cstruct instance. Arguments can be passed to parsers
256-
using kwargs.
257-
258-
The CSTYLE parser was recently replaced with token based parser,
259-
instead of a strictly regex based one. The old parser is still available
260-
by using DEF_LEGACY.
256+
using ``kwargs``.
261257
262258
Args:
263259
definition: The definition to parse.

dissect/cstruct/lexer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ def tokenize(self) -> list[Token]:
482482

483483
elif ch == "-" and (
484484
self._pos == 0
485-
or self._tokens[-1].type not in (TokenType.IDENTIFIER, TokenType.NUMBER, TokenType.RPAREN)
485+
or self._tokens[-1].type
486+
not in (TokenType.IDENTIFIER, TokenType.NUMBER, TokenType.RPAREN, TokenType.RBRACKET)
486487
):
487488
self._emit(TokenType.UNARY_MINUS, self._take(), line, col)
488489

dissect/cstruct/parser.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from dissect.cstruct import compiler
88
from dissect.cstruct.exceptions import (
99
ExpressionParserError,
10+
LexerError,
1011
ParserError,
1112
)
1213
from dissect.cstruct.expression import Expression
@@ -91,11 +92,6 @@ def _at(self, *types: TokenType) -> bool:
9192
"""Return whether the current token matches any of the given types."""
9293
return self._tokens[self._pos].type in types
9394

94-
def _at_value(self, value: str) -> bool:
95-
"""Return whether the current token is an identifier with the given value."""
96-
token = self._tokens[self._pos]
97-
return token.type == TokenType.IDENTIFIER and token.value == value
98-
9995
def _error(self, msg: str, *, token: Token | None = None) -> ParserError:
10096
return ParserError(f"line {(token if token is not None else self._tokens[self._pos]).line}: {msg}")
10197

@@ -188,7 +184,7 @@ def _parse_define(self) -> None:
188184
if isinstance(value, str):
189185
try:
190186
value = Expression(value).evaluate(self.cs)
191-
except ExpressionParserError:
187+
except (LexerError, ExpressionParserError):
192188
# If evaluation fails, just keep it as a string (e.g. for macro-like constants)
193189
pass
194190

dissect/cstruct/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,10 @@ def sizeof(type_: type[BaseType] | BaseType) -> int:
367367
return len(type_)
368368

369369

370-
def offsetof(type_: type[Structure], field: str) -> int:
370+
def offsetof(type_: type[Structure], name: str) -> int:
371371
"""Get the offset of a field in a structure."""
372-
if (field := type_.fields.get(field)) is None:
373-
raise ValueError(f"Structure '{type_.__name__}' does not have a field named '{field}'")
372+
if (field := type_.fields.get(name)) is None:
373+
raise ValueError(f"Structure '{type_.__name__}' does not have a field named '{name}'")
374374
if (offset := field.offset) is None:
375375
raise ValueError(f"Field '{field._name}' of structure '{type_.__name__}' does not have a known offset")
376376
return offset

tests/test_lexer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@
5656
("a << b", [TokenType.IDENTIFIER, TokenType.LSHIFT, TokenType.IDENTIFIER], ["a", "<<", "b"]),
5757
("x >> 2", [TokenType.IDENTIFIER, TokenType.RSHIFT, TokenType.NUMBER], ["x", ">>", "2"]),
5858
("-1", [TokenType.UNARY_MINUS, TokenType.NUMBER], ["-", "1"]),
59+
(
60+
"(0) - 1",
61+
[TokenType.LPAREN, TokenType.NUMBER, TokenType.RPAREN, TokenType.MINUS, TokenType.NUMBER],
62+
["(", "0", ")", "-", "1"],
63+
),
64+
(
65+
"c[1] - 1",
66+
[
67+
TokenType.IDENTIFIER,
68+
TokenType.LBRACKET,
69+
TokenType.NUMBER,
70+
TokenType.RBRACKET,
71+
TokenType.MINUS,
72+
TokenType.NUMBER,
73+
],
74+
["c", "[", "1", "]", "-", "1"],
75+
),
5976
("1 - 1", [TokenType.NUMBER, TokenType.MINUS, TokenType.NUMBER], ["1", "-", "1"]),
6077
# Preprocessor directives
6178
("#define", [TokenType.PP_DEFINE], ["define"]),

0 commit comments

Comments
 (0)