move Error to its own file

This commit is contained in:
2026-02-24 11:04:38 -08:00
parent 90e4bba766
commit a789cffcce
12 changed files with 54 additions and 36 deletions

View File

@@ -16,6 +16,7 @@ import Data.SortedMap
import Lib.Parser import Lib.Parser
import Lib.Syntax import Lib.Syntax
import Lib.Parser.Impl import Lib.Parser.Impl
import Lib.Error
-- For now we cheat and assume capitalized directories are a module component -- For now we cheat and assume capitalized directories are a module component
decomposeName : String String × String decomposeName : String String × String
@@ -31,8 +32,12 @@ decomposeName fn =
switchModule : FileSource String M (Maybe ModContext) switchModule : FileSource String M (Maybe ModContext)
switchModule repo modns = do switchModule repo modns = do
-- TODO processing on hover is expensive, but info is not always there
-- I suspect this picks up the case where a file has been invalidated by a change to
-- another file and we switch editors. Handle that (enqueue a check) and switch this back.
top <- getTop top <- getTop
let (Just mod) = lookupMap' modns top.modules | Nothing => pure Nothing mod <- processModule emptyFC repo Nil modns
-- let (Just mod) = lookupMap' modns top.modules | Nothing => pure Nothing
modifyTop [ currentMod := mod; ops := mod.modOps ] modifyTop [ currentMod := mod; ops := mod.modOps ]
pure $ Just mod pure $ Just mod

View File

@@ -15,6 +15,7 @@ import Node
import Commands import Commands
import Lib.ProcessDecl import Lib.ProcessDecl
import Lib.Prettier import Lib.Prettier
import Lib.Error
pfunc js_castArray : Array JSObject JSObject := `x => x` pfunc js_castArray : Array JSObject JSObject := `x => x`
pfunc js_castInt : Int JSObject := `x => x` pfunc js_castInt : Int JSObject := `x => x`

View File

@@ -113,6 +113,8 @@ record FC where
file : String file : String
bnds : Bounds bnds : Bounds
instance Show FC where
show (MkFC file (MkBounds l c el ec)) = "\{file}:\{show $ l + 1}:\{show $ c + 1}--\{show $ el + 1}:\{show $ ec + 1}"
instance Add FC where instance Add FC where
MkFC fn a + MkFC _ b = MkFC fn (a + b) MkFC fn a + MkFC _ b = MkFC fn (a + b)
@@ -158,40 +160,6 @@ instance Show QName where
instance Ord QName where instance Ord QName where
compare (QN ns nm) (QN ns' nm') = if ns == ns' then compare nm nm' else compare ns ns' compare (QN ns nm) (QN ns' nm') = if ns == ns' then compare nm nm' else compare ns ns'
-- 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 Show FC where
show (MkFC file (MkBounds l c el ec)) = "\{file}:\{show $ l + 1}:\{show $ c + 1}--\{show $ el + 1}:\{show $ ec + 1}"
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
data Fixity = InfixL | InfixR | Infix data Fixity = InfixL | InfixR | Infix

View File

@@ -13,6 +13,7 @@ import Lib.Util
import Lib.TopContext import Lib.TopContext
import Lib.Syntax import Lib.Syntax
import Lib.Types import Lib.Types
import Lib.Error
vprint : Context -> Val -> M String vprint : Context -> Val -> M String
vprint ctx v = do vprint ctx v = do

37
src/Lib/Error.newt Normal file
View 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

View File

@@ -7,7 +7,7 @@ import Data.String
import Data.Int import Data.Int
import Data.List1 import Data.List1
import Data.SortedMap import Data.SortedMap
import Lib.Error
TokenList : U TokenList : U
TokenList = List BTok TokenList = List BTok

View File

@@ -6,6 +6,7 @@ import Data.String
import Lib.Common import Lib.Common
import Lib.Elab import Lib.Elab
import Lib.Error
import Lib.Parser import Lib.Parser
import Lib.Syntax import Lib.Syntax
import Data.SortedMap import Data.SortedMap

View File

@@ -4,6 +4,7 @@ import Prelude
import Lib.Types import Lib.Types
import Lib.Common import Lib.Common
import Lib.Syntax import Lib.Syntax
import Lib.Error
import Lib.ProcessDecl import Lib.ProcessDecl
import Lib.TopContext import Lib.TopContext
import Lib.Tokenizer import Lib.Tokenizer

View File

@@ -12,6 +12,7 @@ import Lib.Token
import Lib.Common import Lib.Common
import Data.String import Data.String
import Data.SnocList import Data.SnocList
import Lib.Error
standalone : List Char standalone : List Char
standalone = unpack "()\\{}[],.@;" standalone = unpack "()\\{}[],.@;"

View File

@@ -6,6 +6,7 @@ import Data.String
import Prelude import Prelude
import Lib.Common import Lib.Common
import Lib.Types import Lib.Types
import Lib.Error
-- TODO move the def in here (along with M) or merge this into types -- TODO move the def in here (along with M) or merge this into types
-- The Monad can be its own file if we pull all of the monad update functions there. -- The Monad can be its own file if we pull all of the monad update functions there.

View File

@@ -3,6 +3,7 @@ module Lib.Types
import Prelude import Prelude
import Lib.Common import Lib.Common
import Lib.Prettier import Lib.Prettier
import Lib.Error
import Data.IORef import Data.IORef
import Data.SnocList import Data.SnocList

View File

@@ -19,6 +19,7 @@ import Lib.Syntax
import Lib.ReplParser import Lib.ReplParser
import Node import Node
import Revision import Revision
import Lib.Error
dirFileSource : String FileSource dirFileSource : String FileSource
dirFileSource base = MkFileSource base $ \fc fn => do dirFileSource base = MkFileSource base $ \fc fn => do