parser: ensure that the parse functions are always directly called by reworking whitespace

This commit is contained in:
2023-09-21 18:04:19 -07:00
parent 4758e92676
commit c4f65a66ca
7 changed files with 103 additions and 76 deletions

View File

@ -17,7 +17,7 @@ class PorkParser : PsiParser {
return builder.treeBuilt
}
class ExitParser(val error: String? = null) : RuntimeException(
class ExitParser(error: String? = null) : RuntimeException(
if (error == null) "Fast Exit" else "Exit Parser: $error"
)
}

View File

@ -4,6 +4,7 @@ import com.intellij.lang.PsiBuilder
import gay.pizza.pork.parser.SourceIndex
import gay.pizza.pork.parser.Token
import gay.pizza.pork.parser.TokenSource
import gay.pizza.pork.parser.TokenType
import com.intellij.psi.TokenType as PsiTokenType
@Suppress("UnstableApiUsage")
@ -29,5 +30,18 @@ class PsiBuilderTokenSource(val builder: PsiBuilder) : TokenSource {
return Token(tokenType, SourceIndex.indexOnly(builder.currentOffset), builder.tokenText!!)
}
override fun peekTypeAhead(ahead: Int): TokenType {
if (builder.eof()) {
return TokenType.EndOfFile
}
val elementType = builder.lookAhead(ahead)
if (elementType == null || elementType == PsiTokenType.BAD_CHARACTER) {
return TokenType.EndOfFile
}
return PorkElementTypes.tokenTypeFor(elementType) ?: TokenType.EndOfFile
}
override fun ignoringParserIgnoredTypes(): TokenSource = this
class BadCharacterError(error: String) : RuntimeException(error)
}