2 | import public Data.Maybe
4 | import public TyRE.Codes
5 | import public TyRE.Core
6 | import public TyRE.RE
7 | import public TyRE.Text.Lexer
8 | import public TyRE.Text.Parser
32 | reCharLit = (non $
some $
oneOf specialChars) <|> (is '\\' <+> any)
35 | reTokenMap : TokenMap Token
37 | (is '[', \x => OParQ),
38 | (is ']', \x => CParQ),
39 | (is '(', \x => OPar),
40 | (is ')', \x => CPar),
41 | (is '.', \x => Dot),
42 | (is '`', \x => BackTic),
43 | (is '-', \x => Dash),
44 | (is '*', \x => Star),
45 | (is '+', \x => Plus),
46 | (is '?', \x => QMark),
47 | (is '|', \x => Alt),
48 | (is '!', \x => Exclamation),
49 | (reCharLit, \x => CharLit x)
54 | Rule ty = Grammar (TokenData Token) True ty
59 | openParQ = terminal "[" (\case {
(MkToken _ _ OParQ) => Just ();
_ => Nothing}
)
62 | closedParQ : Rule ()
63 | closedParQ = terminal "]" (\case {
(MkToken _ _ CParQ) => Just ();
_ => Nothing}
)
67 | openPar = terminal "(" (\case {
(MkToken _ _ OPar) => Just ();
_ => Nothing}
)
71 | closedPar = terminal ")" (\case {
(MkToken _ _ CPar) => Just ();
_ => Nothing}
)
75 | backTic = terminal "`" (\case {
(MkToken _ _ BackTic) => Just ();
_ => Nothing}
)
79 | dash = terminal "-" (\case {
(MkToken _ _ Dash) => Just ();
_ => Nothing}
)
83 | dot = terminal "." (\case {
(MkToken _ _ Dot) => Just ();
_ => Nothing}
)
87 | star = terminal "*" (\case {
(MkToken _ _ Star) => Just ();
_ => Nothing}
)
91 | plus = terminal "+" (\case {
(MkToken _ _ Plus) => Just ();
_ => Nothing}
)
95 | qMark = terminal "?" (\case {
(MkToken _ _ QMark) => Just ();
_ => Nothing}
)
99 | alt = terminal "|" (\case {
(MkToken _ _ Alt) => Just ();
_ => Nothing}
)
103 | end = terminal "" (\case {
(MkToken _ _ End) => Just ();
_ => Nothing}
)
106 | exclamation : Rule ()
107 | exclamation = terminal "!" (\case {
(MkToken _ _ Exclamation) => Just ();
_ => Nothing}
)
110 | getCharLit : (TokenData Token) -> Maybe Char
111 | getCharLit (MkToken _ _ (CharLit str)) with (unpack str)
112 | getCharLit (MkToken _ _ (CharLit str)) | [] = Nothing
113 | getCharLit (MkToken _ _ (CharLit str)) | (c :: []) = Just c
114 | getCharLit (MkToken _ _ (CharLit str)) | ('\\' :: (c :: [])) = Just c
115 | getCharLit (MkToken _ _ (CharLit str)) | ('\\' :: (c :: (c' :: cs))) = Nothing
116 | getCharLit (MkToken _ _ (CharLit str)) | (c :: (c' :: cs)) = Nothing
117 | getCharLit _ = Nothing
120 | charLit : Rule Char
121 | charLit = terminal "char" getCharLit
125 | charLitsRep : Rule (List Char)
126 | charLitsRep = (map (::) charLit <*> charLitsRep) <|> (map (\x => [x]) charLit)
130 | oneOf = map OneOf $
openParQ *> charLitsRep <* closedParQ
134 | range = (map To ((openParQ *> charLit) <* dash) <*> charLit) <* closedParQ
138 | exactly = map Exactly charLit
142 | any = map (\_ => Any) dot
145 | mapRE : RE -> Maybe (RE -> RE) -> RE
146 | mapRE re Nothing = re
147 | mapRE re (Just f) = f re
152 | unit = exactly <|> any <|> oneOf <|> range
153 | <|> (map Group $
backTic *> fullRE <* backTic)
154 | <|> (openPar *> fullRE <* closedPar)
157 | postUnit : Rule (RE -> RE)
158 | postUnit = (map (\_ => Maybe) qMark)
159 | <|> (map (\_ => Rep1) plus)
160 | <|> (map (\_ => Rep0) star)
161 | <|> (map (\_ => Keep) exclamation)
165 | semiUnit = map mapRE unit <*> optional (postUnit)
168 | postSemiUnit : Rule (RE -> RE)
169 | postSemiUnit = (map (\y => \x => Alt x y) (alt *> semiUnit))
170 | <|> (map (\y => \x => Concat x y) fullRE)
174 | fullRE = map mapRE semiUnit <*> optional (postSemiUnit)
177 | reWithEnd : Rule RE
178 | reWithEnd = fullRE <* end
183 | mapTokens : (List (TokenData Token), (Int, Int, String))
184 | -> List (TokenData Token)
185 | mapTokens (tokens, (f, l, _)) = tokens ++ [MkToken f l End]
188 | rAux : String -> Maybe RE
189 | rAux str = case (parse reWithEnd (mapTokens (lex reTokenMap str))) of
190 | Right (reg, _) => Just reg
194 | toRE : (str : String) -> {auto isJust : IsJust (rAux str)} -> RE
195 | toRE str {isJust} = fromJust (rAux str) @{isJust}
198 | r : (str : String) -> {auto isJust : IsJust (rAux str)} -> TyRE (TypeRE $
toRE str {isJust})
199 | r str {isJust} = compile (toRE str {isJust})