Implement a real parser for tags
clipperhouse opened this issue · 4 comments
Tags, of the pattern foo:"Bar,Baz"
are currently parsed with regex. It’s reasonably correct but not good.
Bar
& Baz
above are returned as Tag.Items
, which is simply a []string
. The individual typewriters are then free to interpret them, as (say) a method or a type.
v4 will require an extended syntax that will not play well with the current regex and so it’s a good time to create a real parser.
The new syntax is foo:"Bar,Baz[int]"
, adding one type parameter between brackets. (Regex gets a bit desperate here trying to distinguish commas between items, and commas within [ ]
.)
Tag.Items will probably need to become []Item
, where an item is:
type Item struct {
Name string,
TypeParameters []string,
}
The parser doesn’t need to know if the type parameters are meaningful, it simply needs to understand the syntax. The need to be valid Go identifiers. They are passed to individual typewriters for further evaluation.
Just for the sake of flexibility, it might be better if the TypeParameters allowed integers in addition to identifiers.
For reference, this is the code used to parse struct tags from the reflect package: http://golang.org/src/pkg/reflect/type.go#L763
@infogulch Thanks for the tip on the existing parser. We have a new lexer & parser in production, based on the text/template parser which Rob P often points to.
I think the type parameters will stick to being Go identifiers, and more so, we’ll evaluate them as types before passing to a TypeWriter. I realize allowing other things in there would allow TypeWriters different sorts of interpretation, but as a design choice, I prefer type parameters to be types and nothing else.
A lexer & parser has been implemented. Evaluating type parameters has not been implemented but will consider that a separate issue.