-- | IB016 Seminar on Functional Programming -- | Code from lecture on monadic parsing import Text.Parsec import Control.Monad -- | Convenience type for parsers on String without state using Identity monad. type Parser a = Parsec String () a lectureString :: String lectureString = "Lecture10 Monadic parsing; spring 2018" runLectureParser :: Show a => Parser a -> IO () runLectureParser parser = do case runParser parser () "lecture string" lectureString of Left err -> print err Right x -> print x lecture1 :: Parser (Int, String, String, Int) lecture1 = do string "Lecture" id <- count 2 digit space name <- many (noneOf [';']) string "; " semester <- many letter space year <- count 4 digit pure (read id, name, semester, read year) data Lecture = Lecture Int String Semester deriving Show data Semester = Semester String Int deriving Show semester2 :: Parser Semester semester2 = liftM2 Semester (string "spring" <|> string "autumn") (spaces *> fmap read (count 4 digit)) "semester spec" lecture2 :: Parser Lecture lecture2 = liftM3 Lecture (string "Lecture" *> fmap read (count 2 digit <* spaces)) (many (noneOf [';']) <* string "; ") (semester2) "lecture spec"