@@ -7,6 +7,7 @@ import arrow.core.raise.option
7
7
import io.github.cybercodernaj.parkour.datasource.TextSource
8
8
import io.github.cybercodernaj.parkour.exceptions.LexicalException
9
9
import io.github.cybercodernaj.parkour.utils.Position
10
+ import kotlin.math.sin
10
11
11
12
/* *
12
13
* The lexer is responsible to convert the given string into a stream of [Token]s.
@@ -67,8 +68,6 @@ class Lexer(
67
68
*/
68
69
private var currentLine: Option <String > = None
69
70
70
- private var insideMultilineComment = false
71
-
72
71
private val definitions = StringTrieMap <Kind >()
73
72
74
73
private enum class Kind {
@@ -106,7 +105,7 @@ class Lexer(
106
105
// If the file is fully read, then return EOF
107
106
currentLine.onNone { return Token .EOF }
108
107
109
- val winner = listOf (definitions(), identifiers())
108
+ val winner = listOf (definitions(), literals(), identifiers())
110
109
.mapNotNull { it.getOrNull() }
111
110
.maxByOrNull { it.size }
112
111
@@ -122,36 +121,49 @@ class Lexer(
122
121
* [singleLineComments] or [multilineComments]
123
122
*/
124
123
private fun fastForwardToContent () {
125
- option {
126
- do {
124
+ do {
125
+ val ignoreEnd = option {
127
126
val ignoredMatch = startsWith(ignorePattern)
128
- position = position.copy(col = ignoredMatch.range.last + 1 )
129
- // This loop will break because changing the position can also change currentLine.
130
- } while (true )
131
- }
127
+ position.copy(col = ignoredMatch.range.last + 1 )
128
+ }
129
+ if (ignoreEnd.isSome()) {
130
+ position = ignoreEnd.getOrNull()!!
131
+ continue
132
+ }
132
133
133
- option {
134
- do {
134
+ val singleLineCommentEnd = option {
135
135
startsWith(singleLineComments)
136
136
// If position is pointing at a singleLineComment, then this will continue
137
- position = position.nextLine()
138
- } while (true )
139
- }
137
+ position.nextLine()
138
+ }
139
+ if (singleLineCommentEnd.isSome()) {
140
+ position = singleLineCommentEnd.getOrNull()!!
141
+ continue
142
+ }
140
143
141
- option {
142
- startsWith(multilineComments?.first)
143
- // If position is pointing at the beginning of multiline comment
144
- do {
145
- val line = currentLine.bind()
146
- val endMatch = multilineComments!! .second.find(line, startIndex = position.col)
147
- if (endMatch == null ) {
148
- position = position.nextLine()
149
- } else {
150
- position = position.copy(col = endMatch.range.last + 1 )
151
- break
152
- }
153
- } while (true )
154
- }
144
+ val multilineCommentEnd: Option <Position > = option {
145
+ startsWith(multilineComments?.first)
146
+ // If position is pointing at the beginning of multiline comment
147
+ val end: Position
148
+ do {
149
+ val line = currentLine.bind()
150
+ val endMatch = multilineComments!! .second.find(line, startIndex = position.col)
151
+ if (endMatch == null ) {
152
+ position = position.nextLine()
153
+ } else {
154
+ end = position.copy(col = endMatch.range.last + 1 )
155
+ break
156
+ }
157
+ } while (true )
158
+ end
159
+ }
160
+ if (multilineCommentEnd.isSome()) {
161
+ position = multilineCommentEnd.getOrNull()!!
162
+ continue
163
+ }
164
+
165
+ break
166
+ } while (true )
155
167
}
156
168
157
169
private fun definitions (): Option <Token > = option {
@@ -171,6 +183,28 @@ class Lexer(
171
183
token
172
184
}
173
185
186
+ private fun literals (): Option <Token > = option {
187
+ val floatingLiteral = option {
188
+ val match = startsWith(literals.floatingLiteral)
189
+ val end = position.copy(col = match.range.last)
190
+ val value = match.value.toDoubleOrNull() ? : raise(None )
191
+
192
+ Token .FloatLiteral (value, position, end)
193
+ }
194
+ val intLiteral = option {
195
+ val match = startsWith(literals.integerLiteral)
196
+ val end = position.copy(col = match.range.last)
197
+ val value = match.value.toLongOrNull() ? : raise(None )
198
+
199
+ Token .IntLiteral (value, position, end)
200
+ }
201
+ val stringLiteral = option {
202
+ raise(None )
203
+ }
204
+
205
+ listOf (floatingLiteral, intLiteral, stringLiteral).firstNotNullOfOrNull { it.getOrNull() } ? : raise(None )
206
+ }
207
+
174
208
private fun identifiers (): Option <Token .Identifier > = option {
175
209
val match = startsWith(identifiers)
176
210
0 commit comments