Parse thrift in Go: the Antlr Way
thrift2interface is a small project I developed for auto generating go interfaces from thrift by Antlr4. This could be easily done by following the steps in Parsing with ANTLR 4 and Go with some extra efforts:
Grammar
The struct
definition from the canonical Thrift.g4 may cause exceptions when generating Golang runtimes, this can be fixed by changing struct
to goStruct
(or any other name you desire).
Includes
The sub-include file parsing is just like the Backtracking Algorithm.
Package Name Alias
Well this is tricky — I cannot figure out a way to generate an unique alias name for each package, even with its namespace as a prefix, so I simply apply camel naming convention to each package, e.g.:
my_fancy.name
=> myFancyName
my.another.fancy_name
=> myAnotherFancyName
And if any conflict occurs, there are two ways afaik to handle this awkward situation:
- overwrite the generating rule for a specific package with an additional command line parameter.
- ambiguous aliases can be auto renamed with a suffix, like aliasName1, aliasName2, etc.