mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 21:21:33 +00:00
gradle: 8.4 and parser: lazy tokenization
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
class LazyTokenSource(val tokenizer: Tokenizer) : TokenSource {
|
||||
private val queue = mutableListOf<Token>()
|
||||
private var index = 0
|
||||
override val currentIndex: Int
|
||||
get() = index
|
||||
|
||||
override fun next(): Token {
|
||||
index++
|
||||
if (queue.isNotEmpty()) {
|
||||
return queue.removeFirst()
|
||||
}
|
||||
return tokenizer.next()
|
||||
}
|
||||
|
||||
override fun peek(): Token {
|
||||
if (queue.isNotEmpty()) {
|
||||
return queue.first()
|
||||
}
|
||||
val token = tokenizer.next()
|
||||
queue.add(token)
|
||||
return token
|
||||
}
|
||||
|
||||
override fun peekTypeAhead(ahead: Int): TokenType {
|
||||
wantAtLeast(ahead + 1)
|
||||
return queue[ahead].type
|
||||
}
|
||||
|
||||
private fun wantAtLeast(ahead: Int) {
|
||||
if (queue.size < ahead) {
|
||||
for (i in 1..ahead) {
|
||||
queue.add(tokenizer.next())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -14,6 +14,9 @@ interface TokenSource : PeekableSource<Token> {
|
||||
return tokens
|
||||
}
|
||||
|
||||
fun streamAllRemainingTokens(): TokenStream =
|
||||
TokenStream(consumeAllRemainingTokens().filter { !TokenType.ParserIgnoredTypes.contains(it.type) })
|
||||
|
||||
fun ignoringParserIgnoredTypes(): TokenSource =
|
||||
TokenStreamSource(TokenStream(consumeAllRemainingTokens().filter { !TokenType.ParserIgnoredTypes.contains(it.type) }))
|
||||
TokenStreamSource(streamAllRemainingTokens())
|
||||
}
|
||||
|
Reference in New Issue
Block a user