quox/lib/Quox/Parser/FromParser/Error.idr

147 lines
4.7 KiB
Idris
Raw Normal View History

2023-03-31 13:27:38 -04:00
module Quox.Parser.FromParser.Error
import Quox.Parser.Parser
2023-08-25 12:09:06 -04:00
import Quox.Parser.LoadFile
2023-03-31 13:27:38 -04:00
import Quox.Typing
import System.File
import Quox.Pretty
2023-10-19 23:28:42 -04:00
%default total
2023-06-23 12:32:05 -04:00
%hide Text.PrettyPrint.Prettyprinter.Doc.infixr.(<++>)
2023-03-31 13:27:38 -04:00
2023-04-24 17:19:15 -04:00
public export
TypeError, LexError, ParseError : Type
TypeError = Typing.Error
LexError = Lexer.Error
ParseError = Parser.Error
%hide Typing.Error
%hide Lexer.Error
%hide Parser.Error
2023-03-31 13:27:38 -04:00
public export
data Error =
2023-05-01 21:06:25 -04:00
AnnotationNeeded Loc (NameContexts d n) (Term d n)
| DuplicatesInEnumType Loc (List TagVal)
| DuplicatesInEnumCase Loc (List TagVal)
2023-05-01 21:06:25 -04:00
| TermNotInScope Loc Name
| DimNotInScope Loc PBaseName
| QtyNotGlobal Loc Qty
| DimNameInTerm Loc PBaseName
2023-05-21 14:09:34 -04:00
| DisplacedBoundVar Loc PName
2023-04-24 17:19:15 -04:00
| WrapTypeError TypeError
2023-10-20 11:42:01 -04:00
| AlreadyExists Loc Name
2023-08-25 12:09:06 -04:00
| LoadError Loc FilePath FileError
| ExpectedFail Loc
| SchemeOnNamespace Loc Mods
| MainOnNamespace Loc Mods
| WrongFail String Error Loc
2023-04-24 17:19:15 -04:00
| WrapParseError String ParseError
2023-03-31 13:27:38 -04:00
2023-05-14 13:58:46 -04:00
export
prettyLexError : {opts : _} -> String -> LexError -> Eff Pretty (Doc opts)
prettyLexError file (Err reason line col char) = do
reason <- case reason of
Other msg => pure $ text msg
NoRuleApply => case char of
Just char => pure $ text "unrecognised character: \{show char}"
Nothing => pure $ text "unexpected end of input"
2023-05-14 13:58:46 -04:00
ComposeNotClosing (sl, sc) (el, ec) => pure $
hsep ["unterminated token at", !(prettyBounds (MkBounds sl sc el ec))]
let loc = makeLoc file (MkBounds line col line col)
2023-05-14 13:58:46 -04:00
pure $ vappend !(prettyLoc loc) reason
export
prettyParseError1 : {opts : _} -> String -> ParsingError _ ->
Eff Pretty (Doc opts)
prettyParseError1 file (Error msg Nothing) =
pure $ text msg
prettyParseError1 file (Error msg (Just bounds)) =
pure $ vappend !(prettyLoc $ makeLoc file bounds) (text msg)
export
prettyParseError : {opts : _} -> String -> ParseError ->
Eff Pretty (Doc opts)
prettyParseError file (LexError err) =
pure $ vsep ["lexical error:", !(prettyLexError file err)]
prettyParseError file (ParseError errs) =
map (vsep . ("parse error:" ::)) $
traverse (map ("-" <++>) . prettyParseError1 file) (toList errs)
parameters {opts : LayoutOpts} (showContext : Bool)
2023-04-24 17:19:15 -04:00
export
prettyError : Error -> Eff Pretty (Doc opts)
2023-05-01 21:06:25 -04:00
prettyError (AnnotationNeeded loc ctx tm) =
2023-05-14 13:58:46 -04:00
[|vappend (prettyLoc loc)
(hangD "type annotation needed on"
!(prettyTerm ctx.dnames ctx.tnames tm))|]
-- [todo] print the original PTerm instead
2023-03-31 13:27:38 -04:00
prettyError (DuplicatesInEnumType loc tags) =
2023-05-14 13:58:46 -04:00
[|vappend (prettyLoc loc)
(hangD "duplicate tags in enum type" !(prettyEnum tags))|]
2023-03-31 13:27:38 -04:00
prettyError (DuplicatesInEnumCase loc tags) =
[|vappend (prettyLoc loc)
(hangD "duplicate arms in enum case" !(prettyEnum tags))|]
2023-05-01 21:06:25 -04:00
prettyError (DimNotInScope loc i) =
2023-05-14 13:58:46 -04:00
[|vappend (prettyLoc loc)
(pure $ hsep ["dimension", !(hl DVar $ text i), "not in scope"])|]
2023-05-01 21:06:25 -04:00
prettyError (TermNotInScope loc x) =
2023-05-14 13:58:46 -04:00
[|vappend (prettyLoc loc)
(pure $ hsep ["term variable", !(prettyFree x), "not in scope"])|]
2023-03-31 13:27:38 -04:00
2023-05-14 13:58:46 -04:00
prettyError (QtyNotGlobal loc pi) = pure $
vappend !(prettyLoc loc)
(sep ["quantity" <++> !(prettyQty pi),
"can't be used on a top level declaration"])
2023-03-31 13:27:38 -04:00
2023-05-14 13:58:46 -04:00
prettyError (DimNameInTerm loc i) = pure $
vappend !(prettyLoc loc)
(sep ["dimension" <++> !(hl DVar $ text i),
"used in a term context"])
2023-03-31 13:27:38 -04:00
2023-05-21 14:09:34 -04:00
prettyError (DisplacedBoundVar loc x) = pure $
vappend !(prettyLoc loc)
(sep ["local variable" <++> !(hl TVar $ text $ toDotsP x),
"cannot be displaced"])
2023-04-24 17:19:15 -04:00
prettyError (WrapTypeError err) =
2023-05-14 13:58:46 -04:00
Typing.prettyError showContext $ trimContext 2 err
2023-03-31 13:27:38 -04:00
2023-10-20 11:42:01 -04:00
prettyError (AlreadyExists loc name) = pure $
vsep [!(prettyLoc loc),
sep [!(prettyFree name), "has already been defined"]]
2023-08-25 12:09:06 -04:00
prettyError (LoadError loc file err) = pure $
2023-05-14 13:58:46 -04:00
vsep [!(prettyLoc loc),
2023-08-25 12:09:06 -04:00
"couldn't load file" <++> text file,
2023-05-14 13:58:46 -04:00
text $ show err]
2023-03-31 13:27:38 -04:00
prettyError (ExpectedFail loc) = pure $
vsep [!(prettyLoc loc), "expected error"]
prettyError (SchemeOnNamespace loc ns) = pure $
vsep [!(prettyLoc loc),
hsep ["namespace", !(hl Free $ text $ joinBy "." $ toList ns),
"cannot have #[compile-scheme] attached"]]
prettyError (MainOnNamespace loc ns) = pure $
vsep [!(prettyLoc loc),
hsep ["namespace", !(hl Free $ text $ joinBy "." $ toList ns),
"cannot have #[main] attached"]]
prettyError (WrongFail str err loc) = pure $
vsep [!(prettyLoc loc),
"wrong error, expected to match", !(hl Tag $ text "\"\{str}\""),
"but got", !(prettyError err)]
2023-04-24 17:19:15 -04:00
prettyError (WrapParseError file err) =
prettyParseError file err