parser: major refinement of error handling

This commit is contained in:
2023-09-18 01:07:28 -07:00
parent 5610326eda
commit 7cb3e02b21
20 changed files with 170 additions and 80 deletions

View File

@ -57,8 +57,8 @@ class PorkLexer : LexerBase() {
try {
val currentToken = tokenizer.next()
currentTokenType = PorkElementTypes.elementTypeFor(currentToken.type)
internalTokenStart = currentToken.start
internalTokenEnd = currentToken.start + currentToken.text.length
internalTokenStart = currentToken.sourceIndex.index
internalTokenEnd = currentToken.sourceIndex.index + currentToken.text.length
} catch (e: ProcessCanceledException) {
throw e
} catch (e: Throwable) {

View File

@ -29,12 +29,8 @@ class PsiBuilderMarkAttribution(val builder: PsiBuilder) : ParserNodeAttribution
}
throw PorkParser.ExitParser()
} catch (e: PorkParser.ExitParser) {
if (e.error != null) {
marker.error(e.error)
} else {
marker.done(PorkElementTypes.FailedToParse)
}
throw PorkParser.ExitParser()
marker.done(PorkElementTypes.FailedToParse)
throw e
}
if (map[result] != null) {
marker.drop()

View File

@ -1,6 +1,7 @@
package gay.pizza.pork.idea
import com.intellij.lang.PsiBuilder
import gay.pizza.pork.parser.SourceIndex
import gay.pizza.pork.parser.Token
import gay.pizza.pork.parser.TokenSource
import com.intellij.psi.TokenType as PsiTokenType
@ -17,15 +18,15 @@ class PsiBuilderTokenSource(val builder: PsiBuilder) : TokenSource {
override fun peek(): Token {
if (builder.eof()) {
return Token.endOfFile(builder.currentOffset)
return Token.endOfFile(SourceIndex.indexOnly(builder.currentOffset))
}
val elementType = builder.tokenType!!
if (elementType == PsiTokenType.BAD_CHARACTER) {
throw BadCharacterError("Invalid character.")
throw BadCharacterError("Invalid character")
}
val tokenType = PorkElementTypes.tokenTypeFor(elementType) ?:
throw RuntimeException("Lexing failure: ${elementType.debugName}")
return Token(tokenType, builder.currentOffset, builder.tokenText!!)
return Token(tokenType, SourceIndex.indexOnly(builder.currentOffset), builder.tokenText!!)
}
class BadCharacterError(error: String) : RuntimeException(error)