dev: Move journal and text parsing utilities into more appropriate places, to reduce reliance on Hledger.Read.Common.
This commit is contained in:
		
							parent
							
								
									1e766e481e
								
							
						
					
					
						commit
						df71d2ddd5
					
				| @ -35,6 +35,7 @@ module Hledger.Read ( | |||||||
|   JournalReader.tmpostingrulep, |   JournalReader.tmpostingrulep, | ||||||
|   findReader, |   findReader, | ||||||
|   splitReaderPrefix, |   splitReaderPrefix, | ||||||
|  |   runJournalParser, | ||||||
|   module Hledger.Read.Common, |   module Hledger.Read.Common, | ||||||
|   module Hledger.Read.InputOptions, |   module Hledger.Read.InputOptions, | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -35,12 +35,6 @@ module Hledger.Read.Common ( | |||||||
|   rawOptsToInputOpts, |   rawOptsToInputOpts, | ||||||
| 
 | 
 | ||||||
|   -- * parsing utilities |   -- * parsing utilities | ||||||
|   runTextParser, |  | ||||||
|   rtp, |  | ||||||
|   runJournalParser, |  | ||||||
|   rjp, |  | ||||||
|   runErroringJournalParser, |  | ||||||
|   rejp, |  | ||||||
|   genericSourcePos, |   genericSourcePos, | ||||||
|   journalSourcePos, |   journalSourcePos, | ||||||
|   parseAndFinaliseJournal, |   parseAndFinaliseJournal, | ||||||
| @ -139,7 +133,6 @@ import Data.Decimal (DecimalRaw (Decimal), Decimal) | |||||||
| import Data.Either (lefts, rights) | import Data.Either (lefts, rights) | ||||||
| import Data.Function ((&)) | import Data.Function ((&)) | ||||||
| import Data.Functor ((<&>)) | import Data.Functor ((<&>)) | ||||||
| import Data.Functor.Identity (Identity) |  | ||||||
| import "base-compat-batteries" Data.List.Compat | import "base-compat-batteries" Data.List.Compat | ||||||
| import Data.List.NonEmpty (NonEmpty(..)) | import Data.List.NonEmpty (NonEmpty(..)) | ||||||
| import Data.Maybe (catMaybes, fromMaybe, isJust, listToMaybe) | import Data.Maybe (catMaybes, fromMaybe, isJust, listToMaybe) | ||||||
| @ -155,8 +148,7 @@ import Text.Megaparsec | |||||||
| import Text.Megaparsec.Char (char, char', digitChar, newline, string) | import Text.Megaparsec.Char (char, char', digitChar, newline, string) | ||||||
| import Text.Megaparsec.Char.Lexer (decimal) | import Text.Megaparsec.Char.Lexer (decimal) | ||||||
| import Text.Megaparsec.Custom | import Text.Megaparsec.Custom | ||||||
|   (FinalParseError, attachSource, customErrorBundlePretty, |   (attachSource, customErrorBundlePretty, finalErrorBundlePretty, parseErrorAt, parseErrorAtRegion) | ||||||
|   finalErrorBundlePretty, parseErrorAt, parseErrorAtRegion) |  | ||||||
| 
 | 
 | ||||||
| import Hledger.Data | import Hledger.Data | ||||||
| import Hledger.Query (Query(..), filterQuery, parseQueryTerm, queryEndDate, queryStartDate, queryIsDate, simplifyQuery) | import Hledger.Query (Query(..), filterQuery, parseQueryTerm, queryEndDate, queryStartDate, queryIsDate, simplifyQuery) | ||||||
| @ -261,31 +253,6 @@ commodityStyleFromRawOpts rawOpts = | |||||||
|         Left _ -> Left optStr |         Left _ -> Left optStr | ||||||
|         Right (Amount acommodity _ astyle _) -> Right (acommodity, astyle) |         Right (Amount acommodity _ astyle _) -> Right (acommodity, astyle) | ||||||
| 
 | 
 | ||||||
| --- ** parsing utilities |  | ||||||
| 
 |  | ||||||
| -- | Run a text parser in the identity monad. See also: parseWithState. |  | ||||||
| runTextParser, rtp |  | ||||||
|   :: TextParser Identity a -> Text -> Either (ParseErrorBundle Text CustomErr) a |  | ||||||
| runTextParser p =  runParser p "" |  | ||||||
| rtp = runTextParser |  | ||||||
| 
 |  | ||||||
| -- | Run a journal parser in some monad. See also: parseWithState. |  | ||||||
| runJournalParser, rjp |  | ||||||
|   :: Monad m |  | ||||||
|   => JournalParser m a -> Text -> m (Either (ParseErrorBundle Text CustomErr) a) |  | ||||||
| runJournalParser p = runParserT (evalStateT p nulljournal) "" |  | ||||||
| rjp = runJournalParser |  | ||||||
| 
 |  | ||||||
| -- | Run an erroring journal parser in some monad. See also: parseWithState. |  | ||||||
| runErroringJournalParser, rejp |  | ||||||
|   :: Monad m |  | ||||||
|   => ErroringJournalParser m a |  | ||||||
|   -> Text |  | ||||||
|   -> m (Either FinalParseError (Either (ParseErrorBundle Text CustomErr) a)) |  | ||||||
| runErroringJournalParser p t = |  | ||||||
|   runExceptT $ runParserT (evalStateT p nulljournal) "" t |  | ||||||
| rejp = runErroringJournalParser |  | ||||||
| 
 |  | ||||||
| genericSourcePos :: SourcePos -> GenericSourcePos | genericSourcePos :: SourcePos -> GenericSourcePos | ||||||
| genericSourcePos p = GenericSourcePos (sourceName p) (unPos $ sourceLine p) (unPos $ sourceColumn p) | genericSourcePos p = GenericSourcePos (sourceName p) (unPos $ sourceLine p) (unPos $ sourceColumn p) | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -48,6 +48,8 @@ module Hledger.Read.JournalReader ( | |||||||
|   parseAndFinaliseJournal, |   parseAndFinaliseJournal, | ||||||
|   runJournalParser, |   runJournalParser, | ||||||
|   rjp, |   rjp, | ||||||
|  |   runErroringJournalParser, | ||||||
|  |   rejp, | ||||||
| 
 | 
 | ||||||
|   -- * Parsers used elsewhere |   -- * Parsers used elsewhere | ||||||
|   getParentAccount, |   getParentAccount, | ||||||
| @ -77,7 +79,7 @@ import qualified Control.Exception as C | |||||||
| import Control.Monad (forM_, when, void) | import Control.Monad (forM_, when, void) | ||||||
| import Control.Monad.IO.Class (MonadIO, liftIO) | import Control.Monad.IO.Class (MonadIO, liftIO) | ||||||
| import Control.Monad.Except (ExceptT(..), runExceptT) | import Control.Monad.Except (ExceptT(..), runExceptT) | ||||||
| import Control.Monad.State.Strict (get,modify',put) | import Control.Monad.State.Strict (evalStateT,get,modify',put) | ||||||
| import Control.Monad.Trans.Class (lift) | import Control.Monad.Trans.Class (lift) | ||||||
| import Data.Char (toLower) | import Data.Char (toLower) | ||||||
| import Data.Either (isRight) | import Data.Either (isRight) | ||||||
| @ -108,6 +110,26 @@ import qualified Hledger.Read.CsvReader as CsvReader (reader) | |||||||
| --- ** doctest setup | --- ** doctest setup | ||||||
| -- $setup | -- $setup | ||||||
| -- >>> :set -XOverloadedStrings | -- >>> :set -XOverloadedStrings | ||||||
|  | -- | ||||||
|  | --- ** parsing utilities | ||||||
|  | 
 | ||||||
|  | -- | Run a journal parser in some monad. See also: parseWithState. | ||||||
|  | runJournalParser, rjp | ||||||
|  |   :: Monad m | ||||||
|  |   => JournalParser m a -> Text -> m (Either (ParseErrorBundle Text CustomErr) a) | ||||||
|  | runJournalParser p = runParserT (evalStateT p nulljournal) "" | ||||||
|  | rjp = runJournalParser | ||||||
|  | 
 | ||||||
|  | -- | Run an erroring journal parser in some monad. See also: parseWithState. | ||||||
|  | runErroringJournalParser, rejp | ||||||
|  |   :: Monad m | ||||||
|  |   => ErroringJournalParser m a | ||||||
|  |   -> Text | ||||||
|  |   -> m (Either FinalParseError (Either (ParseErrorBundle Text CustomErr) a)) | ||||||
|  | runErroringJournalParser p t = | ||||||
|  |   runExceptT $ runParserT (evalStateT p nulljournal) "" t | ||||||
|  | rejp = runErroringJournalParser | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| --- ** reader finding utilities | --- ** reader finding utilities | ||||||
| -- Defined here rather than Hledger.Read so that we can use them in includedirectivep below. | -- Defined here rather than Hledger.Read so that we can use them in includedirectivep below. | ||||||
|  | |||||||
| @ -10,6 +10,8 @@ module Hledger.Utils.Parse ( | |||||||
|   choiceInState, |   choiceInState, | ||||||
|   surroundedBy, |   surroundedBy, | ||||||
|   parsewith, |   parsewith, | ||||||
|  |   runTextParser, | ||||||
|  |   rtp, | ||||||
|   parsewithString, |   parsewithString, | ||||||
|   parseWithState, |   parseWithState, | ||||||
|   parseWithState', |   parseWithState', | ||||||
| @ -68,6 +70,12 @@ surroundedBy p = between p p | |||||||
| parsewith :: Parsec e Text a -> Text -> Either (ParseErrorBundle Text e) a | parsewith :: Parsec e Text a -> Text -> Either (ParseErrorBundle Text e) a | ||||||
| parsewith p = runParser p "" | parsewith p = runParser p "" | ||||||
| 
 | 
 | ||||||
|  | -- | Run a text parser in the identity monad. See also: parseWithState. | ||||||
|  | runTextParser, rtp | ||||||
|  |   :: TextParser Identity a -> Text -> Either (ParseErrorBundle Text CustomErr) a | ||||||
|  | runTextParser = parsewith | ||||||
|  | rtp = runTextParser | ||||||
|  | 
 | ||||||
| parsewithString | parsewithString | ||||||
|   :: Parsec e String a -> String -> Either (ParseErrorBundle String e) a |   :: Parsec e String a -> String -> Either (ParseErrorBundle String e) a | ||||||
| parsewithString p = runParser p "" | parsewithString p = runParser p "" | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user