module ParseLib.Abstract.Run where import Data.List.NonEmpty (NonEmpty ((:|))) import ParseLib.Error ( ErrorsPretty (errorBundlePrettyImproved), defaultConfig, ) import ParseLib.Simple.Core (Parser, parseWithConfig) import ParseLib.Simple.Derived (eof) -- | replacement for @Main.run@ in @P3-CSharp@ that uses `parseImproved`, -- specifies the corresponding type class constraints, ensures library -- level error reporting for partial parses, and parses and prints to -- standard error (stderr) once instead of twice run :: (ErrorsPretty s, Ord s, Show a) => String -> Parser s a -> [s] -> a run s p input = case parseWithConfig defaultConfig (p <* eof) input of Right ((result, _) :| _) -> result Left errors -> error $ s <> " error:\n" <> errorBundlePrettyImproved defaultConfig input errors