Skip to content

Instantly share code, notes, and snippets.

@relrod
Created July 31, 2017 19:00
Show Gist options
  • Save relrod/e185d09dd645ee6a989d13884f827d43 to your computer and use it in GitHub Desktop.
Save relrod/e185d09dd645ee6a989d13884f827d43 to your computer and use it in GitHub Desktop.
module Stuff where
import Control.Monad (void)
import qualified Data.Text as T
import Text.Trifecta
import Text.Parser.Char
import Text.Parser.Expression
import Text.Parser.Combinators
data AnswerLetter = A | B | C | D deriving (Show, Eq, Read)
type Answers = [(AnswerLetter, String)]
data Question =
Question { identifier :: String
, answer :: AnswerLetter
, question :: String
, answers :: Answers
} deriving (Show, Eq)
q :: (Monad m, CharParsing m) => m Question
q = do
ident <- count 5 anyChar
space
char '('
ans <- (\x -> read [x]) <$> oneOf "ABCD"
char ')'
manyTill anyChar (try newline)
ques <- manyTill anyChar (try newline)
ansA <- drop 3 <$> manyTill anyChar (try newline)
ansB <- drop 3 <$> manyTill anyChar (try newline)
ansC <- drop 3 <$> manyTill anyChar (try newline)
ansD <- drop 3 <$> manyTill anyChar (try newline)
choice [eof, void (string "--\n")]
return (Question ident ans ques [(A, ansA), (B, ansB), (C, ansC), (D, ansD)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment