Skip to content

Add a regex combinator #28

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 4 commits into from
Mar 1, 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
37 changes: 34 additions & 3 deletions src/Text/Parsing/StringParser/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ module Text.Parsing.StringParser.String
, upperCaseChar
, anyLetter
, alphaNum
, regex
) where

import Prelude

import Control.Alt ((<|>))
import Data.Array ((..))
import Data.Array ((..), uncons)
import Data.Char (toCharCode)
import Data.Either (Either(..))
import Data.Foldable (class Foldable, foldMap, elem, notElem)
import Data.Maybe (Maybe(..))
import Data.String (Pattern(..), charAt, length, indexOf', singleton)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.String (Pattern(..), charAt, drop, length, indexOf', singleton, stripPrefix)
import Data.String.Regex as Regex
import Data.String.Regex.Flags (noFlags)
import Text.Parsing.StringParser (Parser(..), ParseError(..), try, fail)
import Text.Parsing.StringParser.Combinators (many, (<?>))

Expand Down Expand Up @@ -111,3 +114,31 @@ anyLetter = lowerCaseChar <|> upperCaseChar <?> "Expected a letter"
-- | Match a letter or a number.
alphaNum :: Parser Char
alphaNum = anyLetter <|> anyDigit <?> "Expected a letter or a number"

-- | match the regular expression
regex :: String -> Parser String
regex pat =
case Regex.regex pattern noFlags of
Left _ ->
fail $ "Text.Parsing.StringParser.String.regex': illegal regex " <> pat
Right r ->
matchRegex r
where
-- ensure the pattern only matches the current position in the parse
pattern =
case stripPrefix (Pattern "^") pat of
Nothing ->
"^" <> pat
_ ->
pat
matchRegex :: Regex.Regex -> Parser String
matchRegex r =
Parser \{ str, pos } ->
let
remainder = drop pos str
in
case uncons $ fromMaybe [] $ Regex.match r remainder of
Just { head: Just matched, tail: _ } ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Since all matches start at the beginning now, should we take the longest, not the first?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought these other matches were just the the parenthesized substring matches which we weren't interested in - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec. i.e for patterns like (a|A)(bIB) and so on.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you're right, thanks.

Right { result: matched, suffix: { str, pos: pos + length matched } }
_ ->
Left { pos, error: ParseError "no match" }
4 changes: 3 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Test.Assert (assert', ASSERT, assert)
import Text.Parsing.StringParser (Parser, runParser, try)
import Text.Parsing.StringParser.Combinators (many1, endBy1, sepBy1, optionMaybe, many, chainl, fix, between)
import Text.Parsing.StringParser.Expr (Assoc(..), Operator(..), buildExprParser)
import Text.Parsing.StringParser.String (anyDigit, eof, string, anyChar)
import Text.Parsing.StringParser.String (anyDigit, eof, string, anyChar, regex)

parens :: forall a. Parser a -> Parser a
parens = between (string "(") (string ")")
Expand Down Expand Up @@ -48,6 +48,7 @@ exprTest = buildExprParser [ [Infix (string "/" >>= \_ -> pure div) AssocRight]
] digit

tryTest :: Parser String
-- reduce the possible array of matches to 0 or 1 elements to aid Array pattern matching
tryTest = try ((<>) <$> string "aa" <*> string "bb") <|>
(<>) <$> string "aa" <*> string "cc"

Expand Down Expand Up @@ -84,3 +85,4 @@ main = do
assert' "tryTest "$ canParse tryTest "aacc"
assert $ expectResult ('0':'1':'2':'3':'4':Nil) (many1 anyDigit) "01234/"
assert $ expectResult ('5':'6':'7':'8':'9':Nil) (many1 anyDigit) "56789:"
assert $ expectResult "aaaa" (regex "a+") "aaaab"