Accessing custom parser state inside yacc rules
First of all, thanks for maintaining this tool!
The problem
When building an AST, I need a couple of helper objects that are bundled in $$Lexer
implementation right now.
Inside rules handling, I do yylex.(*MyLexer)
and access that state through that.
The only problem is that it's very repetitive and it makes code less readable (and also less efficient, but it's not a bottleneck anyway).
I can't use a global state since parsing can be executed concurrently, this is why these objects are stored inside lexer, which is the only input for the parse function.
Is there a better way to do it?
Potential solutions?
-
Add a way to insert some code in the beginning of the parsing function. Then users can add
c := yylex.(*MyLexer)
thing there once and usec
inside parsing scope easily. -
Don't define
$$Lexer
interface, let users define their own$$Lexer
type that implements two required methods + they can have any custom state and methods they want.
(There can be more, but since I don't know why we have interfaces there in the first place, I would like to know your opinion first.)