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

module UU.Parsing.CharParser where
import GHC.Prim
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 _ = 5#

data Input = Input String !Pos

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

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

parseString :: CharParser a 
            -> [Char] 
            -> Steps (Pair a (Pair Input ())) Char Pos
parseString :: CharParser a -> String -> Steps (Pair a (Pair Input ())) Char Pos
parseString p :: CharParser a
p txt :: 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 "")))

parseStringIO :: (Message Char Pos -> String) 
              -> CharParser a 
              -> [Char] 
              -> IO a
parseStringIO :: (Message Char Pos -> String) -> CharParser a -> String -> IO a
parseStringIO showM :: Message Char Pos -> String
showM p :: CharParser a
p txt :: 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 ""))

parseFile :: (Message Char Pos -> String) -> CharParser a -> [Char] -> IO a
parseFile :: (Message Char Pos -> String) -> CharParser a -> String -> IO a
parseFile showM :: Message Char Pos -> String
showM p :: CharParser a
p filename :: 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))