Skip to content

[migrated] Rewrite lexer and parser - #5

Closed
twiggler wants to merge 2 commits into
masterfrom
migrate/dissect.cstruct/pr-146
Closed

twiggler wants to merge 2 commits into
masterfrom
migrate/dissect.cstruct/pr-146

Conversation

@twiggler

Copy link
Copy Markdown
Owner

Note

Migrated from fox-it/dissect.cstruct#146 by @twiggler.
Original author: @Schamper


Closes #85, partially #142, and will make #86 and #138 a lot easier to implement. Fixes #149.

This PR will (finally) replace the shoddy C syntax parser I originally wrote many moons ago, when I discovered the existence of re.Scanner and ran with it. This PR aims to add a somewhat decent lexer and separate parser. I'm still not a compsci 1337coder, so this is just what I came up with (with some help) and definitely not a textbook implementation. All feedback is welcome.

  • New lexer
  • New C syntax parser that utilizes the new lexer
  • Expression parser re-uses the new lexer
  • Reworked how sizeof works in the expression parser, and added offsetof

The new parser has made changing parsing behavior a lot easier. As such, this PR already makes the following changes:

  • The new parser is slightly stricter, requiring proper semicolon endings for example. We'll need to fix this in any dissect code that has this.

  • An important semantic change is how named nested structures are handled. In my infinite wisdom, I originally figured that named nested structures do not "exist" in the top level scope. That's not true, so now named nested structures get properly registered with the cstruct instance:

struct a {
    struct b {
        ...
    };
};

// Will register both `a` and `b`
  • Another important change is how we deal with struct { ... } name;. We used to parse this first as an anonymous struct, then capture name as the structure type name. That's not strictly correct, name is a variable of an anonymous unnamed struct, so we now treat it as such. We don't error on this, but rather we silently ignore name and skip until we reach a ;
  • typedef enum ... is now allowed
  • Probably some other things I'm forgetting

This probably warrants a major version bump, so maybe good to pair this with #114, #144 and what we discussed in #142.


Migration notes

  • Package: dissect.cstruct
  • Original base branch: main

@twiggler twiggler closed this Apr 20, 2026
@twiggler
twiggler deleted the migrate/dissect.cstruct/pr-146 branch April 20, 2026 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants