move Error to its own file
This commit is contained in:
37
src/Lib/Error.newt
Normal file
37
src/Lib/Error.newt
Normal file
@@ -0,0 +1,37 @@
|
||||
module Lib.Error
|
||||
|
||||
import Prelude
|
||||
import Lib.Common
|
||||
|
||||
-- I'll want to get Context / Val in some of these
|
||||
-- and a pretty printer in the monad
|
||||
data Error
|
||||
= E FC String
|
||||
| ENotFound FC String
|
||||
| Postpone FC QName String
|
||||
|
||||
|
||||
instance HasFC Error where
|
||||
getFC (E x str) = x
|
||||
getFC (ENotFound x _) = x
|
||||
getFC (Postpone x k str) = x
|
||||
|
||||
errorMsg : Error -> String
|
||||
errorMsg (E x str) = str
|
||||
errorMsg (ENotFound x nm) = "\{nm} not in scope"
|
||||
errorMsg (Postpone x k str) = str
|
||||
|
||||
showError : (src : String) -> Error -> String
|
||||
showError src err =
|
||||
let fc = getFC err
|
||||
in "ERROR at \{show $ getFC err}: \{errorMsg err}\n" ++ go fc 0 (lines src)
|
||||
where
|
||||
go : FC → Int → List String → String
|
||||
go fc l Nil = ""
|
||||
go fc l (x :: xs) =
|
||||
if l == fcLine fc then
|
||||
let width = fc.bnds.endCol - fc.bnds.startCol in
|
||||
" \{x}\n \{replicate (cast $ fcCol fc) ' '}\{replicate (cast width) '^'}\n"
|
||||
else if fcLine fc - 3 < l then " " ++ x ++ "\n" ++ go fc (l + 1) xs
|
||||
else go fc (l + 1) xs
|
||||
|
||||
Reference in New Issue
Block a user