...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package protobuf
16
17 import (
18 "fmt"
19 "strings"
20 "text/scanner"
21
22 "github.com/emicklei/proto"
23
24 "cuelang.org/go/cue/ast"
25 "cuelang.org/go/cue/token"
26 )
27
28
29
30 func failf(pos scanner.Position, format string, args ...interface{}) {
31 panic(protoError{pos, fmt.Errorf(format, args...)})
32 }
33
34 func fail(pos scanner.Position, err error) {
35 panic(protoError{pos, err})
36 }
37
38 type protoError struct {
39 pos scanner.Position
40 error
41 }
42
43 var (
44 newSection = token.NewSection.Pos()
45 )
46
47 func addComments(f ast.Node, i int, doc, inline *proto.Comment) bool {
48 cg := comment(doc, true)
49 if cg != nil && len(cg.List) > 0 && i > 0 {
50 cg.List[0].Slash = newSection
51 }
52 f.AddComment(cg)
53 f.AddComment(comment(inline, false))
54 return doc != nil
55 }
56
57 func comment(c *proto.Comment, doc bool) *ast.CommentGroup {
58 if c == nil || len(c.Lines) == 0 {
59 return nil
60 }
61 cg := &ast.CommentGroup{}
62 if doc {
63 cg.Doc = true
64 } else {
65 cg.Line = true
66 cg.Position = 10
67 }
68 for _, s := range c.Lines {
69 s = strings.TrimRight(s, " ")
70 cg.List = append(cg.List, &ast.Comment{Text: "//" + s})
71 }
72 return cg
73 }
74
75 func labelName(s string) string {
76 split := strings.Split(s, "_")
77 for i := 1; i < len(split); i++ {
78 split[i] = strings.Title(split[i])
79 }
80 return strings.Join(split, "")
81 }
82
View as plain text