Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 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 | |
| 15 | package protobuf |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
Marcel van Lohuizen | fbcb339 | 2019-06-25 11:02:21 +0200 | [diff] [blame] | 19 | "text/scanner" |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 20 | |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 21 | "github.com/emicklei/proto" |
| 22 | "golang.org/x/xerrors" |
Koichi Shiraishi | 30a4bee | 2019-12-26 03:06:48 +0900 | [diff] [blame] | 23 | |
| 24 | "cuelang.org/go/cue/ast" |
| 25 | "cuelang.org/go/cue/token" |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // failf panics with a marked error that can be intercepted upon returning |
| 29 | // from parsing. |
Marcel van Lohuizen | fbcb339 | 2019-06-25 11:02:21 +0200 | [diff] [blame] | 30 | func failf(pos scanner.Position, format string, args ...interface{}) { |
| 31 | panic(protoError{pos, xerrors.Errorf(format, args...)}) |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Marcel van Lohuizen | fbcb339 | 2019-06-25 11:02:21 +0200 | [diff] [blame] | 34 | func fail(pos scanner.Position, err error) { |
| 35 | panic(protoError{pos, err}) |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | type protoError struct { |
Marcel van Lohuizen | fbcb339 | 2019-06-25 11:02:21 +0200 | [diff] [blame] | 39 | pos scanner.Position |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 40 | error |
| 41 | } |
| 42 | |
| 43 | var ( |
Marcel van Lohuizen | d094cbe | 2019-05-20 17:46:48 -0400 | [diff] [blame] | 44 | newline = token.Newline.Pos() |
| 45 | newSection = token.NewSection.Pos() |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | func addComments(f ast.Node, i int, doc, inline *proto.Comment) bool { |
| 49 | cg := comment(doc, true) |
| 50 | if cg != nil && len(cg.List) > 0 && i > 0 { |
| 51 | cg.List[0].Slash = newSection |
| 52 | } |
| 53 | f.AddComment(cg) |
| 54 | f.AddComment(comment(inline, false)) |
| 55 | return doc != nil |
| 56 | } |
| 57 | |
| 58 | func comment(c *proto.Comment, doc bool) *ast.CommentGroup { |
| 59 | if c == nil || len(c.Lines) == 0 { |
| 60 | return nil |
| 61 | } |
| 62 | cg := &ast.CommentGroup{} |
| 63 | if doc { |
| 64 | cg.Doc = true |
| 65 | } else { |
| 66 | cg.Line = true |
| 67 | cg.Position = 10 |
| 68 | } |
| 69 | for _, s := range c.Lines { |
Marcel van Lohuizen | b1d4439 | 2019-08-23 13:13:54 +0200 | [diff] [blame] | 70 | s = strings.TrimRight(s, " ") |
| 71 | cg.List = append(cg.List, &ast.Comment{Text: "//" + s}) |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 72 | } |
| 73 | return cg |
| 74 | } |
| 75 | |
Marcel van Lohuizen | 5274e98 | 2019-04-28 17:51:43 +0200 | [diff] [blame] | 76 | func labelName(s string) string { |
| 77 | split := strings.Split(s, "_") |
| 78 | for i := 1; i < len(split); i++ { |
| 79 | split[i] = strings.Title(split[i]) |
| 80 | } |
| 81 | return strings.Join(split, "") |
| 82 | } |