internal: replace ParseAttrBody with ParseAttr
Currently ParseAttrBody is the only way of creating
an internal.Attr, but it doesn't populate the Name
field because it doesn't have access to it.
It turns out that everywhere that is currently using ParseAttrBody
can also use slightly higher level function that takes
an ast.Attribute instead. This can usefully populate the
Name and Pos fields from the AST node,
which removes the need for second-guessing of
the position logic (as currently done in the cue package).
There are some places which don't have an ast.Attribute
already, but they're only in tests and it's easy to
construct one for the purpose.
We make ParseAttr return a pointer because it's
conventional in Go that the constructor for a type
with all pointer methods returns a pointer. There's
a small potential performance overhead to this change
but this should be negligible compared to what's going
on inside the ParseAttr logic itself.
The only wrinkle is that most of the places that currently
call ParseAttrBody invoke Attribute.Split first to find the
attribute name. We add an ast.Attribute.Name method
to make this inspection trivial. This also implies a small
amount of additional runtime overhead, but it should be
lost in the noise: scanning a few bytes is cheap and
even cheaper when we've done exactly the same scan
a few instructions previously. I think that the additional
simplicity makes this worthwhile.
Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I23eb0e3f997ef520a9a8e3d5abe8b9afde3121dc
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1235347
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Matthew Sackman <matthew@cue.works>