diff --git a/src/main/wacc/Error.scala b/src/main/wacc/Error.scala index c702fd2..724c08f 100644 --- a/src/main/wacc/Error.scala +++ b/src/main/wacc/Error.scala @@ -6,7 +6,6 @@ import wacc.types._ enum Error { case DuplicateDeclaration(ident: ast.Ident) case UndefinedIdentifier(ident: ast.Ident, identType: renamer.IdentType) - case FunctionParamsMismatch(pos: Position, expected: Int, got: Int) case SemanticError(pos: Position, msg: String) case TypeMismatch(pos: Position, expected: SemType, got: SemType, msg: String) @@ -18,25 +17,23 @@ def printError(error: Error)(using errorContent: String): Unit = { error match { case Error.DuplicateDeclaration(ident) => printPosition(ident.getPosition) - println( - s"Duplicate declaration of identifier ${ident.v}" - ) + println(s"Duplicate declaration of identifier ${ident.v}") highlight(ident.getPosition, ident.v.length) case Error.UndefinedIdentifier(ident, identType) => printPosition(ident.getPosition) - println( - s"Undefined ${identType.toString.toLowerCase()} ${ident.v}" - ) + println(s"Undefined ${identType.toString.toLowerCase()} ${ident.v}") highlight(ident.getPosition, ident.v.length) case Error.FunctionParamsMismatch(pos, expected, got) => printPosition(pos) println(s"Function expects $expected parameters, got $got") highlight(pos, 1) case Error.TypeMismatch(pos, expected, got, msg) => - println(s"Type mismatch: expected $expected, got $got") + printPosition(pos) + println(msg) + highlight(pos, 1) case Error.SemanticError(pos, msg) => printPosition(pos) - println(s"$msg at line: ${pos.line} column: ${pos.column}") + println(msg) highlight(pos, 1) case wacc.Error.InternalError(pos, msg) => printPosition(pos) diff --git a/src/main/wacc/parser.scala b/src/main/wacc/parser.scala index 7da0a0a..dac9fcd 100644 --- a/src/main/wacc/parser.scala +++ b/src/main/wacc/parser.scala @@ -12,8 +12,6 @@ import cats.data.NonEmptyList import parsley.errors.DefaultErrorBuilder import parsley.errors.ErrorBuilder import parsley.errors.tokenextractors.LexToken -import parsley.errors.patterns.VerifiedErrors -import parsley.character.char object parser { import lexer.implicits.implicitSymbol @@ -50,9 +48,6 @@ object parser { case Expr case Pair - val _parensCheck = - char('(').verifiedExplain("use keyword 'call' to call functions") - implicit val builder: ErrorBuilder[String] = new DefaultErrorBuilder with LexToken { def tokens = errTokens } @@ -71,8 +66,8 @@ object parser { GreaterEq from ">=" ) +: SOps(InfixL)( - ((Add from "+").label("binary operator") | _parensCheck), - ((Sub from "-").label("binary operator") | _parensCheck) + (Add from "+").label("binary operator"), + (Sub from "-").label("binary operator") ) +: SOps(InfixL)(Mul from "*", Div from "/", Mod from "%") +: SOps(Prefix)( @@ -115,16 +110,13 @@ object parser { private lazy val `` = (`` <**> (`` identity)) | ((UntypedPairType from ``) <**> - ((`` <**> ``.explain( - "non-erased pair types cannot be nested" - )).map(arr => (_: UntypedPairType) => arr) identity)) + ((`` <**> ``) + .map(arr => (_: UntypedPairType) => arr) identity)) // Statements private lazy val `` = Program( "begin" ~> many( - atomic( - (``.label("function declaration") <~> `` <~ "(") - ) <**> `` + atomic(``.label("function declaration") <~> `` <~ "(") <**> `` ).label("function declaration"), (atomic(`` <~ "(") ~> fail("function is missing return type") | ``.label( "main program body" @@ -132,9 +124,9 @@ object parser { ) private lazy val `` = FuncDecl( - sepBy(``, ",") <~ ")".label("parenthesis") <~ "is", + sepBy(``, ",") <~ ")" <~ "is", ``.guardAgainst { - case stmts if !stmts.isReturning => Seq("all functions must end in a returning statement") + case stmts if !stmts.isReturning => Seq("All functions must end in a returning statement") } <~ "end" ) private lazy val `` = Param(``, ``) @@ -159,11 +151,8 @@ object parser { ) | While("while" ~> ``.labelWithType(LabelType.Expr) <~ "do", `` <~ "done") | Block("begin" ~> `` <~ "end") - | VarDecl( - ``, - `` <~ "=".explain("functions must be defined on top of the main block"), - ``.label("valid initial value for variable") - ) + | VarDecl(``, `` <~ "=", ``.label("valid initial value for variable")) + // TODO: Can we inline the name of the variable in the message | Assign(`` <~ "=", ``) private lazy val ``: Parsley[LValue] = `` | ``