~yoctocell/flaskell

0013743dcce562c339c58e2070486af282f3d0d6 — Xinglu Chen 1 year, 7 months ago 5982e6e
Use "-" as separator

* Parser.hs (parseHint): Use `manyTill` and consume preceding
whitespace.
* test.txt: Update accordingly.
2 files changed, 22 insertions(+), 17 deletions(-)

M src/Flaskell/Parser.hs
M test/test.txt
M src/Flaskell/Parser.hs => src/Flaskell/Parser.hs +20 -15
@@ 1,13 1,19 @@
{-# LANGUAGE OverloadedStrings #-}

module Flaskell.Parser
  -- ( parseFlashcard
  -- , Hint
  -- , Answer
  -- , Guess
  -- , Flashcard
  -- )
                       where
  ( parseFlashcard
  , Hint
  , Answer
  , Guess
  , Flashcard
  )
where

import           Control.Applicative            ( (*>)
                                                , (<*)
                                                , liftA2
                                                )
import           Control.Monad
-- import           Text.Parsec.Char               ( noneOf
--                                                 , oneOf
--                                                 , endOfLine


@@ 22,19 28,17 @@ module Flaskell.Parser
--                                                 )
-- import           Text.Parsec.Text               ( Parser )
import           Data.Attoparsec.Text           ( Parser
                                                , anyChar
                                                , char
                                                , many'
                                                , manyTill
                                                , notChar
                                                , sepBy
                                                , parseOnly
                                                , sepBy
                                                , string
                                                )
import           Data.OrgMode.Parse.Attoparsec.Headline
import           Control.Monad
import           Data.Text                      ( Text )
import           Control.Applicative            ( (<*)
                                                , (*>)
                                                , liftA2
                                                )

type Hint = String
type Answer = String


@@ 46,11 50,12 @@ type Flashcard = (Hint, Answer)

-- | Parse hint from flashcard
parseHint :: Parser String
parseHint = char '-' *> char ' ' *> many' (notChar '|')
parseHint =
  many' (char ' ') *> char '-' *> char ' ' *> manyTill anyChar (string " - ")

-- | Parse answer from flashcard
parseAnswer :: Parser String
parseAnswer = char '|' *> char ' ' *> many' (notChar '\n')
parseAnswer = many' (notChar '\n')

-- | Return a tuple of hint and answer
-- flashcard :: Parser Flashcard

M test/test.txt => test/test.txt +2 -2
@@ 1,2 1,2 @@
- hint 1 | answer 1
- hint 2 | answer 2
- hint 1 - answer 1
- hint 2 - answer 2