Repository navigation
Expand file tree
/
Copy pathrust_lexer.go
More file actions
223 lines (173 loc) · 6.05 KB
/
Copy pathrust_lexer.go
File metadata and controls
223 lines (173 loc) · 6.05 KB
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
package main
import (
"bytes"
"strings"
"unicode/utf8"
)
type rustLexicalState struct {
insideString bool
insideRawString bool
rawStringHashes int
blockCommentDepth int
}
func (state *rustLexicalState) isInsideLiteral() bool {
return state.insideString || state.insideRawString
}
// Strings, raw strings, and character literals become a placeholder operand followed by spaces, and
// comments become spaces, so structural scans see only code. Lifetimes and labels are kept.
func (state *rustLexicalState) maskLiteralsAndComments(sourceLine string) string {
if !state.isInsideLiteral() && state.blockCommentDepth == 0 && !strings.ContainsAny(sourceLine, "\"'/") {
return sourceLine
}
code := []byte(sourceLine)
for characterIndex := 0; characterIndex < len(code); characterIndex++ {
switch {
case state.blockCommentDepth > 0:
characterIndex = state.maskBlockComment(code, characterIndex)
case state.insideString:
characterIndex = state.maskString(code, characterIndex)
case state.insideRawString:
characterIndex = state.maskRawString(code, characterIndex)
default:
character := code[characterIndex]
switch {
case character == '/' && characterIndex+1 < len(code) && code[characterIndex+1] == '/':
return string(code[:characterIndex])
case character == '/' && characterIndex+1 < len(code) && code[characterIndex+1] == '*':
state.blockCommentDepth = 1
code[characterIndex] = ' '
code[characterIndex+1] = ' '
characterIndex = state.maskBlockComment(code, characterIndex+2)
case character == '"':
state.insideString = true
code[characterIndex] = '0'
characterIndex = state.maskString(code, characterIndex+1)
case character == '\'':
characterIndex = maskRustCharacterLiteral(code, characterIndex)
case character == 'r' && rustStartsRawString(code, characterIndex):
hashCount := 0
for code[characterIndex+1+hashCount] == '#' {
hashCount++
}
state.insideRawString = true
state.rawStringHashes = hashCount
code[characterIndex] = '0'
for maskedIndex := characterIndex + 1; maskedIndex <= characterIndex+1+hashCount; maskedIndex++ {
code[maskedIndex] = ' '
}
characterIndex = state.maskRawString(code, characterIndex+2+hashCount)
}
}
}
return string(code)
}
// Each mask method blanks code from start through the literal's or comment's end and returns the
// index of its final character, leaving the state open when the line ends first.
func (state *rustLexicalState) maskBlockComment(code []byte, start int) int {
characterIndex := start
for ; characterIndex < len(code); characterIndex++ {
pairStartsHere := characterIndex+1 < len(code)
switch {
case pairStartsHere && code[characterIndex] == '/' && code[characterIndex+1] == '*':
state.blockCommentDepth++
case pairStartsHere && code[characterIndex] == '*' && code[characterIndex+1] == '/':
state.blockCommentDepth--
default:
code[characterIndex] = ' '
continue
}
code[characterIndex] = ' '
code[characterIndex+1] = ' '
characterIndex++
if state.blockCommentDepth == 0 {
return characterIndex
}
}
return characterIndex
}
func (state *rustLexicalState) maskString(code []byte, start int) int {
characterIndex := start
for ; characterIndex < len(code); characterIndex++ {
character := code[characterIndex]
code[characterIndex] = ' '
if character == '\\' && characterIndex+1 < len(code) {
code[characterIndex+1] = ' '
characterIndex++
} else if character == '"' {
state.insideString = false
return characterIndex
}
}
return characterIndex
}
func (state *rustLexicalState) maskRawString(code []byte, start int) int {
characterIndex := start
for ; characterIndex < len(code); characterIndex++ {
closesString := code[characterIndex] == '"' && rustHashesFollow(code, characterIndex+1, state.rawStringHashes)
code[characterIndex] = ' '
if closesString {
for maskedIndex := characterIndex + 1; maskedIndex <= characterIndex+state.rawStringHashes; maskedIndex++ {
code[maskedIndex] = ' '
}
state.insideRawString = false
return characterIndex + state.rawStringHashes
}
}
return characterIndex
}
func rustHashesFollow(code []byte, start int, hashCount int) bool {
if start+hashCount > len(code) {
return false
}
for _, character := range code[start : start+hashCount] {
if character != '#' {
return false
}
}
return true
}
// A quote begins a character literal when an escape or a single character and a closing quote
// follow it; otherwise it begins a lifetime or label, which stays visible.
func maskRustCharacterLiteral(code []byte, start int) int {
literalEnd := -1
switch {
case start+3 < len(code) && code[start+1] == '\\':
escapeEnd := min(len(code), start+3+len(`u{10FFFF}`))
if closingOffset := bytes.IndexByte(code[start+3:escapeEnd], '\''); closingOffset >= 0 {
literalEnd = start + 3 + closingOffset
}
case start+1 < len(code):
_, characterWidth := utf8.DecodeRune(code[start+1:])
if closingIndex := start + 1 + characterWidth; closingIndex < len(code) && code[closingIndex] == '\'' {
literalEnd = closingIndex
}
}
if literalEnd < 0 {
return start
}
code[start] = '0'
for maskedIndex := start + 1; maskedIndex <= literalEnd; maskedIndex++ {
code[maskedIndex] = ' '
}
return literalEnd
}
// Raw strings begin with r, br, or cr outside an identifier, then any number of hashes and a quote.
// The same prefix without a quote is a raw identifier.
func rustStartsRawString(code []byte, rawPrefixIndex int) bool {
tokenStart := rawPrefixIndex
if tokenStart > 0 && (code[tokenStart-1] == 'b' || code[tokenStart-1] == 'c') {
tokenStart--
}
if tokenStart > 0 && isRustIdentifierCharacter(code[tokenStart-1]) {
return false
}
hashEnd := rawPrefixIndex + 1
for hashEnd < len(code) && code[hashEnd] == '#' {
hashEnd++
}
return hashEnd < len(code) && code[hashEnd] == '"'
}
func isRustIdentifierCharacter(character byte) bool {
return character == '_' || character >= 0x80 || ('0' <= character && character <= '9') ||
('a' <= character && character <= 'z') || ('A' <= character && character <= 'Z')
}