From 8d325a85ae93a4fe680e243be70cc3cc66af52c9 Mon Sep 17 00:00:00 2001 From: emmabastas Date: Sat, 17 Jul 2021 16:07:41 +0200 Subject: [PATCH] Implement `failure` in a less hackish way --- elm-format-lib/src/Parse/Helpers.hs | 17 ++++++++++++----- elm-format-lib/src/Parse/ParsecAdapter.hs | 19 ------------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/elm-format-lib/src/Parse/Helpers.hs b/elm-format-lib/src/Parse/Helpers.hs index ccb388fcd..164edbe17 100644 --- a/elm-format-lib/src/Parse/Helpers.hs +++ b/elm-format-lib/src/Parse/Helpers.hs @@ -16,6 +16,7 @@ import qualified Parse.State as State import Parse.Comments import Parse.IParser import Parse.Whitespace +import qualified Parse.Primitives as EP import qualified Reporting.Annotation as A import qualified Reporting.Error.Syntax as Syntax import qualified Reporting.Region as R @@ -511,12 +512,18 @@ commentedKeyword elmVersion word parser = -- ODD COMBINATORS +-- Behaves the same as `Parse.ParsecAdapter.fail` except that the consumed +-- continuation is called instead of the empty continuation. failure :: String -> IParser String -failure msg = do - inp <- getInput - setInput ('x':inp) - _ <- anyToken - fail msg +failure msg = + EP.Parser $ \s _ _ cerr _ -> + let + (EP.Parser p) = fail msg + in + -- This looks really unsound, but `p` which was created with `fail` will + -- only ever call the empty error continuation (which in this case + -- re-routes to the consumed error continuation) + p s undefined undefined undefined cerr until :: IParser a -> IParser b -> IParser b diff --git a/elm-format-lib/src/Parse/ParsecAdapter.hs b/elm-format-lib/src/Parse/ParsecAdapter.hs index ce39671d9..bcdd5c9d4 100644 --- a/elm-format-lib/src/Parse/ParsecAdapter.hs +++ b/elm-format-lib/src/Parse/ParsecAdapter.hs @@ -28,8 +28,6 @@ module Parse.ParsecAdapter , skipMany , runParserT , getPosition - , getInput - , setInput , getState , updateState -- Text.Parsec.Pos @@ -311,23 +309,6 @@ getPosition = return $ newPos sourceName row col --- TODO: Figure out why this function is never reached by the test suite. --- --- This function is needed for elm-format to compile, but leaving it undefined --- doesn't cause any of the tests to fail. --- Is there missing coverage in the test suite? Or is this function required by --- dead code? --- --- `setInput` is in the same situation. -getInput :: Parser String -getInput = undefined - - --- TODO: See `getInput` -setInput :: String -> Parser () -setInput = undefined - - getState :: Parser State getState = do (EP.State _ _ _ _ _ _ _ newline) <- getParserState