@@ -3,11 +3,13 @@ package io.github.cybercodernaj.parkour.lexer
3
3
import arrow.core.None
4
4
import arrow.core.Option
5
5
import arrow.core.raise.OptionRaise
6
- import arrow.core.raise.ensureNotNull
7
6
import arrow.core.raise.option
7
+ import arrow.fx.coroutines.parMap
8
+ import arrow.fx.coroutines.parZip
8
9
import io.github.cybercodernaj.parkour.datasource.TextSource
9
10
import io.github.cybercodernaj.parkour.exceptions.LexicalException
10
11
import io.github.cybercodernaj.parkour.utils.Position
12
+ import kotlinx.coroutines.runBlocking
11
13
12
14
/* *
13
15
* The lexer is responsible to convert the given string into a stream of [Token]s.
@@ -86,22 +88,46 @@ class Lexer(
86
88
}
87
89
)
88
90
89
- return fetchToken().getOrThrow { LexicalException ( " Could not identify the given token. " ) }
91
+ return fetchToken()
90
92
}
91
93
92
- private fun fetchToken (): Option < Token > = option {
94
+ private fun fetchToken (): Token {
93
95
if (currentLine.isSome { it.isBlank() })
94
- return @option Token .EOF
96
+ return Token .EOF
95
97
96
- val winningToken = identifiers()
97
- winningToken
98
+ fastForwardToContent()
99
+
100
+ val contenders = listOf (identifiers())
101
+ .mapNotNull { it.getOrNull() }
102
+
103
+ return if (contenders.isEmpty())
104
+ throw LexicalException ()
105
+ else {
106
+ val winner = contenders.maxBy { it.end!! - it.start!! }
107
+ position = position.copy(col = winner.end!! .col + 1 )
108
+ winner
109
+ }
110
+ }
111
+
112
+ /* *
113
+ * Continues to move ahead until the [position] does not look at an [ignorePattern],
114
+ * [singleLineComments] or [multilineComments]
115
+ */
116
+ private fun fastForwardToContent () {
117
+ val endOfIgnoredContent = option {
118
+ val match = startsWith(ignorePattern)
119
+ match.range.last
120
+ }
121
+ endOfIgnoredContent.onSome {
122
+ position = position.copy(col = it + 1 )
123
+ }
98
124
}
99
125
100
- private fun OptionRaise. identifiers (): Token .Identifier {
126
+ private fun identifiers (): Option < Token .Identifier > = option {
101
127
val match = startsWith(identifiers)
102
128
103
129
val end = position.copy(col = match.range.last)
104
- return Token .Identifier (match.value, position, end)
130
+ Token .Identifier (match.value, position, end)
105
131
}
106
132
107
133
private fun fetchCurrentLine () {
0 commit comments