Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module provides a parser for python format string mini language.
Synopsis
- parseGenericFormatString :: Parser [Item]
- data Item
- = Raw String
- | Replacement (HsExpr GhcPs, Exp) (Maybe FormatMode)
- data FormatMode = FormatMode Padding TypeFormat (Maybe Char)
- data Padding
- = PaddingDefault
- | Padding (ExprOrValue Int) (Maybe (Maybe Char, AnyAlign))
- data Precision
- data TypeFormat
- = DefaultF Precision SignMode
- | BinaryF AlternateForm SignMode
- | CharacterF
- | DecimalF SignMode
- | ExponentialF Precision AlternateForm SignMode
- | ExponentialCapsF Precision AlternateForm SignMode
- | FixedF Precision AlternateForm SignMode
- | FixedCapsF Precision AlternateForm SignMode
- | GeneralF Precision AlternateForm SignMode
- | GeneralCapsF Precision AlternateForm SignMode
- | OctalF AlternateForm SignMode
- | StringF Precision
- | HexF AlternateForm SignMode
- | HexCapsF AlternateForm SignMode
- | PercentF Precision AlternateForm SignMode
- data AlternateForm
- pattern DefaultFormatMode :: FormatMode
- type Parser t = ParsecT String () (Reader ParsingContext) t
- data ParsingContext = ParsingContext {
- delimiters :: Maybe (Char, Char)
- enabledExtensions :: [Extension]
- data ExprOrValue t
- = Value t
- | HaskellExpr (HsExpr GhcPs, Exp)
Documentation
parseGenericFormatString :: Parser [Item] Source #
Parse a string, returns a list of raw string or replacement fields
>>>
import Text.Megaparsec
>>>
parse parsePythonFormatString "" "hello {1+1:>10.2f}"
Right [ Raw "hello ", Replacement "1+1" ( Just (FormatMode (Padding 10 (Just (Nothing,AnyAlign AlignRight))) (FixedF (Precision 2) NormalForm Minus) Nothing))]
A format string is composed of many chunks of raw string or replacement
Raw String | A raw string |
Replacement (HsExpr GhcPs, Exp) (Maybe FormatMode) | A replacement string, composed of an arbitrary Haskell expression followed by an optional formatter |
data FormatMode Source #
A Formatter, listing padding, format and and grouping char
Padding, containing the padding width, the padding char and the alignement mode
Floating point precision
Instances
Data Precision Source # | |
Defined in PyF.Internal.PythonSyntax gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Precision -> c Precision # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Precision # toConstr :: Precision -> Constr # dataTypeOf :: Precision -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Precision) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Precision) # gmapT :: (forall b. Data b => b -> b) -> Precision -> Precision # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Precision -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Precision -> r # gmapQ :: (forall d. Data d => d -> u) -> Precision -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Precision -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Precision -> m Precision # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Precision -> m Precision # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Precision -> m Precision # |
data TypeFormat Source #
All formatting type
DefaultF Precision SignMode | Default, depends on the infered type of the expression |
BinaryF AlternateForm SignMode | Binary, such as `0b0121` |
CharacterF | Character, will convert an integer to its character representation |
DecimalF SignMode | Decimal, base 10 integer formatting |
ExponentialF Precision AlternateForm SignMode | Exponential notation for floatting points |
ExponentialCapsF Precision AlternateForm SignMode | Exponential notation with capitalised |
FixedF Precision AlternateForm SignMode | Fixed number of digits floating point |
FixedCapsF Precision AlternateForm SignMode | Capitalized version of the previous |
GeneralF Precision AlternateForm SignMode | General formatting: |
GeneralCapsF Precision AlternateForm SignMode | Same as |
OctalF AlternateForm SignMode | Octal, such as 00245 |
StringF Precision | Simple string |
HexF AlternateForm SignMode | Hexadecimal, such as 0xaf3e |
HexCapsF AlternateForm SignMode | Hexadecimal with capitalized letters, such as 0XAF3E |
PercentF Precision AlternateForm SignMode | Percent representation |
Instances
Data TypeFormat Source # | |
Defined in PyF.Internal.PythonSyntax gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> TypeFormat -> c TypeFormat # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c TypeFormat # toConstr :: TypeFormat -> Constr # dataTypeOf :: TypeFormat -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c TypeFormat) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TypeFormat) # gmapT :: (forall b. Data b => b -> b) -> TypeFormat -> TypeFormat # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> TypeFormat -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> TypeFormat -> r # gmapQ :: (forall d. Data d => d -> u) -> TypeFormat -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> TypeFormat -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> TypeFormat -> m TypeFormat # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> TypeFormat -> m TypeFormat # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> TypeFormat -> m TypeFormat # |
data AlternateForm Source #
If the formatter use its alternate form
Instances
Data AlternateForm Source # | |
Defined in PyF.Internal.PythonSyntax gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> AlternateForm -> c AlternateForm # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c AlternateForm # toConstr :: AlternateForm -> Constr # dataTypeOf :: AlternateForm -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c AlternateForm) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c AlternateForm) # gmapT :: (forall b. Data b => b -> b) -> AlternateForm -> AlternateForm # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> AlternateForm -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> AlternateForm -> r # gmapQ :: (forall d. Data d => d -> u) -> AlternateForm -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> AlternateForm -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> AlternateForm -> m AlternateForm # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> AlternateForm -> m AlternateForm # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> AlternateForm -> m AlternateForm # | |
Show AlternateForm Source # | |
Defined in PyF.Internal.PythonSyntax showsPrec :: Int -> AlternateForm -> ShowS # show :: AlternateForm -> String # showList :: [AlternateForm] -> ShowS # |
pattern DefaultFormatMode :: FormatMode Source #
Default formatting mode, no padding, default precision, no grouping, no sign handling
data ParsingContext Source #
ParsingContext | |
|
Instances
Show ParsingContext Source # | |
Defined in PyF.Internal.PythonSyntax showsPrec :: Int -> ParsingContext -> ShowS # show :: ParsingContext -> String # showList :: [ParsingContext] -> ShowS # |
data ExprOrValue t Source #
Represents a value of type t
or an Haskell expression supposed to represents that value
Value t | |
HaskellExpr (HsExpr GhcPs, Exp) |
Instances
Data t => Data (ExprOrValue t) Source # | |
Defined in PyF.Internal.PythonSyntax gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ExprOrValue t -> c (ExprOrValue t) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (ExprOrValue t) # toConstr :: ExprOrValue t -> Constr # dataTypeOf :: ExprOrValue t -> DataType # dataCast1 :: Typeable t0 => (forall d. Data d => c (t0 d)) -> Maybe (c (ExprOrValue t)) # dataCast2 :: Typeable t0 => (forall d e. (Data d, Data e) => c (t0 d e)) -> Maybe (c (ExprOrValue t)) # gmapT :: (forall b. Data b => b -> b) -> ExprOrValue t -> ExprOrValue t # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ExprOrValue t -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ExprOrValue t -> r # gmapQ :: (forall d. Data d => d -> u) -> ExprOrValue t -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> ExprOrValue t -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> ExprOrValue t -> m (ExprOrValue t) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ExprOrValue t -> m (ExprOrValue t) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ExprOrValue t -> m (ExprOrValue t) # |