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:
Veit Heller 2021-07-23 21:24:59 +02:00 committed by GitHub
parent 760726e001
commit cbdf425247
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 11 deletions

View File

@ -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 =

View File

@ -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

View File

@ -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