module ParseLib.Simple.Run where import Data.List (find) import Data.Maybe (fromMaybe) import ParseLib.Error (ErrorsPretty) import ParseLib.Simple.Core (Parser, parseImproved) -- | replacement for @Main.run@ in @P3-CSharp@ that uses `parseImproved`, -- specifies the corresponding type class constraints, and does not parse -- and print to standard error (stderr) twice run :: (ErrorsPretty s, Ord s, Show a) => String -> Parser s a -> [s] -> a run s p x = fst . firstFullParseOrError . parseImproved p $ x where firstFullParseOrError l = fromMaybe (error $ "The " <> s <> " returned no full parses. " <> "Here are all parses that didn't consume the entire input:\n" <> show l ) $ find (null . snd) $ l