Optional whitespace preservation.

This commit is contained in:
Alex Zenla 2023-08-19 22:20:45 -07:00
parent 903f730d51
commit 77a9136be7
Signed by: alex
GPG Key ID: C0780728420EBFE5
4 changed files with 47 additions and 5 deletions

View File

@ -0,0 +1,19 @@
package gay.pizza.pork.parse
abstract class Highlighter(source: TokenSource) : TokenProcessor(source) {
override fun process(token: Token) {
when {
token.type.keyword != null -> {
keyword(token)
}
token.type == TokenType.Symbol -> {
symbol(token)
}
else -> other(token)
}
}
abstract fun keyword(token: Token)
abstract fun symbol(token: Token)
abstract fun other(token: Token)
}

View File

@ -1,6 +1,6 @@
package gay.pizza.pork.parse
class PorkTokenizer(val source: CharSource) {
class PorkTokenizer(val source: CharSource, val preserveWhitespace: Boolean = false) {
private var tokenStart: Int = 0
private fun isSymbol(c: Char): Boolean =
@ -40,10 +40,14 @@ class PorkTokenizer(val source: CharSource) {
return Token(TokenType.IntLiteral, number)
}
private fun skipWhitespace() {
while (isWhitespace(source.peek())) {
source.next()
private fun readWhitespace(): Token {
val whitespace = buildString {
while (isWhitespace(source.peek())) {
val char = source.next()
append(char)
}
}
return Token(TokenType.Whitespace, whitespace)
}
fun next(): Token {
@ -57,7 +61,10 @@ class PorkTokenizer(val source: CharSource) {
}
if (isWhitespace(char)) {
skipWhitespace()
val whitespace = readWhitespace()
if (preserveWhitespace) {
return whitespace
}
continue
}

View File

@ -0,0 +1,15 @@
package gay.pizza.pork.parse
abstract class TokenProcessor(val source: TokenSource) {
fun processAll() {
while (true) {
val token = source.next()
process(token)
if (token.type == TokenType.EndOfFile) {
break
}
}
}
abstract fun process(token: Token)
}

View File

@ -21,6 +21,7 @@ enum class TokenType(val char: Char? = null, val keyword: String? = null) {
If(keyword = "if"),
Then(keyword = "then"),
Else(keyword = "else"),
Whitespace,
EndOfFile;
companion object {