From 3e7e44acfe9d1808ec71bd874d40566e2f904195 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 3 Apr 2019 22:25:26 +0200 Subject: [PATCH] no hard crash anymore for invalid string escape sequences or unknown petscii characters --- compiler/src/prog8/CompilerMain.kt | 5 +++++ compiler/src/prog8/ast/AST.kt | 11 +++++++++-- examples/test.p8 | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index ee274dfd8..09af5abad 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -152,6 +152,11 @@ private fun compileMain(args: Array) { System.err.println(px.message) System.err.print("\u001b[0m") // reset exitProcess(1) + } catch (ax: AstException) { + System.err.print("\u001b[91m") // bright red + System.err.println(ax.toString()) + System.err.print("\u001b[0m") // reset + exitProcess(1) } catch (x: Exception) { print("\u001b[91m") // bright red println("\n* internal error *") diff --git a/compiler/src/prog8/ast/AST.kt b/compiler/src/prog8/ast/AST.kt index 7895adcce..332a299a4 100644 --- a/compiler/src/prog8/ast/AST.kt +++ b/compiler/src/prog8/ast/AST.kt @@ -11,6 +11,7 @@ import prog8.functions.NotConstArgumentException import prog8.functions.builtinFunctionReturnType import prog8.parser.CustomLexer import prog8.parser.prog8Parser +import java.io.CharConversionException import java.io.File import java.nio.file.Path import kotlin.math.abs @@ -2145,7 +2146,13 @@ private fun prog8Parser.ExpressionContext.toAst() : IExpression { } litval.floatliteral()!=null -> LiteralValue(DataType.FLOAT, floatvalue = litval.floatliteral().toAst(), position = litval.toPosition()) litval.stringliteral()!=null -> LiteralValue(DataType.STR, strvalue = unescape(litval.stringliteral().text, litval.toPosition()), position = litval.toPosition()) - litval.charliteral()!=null -> LiteralValue(DataType.UBYTE, bytevalue = Petscii.encodePetscii(unescape(litval.charliteral().text, litval.toPosition()), true)[0], position = litval.toPosition()) + litval.charliteral()!=null -> { + try { + LiteralValue(DataType.UBYTE, bytevalue = Petscii.encodePetscii(unescape(litval.charliteral().text, litval.toPosition()), true)[0], position = litval.toPosition()) + } catch (ce: CharConversionException) { + throw SyntaxError(ce.message ?: ce.toString(), litval.toPosition()) + } + } litval.arrayliteral()!=null -> { val array = litval.arrayliteral()?.toAst() // the actual type of the arrayspec can not yet be determined here (missing namespace & heap) @@ -2304,7 +2311,7 @@ internal fun unescape(str: String, position: Position): String { 'u' -> { "${iter.nextChar()}${iter.nextChar()}${iter.nextChar()}${iter.nextChar()}".toInt(16).toChar() } - else -> throw AstException("$position invalid escape char in string: \\$ec") + else -> throw SyntaxError("invalid escape char in string: \\$ec", position) }) } else { result.add(c) diff --git a/examples/test.p8 b/examples/test.p8 index 346befdbf..429a5be08 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,7 +6,7 @@ ~ main { sub start() { - ubyte ub=2 + ubyte ub='^' ubyte ub2=7 uword uw=2 uword uw2=5