module TOML.Errors where
import Control.Exception
import Data.List
import Data.Text (Text)
import qualified Data.Text as Text
import TOML.Tokens
import TOML.Located
data TOMLError
= Unexpected (Located Token)
| Unterminated (Located Token)
| OverlappingKey [Text]
deriving (Read, Show)
instance Exception TOMLError where
displayException (Unexpected (Located pos token)) =
show (posLine pos) ++ ":" ++ show (posColumn pos) ++
": unexpected " ++ showToken token
displayException (Unterminated (Located pos token)) =
show (posLine pos) ++ ":" ++ show (posColumn pos) ++
": unterminated " ++ showToken token
displayException (OverlappingKey path) =
"multiple definitions of: " ++
intercalate "." (map Text.unpack path)
showToken :: Token -> String
showToken t =
case t of
StringToken{} -> "string literal"
BareKeyToken k -> "table key ‘" ++ Text.unpack k ++ "’"
IntegerToken i -> "integer " ++ show i
DoubleToken d -> "float " ++ show d
ZonedTimeToken dt -> "offset date-time " ++ show dt
LocalTimeToken dt -> "local data-time " ++ show dt
DayToken dt -> "local date " ++ show dt
TimeOfDayToken dt -> "local time " ++ show dt
CommaToken -> "‘,’"
PeriodToken -> "‘.’"
LeftBracketToken -> "‘[’"
RightBracketToken -> "‘]’"
LeftBraceToken -> "‘{’"
RightBraceToken -> "‘}’"
EqualToken -> "‘=’"
TrueToken -> "‘true’"
FalseToken -> "‘false’"
ErrorToken e -> "lexical error: " ++ showLexerError e
EofToken -> "end-of-file"
showLexerError :: LexerError -> String
showLexerError e =
case e of
UntermString -> "unterminated string literal"
BadEscape -> "bad escape sequence"
NoMatch c -> "unexpected ‘" ++ [c] ++ "’"