-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathEnvSpecLexer.kt
More file actions
459 lines (398 loc) · 16.7 KB
/
EnvSpecLexer.kt
File metadata and controls
459 lines (398 loc) · 16.7 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package dev.dmno.envspec
import com.intellij.lexer.LexerBase
import com.intellij.psi.TokenType
import com.intellij.psi.tree.IElementType
/**
* Line-oriented lexer: comments (#…), assignments (optional export, KEY=value), or unparsed line content.
*/
class EnvSpecLexer : LexerBase() {
private var buffer: CharSequence = ""
private var bufferEnd = 0
private var tokenStart = 0
private var tokenEnd = 0
private var tokenType: IElementType? = null
private val queue = ArrayList<QueuedToken>(8)
private var queuePos = 0
private data class QueuedToken(val start: Int, val end: Int, val type: IElementType)
override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
this.buffer = buffer
this.bufferEnd = endOffset
this.tokenEnd = startOffset
this.queue.clear()
this.queuePos = 0
advance()
}
override fun getState(): Int = 0
override fun getTokenType(): IElementType? = tokenType
override fun getTokenStart(): Int = tokenStart
override fun getTokenEnd(): Int = tokenEnd
override fun getBufferSequence(): CharSequence = buffer
override fun getBufferEnd(): Int = bufferEnd
override fun advance() {
tokenStart = tokenEnd
if (pollQueue()) return
queue.clear()
queuePos = 0
if (tokenEnd >= bufferEnd) {
tokenType = null
return
}
val ch = buffer[tokenEnd]
if (ch == '\r' || ch == '\n') {
emitNewlineAt(tokenEnd)
return
}
val lineStart = findLineStart(tokenEnd)
val lineEnd = findLineEnd(lineStart)
enqueueLineTokens(lineStart, lineEnd)
trimQueuedTokensBefore(tokenEnd)
enqueueNewlineAfterLine(lineEnd)
trimQueuedTokensBefore(tokenEnd)
if (!pollQueue()) {
tokenType = null
}
}
private fun trimQueuedTokensBefore(offset: Int) {
if (queue.isEmpty()) return
val trimmed = ArrayList<QueuedToken>(queue.size)
for (token in queue) {
if (token.end <= offset) continue
if (token.start < offset) {
trimmed.add(QueuedToken(offset, token.end, token.type))
} else {
trimmed.add(token)
}
}
queue.clear()
queue.addAll(trimmed)
queuePos = 0
}
private fun findLineStart(from: Int): Int {
var i = from
while (i > 0) {
val prev = buffer[i - 1]
if (prev == '\n' || prev == '\r') break
i--
}
return i
}
private fun pollQueue(): Boolean {
if (queuePos >= queue.size) return false
val t = queue[queuePos++]
tokenStart = t.start
tokenEnd = t.end
tokenType = t.type
return true
}
private fun findLineEnd(from: Int): Int {
var i = from
while (i < bufferEnd && buffer[i] != '\n' && buffer[i] != '\r') i++
return i
}
private fun enqueueNewlineAfterLine(lineEnd: Int) {
if (lineEnd >= bufferEnd) return
var end = lineEnd
if (buffer[end] == '\r') {
end++
if (end < bufferEnd && buffer[end] == '\n') end++
} else if (buffer[end] == '\n') {
end++
}
queue.add(QueuedToken(lineEnd, end, EnvSpecTokenTypes.NEWLINE))
}
private fun emitNewlineAt(from: Int) {
var end = from
if (end < bufferEnd && buffer[end] == '\r') {
end++
if (end < bufferEnd && buffer[end] == '\n') end++
} else if (end < bufferEnd && buffer[end] == '\n') {
end++
}
tokenStart = from
tokenEnd = end
tokenType = EnvSpecTokenTypes.NEWLINE
}
private fun enqueueLineTokens(lineStart: Int, lineEnd: Int) {
if (lineStart >= lineEnd) return
val lineStr = buffer.subSequence(lineStart, lineEnd).toString()
if (lineStr.isBlank()) {
queue.add(QueuedToken(lineStart, lineEnd, TokenType.WHITE_SPACE))
return
}
val comment = COMMENT_LINE.matchEntire(lineStr)
if (comment != null) {
val ws = comment.groupValues[1]
val wsLen = ws.length
if (wsLen > 0) {
queue.add(QueuedToken(lineStart, lineStart + wsLen, TokenType.WHITE_SPACE))
}
enqueueCommentTokens(lineStart + wsLen, lineEnd)
return
}
val assign = ASSIGNMENT_LINE.matchEntire(lineStr)
if (assign != null) {
val wsLead = assign.groups[1]!!.range
val export = assign.groups[2]
val key = assign.groups[3]!!.range
val wsMid = assign.groups[4]!!.range
val eq = assign.groups[5]!!.range
val value = assign.groups[6]!!.range
if (!wsLead.isEmpty()) {
queue.add(QueuedToken(lineStart + wsLead.first, lineStart + wsLead.last + 1, TokenType.WHITE_SPACE))
}
if (export != null) {
val r = export.range
queue.add(QueuedToken(lineStart + r.first, lineStart + r.last + 1, EnvSpecTokenTypes.EXPORT_KEYWORD))
}
queue.add(QueuedToken(lineStart + key.first, lineStart + key.last + 1, EnvSpecTokenTypes.ENV_KEY))
if (!wsMid.isEmpty()) {
queue.add(QueuedToken(lineStart + wsMid.first, lineStart + wsMid.last + 1, TokenType.WHITE_SPACE))
}
queue.add(QueuedToken(lineStart + eq.first, lineStart + eq.last + 1, EnvSpecTokenTypes.EQUALS))
if (!value.isEmpty()) {
val valueStart = lineStart + value.first
val valueText = lineStr.substring(value.first, value.last + 1)
enqueueAssignmentValueTokens(valueStart, valueText)
}
return
}
queue.add(QueuedToken(lineStart, lineEnd, EnvSpecTokenTypes.LINE_CONTENT))
}
private fun enqueueCommentTokens(start: Int, end: Int) {
if (start >= end) return
val text = buffer.subSequence(start, end).toString()
var i = 0
var plainStart = 0
while (i < text.length) {
val atIndex = text.indexOf('@', i)
if (atIndex < 0) break
if (atIndex + 1 >= text.length || !isDecoratorNameStart(text[atIndex + 1])) {
i = atIndex + 1
continue
}
if (atIndex > plainStart) {
queue.add(QueuedToken(start + plainStart, start + atIndex, EnvSpecTokenTypes.LINE_COMMENT))
}
var nameEnd = atIndex + 2
while (nameEnd < text.length && isDecoratorNamePart(text[nameEnd])) nameEnd++
queue.add(QueuedToken(start + atIndex, start + nameEnd, EnvSpecTokenTypes.DECORATOR))
i = nameEnd
plainStart = i
if (i < text.length && text[i] == '=') {
queue.add(QueuedToken(start + i, start + i + 1, EnvSpecTokenTypes.EQUALS))
i++
val valueStart = i
if (i < text.length && isDecoratorValueNameStart(text[i])) {
var valueEnd = i + 1
while (valueEnd < text.length && isDecoratorValueNamePart(text[valueEnd])) valueEnd++
queue.add(QueuedToken(start + i, start + valueEnd, EnvSpecTokenTypes.DECORATOR_VALUE))
i = valueEnd
if (i < text.length && text[i] == '(') {
queue.add(QueuedToken(start + i, start + i + 1, EnvSpecTokenTypes.PAREN_OPEN))
val close = findClosingParen(text, i)
if (close > i + 1) {
enqueueDecoratorArgsTokens(start + i + 1, text.substring(i + 1, close))
}
if (close < text.length) {
queue.add(QueuedToken(start + close, start + close + 1, EnvSpecTokenTypes.PAREN_CLOSE))
i = close + 1
} else {
i = text.length
}
}
} else {
var valueEnd = i
while (valueEnd < text.length && !text[valueEnd].isWhitespace() && text[valueEnd] != '#') valueEnd++
if (valueEnd > valueStart) {
queue.add(QueuedToken(start + valueStart, start + valueEnd, EnvSpecTokenTypes.DECORATOR_VALUE))
}
i = valueEnd
}
plainStart = i
continue
}
if (i < text.length && text[i] == '(') {
queue.add(QueuedToken(start + i, start + i + 1, EnvSpecTokenTypes.PAREN_OPEN))
val close = findClosingParen(text, i)
if (close > i + 1) {
enqueueDecoratorArgsTokens(start + i + 1, text.substring(i + 1, close))
}
if (close < text.length) {
queue.add(QueuedToken(start + close, start + close + 1, EnvSpecTokenTypes.PAREN_CLOSE))
i = close + 1
} else {
i = text.length
}
plainStart = i
continue
}
plainStart = i
}
if (plainStart < text.length) {
queue.add(QueuedToken(start + plainStart, end, EnvSpecTokenTypes.LINE_COMMENT))
}
}
private fun enqueueDecoratorArgsTokens(argsStartOffset: Int, argsText: String) {
if (argsText.isEmpty()) return
var segmentStart = 0
var i = 0
var depth = 0
var quote: Char? = null
while (i < argsText.length) {
val ch = argsText[i]
when {
quote != null -> if (ch == quote) quote = null
ch == '\'' || ch == '"' || ch == '`' -> quote = ch
ch == '(' -> depth++
ch == ')' -> if (depth > 0) depth--
ch == ',' && depth == 0 -> {
enqueueDecoratorArgSegment(argsStartOffset + segmentStart, argsText.substring(segmentStart, i))
queue.add(QueuedToken(argsStartOffset + i, argsStartOffset + i + 1, EnvSpecTokenTypes.EQUALS))
segmentStart = i + 1
}
}
i++
}
if (segmentStart < argsText.length) {
enqueueDecoratorArgSegment(argsStartOffset + segmentStart, argsText.substring(segmentStart))
}
}
private fun enqueueDecoratorArgSegment(segmentStartOffset: Int, segmentText: String) {
if (segmentText.isEmpty()) return
val leadingWs = segmentText.takeWhile { it.isWhitespace() }.length
val trailingWs = segmentText.takeLastWhile { it.isWhitespace() }.length
val coreStart = leadingWs
val coreEnd = segmentText.length - trailingWs
if (leadingWs > 0) {
queue.add(QueuedToken(segmentStartOffset, segmentStartOffset + leadingWs, TokenType.WHITE_SPACE))
}
if (coreStart >= coreEnd) {
return
}
val core = segmentText.substring(coreStart, coreEnd)
val coreAbsStart = segmentStartOffset + coreStart
val eqIdx = findTopLevelEquals(core)
val keyCandidateEnd = if (eqIdx > 0) core.substring(0, eqIdx).trimEnd().length else -1
val hasKeyValueShape = keyCandidateEnd > 0 &&
core.substring(0, keyCandidateEnd).matches(Regex("""[A-Za-z][A-Za-z0-9_-]*"""))
if (!hasKeyValueShape) {
queue.add(QueuedToken(coreAbsStart, coreAbsStart + core.length, EnvSpecTokenTypes.DECORATOR_ARG_VALUE))
} else {
queue.add(QueuedToken(coreAbsStart, coreAbsStart + keyCandidateEnd, EnvSpecTokenTypes.DECORATOR_ARG_KEY))
var cursor = keyCandidateEnd
while (cursor < eqIdx) {
queue.add(QueuedToken(coreAbsStart + cursor, coreAbsStart + cursor + 1, TokenType.WHITE_SPACE))
cursor++
}
queue.add(QueuedToken(coreAbsStart + eqIdx, coreAbsStart + eqIdx + 1, EnvSpecTokenTypes.EQUALS))
if (eqIdx + 1 < core.length) {
queue.add(QueuedToken(coreAbsStart + eqIdx + 1, coreAbsStart + core.length, EnvSpecTokenTypes.DECORATOR_ARG_VALUE))
}
}
if (trailingWs > 0) {
queue.add(QueuedToken(segmentStartOffset + coreEnd, segmentStartOffset + segmentText.length, TokenType.WHITE_SPACE))
}
}
private fun findTopLevelEquals(text: String): Int {
var depth = 0
var quote: Char? = null
for (i in text.indices) {
val ch = text[i]
when {
quote != null -> if (ch == quote) quote = null
ch == '\'' || ch == '"' || ch == '`' -> quote = ch
ch == '(' -> depth++
ch == ')' -> if (depth > 0) depth--
ch == '=' && depth == 0 -> return i
}
}
return -1
}
private fun enqueueAssignmentValueTokens(valueStartOffset: Int, valueText: String) {
var i = 0
var plainStart = 0
var quote: Char? = null
while (i < valueText.length) {
val ch = valueText[i]
if (quote != null) {
if (ch == quote) quote = null
i++
continue
}
if (ch == '"' || ch == '\'' || ch == '`') {
quote = ch
i++
continue
}
if (ch == '$') {
val refEnd = findReferenceEnd(valueText, i)
if (refEnd > i + 1) {
if (i > plainStart) {
queue.add(QueuedToken(valueStartOffset + plainStart, valueStartOffset + i, EnvSpecTokenTypes.ENV_VALUE))
}
queue.add(QueuedToken(valueStartOffset + i, valueStartOffset + refEnd, EnvSpecTokenTypes.VALUE_REFERENCE))
i = refEnd
plainStart = i
continue
}
}
if (isValueFunctionNameStart(ch)) {
val nameStart = i
var nameEnd = i + 1
while (nameEnd < valueText.length && isDecoratorValueNamePart(valueText[nameEnd])) nameEnd++
if (nameEnd < valueText.length && valueText[nameEnd] == '(') {
if (nameStart > plainStart) {
queue.add(QueuedToken(valueStartOffset + plainStart, valueStartOffset + nameStart, EnvSpecTokenTypes.ENV_VALUE))
}
queue.add(QueuedToken(valueStartOffset + nameStart, valueStartOffset + nameEnd, EnvSpecTokenTypes.VALUE_FUNCTION))
i = nameEnd
plainStart = i
continue
}
}
i++
}
if (plainStart < valueText.length) {
queue.add(QueuedToken(valueStartOffset + plainStart, valueStartOffset + valueText.length, EnvSpecTokenTypes.ENV_VALUE))
}
}
private fun findReferenceEnd(text: String, dollarIndex: Int): Int {
if (dollarIndex + 1 >= text.length) return dollarIndex + 1
if (text[dollarIndex + 1] == '{') {
var i = dollarIndex + 2
while (i < text.length && text[i] != '}') i++
return if (i < text.length) i + 1 else dollarIndex + 1
}
var i = dollarIndex + 1
if (!isRefNameStart(text[i])) return dollarIndex + 1
i++
while (i < text.length && isRefNamePart(text[i])) i++
return i
}
private fun findClosingParen(text: String, openIndex: Int): Int {
var depth = 0
var i = openIndex
while (i < text.length) {
when (text[i]) {
'(' -> depth++
')' -> {
depth--
if (depth == 0) return i
}
}
i++
}
return text.length
}
private fun isDecoratorNameStart(ch: Char): Boolean = ch.isLetter()
private fun isDecoratorNamePart(ch: Char): Boolean = ch.isLetterOrDigit() || ch == '_' || ch == '-'
private fun isDecoratorValueNameStart(ch: Char): Boolean = ch.isLetter()
private fun isDecoratorValueNamePart(ch: Char): Boolean = ch.isLetterOrDigit() || ch == '_' || ch == '-'
private fun isValueFunctionNameStart(ch: Char): Boolean = ch.isLetter()
private fun isRefNameStart(ch: Char): Boolean = ch.isLetter() || ch == '_'
private fun isRefNamePart(ch: Char): Boolean = ch.isLetterOrDigit() || ch == '_'
companion object {
private val COMMENT_LINE = Regex("""^([ \t]*)(#.*)$""")
private val ASSIGNMENT_LINE = Regex("""^([ \t]*)(export[ \t]+)?([A-Za-z_][\w.-]*)([ \t]*)(=)(.*)$""")
}
}