{-|
Module      : Toml.Raw
Description : Raw expressions from a parsed TOML file
Copyright   : (c) Eric Mertens, 2023
License     : ISC
Maintainer  : emertens@gmail.com

This module provides a raw representation of TOML files as
a list of table definitions and key-value assignments.

These values use the raw dotted keys and have no detection
for overlapping assignments.

Further processing will happen in the "Semantics" module.

-}
module Toml.Parser.Types (
    Key,
    Expr(..),
    Val(..),
    SectionKind(..),
    ) where

import Data.List.NonEmpty (NonEmpty)
import Data.Time (Day, LocalTime, TimeOfDay, ZonedTime)
import Toml.Located (Located)

-- | Non-empty sequence of dotted simple keys
type Key = NonEmpty (Located String)

-- | Headers and assignments corresponding to lines of a TOML file
data Expr
    = KeyValExpr     Key Val -- ^ key value assignment: @key = value@
    | TableExpr      Key     -- ^ table: @[key]@
    | ArrayTableExpr Key     -- ^ array of tables: @[[key]]@
    deriving (ReadPrec [Expr]
ReadPrec Expr
Int -> ReadS Expr
ReadS [Expr]
(Int -> ReadS Expr)
-> ReadS [Expr] -> ReadPrec Expr -> ReadPrec [Expr] -> Read Expr
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Expr
readsPrec :: Int -> ReadS Expr
$creadList :: ReadS [Expr]
readList :: ReadS [Expr]
$creadPrec :: ReadPrec Expr
readPrec :: ReadPrec Expr
$creadListPrec :: ReadPrec [Expr]
readListPrec :: ReadPrec [Expr]
Read, Int -> Expr -> ShowS
[Expr] -> ShowS
Expr -> String
(Int -> Expr -> ShowS)
-> (Expr -> String) -> ([Expr] -> ShowS) -> Show Expr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Expr -> ShowS
showsPrec :: Int -> Expr -> ShowS
$cshow :: Expr -> String
show :: Expr -> String
$cshowList :: [Expr] -> ShowS
showList :: [Expr] -> ShowS
Show)

-- | Unvalidated TOML values. Table are represented as a list of
-- assignments rather than as resolved maps.
data Val
    = ValInteger   Integer
    | ValFloat     Double
    | ValArray     [Val]
    | ValTable     [(Key, Val)]
    | ValBool      Bool
    | ValString    String
    | ValTimeOfDay TimeOfDay
    | ValZonedTime ZonedTime
    | ValLocalTime LocalTime
    | ValDay       Day
    deriving (ReadPrec [Val]
ReadPrec Val
Int -> ReadS Val
ReadS [Val]
(Int -> ReadS Val)
-> ReadS [Val] -> ReadPrec Val -> ReadPrec [Val] -> Read Val
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Val
readsPrec :: Int -> ReadS Val
$creadList :: ReadS [Val]
readList :: ReadS [Val]
$creadPrec :: ReadPrec Val
readPrec :: ReadPrec Val
$creadListPrec :: ReadPrec [Val]
readListPrec :: ReadPrec [Val]
Read, Int -> Val -> ShowS
[Val] -> ShowS
Val -> String
(Int -> Val -> ShowS)
-> (Val -> String) -> ([Val] -> ShowS) -> Show Val
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Val -> ShowS
showsPrec :: Int -> Val -> ShowS
$cshow :: Val -> String
show :: Val -> String
$cshowList :: [Val] -> ShowS
showList :: [Val] -> ShowS
Show)

-- | Kinds of table headers.
data SectionKind
    = TableKind -- ^ [table]
    | ArrayTableKind -- ^ [[array of tables]]
    deriving (ReadPrec [SectionKind]
ReadPrec SectionKind
Int -> ReadS SectionKind
ReadS [SectionKind]
(Int -> ReadS SectionKind)
-> ReadS [SectionKind]
-> ReadPrec SectionKind
-> ReadPrec [SectionKind]
-> Read SectionKind
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS SectionKind
readsPrec :: Int -> ReadS SectionKind
$creadList :: ReadS [SectionKind]
readList :: ReadS [SectionKind]
$creadPrec :: ReadPrec SectionKind
readPrec :: ReadPrec SectionKind
$creadListPrec :: ReadPrec [SectionKind]
readListPrec :: ReadPrec [SectionKind]
Read, Int -> SectionKind -> ShowS
[SectionKind] -> ShowS
SectionKind -> String
(Int -> SectionKind -> ShowS)
-> (SectionKind -> String)
-> ([SectionKind] -> ShowS)
-> Show SectionKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SectionKind -> ShowS
showsPrec :: Int -> SectionKind -> ShowS
$cshow :: SectionKind -> String
show :: SectionKind -> String
$cshowList :: [SectionKind] -> ShowS
showList :: [SectionKind] -> ShowS
Show, SectionKind -> SectionKind -> Bool
(SectionKind -> SectionKind -> Bool)
-> (SectionKind -> SectionKind -> Bool) -> Eq SectionKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SectionKind -> SectionKind -> Bool
== :: SectionKind -> SectionKind -> Bool
$c/= :: SectionKind -> SectionKind -> Bool
/= :: SectionKind -> SectionKind -> Bool
Eq)