Created
June 1, 2018 20:16
-
-
Save Quelklef/0a34f2e5a73d4a1bafa15705f3e97e1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Maps input string to leftover string | |
type Reg = String -> String | |
-- Literal match | |
lit :: String -> Reg | |
lit s (s ++ s') = s' | |
-- Optional; regex EXPR? | |
opt :: Reg -> Reg | |
opt r s = s ? r s | |
-- Plus; regex EXPR+ | |
plus :: Reg -> Reg | |
plus r s | |
| s /= "" = r s ? plus r (r s) | |
-- Star; regex EXPR* | |
star :: Reg -> Reg | |
star r s = opt (plus r) s | |
-- Adjacency; regex EXPR0 EXPR1 | |
(%&) :: Reg -> Reg -> Reg | |
(%&) r0 r1 s = r1 (r0 s) | |
-- Choice; regex EXPR0 | EXPR1 | |
(%|) :: Reg -> Reg -> Reg | |
(%|) r0 r1 s = r0 s ? r1 s | |
-- Define the javascript-function-decl-ish syntax: | |
-- function\((NAME(,NAME)*)?\) | |
-- regex [abcdefghijklmnopqrstuvwxyz1234567890] | |
alph :: Reg | |
alph = foldl (%|) (lit "a") $ map (lit.(:[])) "abcdefghijklmnopqrstuvwxyz1234567890" | |
name :: Reg | |
name = plus alph | |
-- whitespace | |
ws :: Reg | |
ws = star (lit " " %| lit "\n") | |
-- matches javascript function declarartion-type strings | |
jsfdecl :: Reg | |
jsfdecl = | |
lit "function(" %& ws %& opt (name %& star (lit "," %& ws %& name)) %& lit ")" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment