Skip to content

Avoid Discard constraints #47

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 1 commit into from
Feb 15, 2017
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
29 changes: 7 additions & 22 deletions src/Text/Parsing/Parser/Combinators.purs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,15 @@ infix 3 asErrorMessage as <??>
-- | parens = between (string "(") (string ")")
-- | ```
between :: forall m s a open close. Monad m => ParserT s m open -> ParserT s m close -> ParserT s m a -> ParserT s m a
between open close p = do
open
a <- p
close
pure a
between open close p = open *> p <* close

-- | Provide a default result in the case where a parser fails without consuming input.
option :: forall m s a. Monad m => a -> ParserT s m a -> ParserT s m a
option a p = p <|> pure a

-- | Optionally parse something, failing quietly.
optional :: forall m s a. Monad m => ParserT s m a -> ParserT s m Unit
optional p = (do p
pure unit) <|> pure unit
optional p = (void p) <|> pure unit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Redundant parens here.


-- | pure `Nothing` in the case where a parser fails without consuming input.
optionMaybe :: forall m s a. Monad m => ParserT s m a -> ParserT s m (Maybe a)
Expand Down Expand Up @@ -99,9 +94,7 @@ sepBy p sep = sepBy1 p sep <|> pure Nil
sepBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
sepBy1 p sep = do
a <- p
as <- many $ do
sep
p
as <- many $ sep *> p
pure (a : as)

-- | Parse phrases delimited and optionally terminated by a separator.
Expand All @@ -112,23 +105,17 @@ sepEndBy p sep = sepEndBy1 p sep <|> pure Nil
sepEndBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
sepEndBy1 p sep = do
a <- p
(do sep
(do _ <- sep
as <- sepEndBy p sep
pure (a : as)) <|> pure (singleton a)

-- | Parse phrases delimited and terminated by a separator, requiring at least one match.
endBy1 :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
endBy1 p sep = some $ do
a <- p
sep
pure a
endBy1 p sep = some $ p <* sep

-- | Parse phrases delimited and terminated by a separator.
endBy :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
endBy p sep = many $ do
a <- p
sep
pure a
endBy p sep = many $ p <* sep

-- | Parse phrases delimited by a right-associative operator.
-- |
Expand Down Expand Up @@ -189,9 +176,7 @@ notFollowedBy p = try $ (try p *> fail "Negated parser succeeded") <|> pure unit
manyTill :: forall s a m e. Monad m => ParserT s m a -> ParserT s m e -> ParserT s m (List a)
manyTill p end = scan
where
scan = (do
end
pure Nil)
scan = (end $> Nil)
<|> (do
x <- p
xs <- scan
Expand Down
4 changes: 1 addition & 3 deletions src/Text/Parsing/Parser/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ whiteSpace = do

-- | Skip whitespace characters.
skipSpaces :: forall s m. (StringLike s, Monad m) => ParserT s m Unit
skipSpaces = do
whiteSpace
pure unit
skipSpaces = void whiteSpace

-- | Match one of the characters in the array.
oneOf :: forall s m. (StringLike s, Monad m) => Array Char -> ParserT s m Char
Expand Down
10 changes: 5 additions & 5 deletions src/Text/Parsing/Parser/Token.purs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ makeTokenParser (LanguageDef languageDef)

stringEscape :: ParserT String m (Maybe Char)
stringEscape = do
char '\\'
_ <- char '\\'
(escapeGap $> Nothing) <|> (escapeEmpty $> Nothing) <|> (Just <$> escapeCode)

escapeEmpty :: ParserT String m Char
Expand All @@ -430,7 +430,7 @@ makeTokenParser (LanguageDef languageDef)

charControl :: ParserT String m Char
charControl = do
char '^'
_ <- char '^'
code <- upper
pure <<< fromCharCode $ toCharCode code - toCharCode 'A' + 1

Expand Down Expand Up @@ -537,7 +537,7 @@ makeTokenParser (LanguageDef languageDef)

fraction :: ParserT String m Number
fraction = "fraction" <??> do
char '.'
_ <- char '.'
digits <- Array.some digit <?> "fraction"
maybe (fail "not digit") pure $ foldr op (Just 0.0) digits
where
Expand All @@ -549,7 +549,7 @@ makeTokenParser (LanguageDef languageDef)

exponent' :: ParserT String m Number
exponent' = "exponent" <??> do
oneOf ['e', 'E']
_ <- oneOf ['e', 'E']
f <- sign
e <- decimal <?> "exponent"
pure $ power (f e)
Expand Down Expand Up @@ -604,7 +604,7 @@ makeTokenParser (LanguageDef languageDef)
where
go :: ParserT String m Unit
go = do
string name
_ <- string name
notFollowedBy languageDef.opLetter <?> "end of " <> name

operator :: ParserT String m String
Expand Down