Skip to content

Additions #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Text/Parsing/StringParser/Combinators.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Text.Parsing.StringParser.Combinators where

import Control.Alt ((<|>))
import Control.Apply ((*>))
import Data.Maybe (Maybe(..))

import Control.Alt

import Text.Parsing.StringParser

lookAhead :: forall a. Parser a -> Parser a
lookAhead p = Parser \ps fc sc -> unParser p ps fc (\s _ -> sc s ps)

many :: forall a. Parser a -> Parser [a]
many p = many1 p <|> return []

Expand Down Expand Up @@ -100,3 +102,10 @@ choice :: forall a. [Parser a] -> Parser a
choice [] = fail "Nothing to parse"
choice [x] = x
choice (x:xs) = x <|> choice xs

manyTill :: forall a end. Parser a -> Parser end -> Parser [a]
manyTill p end = scan
where
scan = (end *> return []) <|> do x <- p
xs <- scan
return (x:xs)
24 changes: 21 additions & 3 deletions src/Text/Parsing/StringParser/String.purs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
module Text.Parsing.StringParser.String where
module Text.Parsing.StringParser.String
( eof
, anyChar
, anyDigit
, string
) where

import Data.Maybe
import Data.String (charAt, fromChar, length, take, indexOf')
import Data.Maybe (Maybe(..))
import Data.String (charAt, fromChar, length, indexOf')
import Text.Parsing.StringParser
import qualified Data.String.Regex as Rx

eof :: Parser Unit
eof = Parser (\s fc sc -> case s of
Expand All @@ -15,6 +21,18 @@ anyChar = Parser (\s fc sc -> case s of
Just chr -> sc (fromChar chr) { str: str, pos: i + 1 }
Nothing -> fc i (ParseError "Unexpected EOF"))

anyDigit :: Parser String
anyDigit = Parser \{ str: str, pos: i } fc sc -> case charAt i str of
Just chr ->
let chrS = fromChar chr
in if Rx.test rxDigit chrS
then sc chrS { str: str, pos: i + 1 }
else fc i (ParseError "Expected digit")
Nothing -> fc i (ParseError "Unexpected EOF")
where
rxDigit :: Rx.Regex
rxDigit = Rx.regex "^[0-9]" Rx.noFlags

string :: String -> Parser String
string nt = Parser (\s fc sc -> case s of
{ str = str, pos = i } | indexOf' nt i str == i -> sc nt { str: str, pos: i + length nt }
Expand Down