-
-
Notifications
You must be signed in to change notification settings - Fork 247
/
state_code.go
92 lines (73 loc) · 2.28 KB
/
state_code.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
package processor
type StateCode struct {}
func (state *StateCode) String() string {
return "code"
}
func (state *StateCode) Process(job *FileJob, lang *LanguageFeature, index int, lineType LineType) (int, LineType, State) {
// Hacky fix to https://github.com/boyter/scc/issues/181
endPoint := job.EndPoint
if endPoint > len(job.Content) {
endPoint--
}
var i int
for i = index; i < endPoint; i++ {
curByte := job.Content[i]
if curByte == '\n' {
return i, LINE_CODE, state
}
if isBinary(i, curByte) {
job.Binary = true
return i, LINE_CODE, state
}
if shouldProcess(curByte, lang.ProcessMask) {
if Duplicates {
// Technically this is wrong because we skip bytes so this is not a true
// hash of the file contents, but for duplicate files it shouldn't matter
// as both will skip the same way
digestible := []byte{job.Content[index]}
job.Hash.Write(digestible)
}
switch tokenType, offsetJump, endString := lang.Tokens.Match(job.Content[i:]); tokenType {
case TString:
// If we are in string state then check what sort of string so we know if docstring OR ignoreescape string
// It is safe to -1 here as to enter the code state we need to have
// transitioned from blank to here hence i should always be >= 1
// This check is to ensure we aren't in a character declaration
// TODO this should use language features
if job.Content[i-1] == '\\' {
break // from switch, not from the loop
}
i, docString, skipEsc := verifyIgnoreEscape(lang, job, i)
if docString {
commentType := lineType
if commentType == LINE_BLANK {
commentType = LINE_COMMENT
}
return i, commentType, &StateDocString{
End: endString,
SkipEsc: skipEsc,
}
}
// i += offsetJump - 1
return i, LINE_CODE, &StateString{
End: endString,
SkipEsc: skipEsc,
}
case TSlcomment:
i += offsetJump - 1
return i, LINE_CODE, &StateCommentSingle{}
case TMlcomment:
i += offsetJump - 1
return i, LINE_CODE, NewStateCommentMulti(endString)
case TComplexity:
if i == 0 || isWhitespace(job.Content[i-1]) {
job.Complexity++
}
}
}
}
return i, LINE_CODE, state
}
func (state *StateCode) Reset() (LineType, State) {
return LINE_BLANK, &StateBlank{}
}