blob: 38521ab003aefe65a406944c112cf676223c0f8c [file] [log] [blame]
Marcel van Lohuizen5274e982019-04-28 17:51:43 +02001// 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 "strings"
Marcel van Lohuizenfbcb3392019-06-25 11:02:21 +020019 "text/scanner"
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020020
21 "cuelang.org/go/cue/ast"
22 "cuelang.org/go/cue/token"
23 "github.com/emicklei/proto"
24 "golang.org/x/xerrors"
25)
26
27// failf panics with a marked error that can be intercepted upon returning
28// from parsing.
Marcel van Lohuizenfbcb3392019-06-25 11:02:21 +020029func failf(pos scanner.Position, format string, args ...interface{}) {
30 panic(protoError{pos, xerrors.Errorf(format, args...)})
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020031}
32
Marcel van Lohuizenfbcb3392019-06-25 11:02:21 +020033func fail(pos scanner.Position, err error) {
34 panic(protoError{pos, err})
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020035}
36
37type protoError struct {
Marcel van Lohuizenfbcb3392019-06-25 11:02:21 +020038 pos scanner.Position
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020039 error
40}
41
42var (
Marcel van Lohuizend094cbe2019-05-20 17:46:48 -040043 newline = token.Newline.Pos()
44 newSection = token.NewSection.Pos()
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020045)
46
47func 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
57func 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 cg.List = append(cg.List, &ast.Comment{Text: "// " + s})
70 }
71 return cg
72}
73
Marcel van Lohuizen5274e982019-04-28 17:51:43 +020074func labelName(s string) string {
75 split := strings.Split(s, "_")
76 for i := 1; i < len(split); i++ {
77 split[i] = strings.Title(split[i])
78 }
79 return strings.Join(split, "")
80}