Debian Codegraph @

comment.go

raw symbols SCIP
1 // Copyright (c) 2017 Ernest Micklei
2 //
3 // MIT License
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 package protoscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/
25
26 import (
27 "stringsscip-go gomod github.com/golang/go/src go1.12 strings/"
28 "text/scannerscip-go gomod github.com/golang/go/src go1.12 `text/scanner`/"
29 )
30
31 // Comment one or more comment text lines, either in c- or c++ style.
32 type Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment# struct {
33 Positionscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Position. scannerscip-go gomod github.com/golang/go/src go1.12 `text/scanner`/.Positionscip-go gomod github.com/golang/go/src go1.12 `text/scanner`/Position#
34 // Lines are comment text lines without prefixes //, ///, /* or suffix */
35 Linesscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Lines. []string
36 Cstylescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Cstyle. bool // refers to /* ... */, C++ style is using //
37 ExtraSlashscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#ExtraSlash. bool // is true if the comment starts with 3 slashes
38 }
39
40 // newComment returns a comment.
41 func newCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/newComment().(pos scannerscip-go gomod github.com/golang/go/src go1.12 `text/scanner`/.Positionscip-go gomod github.com/golang/go/src go1.12 `text/scanner`/Position#, lit string) *Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment# {
42 extraSlash := stringsscip-go gomod github.com/golang/go/src go1.12 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.12 strings/HasPrefix().(lit, "///")
43 isCstyle := stringsscip-go gomod github.com/golang/go/src go1.12 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.12 strings/HasPrefix().(lit, "/*") && stringsscip-go gomod github.com/golang/go/src go1.12 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.12 strings/HasSuffix().(lit, "*/")
44 var lines []string
45 if isCstyle {
46 withoutMarkers := stringsscip-go gomod github.com/golang/go/src go1.12 strings/.TrimRightscip-go gomod github.com/golang/go/src go1.12 strings/TrimRight().(stringsscip-go gomod github.com/golang/go/src go1.12 strings/.TrimLeftscip-go gomod github.com/golang/go/src go1.12 strings/TrimLeft().(lit, "/*"), "*/")
47 lines = stringsscip-go gomod github.com/golang/go/src go1.12 strings/.Splitscip-go gomod github.com/golang/go/src go1.12 strings/Split().(withoutMarkers, "\n")
48 } else {
49 lines = stringsscip-go gomod github.com/golang/go/src go1.12 strings/.Splitscip-go gomod github.com/golang/go/src go1.12 strings/Split().(stringsscip-go gomod github.com/golang/go/src go1.12 strings/.TrimLeftscip-go gomod github.com/golang/go/src go1.12 strings/TrimLeft().(lit, "/"), "\n")
50 }
51 return &Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#{Positionscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Position.: pos, Linesscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Lines.: lines, Cstylescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Cstyle.: isCstyle, ExtraSlashscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#ExtraSlash.: extraSlash}
52 }
53
54 type inlineCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/inlineComment# struct {
55 linescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/inlineComment#line. string
56 extraSlashscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/inlineComment#extraSlash. bool
57 }
58
59 // Accept dispatches the call to the visitor.
60 func (c *Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#) Acceptscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Accept().(v Visitorscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Visitor#) {
61 v.VisitCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Visitor#VisitComment.(c)
62 }
63
64 // Merge appends all lines from the argument comment.
65 func (c *Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#) Mergescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Merge().(other *Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#) {
66 c.Linesscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Lines. = append(c.Linesscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Lines., other.Linesscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Lines....)
67 c.Cstylescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Cstyle. = c.Cstylescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Cstyle. || other.Cstylescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Cstyle.
68 }
69
70 func (c Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#) hasTextOnLinescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#hasTextOnLine().(line int) bool {
71 if len(c.Linesscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Lines.) == 0 {
72 return false
73 }
74 return c.Positionscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Position..Linescip-go gomod github.com/golang/go/src go1.12 `text/scanner`/Position#Line. <= line && line <= c.Positionscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Position..Linescip-go gomod github.com/golang/go/src go1.12 `text/scanner`/Position#Line.+len(c.Linesscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Lines.)-1
75 }
76
77 // Message returns the first line or empty if no lines.
78 func (c Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#) Messagescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Message().() string {
79 if len(c.Linesscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Lines.) == 0 {
80 return ""
81 }
82 return c.Linesscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Lines.[0]
83 }
84
85 // commentInliner is for types that can have an inline comment.
86 type commentInlinerscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/commentInliner# interface {
87 inlineCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/commentInliner#inlineComment.(c *Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#)
88 }
89
90 // maybeScanInlineComment tries to scan comment on the current line ; if present then set it for the last element added.
91 func maybeScanInlineCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/maybeScanInlineComment().(p *Parserscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Parser#, c elementContainerscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/elementContainer#) {
92 currentPos := p.scannerscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Parser#scanner..Positionscip-go gomod github.com/golang/go/src go1.12 `text/scanner`/Scanner#Position.
93 // see if there is an inline Comment
94 pos, tok, lit := p.nextscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Parser#next().()
95 esize := len(c.elementsscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/elementContainer#elements.())
96 // seen comment and on same line and elements have been added
97 if tCOMMENTscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/tCOMMENT. == tok && pos.Linescip-go gomod github.com/golang/go/src go1.12 `text/scanner`/Position#Line. == currentPos.Linescip-go gomod github.com/golang/go/src go1.12 `text/scanner`/Position#Line. && esize > 0 {
98 // if the last added element can have an inline comment then set it
99 last := c.elementsscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/elementContainer#elements.()[esize-1]
100 if inliner, ok := last.(commentInlinerscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/commentInliner#); ok {
101 // TODO skip multiline?
102 inliner.inlineCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/commentInliner#inlineComment.(newCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/newComment().(pos, lit))
103 }
104 } else {
105 p.nextPutscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Parser#nextPut().(pos, tok, lit)
106 }
107 }
108
109 // takeLastCommentIfEndsOnLine removes and returns the last element of the list if it is a Comment
110 func takeLastCommentIfEndsOnLinescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/takeLastCommentIfEndsOnLine().(list []Visiteescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Visitee#, line int) (*Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#, []Visiteescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Visitee#) {
111 if len(list) == 0 {
112 return nil, list
113 }
114 if last, ok := list[len(list)-1].(*Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#); ok && last.hasTextOnLinescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#hasTextOnLine().(line) {
115 return last, list[:len(list)-1]
116 }
117 return nil, list
118 }
119
120 // mergeOrReturnComment creates a new comment and tries to merge it with the last element (if is a comment and is on the next line).
121 func mergeOrReturnCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/mergeOrReturnComment().(elements []Visiteescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Visitee#, lit string, pos scannerscip-go gomod github.com/golang/go/src go1.12 `text/scanner`/.Positionscip-go gomod github.com/golang/go/src go1.12 `text/scanner`/Position#) *Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment# {
122 com := newCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/newComment().(pos, lit)
123 esize := len(elements)
124 if esize == 0 {
125 return com
126 }
127 // last element must be a comment to merge
128 last, ok := elements[esize-1].(*Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#)
129 if !ok {
130 return com
131 }
132 // do not merge c-style comments
133 if last.Cstylescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Cstyle. {
134 return com
135 }
136 // last comment has text on previous line
137 // TODO handle last line of file could be inline comment
138 if !last.hasTextOnLinescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#hasTextOnLine().(pos.Linescip-go gomod github.com/golang/go/src go1.12 `text/scanner`/Position#Line. - 1) {
139 return com
140 }
141 last.Mergescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#Merge().(com)
142 return nil
143 }
144
145 // parent is part of elementContainer
146 func (c *Commentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#) parentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Comment#parent().(Visiteescip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Visitee#) {}
147
148 // consumeCommentFor is for reading and taking all comment lines before the body of an element (starting at {)
149 func consumeCommentForscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/consumeCommentFor().(p *Parserscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Parser#, e elementContainerscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/elementContainer#) {
150 pos, tok, lit := p.nextscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Parser#next().()
151 if tok == tCOMMENTscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/tCOMMENT. {
152 if com := mergeOrReturnCommentscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/mergeOrReturnComment().(e.elementsscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/elementContainer#elements.(), lit, pos); com != nil { // not merged?
153 e.addElementscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/elementContainer#addElement.(com)
154 }
155 consumeCommentForscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/consumeCommentFor().(p, e) // bit of recursion is fine
156 } else {
157 p.nextPutscip-go gomod github.com/emicklei/proto . `github.com/emicklei/proto`/Parser#nextPut().(pos, tok, lit)
158 }
159 }