{-# LANGUAGE  MagicHash,
              UnboxedTuples,
              ScopedTypeVariables #-}

module UU.Parsing.CharParser where
import UU.Parsing.Interface
import UU.Scanner.Position


type CharParser = AnaParser Input Pair Char Pos

instance Symbol Char where
 symBefore :: Char -> Char
symBefore    = Char -> Char
forall a. Enum a => a -> a
pred
 symAfter :: Char -> Char
symAfter     = Char -> Char
forall a. Enum a => a -> a
succ
 deleteCost :: Char -> Int#
deleteCost Char
_ = Int#
5#

data Input = Input String !Pos

instance InputState Input Char Pos where
  splitStateE :: Input -> Either' Input Char
splitStateE (Input String
inp Pos
pos) = 
        case String
inp of
          (Char
'\CR':      String
xs) -> case String
xs of
                                (Char
'\LF' : String
_ ) -> Char -> Input -> Either' Input Char
forall state s. s -> state -> Either' state s
Left' Char
'\CR' (String -> Pos -> Input
Input String
xs Pos
pos)
                                String
_            -> Char -> Input -> Either' Input Char
forall state s. s -> state -> Either' state s
Left' Char
'\CR' (String -> Pos -> Input
Input String
xs (Pos -> Pos
newl Pos
pos))
          (Char
'\LF':      String
xs) -> Char -> Input -> Either' Input Char
forall state s. s -> state -> Either' state s
Left' Char
'\LF' (String -> Pos -> Input
Input String
xs (Pos -> Pos
newl   Pos
pos))
--          ('\n' :      xs) -> Left' '\n'  (Input xs (newl pos))  -- \n already captured above
          (Char
'\t' :      String
xs) -> Char -> Input -> Either' Input Char
forall state s. s -> state -> Either' state s
Left' Char
'\t' (String -> Pos -> Input
Input String
xs (Pos -> Pos
tab    Pos
pos))
          (Char
x    :      String
xs) -> Char -> Input -> Either' Input Char
forall state s. s -> state -> Either' state s
Left' Char
x    (String -> Pos -> Input
Input String
xs (Column -> Pos -> Pos
advc Column
1 Pos
pos))
          []               -> Input -> Either' Input Char
forall state s. state -> Either' state s
Right'     (String -> Pos -> Input
Input [] Pos
pos)
            
  splitState :: Input -> (# Char, Input #)
splitState  (Input String
inp Pos
pos) =  
        case String
inp of
          (Char
'\CR':      String
xs) -> case String
xs of
                                (Char
'\LF' : String
_ ) -> (# Char
'\CR', String -> Pos -> Input
Input String
xs Pos
pos #)
                                String
_            -> (# Char
'\CR', String -> Pos -> Input
Input String
xs (Pos -> Pos
newl Pos
pos) #)
          (Char
'\LF':      String
xs) -> (# Char
'\LF', String -> Pos -> Input
Input String
xs (Pos -> Pos
newl   Pos
pos) #)
--          ('\n' :      xs) -> ( '\n' , Input xs (newl   pos)) -- \n already captured above
          (Char
'\t' :      String
xs) -> (# Char
'\t' , String -> Pos -> Input
Input String
xs (Pos -> Pos
tab    Pos
pos) #)
          (Char
x    :      String
xs) -> (# Char
x    , String -> Pos -> Input
Input String
xs (Column -> Pos -> Pos
advc Column
1 Pos
pos) #)

  getPosition :: Input -> Pos
getPosition (Input String
inp Pos
pos) = Pos
pos

parseString :: CharParser a 
            -> [Char] 
            -> Steps (Pair a (Pair Input ())) Char Pos
parseString :: forall a.
CharParser a -> String -> Steps (Pair a (Pair Input ())) Char Pos
parseString CharParser a
p String
txt = CharParser a -> Input -> Steps (Pair a (Pair Input ())) Char Pos
forall s inp pos a.
(Symbol s, InputState inp s pos) =>
AnaParser inp Pair s pos a
-> inp -> Steps (Pair a (Pair inp ())) s pos
parse CharParser a
p ((String -> Pos -> Input
Input String
txt (String -> Pos
initPos String
"")))

parseStringIO :: (Message Char Pos -> String) 
              -> CharParser a 
              -> [Char] 
              -> IO a
parseStringIO :: forall a.
(Message Char Pos -> String) -> CharParser a -> String -> IO a
parseStringIO Message Char Pos -> String
showM CharParser a
p String
txt = (Message Char Pos -> String) -> CharParser a -> Input -> IO a
forall s inp p a.
(Symbol s, InputState inp s p) =>
(Message s p -> String) -> AnaParser inp Pair s p a -> inp -> IO a
parseIOMessage Message Char Pos -> String
showM CharParser a
p (String -> Pos -> Input
Input String
txt (String -> Pos
initPos String
""))

parseFile :: (Message Char Pos -> String) -> CharParser a -> [Char] -> IO a
parseFile :: forall a.
(Message Char Pos -> String) -> CharParser a -> String -> IO a
parseFile Message Char Pos -> String
showM CharParser a
p String
filename = do String
txt <- String -> IO String
readFile String
filename
                                (Message Char Pos -> String) -> CharParser a -> Input -> IO a
forall s inp p a.
(Symbol s, InputState inp s p) =>
(Message s p -> String) -> AnaParser inp Pair s p a -> inp -> IO a
parseIOMessage Message Char Pos -> String
showM CharParser a
p (String -> Pos -> Input
Input String
txt (String -> Pos
initPos String
filename))