Debian Codegraph @

ast/comments.go

raw symbols SCIP
1 package ast
2
3 import (
4 "fmt"
5
6 "github.com/robertkrimen/otto/file"
7 )
8
9 // CommentPosition determines where the comment is in a given context.
10 type CommentPosition int
11
12 // Available comment positions.
13 const (
14 _ CommentPosition = iota
15 // LEADING is before the pertinent expression.
16 LEADING
17 // TRAILING is after the pertinent expression.
18 TRAILING
19 // KEY is before a key in an object.
20 KEY
21 // COLON is after a colon in a field declaration.
22 COLON
23 // FINAL is the final comments in a block, not belonging to a specific expression or the comment after a trailing , in an array or object literal.
24 FINAL
25 // IF is after an if keyword.
26 IF
27 // WHILE is after a while keyword.
28 WHILE
29 // DO is after do keyword.
30 DO
31 // FOR is after a for keyword.
32 FOR
33 // WITH is after a with keyword.
34 WITH
35 // TBD is unknown.
36 TBD
37 )
38
39 // Comment contains the data of the comment.
40 type Comment struct {
41 Text string
42 Begin file.Idx
43 Position CommentPosition
44 }
45
46 // NewComment creates a new comment.
47 func NewComment(text string, idx file.Idx) *Comment {
48 comment := &Comment{
49 Begin: idx,
50 Text: text,
51 Position: TBD,
52 }
53
54 return comment
55 }
56
57 // String returns a stringified version of the position.
58 func (cp CommentPosition) String() string {
59 switch cp {
60 case LEADING:
61 return "Leading"
62 case TRAILING:
63 return "Trailing"
64 case KEY:
65 return "Key"
66 case COLON:
67 return "Colon"
68 case FINAL:
69 return "Final"
70 case IF:
71 return "If"
72 case WHILE:
73 return "While"
74 case DO:
75 return "Do"
76 case FOR:
77 return "For"
78 case WITH:
79 return "With"
80 default:
81 return "???"
82 }
83 }
84
85 // String returns a stringified version of the comment.
86 func (c Comment) String() string {
87 return fmt.Sprintf("Comment: %v", c.Text)
88 }
89
90 // Comments defines the current view of comments from the parser.
91 type Comments struct {
92 // CommentMap is a reference to the parser comment map
93 CommentMap CommentMap
94 // Comments lists the comments scanned, not linked to a node yet
95 Comments []*Comment
96 // Current is node for which comments are linked to
97 Current Expression
98
99 // future lists the comments after a line break during a sequence of comments
100 future []*Comment
101 // wasLineBreak determines if a line break occurred while scanning for comments
102 wasLineBreak bool
103 // primary determines whether or not processing a primary expression
104 primary bool
105 // afterBlock determines whether or not being after a block statement
106 afterBlock bool
107 }
108
109 // NewComments returns a new Comments.
110 func NewComments() *Comments {
111 comments := &Comments{
112 CommentMap: CommentMap{},
113 }
114
115 return comments
116 }
117
118 func (c *Comments) String() string {
119 return fmt.Sprintf("NODE: %v, Comments: %v, Future: %v(LINEBREAK:%v)", c.Current, len(c.Comments), len(c.future), c.wasLineBreak)
120 }
121
122 // FetchAll returns all the currently scanned comments,
123 // including those from the next line.
124 func (c *Comments) FetchAll() []*Comment {
125 defer func() {
126 c.Comments = nil
127 c.future = nil
128 }()
129
130 return append(c.Comments, c.future...)
131 }
132
133 // Fetch returns all the currently scanned comments.
134 func (c *Comments) Fetch() []*Comment {
135 defer func() {
136 c.Comments = nil
137 }()
138
139 return c.Comments
140 }
141
142 // ResetLineBreak marks the beginning of a new statement.
143 func (c *Comments) ResetLineBreak() {
144 c.wasLineBreak = false
145 }
146
147 // MarkPrimary will mark the context as processing a primary expression.
148 func (c *Comments) MarkPrimary() {
149 c.primary = true
150 c.wasLineBreak = false
151 }
152
153 // AfterBlock will mark the context as being after a block.
154 func (c *Comments) AfterBlock() {
155 c.afterBlock = true
156 }
157
158 // AddComment adds a comment to the view.
159 // Depending on the context, comments are added normally or as post line break.
160 func (c *Comments) AddComment(comment *Comment) {
161 if c.primary {
162 if !c.wasLineBreak {
163 c.Comments = append(c.Comments, comment)
164 } else {
165 c.future = append(c.future, comment)
166 }
167 } else {
168 if !c.wasLineBreak || (c.Current == nil && !c.afterBlock) {
169 c.Comments = append(c.Comments, comment)
170 } else {
171 c.future = append(c.future, comment)
172 }
173 }
174 }
175
176 // MarkComments will mark the found comments as the given position.
177 func (c *Comments) MarkComments(position CommentPosition) {
178 for _, comment := range c.Comments {
179 if comment.Position == TBD {
180 comment.Position = position
181 }
182 }
183 for _, c := range c.future {
184 if c.Position == TBD {
185 c.Position = position
186 }
187 }
188 }
189
190 // Unset the current node and apply the comments to the current expression.
191 // Resets context variables.
192 func (c *Comments) Unset() {
193 if c.Current != nil {
194 c.applyComments(c.Current, c.Current, TRAILING)
195 c.Current = nil
196 }
197 c.wasLineBreak = false
198 c.primary = false
199 c.afterBlock = false
200 }
201
202 // SetExpression sets the current expression.
203 // It is applied the found comments, unless the previous expression has not been unset.
204 // It is skipped if the node is already set or if it is a part of the previous node.
205 func (c *Comments) SetExpression(node Expression) {
206 // Skipping same node
207 if c.Current == node {
208 return
209 }
210 if c.Current != nil && c.Current.Idx1() == node.Idx1() {
211 c.Current = node
212 return
213 }
214 previous := c.Current
215 c.Current = node
216
217 // Apply the found comments and futures to the node and the previous.
218 c.applyComments(node, previous, TRAILING)
219 }
220
221 // PostProcessNode applies all found comments to the given node.
222 func (c *Comments) PostProcessNode(node Node) {
223 c.applyComments(node, nil, TRAILING)
224 }
225
226 // applyComments applies both the comments and the future comments to the given node and the previous one,
227 // based on the context.
228 func (c *Comments) applyComments(node, previous Node, position CommentPosition) {
229 if previous != nil {
230 c.CommentMap.AddComments(previous, c.Comments, position)
231 c.Comments = nil
232 } else {
233 c.CommentMap.AddComments(node, c.Comments, position)
234 c.Comments = nil
235 }
236 // Only apply the future comments to the node if the previous is set.
237 // This is for detecting end of line comments and which node comments on the following lines belongs to
238 if previous != nil {
239 c.CommentMap.AddComments(node, c.future, position)
240 c.future = nil
241 }
242 }
243
244 // AtLineBreak will mark a line break.
245 func (c *Comments) AtLineBreak() {
246 c.wasLineBreak = true
247 }
248
249 // CommentMap is the data structure where all found comments are stored.
250 type CommentMap map[Node][]*Comment
251
252 // AddComment adds a single comment to the map.
253 func (cm CommentMap) AddComment(node Node, comment *Comment) {
254 list := cm[node]
255 list = append(list, comment)
256
257 cm[node] = list
258 }
259
260 // AddComments adds a slice of comments, given a node and an updated position.
261 func (cm CommentMap) AddComments(node Node, comments []*Comment, position CommentPosition) {
262 for _, comment := range comments {
263 if comment.Position == TBD {
264 comment.Position = position
265 }
266 cm.AddComment(node, comment)
267 }
268 }
269
270 // Size returns the size of the map.
271 func (cm CommentMap) Size() int {
272 size := 0
273 for _, comments := range cm {
274 size += len(comments)
275 }
276
277 return size
278 }
279
280 // MoveComments moves comments with a given position from a node to another.
281 func (cm CommentMap) MoveComments(from, to Node, position CommentPosition) {
282 for i, c := range cm[from] {
283 if c.Position == position {
284 cm.AddComment(to, c)
285
286 // Remove the comment from the "from" slice
287 cm[from][i] = cm[from][len(cm[from])-1]
288 cm[from][len(cm[from])-1] = nil
289 cm[from] = cm[from][:len(cm[from])-1]
290 }
291 }
292 }