> -- | helpers to work with parsec errors more nicely
> module Language.SQL.SimpleSQL.Errors
>     (ParseError(..)
>     --,formatError
>     ,convParseError
>     ) where

> import Text.Parsec (sourceColumn,sourceLine,sourceName,errorPos)
> import qualified Text.Parsec as P (ParseError)

> -- | Type to represent parse errors.
> data ParseError = ParseError
>                   {ParseError -> String
peErrorString :: String
>                    -- ^ contains the error message
>                   ,ParseError -> String
peFilename :: FilePath
>                    -- ^ filename location for the error
>                   ,ParseError -> (Int, Int)
pePosition :: (Int,Int)
>                    -- ^ line number and column number location for the error
>                   ,ParseError -> String
peFormattedError :: String
>                    -- ^ formatted error with the position, error
>                    -- message and source context
>                   } deriving (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,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)

> convParseError :: String -> P.ParseError -> ParseError
> convParseError :: String -> ParseError -> ParseError
convParseError src :: String
src e :: ParseError
e =
>     ParseError :: String -> String -> (Int, Int) -> String -> ParseError
ParseError
>     {peErrorString :: String
peErrorString = ParseError -> String
forall a. Show a => a -> String
show ParseError
e
>     ,peFilename :: String
peFilename = SourcePos -> String
sourceName SourcePos
p
>     ,pePosition :: (Int, Int)
pePosition = (SourcePos -> Int
sourceLine SourcePos
p, SourcePos -> Int
sourceColumn SourcePos
p)
>     ,peFormattedError :: String
peFormattedError = String -> ParseError -> String
formatError String
src ParseError
e}
>   where
>     p :: SourcePos
p = ParseError -> SourcePos
errorPos ParseError
e

format the error more nicely: emacs format for positioning, plus
context

> formatError :: String -> P.ParseError -> String
> formatError :: String -> ParseError -> String
formatError src :: String
src e :: ParseError
e =
>     SourcePos -> String
sourceName SourcePos
p String -> ShowS
forall a. [a] -> [a] -> [a]
++ ":" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (SourcePos -> Int
sourceLine SourcePos
p)
>     String -> ShowS
forall a. [a] -> [a] -> [a]
++ ":" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (SourcePos -> Int
sourceColumn SourcePos
p) String -> ShowS
forall a. [a] -> [a] -> [a]
++ ":"
>     String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
context
>     String -> ShowS
forall a. [a] -> [a] -> [a]
++ ParseError -> String
forall a. Show a => a -> String
show ParseError
e
>   where
>     context :: String
context =
>         let lns :: [String]
lns = Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
take 1 ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop (SourcePos -> Int
sourceLine SourcePos
p Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines String
src
>         in case [String]
lns of
>              [x :: String
x] -> "\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ "\n"
>                     String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> Char -> String
forall a. Int -> a -> [a]
replicate (SourcePos -> Int
sourceColumn SourcePos
p Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) ' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ "^\n"
>              _ -> ""
>     p :: SourcePos
p = ParseError -> SourcePos
errorPos ParseError
e