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

110 lines
3.4 KiB
Idris
Raw Normal View History

2023-03-31 13:27:38 -04:00
module Quox.Parser.FromParser.Error
import Quox.Parser.Parser
import Quox.Typing
import System.File
import Quox.Pretty
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)
| DuplicatesInEnum Loc (List TagVal)
| 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-05-01 21:06:25 -04:00
| LoadError Loc String FileError
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
let loc = makeLoc file (MkBounds line col line col)
reason <- case reason of
EndInput => pure "unexpected end of input"
NoRuleApply => pure $ text "unrecognised character: \{show char}"
ComposeNotClosing (sl, sc) (el, ec) => pure $
hsep ["unterminated token at", !(prettyBounds (MkBounds sl sc el ec))]
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 (showContext : Bool)
2023-04-24 17:19:15 -04:00
export
2023-05-14 13:58:46 -04:00
prettyError : {opts : _} -> 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
2023-05-01 21:06:25 -04:00
prettyError (DuplicatesInEnum 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
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-05-14 13:58:46 -04:00
prettyError (LoadError loc str err) = pure $
vsep [!(prettyLoc loc),
"couldn't load file" <++> text str,
text $ show err]
2023-03-31 13:27:38 -04:00
2023-04-24 17:19:15 -04:00
prettyError (WrapParseError file err) =
prettyParseError file err