Skip to content

Commit

Permalink
update; fixed #1;
Browse files Browse the repository at this point in the history
fix a error: can not parsing the choices rightly when there are more than one choices in the single line
rewrite the codes to parsing those.
  • Loading branch information
Qinka committed Nov 24, 2017
1 parent 35cfb62 commit 0c7063d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion fluffy-parser/fluffy-parser.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- documentation, see http://haskell.org/cabal/users-guide/

name: fluffy-parser
version: 0.1.0.0
version: 0.1.0.50
-- synopsis:
-- description:
license: GPL-3
Expand Down
42 changes: 25 additions & 17 deletions fluffy-parser/src/Fluffy/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -330,42 +330,50 @@ parserMCQuestBody = do
str <- many anyChar
return (ans, reverse str)

parserMCQuestHead :: Stream s m Char => ParsecT s u m MultipleChoiceContext
parserMCQuestHead :: Stream s m Char => ParsecT s u m [MultipleChoiceContext]
parserMCQuestHead = do
spaces
string "Question"
spaces
many digit
spaces
char ':'
MCCQuestHead <$> many1 anyChar

parserMCQuestRest :: Stream s m Char => ParsecT s u m MultipleChoiceContext
parserMCQuestRest = MCCQuestBody <$> many1 anyChar

parserMCQuestItem :: Stream s m Char => ParsecT s u m MultipleChoiceContext
parserMCQuestItem = do
item <- (\x -> x - ord 'A') . ord <$> oneOf ['A'..'Z']
char ':'
spaces
str <- many1 anyChar
return $ MCCQuestItem item str
pure . MCCQuestHead <$> many1 anyChar

parserMCQuestRest :: Stream s m Char => ParsecT s u m [MultipleChoiceContext]
parserMCQuestRest = pure . MCCQuestBody <$> many1 anyChar

parserMCQuestItem :: Stream s m Char => ParsecT s u m [MultipleChoiceContext]
parserMCQuestItem = step []
where step xs = do
item <- (\x -> x - ord 'A') . ord <$> oneOf ['A'..'Z']
char ':'
spaces
str <- many $ noneOf ['A'..'Z']
let xs' = MCCQuestItem item str:xs
try (end xs') <|> try (step xs') <|> (more xs item str)
more xs item str = do
str' <- many (oneOf $ ':':['A'..'Z'])
str'' <- many $ noneOf ['A'..'Z']
let xs' = MCCQuestItem item (str++str'++str''):xs
try (end xs') <|> try (step xs') <|> (more xs item (str++str'++str''))
end xs = eof >> return xs

parserMC :: Stream s m Char => ParsecT s uP m MultipleChoiceContext
parserMC = try parserMCQuestHead <|> try parserMCQuestItem <|> parserMCQuestRest
parserMC :: Stream s m Char => ParsecT s uP m [MultipleChoiceContext]
parserMC = try parserMCQuestHead <|> try parserMCQuestItem <|> parserMCQuestRest

parseMCBody :: [(Int,String)] -> String -> MultipleChoiceProb
parseMCBody c b =
let (Right (ans,body)) = parse parserMCQuestBody "function parseMCBody" $ replace160 $ reverse b
in MultipleChoiceProb body ans c

parseMCfBlock :: Block -> MultipleChoiceContext
parseMCfBlock :: Block -> [MultipleChoiceContext]
parseMCfBlock (Para il) =
let body = renderText il
rt = parse parserMC "function parseMCBlock" $ replace160 body
in case rt of
Right i -> i
Left _ -> MCCNull
Left e -> error $ show e-- [MCCNull]

data MultipleChoiceProb = MultipleChoiceProb
{ mcbBody :: String
Expand Down
2 changes: 1 addition & 1 deletion fluffy/fluffy.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- documentation, see http://haskell.org/cabal/users-guide/

name: fluffy
version: 0.1.0.0
version: 0.1.0.70
-- synopsis:
-- description:
license: GPL-3
Expand Down
2 changes: 1 addition & 1 deletion scripts/update.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ readUpdateMC :: MonadIO m => Connection -> FilePath -> m ()
readUpdateMC c fp = do
Right (Pandoc _ bs) <- liftIO $ loadFileWithDocx fp
let mccs = map parseMCfBlock bs
mcps = toMCPfMCC mccs
mcps = toMCPfMCC $ concat mccs
mcs = map toMCfMCP mcps
mapM_ (liftIO.updateMC c) mcs

Expand Down

0 comments on commit 0c7063d

Please sign in to comment.