forked from JetBrains/markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdownLexer.kt
83 lines (68 loc) · 2.18 KB
/
MarkdownLexer.kt
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
package org.intellij.markdown.lexer
import org.intellij.markdown.IElementType
import org.intellij.markdown.MarkdownTokenTypes
import java.io.IOException
open class MarkdownLexer(private val baseLexer: GeneratedLexer) {
var type: IElementType? = null
private set
private var nextType: IElementType? = null
var originalText: CharSequence = ""
private set
var bufferStart: Int = 0
private set
var bufferEnd: Int = 0
private set
var tokenStart: Int = 0
private set
var tokenEnd: Int = 0
private set
fun start(originalText: CharSequence,
bufferStart: Int = 0,
bufferEnd: Int = originalText.length) {
this.originalText = originalText
this.bufferStart = bufferStart
this.bufferEnd = bufferEnd
baseLexer.reset(originalText, bufferStart, bufferEnd, 0)
type = advanceBase()
tokenStart = baseLexer.tokenStart
calcNextType()
}
fun advance(): Boolean {
return locateToken()
}
private fun locateToken(): Boolean {
`type` = nextType
tokenStart = tokenEnd
if (`type` == null) {
return false
}
calcNextType()
return true
}
private fun calcNextType() {
do {
tokenEnd = baseLexer.tokenEnd
nextType = advanceBase()
} while (type.let { nextType == it && it != null && it in TOKENS_TO_MERGE })
}
private fun advanceBase(): IElementType? {
try {
return baseLexer.advance()
} catch (e: IOException) {
e.printStackTrace()
throw AssertionError("This could not be!")
}
}
companion object {
private val TOKENS_TO_MERGE = setOf(
MarkdownTokenTypes.TEXT,
MarkdownTokenTypes.WHITE_SPACE,
MarkdownTokenTypes.CODE_LINE,
MarkdownTokenTypes.LINK_ID,
MarkdownTokenTypes.LINK_TITLE,
MarkdownTokenTypes.URL,
MarkdownTokenTypes.AUTOLINK,
MarkdownTokenTypes.EMAIL_AUTOLINK,
MarkdownTokenTypes.BAD_CHARACTER)
}
}