Skip to content

Instantly share code, notes, and snippets.

@alogic0
Created April 7, 2016 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alogic0/5275068f1e2f6e5025aaa49a559a18f6 to your computer and use it in GitHub Desktop.
Save alogic0/5275068f1e2f6e5025aaa49a559a18f6 to your computer and use it in GitHub Desktop.
Tranlation of regex to parser combinator
/a/      => char 'a'
/abcd/   => string "abcd"
            sequence [ char 'a', char 'b', char 'c', char 'd' ]
            (:) <$> char 'a' <*> string "bcd"
            (++) <$> string "ab" <*> string "cd"

/[abcd]/ => oneOf "abcd"
/[^abcd]/ => noneOf "abcd"
/[a-z]/  => oneOf ['a'..'z']
/[^a-z]/ => noneOf ['a'..'z']

/a*/     => many (char 'a')
/a+/     => many1 (char 'a')
/a?/     => optionMaybe (char 'a')

/(abcd)*/ => many (try $ string "abcd")
/(abcd)+/ => many1 (try $ string "abcd")
/(abcd)?/ => optionMaybe (try $ string "abcd")

/a|b/    => char 'a' <|> char 'b'

/$/      => eof

/(?=abcd)/ => lookAhead (try $ string "abcd")
/(?!abcd)/ => notFollowedBy (string "abcd")

source https://www.reddit.com/r/haskell/comments/421how/regex_library_for_custom_containers/cz7sti0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment