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, | ||||
|   findReader, | ||||
|   splitReaderPrefix, | ||||
|   runJournalParser, | ||||
|   module Hledger.Read.Common, | ||||
|   module Hledger.Read.InputOptions, | ||||
| 
 | ||||
|  | ||||
| @ -35,12 +35,6 @@ module Hledger.Read.Common ( | ||||
|   rawOptsToInputOpts, | ||||
| 
 | ||||
|   -- * parsing utilities | ||||
|   runTextParser, | ||||
|   rtp, | ||||
|   runJournalParser, | ||||
|   rjp, | ||||
|   runErroringJournalParser, | ||||
|   rejp, | ||||
|   genericSourcePos, | ||||
|   journalSourcePos, | ||||
|   parseAndFinaliseJournal, | ||||
| @ -139,7 +133,6 @@ import Data.Decimal (DecimalRaw (Decimal), Decimal) | ||||
| import Data.Either (lefts, rights) | ||||
| import Data.Function ((&)) | ||||
| import Data.Functor ((<&>)) | ||||
| import Data.Functor.Identity (Identity) | ||||
| import "base-compat-batteries" Data.List.Compat | ||||
| import Data.List.NonEmpty (NonEmpty(..)) | ||||
| 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.Lexer (decimal) | ||||
| import Text.Megaparsec.Custom | ||||
|   (FinalParseError, attachSource, customErrorBundlePretty, | ||||
|   finalErrorBundlePretty, parseErrorAt, parseErrorAtRegion) | ||||
|   (attachSource, customErrorBundlePretty, finalErrorBundlePretty, parseErrorAt, parseErrorAtRegion) | ||||
| 
 | ||||
| import Hledger.Data | ||||
| import Hledger.Query (Query(..), filterQuery, parseQueryTerm, queryEndDate, queryStartDate, queryIsDate, simplifyQuery) | ||||
| @ -261,31 +253,6 @@ commodityStyleFromRawOpts rawOpts = | ||||
|         Left _ -> Left optStr | ||||
|         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 p = GenericSourcePos (sourceName p) (unPos $ sourceLine p) (unPos $ sourceColumn p) | ||||
| 
 | ||||
|  | ||||
| @ -48,6 +48,8 @@ module Hledger.Read.JournalReader ( | ||||
|   parseAndFinaliseJournal, | ||||
|   runJournalParser, | ||||
|   rjp, | ||||
|   runErroringJournalParser, | ||||
|   rejp, | ||||
| 
 | ||||
|   -- * Parsers used elsewhere | ||||
|   getParentAccount, | ||||
| @ -77,7 +79,7 @@ import qualified Control.Exception as C | ||||
| import Control.Monad (forM_, when, void) | ||||
| import Control.Monad.IO.Class (MonadIO, liftIO) | ||||
| 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 Data.Char (toLower) | ||||
| import Data.Either (isRight) | ||||
| @ -108,6 +110,26 @@ import qualified Hledger.Read.CsvReader as CsvReader (reader) | ||||
| --- ** doctest setup | ||||
| -- $setup | ||||
| -- >>> :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 | ||||
| -- Defined here rather than Hledger.Read so that we can use them in includedirectivep below. | ||||
|  | ||||
| @ -10,6 +10,8 @@ module Hledger.Utils.Parse ( | ||||
|   choiceInState, | ||||
|   surroundedBy, | ||||
|   parsewith, | ||||
|   runTextParser, | ||||
|   rtp, | ||||
|   parsewithString, | ||||
|   parseWithState, | ||||
|   parseWithState', | ||||
| @ -68,6 +70,12 @@ surroundedBy p = between p p | ||||
| parsewith :: Parsec e Text a -> Text -> Either (ParseErrorBundle Text e) a | ||||
| 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 | ||||
|   :: Parsec e String a -> String -> Either (ParseErrorBundle String e) a | ||||
| parsewithString p = runParser p "" | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user