1// Copyright 2019 CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package protobuf
16
17import (
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// failf panics with a marked error that can be intercepted upon returning
29// from parsing.
30func failf(pos scanner.Position, format string, args ...interface{}) {
31 panic(protoError{pos, fmt.Errorf(format, args...)})
32}
33
34func fail(pos scanner.Position, err error) {
35 panic(protoError{pos, err})
36}
37
38type protoError struct {
39 pos scanner.Position
40 error
41}
42
43var newSection = token.NewSection.Pos()
44
45func addComments(f ast.Node, i int, doc, inline *proto.Comment) {
46 cg := comment(doc, true)
47 if cg != nil && len(cg.List) > 0 && i > 0 {
48 cg.List[0].Slash = newSection
49 }
50 ast.AddComment(f, cg)
51 ast.AddComment(f, comment(inline, false))
52}
53
54func comment(c *proto.Comment, doc bool) *ast.CommentGroup {
55 if c == nil || len(c.Lines) == 0 {
56 return nil
57 }
58 cg := &ast.CommentGroup{}
59 if doc {
60 cg.Doc = true
61 } else {
62 cg.Line = true
63 cg.Position = 10
64 }
65 for _, s := range c.Lines {
66 s = strings.TrimRight(s, " ")
67 cg.List = append(cg.List, &ast.Comment{Text: "//" + s})
68 }
69 return cg
70}
71
72func labelName(s string) string {
73 split := strings.Split(s, "_")
74 for i := 1; i < len(split); i++ {
75 split[i] = strings.Title(split[i])
76 }
77 return strings.Join(split, "")
78}