encoding/protobuf: don't rely on verbatim Ident formatting

Parse the expressions into proper CUE before
formatting.

Change-Id: I66fdf247c7f5d949aa76aa46906b36656c502f25
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4721
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/protobuf/parse.go b/encoding/protobuf/parse.go
index a58943d..b35e6ae 100644
--- a/encoding/protobuf/parse.go
+++ b/encoding/protobuf/parse.go
@@ -291,9 +291,11 @@
 }
 
 func (p *protoConverter) toExpr(pos scanner.Position, name string) (expr ast.Expr) {
-	a := strings.Split(name, ".")
-	expr = &ast.Ident{NamePos: p.toCUEPos(pos), Name: a[0]}
-	expr = ast.NewSel(expr, a[1:]...)
+	expr, err := parser.ParseExpr("", name, parser.ParseComments)
+	if err != nil {
+		panic(err)
+	}
+	ast.SetPos(expr, p.toCUEPos(pos))
 	return expr
 }
 
diff --git a/encoding/protobuf/testdata/attributes.proto.out.cue b/encoding/protobuf/testdata/attributes.proto.out.cue
index 319e791..28c89d0 100644
--- a/encoding/protobuf/testdata/attributes.proto.out.cue
+++ b/encoding/protobuf/testdata/attributes.proto.out.cue
@@ -21,9 +21,10 @@
 )
 
 StructWrap: {
-	struct?:    {}     @protobuf(1,type=google.protobuf.Struct)
-	any?:       _      @protobuf(2,type=google.protobuf.Value)
-	listVal?:   [...]  @protobuf(3,type=google.protobuf.ListValue)
+	struct?: {
+	} @protobuf(1,type=google.protobuf.Struct)
+	any?: _ @protobuf(2,type=google.protobuf.Value)
+	listVal?: [...] @protobuf(3,type=google.protobuf.ListValue)
 	boolVal?:   bool   @protobuf(4,type=google.protobuf.BoolValue)
 	stringVal?: string @protobuf(5,type=google.protobuf.StringValue)
 	numberVal?: number @protobuf(6,type=google.protobuf.NumberValue)
diff --git a/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue
index 0918e35..28c89d0 100644
--- a/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue
+++ b/encoding/protobuf/testdata/istio.io/api/mixer/v1/attributes_proto_gen.cue
@@ -21,7 +21,8 @@
 )
 
 StructWrap: {
-	struct?: {} @protobuf(1,type=google.protobuf.Struct)
+	struct?: {
+	} @protobuf(1,type=google.protobuf.Struct)
 	any?: _ @protobuf(2,type=google.protobuf.Value)
 	listVal?: [...] @protobuf(3,type=google.protobuf.ListValue)
 	boolVal?:   bool   @protobuf(4,type=google.protobuf.BoolValue)