module Toml.Parser.Item
( TomlItem (..)
, Table (..)
, setTableName
, tomlP
, keyValP
) where
import Control.Applicative (liftA2, many)
import Control.Applicative.Combinators.NonEmpty (sepEndBy1)
import Control.Monad.Combinators (between, sepEndBy)
import Data.Foldable (asum)
import Data.List.NonEmpty (NonEmpty)
import Toml.Parser.Core (Parser, eof, sc, text, try, (<?>))
import Toml.Parser.Key (keyP, tableArrayNameP, tableNameP)
import Toml.Parser.Value (anyValueP)
import Toml.Type.AnyValue (AnyValue)
import Toml.Type.Key (Key)
data TomlItem
= TableName !Key
| TableArrayName !Key
| KeyVal !Key !AnyValue
| InlineTable !Key !Table
| InlineTableArray !Key !(NonEmpty Table)
deriving stock (Int -> TomlItem -> ShowS
[TomlItem] -> ShowS
TomlItem -> String
(Int -> TomlItem -> ShowS)
-> (TomlItem -> String) -> ([TomlItem] -> ShowS) -> Show TomlItem
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TomlItem] -> ShowS
$cshowList :: [TomlItem] -> ShowS
show :: TomlItem -> String
$cshow :: TomlItem -> String
showsPrec :: Int -> TomlItem -> ShowS
$cshowsPrec :: Int -> TomlItem -> ShowS
Show, TomlItem -> TomlItem -> Bool
(TomlItem -> TomlItem -> Bool)
-> (TomlItem -> TomlItem -> Bool) -> Eq TomlItem
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TomlItem -> TomlItem -> Bool
$c/= :: TomlItem -> TomlItem -> Bool
== :: TomlItem -> TomlItem -> Bool
$c== :: TomlItem -> TomlItem -> Bool
Eq)
setTableName :: Key -> TomlItem -> TomlItem
setTableName :: Key -> TomlItem -> TomlItem
setTableName new :: Key
new = \case
TableName _ -> Key -> TomlItem
TableName Key
new
TableArrayName _ -> Key -> TomlItem
TableArrayName Key
new
item :: TomlItem
item -> TomlItem
item
newtype Table = Table
{ Table -> [(Key, AnyValue)]
unTable :: [(Key, AnyValue)]
} deriving stock (Int -> Table -> ShowS
[Table] -> ShowS
Table -> String
(Int -> Table -> ShowS)
-> (Table -> String) -> ([Table] -> ShowS) -> Show Table
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Table] -> ShowS
$cshowList :: [Table] -> ShowS
show :: Table -> String
$cshow :: Table -> String
showsPrec :: Int -> Table -> ShowS
$cshowsPrec :: Int -> Table -> ShowS
Show)
deriving newtype (Table -> Table -> Bool
(Table -> Table -> Bool) -> (Table -> Table -> Bool) -> Eq Table
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Table -> Table -> Bool
$c/= :: Table -> Table -> Bool
== :: Table -> Table -> Bool
$c== :: Table -> Table -> Bool
Eq)
inlineTableP :: Parser Table
inlineTableP :: Parser Table
inlineTableP =
([(Key, AnyValue)] -> Table)
-> ParsecT Void Text Identity [(Key, AnyValue)] -> Parser Table
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(Key, AnyValue)] -> Table
Table
(ParsecT Void Text Identity [(Key, AnyValue)] -> Parser Table)
-> ParsecT Void Text Identity [(Key, AnyValue)] -> Parser Table
forall a b. (a -> b) -> a -> b
$ ParsecT Void Text Identity Text
-> ParsecT Void Text Identity Text
-> ParsecT Void Text Identity [(Key, AnyValue)]
-> ParsecT Void Text Identity [(Key, AnyValue)]
forall (m :: * -> *) open close a.
Applicative m =>
m open -> m close -> m a -> m a
between (Text -> ParsecT Void Text Identity Text
text "{") (Text -> ParsecT Void Text Identity Text
text "}")
(ParsecT Void Text Identity [(Key, AnyValue)]
-> ParsecT Void Text Identity [(Key, AnyValue)])
-> ParsecT Void Text Identity [(Key, AnyValue)]
-> ParsecT Void Text Identity [(Key, AnyValue)]
forall a b. (a -> b) -> a -> b
$ (Key -> AnyValue -> (Key, AnyValue))
-> ParsecT Void Text Identity Key
-> ParsecT Void Text Identity AnyValue
-> ParsecT Void Text Identity (Key, AnyValue)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (ParsecT Void Text Identity Key
keyP ParsecT Void Text Identity Key
-> ParsecT Void Text Identity Text
-> ParsecT Void Text Identity Key
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void Text Identity Text
text "=") ParsecT Void Text Identity AnyValue
anyValueP ParsecT Void Text Identity (Key, AnyValue)
-> ParsecT Void Text Identity Text
-> ParsecT Void Text Identity [(Key, AnyValue)]
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
`sepEndBy` Text -> ParsecT Void Text Identity Text
text ","
inlineTableArrayP :: Parser (NonEmpty Table)
inlineTableArrayP :: Parser (NonEmpty Table)
inlineTableArrayP = ParsecT Void Text Identity Text
-> ParsecT Void Text Identity Text
-> Parser (NonEmpty Table)
-> Parser (NonEmpty Table)
forall (m :: * -> *) open close a.
Applicative m =>
m open -> m close -> m a -> m a
between (Text -> ParsecT Void Text Identity Text
text "[") (Text -> ParsecT Void Text Identity Text
text "]")
(Parser (NonEmpty Table) -> Parser (NonEmpty Table))
-> Parser (NonEmpty Table) -> Parser (NonEmpty Table)
forall a b. (a -> b) -> a -> b
$ Parser Table
inlineTableP Parser Table
-> ParsecT Void Text Identity Text -> Parser (NonEmpty Table)
forall (m :: * -> *) a sep.
Alternative m =>
m a -> m sep -> m (NonEmpty a)
`sepEndBy1` Text -> ParsecT Void Text Identity Text
text ","
tomlItemP :: Parser TomlItem
tomlItemP :: Parser TomlItem
tomlItemP = [Parser TomlItem] -> Parser TomlItem
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
[ Key -> TomlItem
TableName (Key -> TomlItem)
-> ParsecT Void Text Identity Key -> Parser TomlItem
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void Text Identity Key -> ParsecT Void Text Identity Key
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT Void Text Identity Key
tableNameP Parser TomlItem -> String -> Parser TomlItem
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> "table name"
, Key -> TomlItem
TableArrayName (Key -> TomlItem)
-> ParsecT Void Text Identity Key -> Parser TomlItem
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void Text Identity Key
tableArrayNameP Parser TomlItem -> String -> Parser TomlItem
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> "array of tables name"
, Parser TomlItem
keyValP
]
keyValP :: Parser TomlItem
keyValP :: Parser TomlItem
keyValP = do
Key
key <- ParsecT Void Text Identity Key
keyP ParsecT Void Text Identity Key
-> ParsecT Void Text Identity Text
-> ParsecT Void Text Identity Key
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Text -> ParsecT Void Text Identity Text
text "="
[Parser TomlItem] -> Parser TomlItem
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
[ Key -> Table -> TomlItem
InlineTable Key
key (Table -> TomlItem) -> Parser Table -> Parser TomlItem
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Table
inlineTableP Parser TomlItem -> String -> Parser TomlItem
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> "inline table"
, Key -> NonEmpty Table -> TomlItem
InlineTableArray Key
key (NonEmpty Table -> TomlItem)
-> Parser (NonEmpty Table) -> Parser TomlItem
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (NonEmpty Table) -> Parser (NonEmpty Table)
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try Parser (NonEmpty Table)
inlineTableArrayP Parser TomlItem -> String -> Parser TomlItem
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> "inline array of tables"
, Key -> AnyValue -> TomlItem
KeyVal Key
key (AnyValue -> TomlItem)
-> ParsecT Void Text Identity AnyValue -> Parser TomlItem
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Void Text Identity AnyValue
anyValueP Parser TomlItem -> String -> Parser TomlItem
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> "key-value pair"
]
tomlP :: Parser [TomlItem]
tomlP :: Parser [TomlItem]
tomlP = Parser ()
sc Parser () -> Parser [TomlItem] -> Parser [TomlItem]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser TomlItem -> Parser [TomlItem]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many Parser TomlItem
tomlItemP Parser [TomlItem] -> Parser () -> Parser [TomlItem]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ()
forall e s (m :: * -> *). MonadParsec e s m => m ()
eof