jplevyak / dparser

A Scannerless GLR parser/parser generater.
https://github.com/jplevyak/dparser
BSD 3-Clause "New" or "Revised" License
105 stars 14 forks source link

How to set a syntax error callback without global variables? #32

Closed Phytolizer closed 2 years ago

Phytolizer commented 2 years ago

The declaration of D_SyntaxErrorFn is simple:

typedef void (*D_SyntaxErrorFn)(struct D_Parser *);

However, as far as I see, I can't pass in any of my own data (in my case, a logger). Is there a way to do that without globals?

jplevyak commented 2 years ago

When you create the struct D_Parser object with the function new_D_Parser it creates an extended object with more storage (struct Parser). You can add fields to struct D_Parser or struct Parser or realloc with additonal storage and cast (D_Parserp) to your struct (struct MyData)((char)p + sizeof(Parser)). I can add a 'struct D_Parser_User user_data' to struct D_Parser if you want to use the unmodified distribution then you can set it to your own struct during parser initialization.

Phytolizer commented 2 years ago

That would be great, thank you! For now I'll do it myself, but having it in the original source would be neat!

Edit: actually the change required is really minimal, perhaps I can just make a PR

jplevyak commented 2 years ago

Done in version 1.34