Skip to content

Commit 6396835

Browse files
arrows and skipping over ignorePattern
1 parent ac1f10d commit 6396835

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

core/src/main/kotlin/io/github/cybercodernaj/parkour/lexer/Lexer.kt

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package io.github.cybercodernaj.parkour.lexer
33
import arrow.core.None
44
import arrow.core.Option
55
import arrow.core.raise.OptionRaise
6-
import arrow.core.raise.ensureNotNull
76
import arrow.core.raise.option
7+
import arrow.fx.coroutines.parMap
8+
import arrow.fx.coroutines.parZip
89
import io.github.cybercodernaj.parkour.datasource.TextSource
910
import io.github.cybercodernaj.parkour.exceptions.LexicalException
1011
import io.github.cybercodernaj.parkour.utils.Position
12+
import kotlinx.coroutines.runBlocking
1113

1214
/**
1315
* The lexer is responsible to convert the given string into a stream of [Token]s.
@@ -86,22 +88,46 @@ class Lexer(
8688
}
8789
)
8890

89-
return fetchToken().getOrThrow { LexicalException("Could not identify the given token.") }
91+
return fetchToken()
9092
}
9193

92-
private fun fetchToken(): Option<Token> = option {
94+
private fun fetchToken(): Token {
9395
if (currentLine.isSome { it.isBlank() })
94-
return@option Token.EOF
96+
return Token.EOF
9597

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+
}
98124
}
99125

100-
private fun OptionRaise.identifiers(): Token.Identifier {
126+
private fun identifiers(): Option<Token.Identifier> = option {
101127
val match = startsWith(identifiers)
102128

103129
val end = position.copy(col = match.range.last)
104-
return Token.Identifier(match.value, position, end)
130+
Token.Identifier(match.value, position, end)
105131
}
106132

107133
private fun fetchCurrentLine() {

core/src/main/kotlin/io/github/cybercodernaj/parkour/utils/Position.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ internal data class Position(
3232
operator fun compareTo(end: Position): Int {
3333
return this.col compareTo end.col
3434
}
35+
36+
operator fun minus(other: Position): Int {
37+
return this.col - other.col
38+
}
3539
}
3640

3741
internal operator fun Position.plus(other: Position): Position {

0 commit comments

Comments
 (0)