title: goyacc: getting context to the yacc parser / no `%param`
body: <p>What is the most idiomatic way to get some form of context to the <code>yacc</code> parser in <a href="https://godoc.org/golang.org/x/tools/cmd/goyacc" rel="nofollow noreferrer"><code>goyacc</code></a>, i.e. emulate the <a href="https://www.gnu.org/software/bison/manual/html_node/Pure-Calling.html" rel="nofollow noreferrer"><code>%param</code></a> command in traditional <code>yacc</code>?</p>
  
  <p>I need to parse to my <code>.Parse</code> function some context (in this case including for instance where to build its parse tree).</p>
  
  <p>The <code>goyacc</code> <code>.Parse</code> function is <a href="https://github.com/golang/tools/blob/master/cmd/goyacc/yacc.go#L3416" rel="nofollow noreferrer">declared</a></p>
  
  <pre><code>func ($$rcvr *$$ParserImpl) Parse($$lex $$Lexer) int {
  </code></pre>
  
  <p>Things I've thought of:</p>
  
  <ul>
  <li><code>$$ParserImpl</code> <a href="https://github.com/golang/tools/blob/master/cmd/goyacc/yacc.go#L3279" rel="nofollow noreferrer">cannot be changed</a> by the <code>.y</code> file, so the obvious solution (to add fields to it) is right out, which is a pity.</li>
  <li>As <code>$$Lexer</code> is an interface, I could stuff the parser context into the Lexer implementation, then force type convert <code>$$lex</code> to 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 the <code>Parse</code> function like <code>c := yylex.(*lexer).c</code>, so in the many tens of places I want to refer to this variable, I have to use the rather ugly form <code>yylex.(*lexer).c</code> rather than just <code>c</code>.</li>
  <li>Normally I'd use <a href="https://www.gnu.org/software/bison/manual/html_node/Pure-Calling.html" rel="nofollow noreferrer"><code>%param</code></a> in normal <code>yacc</code> / C (well, <code>bison</code> anyway), but that doesn't exist in <code>goyacc</code>.</li>
  <li>I'd like to avoid postprocessing my generated <code>.go</code> file with <code>sed</code> or <code>perl</code> for what are hopefully obvious reasons.</li>
  <li>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).</li>
  </ul>
  
  <p>What's the most idiomatic solution here? I keep thinking I must be missing something simple.</p>