{-# LANGUAGE Safe #-}
module Config
(
parse
, Position(..)
, pretty
, prettyInline
, Section(..)
, Value(..)
, Atom(..)
, valueAnn
, Number
, numberToInteger
, numberToRational
, integerToNumber
, rationalToNumber
, ParseError(..)
) where
import Config.Number (Number, numberToInteger, numberToRational, integerToNumber, rationalToNumber)
import Config.Value (Atom(..), Value(..), Section(..), valueAnn)
import Config.Parser (parseValue)
import Config.Pretty (pretty, prettyInline)
import Config.Lexer (scanTokens)
import Config.Tokens (Error(..), Position(..), Located(..), layoutPass, Token)
import qualified Config.Tokens as T
import Control.Exception (Exception(..))
import Data.Text (Text)
import qualified Data.Text as Text
parse ::
Text ->
Either ParseError (Value Position)
parse :: Text -> Either ParseError (Value Position)
parse Text
txt =
case [Located Token] -> Either (Located Token) (Value Position)
parseValue ([Located Token] -> [Located Token]
layoutPass (Text -> [Located Token]
scanTokens Text
txt)) of
Right Value Position
x -> Value Position -> Either ParseError (Value Position)
forall a b. b -> Either a b
Right Value Position
x
Left (Located Position
posn Token
token) -> ParseError -> Either ParseError (Value Position)
forall a b. a -> Either a b
Left (Position -> String -> ParseError
ParseError Position
posn (Token -> String
explainToken Token
token))
data ParseError = ParseError Position String
deriving (ReadPrec [ParseError]
ReadPrec ParseError
Int -> ReadS ParseError
ReadS [ParseError]
(Int -> ReadS ParseError)
-> ReadS [ParseError]
-> ReadPrec ParseError
-> ReadPrec [ParseError]
-> Read ParseError
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ParseError]
$creadListPrec :: ReadPrec [ParseError]
readPrec :: ReadPrec ParseError
$creadPrec :: ReadPrec ParseError
readList :: ReadS [ParseError]
$creadList :: ReadS [ParseError]
readsPrec :: Int -> ReadS ParseError
$creadsPrec :: Int -> ReadS ParseError
Read, Int -> ParseError -> ShowS
[ParseError] -> ShowS
ParseError -> String
(Int -> ParseError -> ShowS)
-> (ParseError -> String)
-> ([ParseError] -> ShowS)
-> Show ParseError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseError] -> ShowS
$cshowList :: [ParseError] -> ShowS
show :: ParseError -> String
$cshow :: ParseError -> String
showsPrec :: Int -> ParseError -> ShowS
$cshowsPrec :: Int -> ParseError -> ShowS
Show, ParseError -> ParseError -> Bool
(ParseError -> ParseError -> Bool)
-> (ParseError -> ParseError -> Bool) -> Eq ParseError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParseError -> ParseError -> Bool
$c/= :: ParseError -> ParseError -> Bool
== :: ParseError -> ParseError -> Bool
$c== :: ParseError -> ParseError -> Bool
Eq, Eq ParseError
Eq ParseError
-> (ParseError -> ParseError -> Ordering)
-> (ParseError -> ParseError -> Bool)
-> (ParseError -> ParseError -> Bool)
-> (ParseError -> ParseError -> Bool)
-> (ParseError -> ParseError -> Bool)
-> (ParseError -> ParseError -> ParseError)
-> (ParseError -> ParseError -> ParseError)
-> Ord ParseError
ParseError -> ParseError -> Bool
ParseError -> ParseError -> Ordering
ParseError -> ParseError -> ParseError
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ParseError -> ParseError -> ParseError
$cmin :: ParseError -> ParseError -> ParseError
max :: ParseError -> ParseError -> ParseError
$cmax :: ParseError -> ParseError -> ParseError
>= :: ParseError -> ParseError -> Bool
$c>= :: ParseError -> ParseError -> Bool
> :: ParseError -> ParseError -> Bool
$c> :: ParseError -> ParseError -> Bool
<= :: ParseError -> ParseError -> Bool
$c<= :: ParseError -> ParseError -> Bool
< :: ParseError -> ParseError -> Bool
$c< :: ParseError -> ParseError -> Bool
compare :: ParseError -> ParseError -> Ordering
$ccompare :: ParseError -> ParseError -> Ordering
$cp1Ord :: Eq ParseError
Ord)
instance Exception ParseError where
displayException :: ParseError -> String
displayException (ParseError Position
posn String
msg) =
String
"line " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Position -> Int
posLine Position
posn) String -> ShowS
forall a. [a] -> [a] -> [a]
++
String
" column " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Position -> Int
posColumn Position
posn) String -> ShowS
forall a. [a] -> [a] -> [a]
++
String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
msg
explainToken :: Token -> String
explainToken :: Token -> String
explainToken Token
token =
case Token
token of
T.Error Error
e -> Error -> String
explainError Error
e
T.Atom Text
atom -> String
"parse error: unexpected atom: `" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
Text.unpack Text
atom String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"`"
T.String Text
str -> String
"parse error: unexpected string: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show (Text -> String
Text.unpack Text
str)
T.Bullet Text
s -> String
"parse error: unexpected bullet '" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
Text.unpack Text
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"'"
Token
T.Comma -> String
"parse error: unexpected comma ','"
T.Section Text
s -> String
"parse error: unexpected section: `" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
Text.unpack Text
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"`"
T.Number{} -> String
"parse error: unexpected number"
Token
T.OpenList -> String
"parse error: unexpected start of list '['"
Token
T.CloseList -> String
"parse error: unexpected end of list ']'"
Token
T.OpenMap -> String
"parse error: unexpected start of section '{'"
Token
T.CloseMap -> String
"parse error: unexpected end of section '}'"
Token
T.LayoutSep -> String
"parse error: unexpected end of block"
Token
T.LayoutEnd -> String
"parse error: unexpected end of block"
Token
T.EOF -> String
"parse error: unexpected end of file"
explainError :: Error -> String
explainError :: Error -> String
explainError Error
e =
case Error
e of
Error
T.UntermComment -> String
"lexical error: unterminated comment"
Error
T.UntermString -> String
"lexical error: unterminated string literal"
Error
T.UntermSections -> String
"lexical error: unterminated sections"
Error
T.UntermList -> String
"lexical error: unterminated list"
T.BadEscape Text
c -> String
"lexical error: bad escape sequence: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
Text.unpack Text
c
T.NoMatch Char
c -> String
"lexical error at character " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Char -> String
forall a. Show a => a -> String
show Char
c