From 89ff89bc48ea1d6406b2ebdf0bf07a974739be34 Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Fri, 31 Jul 2020 17:11:30 +0200 Subject: [PATCH] Some more defensive programming --- src/main/scala/millfork/parser/MfParser.scala | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/scala/millfork/parser/MfParser.scala b/src/main/scala/millfork/parser/MfParser.scala index 519e8f44..9783a27b 100644 --- a/src/main/scala/millfork/parser/MfParser.scala +++ b/src/main/scala/millfork/parser/MfParser.scala @@ -33,14 +33,16 @@ abstract class MfParser[T](fileId: String, input: String, currentDirectory: Stri def toAst: Parsed[Program] = { val parse = program.parse(input + "\n\n\n") parse match { - case _:Failure[_, _] => - val c = input(lastPosition.cursor) - if (c >= 0x100 || c < 0x20 || c == '`') { - log.error("Invalid character U+%04X %s".format(c.toInt, Character.getName(c)), Some(lastPosition)) - Confusables.map.get(c) match { - case Some(ascii) => - log.info(s"Did you mean: $ascii") - case _ => + case _: Failure[_, _] => + if (lastPosition.cursor >= 0 && lastPosition.cursor < input.length) { + val c = input(lastPosition.cursor) + if (c >= 0x100 || c < 0x20 || c == '`') { + log.error("Invalid character U+%04X %s".format(c.toInt, Character.getName(c)), Some(lastPosition)) + Confusables.map.get(c) match { + case Some(ascii) => + log.info(s"Did you mean: $ascii") + case _ => + } } } case _ =>