goccmack / gocc

Parser / Scanner Generator
Other
618 stars 48 forks source link

Feature: add user-defined context attribute to parser #112

Closed kfsone closed 3 years ago

kfsone commented 3 years ago

(See https://github.com/goccmack/gocc/issues/111)

This change adds a 'UserContext interface{}' to parser.Parser that is opaque to gocc, but can be accessed in sdt as '$Context'. This is achieved by passing it as an new 'C' argument to ReduceFunc.

I was going to append it to the attrib list we pass to ReduceFunc and have it correspond to "X[len(X)-1]", but I wasn't sure if that would be too hacky. It might be a lower perf burden than passing an extra argument?

A further alternative would be to pass the *parser.Parser itself, and have the user extend the class as they so need, rather than add a field to the structure.

My specific case is tracking filenames while running concurrent parses as well as tracking recursion during includes.

Added an example with testcases.

kfsone commented 3 years ago

Going to close this while I do some additional work on it. I found it makes most sense to formalize the basic "Context" and attach it to Token, Lexer and Parser.