diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index 70dbcdabf..110e56a0e 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -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") diff --git a/compilerAst/src/prog8/ast/base/Errors.kt b/compilerAst/src/prog8/ast/base/Errors.kt index 1313e664f..e547bbcbf 100644 --- a/compilerAst/src/prog8/ast/base/Errors.kt +++ b/compilerAst/src/prog8/ast/base/Errors.kt @@ -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"