module Toml.Syntax.Token (
Token(..),
mkBinInteger,
mkDecInteger,
mkOctInteger,
mkHexInteger,
mkFloat,
localDatePatterns,
localTimePatterns,
localDateTimePatterns,
offsetDateTimePatterns,
) where
import Data.Char (digitToInt)
import Data.Time (Day, LocalTime, TimeOfDay, ZonedTime)
import Data.Text (Text)
import Numeric (readInt, readHex, readOct)
data Token
= TokTrue
| TokFalse
| TokComma
| TokEquals
| TokNewline
| TokPeriod
| TokSquareO
| TokSquareC
| Tok2SquareO
| Tok2SquareC
| TokCurlyO
| TokCurlyC
| TokBareKey Text
| TokString Text
| TokMlString Text
| TokInteger !Integer
| TokFloat !Double
| TokOffsetDateTime !ZonedTime
| TokLocalDateTime !LocalTime
| TokLocalDate !Day
| TokLocalTime !TimeOfDay
| TokEOF
deriving (ReadPrec [Token]
ReadPrec Token
Int -> ReadS Token
ReadS [Token]
(Int -> ReadS Token)
-> ReadS [Token]
-> ReadPrec Token
-> ReadPrec [Token]
-> Read Token
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Token
readsPrec :: Int -> ReadS Token
$creadList :: ReadS [Token]
readList :: ReadS [Token]
$creadPrec :: ReadPrec Token
readPrec :: ReadPrec Token
$creadListPrec :: ReadPrec [Token]
readListPrec :: ReadPrec [Token]
Read, Int -> Token -> ShowS
[Token] -> ShowS
Token -> String
(Int -> Token -> ShowS)
-> (Token -> String) -> ([Token] -> ShowS) -> Show Token
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Token -> ShowS
showsPrec :: Int -> Token -> ShowS
$cshow :: Token -> String
show :: Token -> String
$cshowList :: [Token] -> ShowS
showList :: [Token] -> ShowS
Show)
scrub :: String -> String
scrub :: ShowS
scrub = (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
filter (Char
'_' Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=)
mkDecInteger :: String -> Token
mkDecInteger :: String -> Token
mkDecInteger (Char
'+':String
xs) = Integer -> Token
TokInteger (String -> Integer
forall a. Read a => String -> a
read (ShowS
scrub String
xs))
mkDecInteger String
xs = Integer -> Token
TokInteger (String -> Integer
forall a. Read a => String -> a
read (ShowS
scrub String
xs))
mkHexInteger :: String -> Token
mkHexInteger :: String -> Token
mkHexInteger (Char
'0':Char
'x':String
xs) = Integer -> Token
TokInteger ((Integer, String) -> Integer
forall a b. (a, b) -> a
fst ([(Integer, String)] -> (Integer, String)
forall a. HasCallStack => [a] -> a
head (ReadS Integer
forall a. (Eq a, Num a) => ReadS a
readHex (ShowS
scrub String
xs))))
mkHexInteger String
_ = String -> Token
forall a. HasCallStack => String -> a
error String
"processHex: bad input"
mkOctInteger :: String -> Token
mkOctInteger :: String -> Token
mkOctInteger (Char
'0':Char
'o':String
xs) = Integer -> Token
TokInteger ((Integer, String) -> Integer
forall a b. (a, b) -> a
fst ([(Integer, String)] -> (Integer, String)
forall a. HasCallStack => [a] -> a
head (ReadS Integer
forall a. (Eq a, Num a) => ReadS a
readOct (ShowS
scrub String
xs))))
mkOctInteger String
_ = String -> Token
forall a. HasCallStack => String -> a
error String
"processHex: bad input"
mkBinInteger :: String -> Token
mkBinInteger :: String -> Token
mkBinInteger (Char
'0':Char
'b':String
xs) = Integer -> Token
TokInteger ((Integer, String) -> Integer
forall a b. (a, b) -> a
fst ([(Integer, String)] -> (Integer, String)
forall a. HasCallStack => [a] -> a
head (ReadS Integer
forall a. (Eq a, Num a) => ReadS a
readBin (ShowS
scrub String
xs))))
mkBinInteger String
_ = String -> Token
forall a. HasCallStack => String -> a
error String
"processHex: bad input"
readBin :: (Eq a, Num a) => ReadS a
readBin :: forall a. (Eq a, Num a) => ReadS a
readBin = a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
forall a. Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
readInt a
2 Char -> Bool
isBinDigit Char -> Int
digitToInt
isBinDigit :: Char -> Bool
isBinDigit :: Char -> Bool
isBinDigit Char
x = Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'0' Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'1'
mkFloat :: String -> Token
mkFloat :: String -> Token
mkFloat String
"nan" = Double -> Token
TokFloat (Double
0Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0)
mkFloat String
"+nan" = Double -> Token
TokFloat (Double
0Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0)
mkFloat String
"-nan" = Double -> Token
TokFloat (Double
0Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0)
mkFloat String
"inf" = Double -> Token
TokFloat (Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0)
mkFloat String
"+inf" = Double -> Token
TokFloat (Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0)
mkFloat String
"-inf" = Double -> Token
TokFloat (-Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0)
mkFloat (Char
'+':String
x) = Double -> Token
TokFloat (String -> Double
forall a. Read a => String -> a
read (ShowS
scrub String
x))
mkFloat String
x = Double -> Token
TokFloat (String -> Double
forall a. Read a => String -> a
read (ShowS
scrub String
x))
localDatePatterns :: [String]
localDatePatterns :: [String]
localDatePatterns = [String
"%Y-%m-%d"]
localTimePatterns :: [String]
localTimePatterns :: [String]
localTimePatterns = [String
"%H:%M:%S%Q"]
localDateTimePatterns :: [String]
localDateTimePatterns :: [String]
localDateTimePatterns =
[String
"%Y-%m-%dT%H:%M:%S%Q",
String
"%Y-%m-%d %H:%M:%S%Q"]
offsetDateTimePatterns :: [String]
offsetDateTimePatterns :: [String]
offsetDateTimePatterns =
[String
"%Y-%m-%dT%H:%M:%S%Q%Ez",String
"%Y-%m-%dT%H:%M:%S%QZ",
String
"%Y-%m-%d %H:%M:%S%Q%Ez",String
"%Y-%m-%d %H:%M:%S%QZ"]