don't throw basic AstException but SyntaxError instead

This commit is contained in:
Irmen de Jong 2021-10-12 22:30:38 +02:00
parent 367a2a4cee
commit 71a9a84211
2 changed files with 10 additions and 5 deletions

View File

@ -345,7 +345,7 @@ internal fun Prog8ANTLRParser.DirectiveContext.toAst() : Directive =
private fun Prog8ANTLRParser.DirectiveargContext.toAst() : DirectiveArg {
val str = stringliteral()
if(str?.ALT_STRING_ENCODING() != null)
throw AstException("${toPosition()} can't use alternate string s for directive arguments")
throw SyntaxError("can't use alternate string s for directive arguments", toPosition())
return DirectiveArg(stringliteral()?.text, identifier()?.text, integerliteral()?.toAst()?.number?.toInt(), toPosition())
}
@ -359,7 +359,7 @@ private fun Prog8ANTLRParser.IntegerliteralContext.toAst(): NumericLiteral {
integer = try {
text.toInt()
} catch(x: NumberFormatException) {
throw AstException("${toPosition()} invalid decimal literal ${x.message}")
throw SyntaxError("invalid decimal literal ${x.message}", toPosition())
}
datatype = when(integer) {
in 0..255 -> DataType.UBYTE
@ -375,7 +375,7 @@ private fun Prog8ANTLRParser.IntegerliteralContext.toAst(): NumericLiteral {
try {
integer = text.toInt(2)
} catch(x: NumberFormatException) {
throw AstException("${toPosition()} invalid binary literal ${x.message}")
throw SyntaxError("invalid binary literal ${x.message}", toPosition())
}
}
16 -> {
@ -384,7 +384,7 @@ private fun Prog8ANTLRParser.IntegerliteralContext.toAst(): NumericLiteral {
try {
integer = text.toInt(16)
} catch(x: NumberFormatException) {
throw AstException("${toPosition()} invalid hexadecimal literal ${x.message}")
throw SyntaxError("invalid hexadecimal literal ${x.message}", toPosition())
}
}
else -> throw FatalAstException("invalid radix")

View File

@ -2,9 +2,14 @@ package prog8.ast.base
import prog8.ast.expressions.IdentifierReference
/**
* A severe problem in the Ast (such as internal inconsistency or failed invariant)
* It is not useful to continue processing, this aborts the compiler immediately
*/
open class FatalAstException (override var message: String) : Exception(message)
open class AstException (override var message: String) : Exception(message)
abstract class AstException (override var message: String) : Exception(message)
open class SyntaxError(override var message: String, val position: Position) : AstException(message) {
override fun toString() = "${position.toClickableStr()} Syntax error: $message"