mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-17 16:38:14 +03:00
fix: respect line number in repl (#1282)
* fix: respect line number in repl * refactor: simplify Parse.parse * fix: multiline line counter in repl
This commit is contained in:
parent
760726e001
commit
cbdf425247
@ -501,10 +501,13 @@ apply ctx@Context {contextInternalEnv = internal} body params args =
|
||||
|
||||
-- | Parses a string and then converts the resulting forms to commands, which are evaluated in order.
|
||||
executeString :: Bool -> Bool -> Context -> String -> String -> IO Context
|
||||
executeString doCatch printResult ctx input fileName =
|
||||
executeString = executeStringAtLine 1
|
||||
|
||||
executeStringAtLine :: Int -> Bool -> Bool -> Context -> String -> String -> IO Context
|
||||
executeStringAtLine line doCatch printResult ctx input fileName =
|
||||
if doCatch then catch exec (catcher ctx) else exec
|
||||
where
|
||||
exec = case parse input fileName of
|
||||
exec = case parseAtLine line input fileName of
|
||||
Left parseError ->
|
||||
let sourcePos = Parsec.errorPos parseError
|
||||
parseErrorXObj =
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
module Parsing
|
||||
( parse,
|
||||
parseAtLine,
|
||||
validCharacters,
|
||||
balance,
|
||||
)
|
||||
@ -599,11 +600,14 @@ lispSyntax = do
|
||||
Parsec.eof
|
||||
pure result
|
||||
|
||||
parse :: String -> String -> Either Parsec.ParseError [XObj]
|
||||
parse text fileName =
|
||||
let initState = ParseState (Info 1 1 fileName (Set.fromList []) 0)
|
||||
parseAtLine :: Int -> String -> String -> Either Parsec.ParseError [XObj]
|
||||
parseAtLine line text fileName =
|
||||
let initState = ParseState (Info line 1 fileName (Set.fromList []) 0)
|
||||
in Parsec.runParser lispSyntax initState fileName text
|
||||
|
||||
parse :: String -> String -> Either Parsec.ParseError [XObj]
|
||||
parse = parseAtLine 1
|
||||
|
||||
{-# ANN balance "HLint: ignore Use String" #-}
|
||||
|
||||
-- | For detecting the parenthesis balance in a string, i.e. "((( ))" = 1
|
||||
|
12
src/Repl.hs
12
src/Repl.hs
@ -112,8 +112,8 @@ treatSpecialInput (':' : rest) =
|
||||
Nothing -> rewriteError ("Unknown special command: :" ++ [cmd])
|
||||
treatSpecialInput arg = arg
|
||||
|
||||
repl :: String -> String -> InputT (StateT Context IO) ()
|
||||
repl readSoFar prompt =
|
||||
repl :: Int -> String -> String -> InputT (StateT Context IO) ()
|
||||
repl line readSoFar prompt =
|
||||
do
|
||||
context <- lift get
|
||||
input <- getInputLine (strWithColor Yellow prompt)
|
||||
@ -128,10 +128,10 @@ repl readSoFar prompt =
|
||||
case balanced of
|
||||
"" -> do
|
||||
let input' = if concatenated == "\n" then contextLastInput context else concatenated -- Entering an empty string repeats last input
|
||||
context' <- liftIO $ executeString True True (resetAlreadyLoadedFiles context) (treatSpecialInput input') "REPL"
|
||||
context' <- liftIO $ executeStringAtLine line True True (resetAlreadyLoadedFiles context) (treatSpecialInput input') "REPL"
|
||||
lift $ put context'
|
||||
repl "" (projectPrompt proj)
|
||||
_ -> repl concatenated (if projectBalanceHints proj then balanced else "")
|
||||
repl (line + (length (filter ('\n' ==) input'))) "" (projectPrompt proj)
|
||||
_ -> repl line concatenated (if projectBalanceHints proj then balanced else "")
|
||||
|
||||
resetAlreadyLoadedFiles :: Context -> Context
|
||||
resetAlreadyLoadedFiles context =
|
||||
@ -143,4 +143,4 @@ runRepl :: Context -> IO ((), Context)
|
||||
runRepl context = do
|
||||
historyPath <- configPath "history"
|
||||
createDirectoryIfMissing True (takeDirectory historyPath)
|
||||
runStateT (runInputT (readlineSettings historyPath) (repl "" (projectPrompt (contextProj context)))) context
|
||||
runStateT (runInputT (readlineSettings historyPath) (repl 1 "" (projectPrompt (contextProj context)))) context
|
||||
|
Loading…
Reference in New Issue
Block a user