What is the most idiomatic way to get some form of context to the yacc parser in goyacc, i.e. emulate the %param command in traditional yacc?
I need to parse to my .Parse function some context (in this case including for instance where to build its parse tree).
The goyacc .Parse function is declared
func ($$rcvr *$$ParserImpl) Parse($$lex $$Lexer) int {
Things I've thought of:
-
$$ParserImplcannot be changed by the.yfile, so the obvious solution (to add fields to it) is right out, which is a pity. - As
$$Lexeris an interface, I could stuff the parser context into the Lexer implementation, then force type convert$$lexto that implementation (assuming my parser always used the same lexer), but this seems pretty disgusting (for which read non-idiomatic). Moreover there is (seemingly) no way to put a user-generated line at the top of theParsefunction likec := yylex.(*lexer).c, so in the many tens of places I want to refer to this variable, I have to use the rather ugly formyylex.(*lexer).crather than justc. - Normally I'd use
%paramin normalyacc/ C (well,bisonanyway), but that doesn't exist ingoyacc. - I'd like to avoid postprocessing my generated
.gofile withsedorperlfor what are hopefully obvious reasons. - I want to be able to (go)yacc parse more than one file at once, so a global variable is not possible (and global variables are hardly idiomatic).
What's the most idiomatic solution here? I keep thinking I must be missing something simple.