hledger-lib-1.33.1: A library providing the core functionality of hledger

Safe Haskell | None
Language | Haskell2010

Hledger.Utils.Parse

Contents

- SourcePos
  - Trace the state of hledger parsers
- re-exports

Synopsis

- type SimpleStringParser a = Parsec HledgerParseErrorData String a
- type SimpleTextParser = Parsec HledgerParseErrorData Text
- type TextParser (m :: Type -> Type) a = ParsecT HledgerParseErrorData Text m a
- data SourcePos = SourcePos {
  - sourceName :: FilePath
  - sourceLine :: !Pos
  - sourceColumn :: !Pos
  }
- mkPos :: Int -> Pos
- unPos :: Pos -> Int
- initialPos :: FilePath -> SourcePos
- sourcePosPretty :: SourcePos -> String
- sourcePosPairPretty :: (SourcePos, SourcePos) -> String
- choice' :: forall (m :: Type -> Type) a. [TextParser m a] -> TextParser m a
- choiceInState :: forall s (m :: Type -> Type) a. [StateT s (ParsecT HledgerParseErrorData Text m) a] -> StateT s (ParsecT HledgerParseErrorData Text m) a
- surroundedBy :: Applicative m => m openclose -> m a -> m a
- parsewith :: Parsec e Text a -> Text -> Either (ParseErrorBundle Text e) a
- runTextParser :: TextParser Identity a -> Text -> Either HledgerParseErrors a
- rtp :: TextParser Identity a -> Text -> Either HledgerParseErrors a
- parsewithString :: Parsec e String a -> String -> Either (ParseErrorBundle String e) a
- parseWithState :: Monad m => st -> StateT st (ParsecT HledgerParseErrorData Text m) a -> Text -> m (Either HledgerParseErrors a)
- parseWithState' :: Stream s => st -> StateT st (ParsecT e s Identity) a -> s -> Either (ParseErrorBundle s e) a
- fromparse :: (Show t, Show (Token t), Show e) => Either (ParseErrorBundle t e) a -> a
- parseerror :: (Show t, Show (Token t), Show e) => ParseErrorBundle t e -> a
- showDateParseError :: (Show t, Show (Token t), Show e) => ParseErrorBundle t e -> String
- nonspace :: forall (m :: Type -> Type). TextParser m Char
- isNewline :: Char -> Bool
- isNonNewlineSpace :: Char -> Bool
- restofline :: forall (m :: Type -> Type). TextParser m String
- eolof :: forall (m :: Type -> Type). TextParser m ()
- spacenonewline :: forall s (m :: Type -> Type). (Stream s, Char ~ Token s) => ParsecT HledgerParseErrorData s m Char
- skipNonNewlineSpaces :: forall s (m :: Type -> Type). (Stream s, Token s ~ Char) => ParsecT HledgerParseErrorData s m ()
- skipNonNewlineSpaces1 :: forall s (m :: Type -> Type). (Stream s, Token s ~ Char) => ParsecT HledgerParseErrorData s m ()
- skipNonNewlineSpaces' :: forall s (m :: Type -> Type). (Stream s, Token s ~ Char) => ParsecT HledgerParseErrorData s m Bool
- dbgparse :: forall (m :: Type -> Type). Int -> String -> TextParser m ()
- traceOrLogParse :: forall (m :: Type -> Type). String -> TextParser m ()
- type HledgerParseErrors = ParseErrorBundle Text HledgerParseErrorData
- data HledgerParseErrorData
- customErrorBundlePretty :: HledgerParseErrors -> String

# Documentation

type SimpleStringParser a = Parsec HledgerParseErrorData String a #

A parser of string to some type.

type SimpleTextParser = Parsec HledgerParseErrorData Text #

A parser of strict text to some type.

type TextParser (m :: Type -> Type) a = ParsecT HledgerParseErrorData Text m a #

A parser of text that runs in some monad.

# SourcePos

data SourcePos #

The data type `SourcePos` represents source positions. It contains the name of the source file, a line number, and a column number. Source line and column positions change intensively during parsing, so we need to make them strict to avoid memory leaks.

Constructors

SourcePos |
Fields sourceName :: FilePath Name of source file sourceLine :: !Pos Line number sourceColumn :: !Pos Column number

#### Instances

Instances details

FromJSON SourcePos # |
Instance details Defined in Hledger.Data.Json Methods parseJSON :: Value -> Parser SourcePos # parseJSONList :: Value -> Parser [SourcePos] # omittedField :: Maybe SourcePos #
ToJSON SourcePos # |
Instance details Defined in Hledger.Data.Json Methods toJSON :: SourcePos -> Value # toEncoding :: SourcePos -> Encoding # toJSONList :: [SourcePos] -> Value # toEncodingList :: [SourcePos] -> Encoding # omitField :: SourcePos -> Bool #
Data SourcePos |
Instance details Defined in Text.Megaparsec.Pos Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> SourcePos -> c SourcePos # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c SourcePos # toConstr :: SourcePos -> Constr # dataTypeOf :: SourcePos -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c SourcePos) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SourcePos) # gmapT :: (forall b. Data b => b -> b) -> SourcePos -> SourcePos # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> SourcePos -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> SourcePos -> r # gmapQ :: (forall d. Data d => d -> u) -> SourcePos -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> SourcePos -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos #
Generic SourcePos |
Instance details Defined in Text.Megaparsec.Pos Associated Types
type Rep SourcePos |
Instance details Defined in Text.Megaparsec.Pos type Rep SourcePos = D1 ('MetaData "SourcePos" "Text.Megaparsec.Pos" "megaparsec-9.6.1-9o4Rv4HGhFA9HyJqQHvRdY" 'False) (C1 ('MetaCons "SourcePos" 'PrefixI 'True) (S1 ('MetaSel ('Just "sourceName") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 FilePath) :*: (S1 ('MetaSel ('Just "sourceLine") 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 Pos) :*: S1 ('MetaSel ('Just "sourceColumn") 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 Pos))))
Methods from :: SourcePos -> Rep SourcePos x # to :: Rep SourcePos x -> SourcePos #
Read SourcePos |
Instance details Defined in Text.Megaparsec.Pos Methods readsPrec :: Int -> ReadS SourcePos # readList :: ReadS [SourcePos] # readPrec :: ReadPrec SourcePos # readListPrec :: ReadPrec [SourcePos] #
Show SourcePos |
Instance details Defined in Text.Megaparsec.Pos Methods showsPrec :: Int -> SourcePos -> ShowS # show :: SourcePos -> String # showList :: [SourcePos] -> ShowS #
NFData SourcePos |
Instance details Defined in Text.Megaparsec.Pos Methods rnf :: SourcePos -> () #
Eq SourcePos |
Instance details Defined in Text.Megaparsec.Pos Methods (==) :: SourcePos -> SourcePos -> Bool # (/=) :: SourcePos -> SourcePos -> Bool #
Ord SourcePos |
Instance details Defined in Text.Megaparsec.Pos Methods compare :: SourcePos -> SourcePos -> Ordering # (<) :: SourcePos -> SourcePos -> Bool # (<=) :: SourcePos -> SourcePos -> Bool # (>) :: SourcePos -> SourcePos -> Bool # (>=) :: SourcePos -> SourcePos -> Bool # max :: SourcePos -> SourcePos -> SourcePos # min :: SourcePos -> SourcePos -> SourcePos #
type Rep SourcePos |
Instance details Defined in Text.Megaparsec.Pos type Rep SourcePos = D1 ('MetaData "SourcePos" "Text.Megaparsec.Pos" "megaparsec-9.6.1-9o4Rv4HGhFA9HyJqQHvRdY" 'False) (C1 ('MetaCons "SourcePos" 'PrefixI 'True) (S1 ('MetaSel ('Just "sourceName") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 FilePath) :*: (S1 ('MetaSel ('Just "sourceLine") 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 Pos) :*: S1 ('MetaSel ('Just "sourceColumn") 'NoSourceUnpackedness 'SourceStrict 'DecidedUnpack) (Rec0 Pos))))

mkPos :: Int -> Pos #

Construction of `Pos` from `Int`. The function throws `InvalidPosException` when given a non-positive argument.

Since: megaparsec-6.0.0

unPos :: Pos -> Int #

Extract `Int` from `Pos`.

Since: megaparsec-6.0.0

initialPos :: FilePath -> SourcePos #

Construct initial position (line 1, column 1) given name of source file.

sourcePosPretty :: SourcePos -> String #

Pretty-print a `SourcePos`.

Since: megaparsec-5.0.0

sourcePosPairPretty :: (SourcePos, SourcePos) -> String #

Render a pair of source positions in human-readable form, only displaying the range of lines.

choice' :: forall (m :: Type -> Type) a. [TextParser m a] -> TextParser m a #

Backtracking choice, use this when alternatives share a prefix. Consumes no input if all choices fail.

choiceInState :: forall s (m :: Type -> Type) a. [StateT s (ParsecT HledgerParseErrorData Text m) a] -> StateT s (ParsecT HledgerParseErrorData Text m) a #

Backtracking choice, use this when alternatives share a prefix. Consumes no input if all choices fail.

surroundedBy :: Applicative m => m openclose -> m a -> m a #

parsewith :: Parsec e Text a -> Text -> Either (ParseErrorBundle Text e) a #

runTextParser :: TextParser Identity a -> Text -> Either HledgerParseErrors a #

Run a text parser in the identity monad. See also: parseWithState.

rtp :: TextParser Identity a -> Text -> Either HledgerParseErrors a #

Run a text parser in the identity monad. See also: parseWithState.

parsewithString :: Parsec e String a -> String -> Either (ParseErrorBundle String e) a #

parseWithState :: Monad m => st -> StateT st (ParsecT HledgerParseErrorData Text m) a -> Text -> m (Either HledgerParseErrors a) #

Run a stateful parser with some initial state on a text. See also: runTextParser, runJournalParser.

parseWithState' :: Stream s => st -> StateT st (ParsecT e s Identity) a -> s -> Either (ParseErrorBundle s e) a #

fromparse :: (Show t, Show (Token t), Show e) => Either (ParseErrorBundle t e) a -> a #

parseerror :: (Show t, Show (Token t), Show e) => ParseErrorBundle t e -> a #

showDateParseError :: (Show t, Show (Token t), Show e) => ParseErrorBundle t e -> String #

nonspace :: forall (m :: Type -> Type). TextParser m Char #

isNewline :: Char -> Bool #

isNonNewlineSpace :: Char -> Bool #

restofline :: forall (m :: Type -> Type). TextParser m String #

eolof :: forall (m :: Type -> Type). TextParser m () #

spacenonewline :: forall s (m :: Type -> Type). (Stream s, Char ~ Token s) => ParsecT HledgerParseErrorData s m Char #

skipNonNewlineSpaces :: forall s (m :: Type -> Type). (Stream s, Token s ~ Char) => ParsecT HledgerParseErrorData s m () #

skipNonNewlineSpaces1 :: forall s (m :: Type -> Type). (Stream s, Token s ~ Char) => ParsecT HledgerParseErrorData s m () #

skipNonNewlineSpaces' :: forall s (m :: Type -> Type). (Stream s, Token s ~ Char) => ParsecT HledgerParseErrorData s m Bool #

## Trace the state of hledger parsers

dbgparse :: forall (m :: Type -> Type). Int -> String -> TextParser m () #

Trace to stderr or log to debug log the provided label (if non-null) and current parser state (position and next input), if the global debug level is at or above the specified level. Uses unsafePerformIO.

traceOrLogParse :: forall (m :: Type -> Type). String -> TextParser m () #

Trace to stderr or log to debug log the provided label (if non-null) and current parser state (position and next input). See also: Hledger.Utils.Debug, megaparsec's dbg. Uses unsafePerformIO. XXX Can be hard to make this evaluate.

# re-exports

type HledgerParseErrors = ParseErrorBundle Text HledgerParseErrorData #

A specialised version of ParseErrorBundle: a non-empty collection of hledger parse errors, equipped with PosState to help pretty-print them. Specialised for a `Text` parse stream.

data HledgerParseErrorData #

Custom error data for hledger parsers. Specialised for a `Text` parse stream. ReparseableTextParseErrorData ?

#### Instances

Instances details

Show HledgerParseErrorData # |
Instance details Defined in Text.Megaparsec.Custom Methods showsPrec :: Int -> HledgerParseErrorData -> ShowS # show :: HledgerParseErrorData -> String # showList :: [HledgerParseErrorData] -> ShowS #
Eq HledgerParseErrorData # |
Instance details Defined in Text.Megaparsec.Custom Methods (==) :: HledgerParseErrorData -> HledgerParseErrorData -> Bool # (/=) :: HledgerParseErrorData -> HledgerParseErrorData -> Bool #
Ord HledgerParseErrorData # |
Instance details Defined in Text.Megaparsec.Custom Methods compare :: HledgerParseErrorData -> HledgerParseErrorData -> Ordering # (<) :: HledgerParseErrorData -> HledgerParseErrorData -> Bool # (<=) :: HledgerParseErrorData -> HledgerParseErrorData -> Bool # (>) :: HledgerParseErrorData -> HledgerParseErrorData -> Bool # (>=) :: HledgerParseErrorData -> HledgerParseErrorData -> Bool # max :: HledgerParseErrorData -> HledgerParseErrorData -> HledgerParseErrorData # min :: HledgerParseErrorData -> HledgerParseErrorData -> HledgerParseErrorData #
ShowErrorComponent HledgerParseErrorData # |
Instance details Defined in Text.Megaparsec.Custom Methods showErrorComponent :: HledgerParseErrorData -> String # errorComponentLen :: HledgerParseErrorData -> Int #
Ord (ParseError Text HledgerParseErrorData) # |
Instance details Defined in Text.Megaparsec.Custom Methods compare :: ParseError Text HledgerParseErrorData -> ParseError Text HledgerParseErrorData -> Ordering # (<) :: ParseError Text HledgerParseErrorData -> ParseError Text HledgerParseErrorData -> Bool # (<=) :: ParseError Text HledgerParseErrorData -> ParseError Text HledgerParseErrorData -> Bool # (>) :: ParseError Text HledgerParseErrorData -> ParseError Text HledgerParseErrorData -> Bool # (>=) :: ParseError Text HledgerParseErrorData -> ParseError Text HledgerParseErrorData -> Bool # max :: ParseError Text HledgerParseErrorData -> ParseError Text HledgerParseErrorData -> ParseError Text HledgerParseErrorData # min :: ParseError Text HledgerParseErrorData -> ParseError Text HledgerParseErrorData -> ParseError Text HledgerParseErrorData #

customErrorBundlePretty :: HledgerParseErrors -> String #

Pretty-print our custom parse errors. It is necessary to use this instead of `errorBundlePretty` when custom parse errors are thrown.

This function intercepts our custom parse errors and applies final adjustments (`finalizeCustomError`) before passing them to `errorBundlePretty`. These adjustments are part of the implementation of the behaviour of our custom parse errors.

Note: We must ensure that the offset of the `PosState` of the provided `ParseErrorBundle` is no larger than the offset specified by a `ErrorFailAt` constructor. This is guaranteed if this offset is set to 0 (that is, the beginning of the source file), which is the case for `ParseErrorBundle`s returned from `runParserT`.