From 33405ffcd66e6425fa59cd8a110f2d0f06bbaf69 Mon Sep 17 00:00:00 2001 From: Karol Stasiak Date: Thu, 31 Oct 2019 12:11:05 +0100 Subject: [PATCH] Better handling for invalid statements inside segment blocks --- src/main/scala/millfork/parser/MfParser.scala | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/scala/millfork/parser/MfParser.scala b/src/main/scala/millfork/parser/MfParser.scala index a8169ac8..6cd1e78e 100644 --- a/src/main/scala/millfork/parser/MfParser.scala +++ b/src/main/scala/millfork/parser/MfParser.scala @@ -630,20 +630,26 @@ abstract class MfParser[T](fileId: String, input: String, currentDirectory: Stri } yield Seq(UnionDefinitionStatement(name, fields).pos(p)) val segmentBlock: P[Seq[BankedDeclarationStatement]] = for { - bankName <- "segment" ~ AWS ~ "(" ~ AWS ~ identifier ~ AWS ~ ")" ~ AWS ~ "{" ~/ AWS - body <- locatableDefinition.rep(sep = EOL) - _ <- AWS ~ "}" ~/ Pass + (_, bankName) <- "segment" ~ AWS ~ "(" ~ AWS ~ position("segment name") ~ identifier ~ AWS ~ ")" ~ AWS ~ "{" ~/ AWS + body <- locatableDefinition.rep(sep = EOL) + _ <- AWS ~ "}" ~/ Pass } yield { - body.flatten.map{ stmt => + body.flatten.map { stmt => if (stmt.bank.isEmpty) stmt.withChangedBank(bankName) else stmt } } - def locatableDefinition: P[Seq[BankedDeclarationStatement]] = segmentBlock | arrayDefinition | functionDefinition | globalVariableDefinition + + def checkForNonlocatableDefinitions: P[Seq[BankedDeclarationStatement]] = + ((StringIn("alias", "enum", "struct", "union", "import").! ~ SWS) ~/ position()).map{ x => + log.fatal(s"`${x._1}` statements are not allowed inside segment blocks", Some(x._2)) + } + + def locatableDefinition: P[Seq[BankedDeclarationStatement]] = checkForNonlocatableDefinitions | segmentBlock | arrayDefinition | functionDefinition | globalVariableDefinition val program: Parser[Program] = for { - _ <- Start ~/ AWS ~/ Pass + _ <- Start ~/ AWS ~/ position("top level statement") definitions <- (importStatement | aliasDefinition | enumDefinition | structDefinition | unionDefinition | locatableDefinition).rep(sep = EOL) _ <- AWS ~ End } yield Program(definitions.flatten.toList)