2018-09-30 04:32:08 +03:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2018-07-31 11:30:08 +03:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
2018-06-05 23:23:47 +03:00
|
|
|
|
|
|
|
module Hledger.Utils.Parse (
|
|
|
|
SimpleStringParser,
|
|
|
|
SimpleTextParser,
|
|
|
|
TextParser,
|
|
|
|
JournalParser,
|
2018-09-26 01:07:58 +03:00
|
|
|
ErroringJournalParser,
|
2018-06-05 23:23:47 +03:00
|
|
|
|
|
|
|
choice',
|
|
|
|
choiceInState,
|
|
|
|
surroundedBy,
|
|
|
|
parsewith,
|
|
|
|
parsewithString,
|
|
|
|
parseWithState,
|
|
|
|
parseWithState',
|
|
|
|
fromparse,
|
|
|
|
parseerror,
|
|
|
|
showDateParseError,
|
|
|
|
nonspace,
|
2021-06-24 03:12:17 +03:00
|
|
|
isNewline,
|
2018-06-05 23:23:47 +03:00
|
|
|
isNonNewlineSpace,
|
|
|
|
restofline,
|
|
|
|
eolof,
|
|
|
|
|
2020-07-20 18:09:46 +03:00
|
|
|
spacenonewline,
|
|
|
|
skipNonNewlineSpaces,
|
|
|
|
skipNonNewlineSpaces1,
|
|
|
|
skipNonNewlineSpaces',
|
|
|
|
|
2018-06-05 23:23:47 +03:00
|
|
|
-- * re-exports
|
|
|
|
CustomErr
|
|
|
|
)
|
|
|
|
where
|
2015-08-19 23:47:26 +03:00
|
|
|
|
2018-09-26 01:07:58 +03:00
|
|
|
import Control.Monad.Except (ExceptT)
|
2017-07-27 14:59:55 +03:00
|
|
|
import Control.Monad.State.Strict (StateT, evalStateT)
|
2015-08-19 23:47:26 +03:00
|
|
|
import Data.Char
|
2017-07-27 14:59:55 +03:00
|
|
|
import Data.Functor.Identity (Identity(..))
|
2015-08-19 23:47:26 +03:00
|
|
|
import Data.List
|
2016-07-29 18:57:10 +03:00
|
|
|
import Data.Text (Text)
|
2018-05-22 01:47:56 +03:00
|
|
|
import Text.Megaparsec
|
|
|
|
import Text.Megaparsec.Char
|
2018-06-11 22:49:14 +03:00
|
|
|
import Text.Megaparsec.Custom
|
2015-08-19 23:47:26 +03:00
|
|
|
import Text.Printf
|
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
import Hledger.Data.Types
|
2015-08-19 23:47:26 +03:00
|
|
|
import Hledger.Utils.UTF8IOCompat (error')
|
|
|
|
|
2017-07-27 14:59:55 +03:00
|
|
|
-- | A parser of string to some type.
|
2018-06-05 23:23:47 +03:00
|
|
|
type SimpleStringParser a = Parsec CustomErr String a
|
2017-07-27 14:59:55 +03:00
|
|
|
|
|
|
|
-- | A parser of strict text to some type.
|
2018-06-05 23:23:47 +03:00
|
|
|
type SimpleTextParser = Parsec CustomErr Text -- XXX an "a" argument breaks the CsvRulesParser declaration somehow
|
2016-07-29 18:57:10 +03:00
|
|
|
|
2019-09-08 20:40:47 +03:00
|
|
|
-- | A parser of text that runs in some monad.
|
2018-06-05 23:23:47 +03:00
|
|
|
type TextParser m a = ParsecT CustomErr Text m a
|
2016-07-29 18:57:10 +03:00
|
|
|
|
2019-09-08 20:40:47 +03:00
|
|
|
-- | A parser of text that runs in some monad, keeping a Journal as state.
|
2018-06-05 23:23:47 +03:00
|
|
|
type JournalParser m a = StateT Journal (ParsecT CustomErr Text m) a
|
2016-07-29 18:57:10 +03:00
|
|
|
|
2019-09-08 20:40:47 +03:00
|
|
|
-- | A parser of text that runs in some monad, keeping a Journal as
|
|
|
|
-- state, that can throw an exception to end parsing, preventing
|
|
|
|
-- further parser backtracking.
|
2018-09-26 01:07:58 +03:00
|
|
|
type ErroringJournalParser m a =
|
|
|
|
StateT Journal (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
|
|
|
|
|
2015-08-19 23:47:26 +03:00
|
|
|
-- | Backtracking choice, use this when alternatives share a prefix.
|
|
|
|
-- Consumes no input if all choices fail.
|
2016-07-29 18:57:10 +03:00
|
|
|
choice' :: [TextParser m a] -> TextParser m a
|
2017-07-27 14:59:55 +03:00
|
|
|
choice' = choice . map try
|
2015-08-19 23:47:26 +03:00
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
-- | Backtracking choice, use this when alternatives share a prefix.
|
|
|
|
-- Consumes no input if all choices fail.
|
2018-06-05 23:23:47 +03:00
|
|
|
choiceInState :: [StateT s (ParsecT CustomErr Text m) a] -> StateT s (ParsecT CustomErr Text m) a
|
2017-07-27 14:59:55 +03:00
|
|
|
choiceInState = choice . map try
|
2016-07-29 18:57:10 +03:00
|
|
|
|
2017-11-26 06:58:53 +03:00
|
|
|
surroundedBy :: Applicative m => m openclose -> m a -> m a
|
|
|
|
surroundedBy p = between p p
|
|
|
|
|
2018-09-30 04:32:08 +03:00
|
|
|
parsewith :: Parsec e Text a -> Text -> Either (ParseErrorBundle Text e) a
|
2016-07-29 18:57:10 +03:00
|
|
|
parsewith p = runParser p ""
|
|
|
|
|
2018-09-30 04:32:08 +03:00
|
|
|
parsewithString
|
|
|
|
:: Parsec e String a -> String -> Either (ParseErrorBundle String e) a
|
2016-07-29 18:57:10 +03:00
|
|
|
parsewithString p = runParser p ""
|
2015-08-19 23:47:26 +03:00
|
|
|
|
2018-08-04 18:11:32 +03:00
|
|
|
-- | Run a stateful parser with some initial state on a text.
|
|
|
|
-- See also: runTextParser, runJournalParser.
|
2018-09-30 04:32:08 +03:00
|
|
|
parseWithState
|
|
|
|
:: Monad m
|
|
|
|
=> st
|
|
|
|
-> StateT st (ParsecT CustomErr Text m) a
|
|
|
|
-> Text
|
|
|
|
-> m (Either (ParseErrorBundle Text CustomErr) a)
|
2016-07-29 18:57:10 +03:00
|
|
|
parseWithState ctx p s = runParserT (evalStateT p ctx) "" s
|
2015-08-19 23:47:26 +03:00
|
|
|
|
2018-05-22 04:52:34 +03:00
|
|
|
parseWithState'
|
|
|
|
:: (Stream s)
|
|
|
|
=> st
|
|
|
|
-> StateT st (ParsecT e s Identity) a
|
|
|
|
-> s
|
2018-09-30 04:32:08 +03:00
|
|
|
-> (Either (ParseErrorBundle s e) a)
|
2016-07-29 18:57:10 +03:00
|
|
|
parseWithState' ctx p s = runParser (evalStateT p ctx) "" s
|
|
|
|
|
2018-09-30 04:32:08 +03:00
|
|
|
fromparse
|
|
|
|
:: (Show t, Show (Token t), Show e) => Either (ParseErrorBundle t e) a -> a
|
2015-08-19 23:47:26 +03:00
|
|
|
fromparse = either parseerror id
|
|
|
|
|
2018-09-30 04:32:08 +03:00
|
|
|
parseerror :: (Show t, Show (Token t), Show e) => ParseErrorBundle t e -> a
|
2020-08-06 02:05:56 +03:00
|
|
|
parseerror e = error' $ showParseError e -- PARTIAL:
|
2015-08-19 23:47:26 +03:00
|
|
|
|
2018-09-30 04:32:08 +03:00
|
|
|
showParseError
|
|
|
|
:: (Show t, Show (Token t), Show e)
|
|
|
|
=> ParseErrorBundle t e -> String
|
2015-08-19 23:47:26 +03:00
|
|
|
showParseError e = "parse error at " ++ show e
|
|
|
|
|
2018-09-30 04:32:08 +03:00
|
|
|
showDateParseError
|
|
|
|
:: (Show t, Show (Token t), Show e) => ParseErrorBundle t e -> String
|
2015-08-19 23:47:26 +03:00
|
|
|
showDateParseError e = printf "date parse error (%s)" (intercalate ", " $ tail $ lines $ show e)
|
|
|
|
|
2021-06-24 03:12:17 +03:00
|
|
|
isNewline :: Char -> Bool
|
|
|
|
isNewline '\n' = True
|
|
|
|
isNewline _ = False
|
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
nonspace :: TextParser m Char
|
2015-08-19 23:47:26 +03:00
|
|
|
nonspace = satisfy (not . isSpace)
|
|
|
|
|
2018-05-22 04:09:47 +03:00
|
|
|
isNonNewlineSpace :: Char -> Bool
|
2021-06-24 03:12:17 +03:00
|
|
|
isNonNewlineSpace c = not (isNewline c) && isSpace c
|
2018-05-22 04:09:47 +03:00
|
|
|
|
2018-06-05 23:23:47 +03:00
|
|
|
spacenonewline :: (Stream s, Char ~ Token s) => ParsecT CustomErr s m Char
|
2018-05-22 04:09:47 +03:00
|
|
|
spacenonewline = satisfy isNonNewlineSpace
|
2020-07-20 18:09:46 +03:00
|
|
|
{-# INLINABLE spacenonewline #-}
|
2015-08-19 23:47:26 +03:00
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
restofline :: TextParser m String
|
2020-02-28 12:54:26 +03:00
|
|
|
restofline = anySingle `manyTill` eolof
|
2015-08-19 23:47:26 +03:00
|
|
|
|
2020-07-20 18:09:46 +03:00
|
|
|
-- Skip many non-newline spaces.
|
|
|
|
skipNonNewlineSpaces :: (Stream s, Token s ~ Char) => ParsecT CustomErr s m ()
|
|
|
|
skipNonNewlineSpaces = () <$ takeWhileP Nothing isNonNewlineSpace
|
|
|
|
{-# INLINABLE skipNonNewlineSpaces #-}
|
|
|
|
|
|
|
|
-- Skip many non-newline spaces, failing if there are none.
|
|
|
|
skipNonNewlineSpaces1 :: (Stream s, Token s ~ Char) => ParsecT CustomErr s m ()
|
|
|
|
skipNonNewlineSpaces1 = () <$ takeWhile1P Nothing isNonNewlineSpace
|
|
|
|
{-# INLINABLE skipNonNewlineSpaces1 #-}
|
|
|
|
|
|
|
|
-- Skip many non-newline spaces, returning True if any have been skipped.
|
|
|
|
skipNonNewlineSpaces' :: (Stream s, Token s ~ Char) => ParsecT CustomErr s m Bool
|
|
|
|
skipNonNewlineSpaces' = True <$ skipNonNewlineSpaces1 <|> pure False
|
|
|
|
{-# INLINABLE skipNonNewlineSpaces' #-}
|
|
|
|
|
|
|
|
|
2016-07-29 18:57:10 +03:00
|
|
|
eolof :: TextParser m ()
|
2015-08-19 23:47:26 +03:00
|
|
|
eolof = (newline >> return ()) <|> eof
|