forked from google/licenseclassifier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
comment_parser.go
380 lines (346 loc) · 9.15 KB
/
comment_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package commentparser does a basic parse over a source file and returns all
// of the comments from the code. This is useful for when you want to analyze
// text written in comments (like copyright notices) but not in the code
// itself.
package commentparser
import (
"bytes"
"strings"
"unicode/utf8"
"github.com/jfrog/licenseclassifier/commentparser/language"
)
const (
eofInString = "%d:EOF in string"
eofInSingleLineComment = "%d:EOF in single line comment"
eofInMultilineComment = "%d:EOF in multiline comment"
)
// Parse parses the input data and returns the comments.
func Parse(contents []byte, lang language.Language) Comments {
if len(contents) == 0 {
return nil
}
c := string(contents)
if !strings.HasSuffix(c, "\n") {
// Force a terminating newline if one isn't present.
c += "\n"
}
i := &input{
s: c,
lang: lang,
offset: 0,
pos: position{line: 1, lineRune: []int{0}},
}
i.lex()
return i.comments
}
// Comment is either a single line or multiline comment in a source code file.
// A single line comment has StartLine equal to EndLine. The lines are 1-based.
type Comment struct {
StartLine int
EndLine int
Text string
}
// Comments allows us to treat a slice of comments as a unit.
type Comments []*Comment
// ChunkIterator returns a read-only channel and generates the comments in a
// goroutine, then closes the channel.
func (c Comments) ChunkIterator() <-chan Comments {
ch := make(chan Comments)
go func() {
defer close(ch)
if len(c) == 0 {
return
}
prevChunk := c[0]
for index := 0; index < len(c); index++ {
var chunk Comments
for ; index < len(c); index++ {
if c[index].StartLine > prevChunk.StartLine+1 {
break
}
if c[index].StartLine == prevChunk.StartLine+2 {
if c[index].StartLine != c[index].EndLine || prevChunk.StartLine != prevChunk.EndLine {
break
}
}
chunk = append(chunk, c[index])
prevChunk = c[index]
}
if len(chunk) == 0 {
break
}
ch <- chunk
if index >= len(c) {
break
}
prevChunk = c[index]
index--
}
}()
return ch
}
// StartLine is the line number (1-based) the first part of the comment block
// starts on.
func (c Comments) StartLine() int {
if len(c) == 0 {
return 0
}
return c[0].StartLine
}
// String creates a string out of the text of the comments. Comment begin and
// end markers are removed.
func (c Comments) String() string {
var s []string
for _, cmt := range c {
s = append(s, cmt.Text)
}
return strings.Join(s, "\n")
}
// position records the location of a lexeme.
type position struct {
line int // Line number of input: 1-based
lineRune []int // Rune offset from beginning of line: 0-based
}
// input holds the current state of the lexer.
type input struct {
s string // Entire input.
lang language.Language // Source code language.
offset int // Offset into input.
pos position // Current position in the input.
comments Comments // Comments in the source file.
}
// lex is called to obtain the comments.
func (i *input) lex() {
for {
c, ok := i.peekRune()
if !ok {
break
}
switch c {
case '"', '\'', '`': // String
// Ignore strings because they could contain comment
// start or end sequences which we need to ignore.
if i.lang == language.HTML {
// Quotes in HTML-like files aren't meaningful,
// because it's basically plain text
break
}
ok, hasEscape := i.lang.QuoteCharacter(c)
if !ok {
break
}
var content bytes.Buffer
isDocString := false
quote := string(c)
if i.lang == language.Python {
if c == '\'' && i.match("'''") {
quote = "'''"
// Assume module-level docstrings start at the
// beginning of a line. Function docstrings not
// supported.
if i.pos.lineRune[len(i.pos.lineRune)-1] == 3 {
isDocString = true
}
} else if c == '"' && i.match(`"""`) {
quote = `"""`
if i.pos.lineRune[len(i.pos.lineRune)-1] == 3 {
isDocString = true
}
} else {
i.readRune() // Eat quote.
}
} else {
i.readRune() // Eat quote.
}
startLine := i.pos.line
for {
c, ok = i.peekRune()
if !ok {
return
}
if hasEscape && c == '\\' {
i.readRune() // Eat escape.
} else if i.match(quote) {
break
} else if (i.lang == language.JavaScript || i.lang == language.Perl) && c == '\n' {
// JavaScript and Perl allow you to
// specify regexes without quotes, but
// which contain quotes. So treat the
// newline as terminating the string.
break
}
c := i.readRune()
if isDocString {
content.WriteRune(c)
}
if i.eof() {
return
}
}
if isDocString {
i.comments = append(i.comments, &Comment{
StartLine: startLine,
EndLine: i.pos.line,
Text: content.String(),
})
}
default:
startLine := i.pos.line
var comment bytes.Buffer
if ok, start, end := i.multiLineComment(); ok { // Multiline comment
nesting := 0
startLine := i.pos.line
for {
if i.eof() {
return
}
c := i.readRune()
comment.WriteRune(c)
if i.lang.NestedComments() && i.match(start) {
// Allows nested comments.
comment.WriteString(start)
nesting++
}
if i.match(end) {
if nesting > 0 {
comment.WriteString(end)
nesting--
} else {
break
}
}
}
i.comments = append(i.comments, &Comment{
StartLine: startLine,
EndLine: i.pos.line,
Text: comment.String(),
})
} else if i.singleLineComment() { // Single line comment
for {
if i.eof() {
return
}
c = i.readRune()
if c == '\n' {
i.unreadRune(c)
break
}
comment.WriteRune(c)
}
i.comments = append(i.comments, &Comment{
StartLine: startLine,
EndLine: i.pos.line,
Text: comment.String(),
})
}
}
i.readRune() // Ignore non-comments.
}
}
// singleLineComment returns 'true' if we've run across a single line comment
// in the given language.
func (i *input) singleLineComment() bool {
if i.match(i.lang.SingleLineCommentStart()) {
return true
}
if i.lang == language.SQL {
return i.match(language.MySQL.SingleLineCommentStart())
} else if i.lang == language.ObjectiveC {
return i.match(language.Matlab.SingleLineCommentStart())
}
return false
}
// multiLineComment returns 'true' if we've run across a multiline comment in
// the given language.
func (i *input) multiLineComment() (bool, string, string) {
if s := i.lang.MultilineCommentStart(); i.match(s) {
return true, s, i.lang.MultilineCommentEnd()
}
if i.lang == language.SQL {
if s := language.MySQL.MultilineCommentStart(); i.match(s) {
return true, s, language.MySQL.MultilineCommentEnd()
}
} else if i.lang == language.ObjectiveC {
if s := language.Matlab.MultilineCommentStart(); i.match(s) {
return true, s, language.Matlab.MultilineCommentEnd()
}
}
return false, "", ""
}
// match returns 'true' if the next tokens in the stream match the given
// string.
func (i *input) match(s string) bool {
if s == "" {
return false
}
saved := s
var read []rune
for len(s) > 0 && !i.eof() {
r, size := utf8.DecodeRuneInString(s)
if c, ok := i.peekRune(); ok && c == r {
read = append(read, c)
} else {
// No match. Push the tokens we read back onto the stack.
for idx := len(read) - 1; idx >= 0; idx-- {
i.unreadRune(read[idx])
}
return false
}
s = s[size:]
i.readRune() // Eat token.
}
return string(read) == saved
}
// eof reports whether the input has reached the end of the file.
func (i *input) eof() bool {
return len(i.s) <= i.offset
}
// peekRune returns the next rune in the input without consuming it.
func (i *input) peekRune() (rune, bool) {
if i.eof() {
return rune(0), false
}
r, _ := utf8.DecodeRuneInString(i.s[i.offset:])
return r, true
}
// readRune consumes and returns the next rune in the input.
func (i *input) readRune() rune {
r, size := utf8.DecodeRuneInString(i.s[i.offset:])
if r == '\n' {
i.pos.line++
i.pos.lineRune = append(i.pos.lineRune, 0)
} else {
i.pos.lineRune[len(i.pos.lineRune)-1]++
}
i.offset += size
return r
}
// unreadRune winds the lexer's state back to before the rune was read.
func (i *input) unreadRune(c rune) {
p := make([]byte, utf8.UTFMax)
size := utf8.EncodeRune(p, c)
i.offset -= size
if c == '\n' {
i.pos.line--
if len(i.pos.lineRune) > 1 {
i.pos.lineRune = i.pos.lineRune[:len(i.pos.lineRune)-1]
} else {
i.pos.lineRune[len(i.pos.lineRune)-1] = 0
}
} else {
i.pos.lineRune[len(i.pos.lineRune)-1]--
}
}