Commit 2a5771fe authored by Camil Staps's avatar Camil Staps 🐧

(Some,Many,Optional) instances of Repeated; Concat uses a list now

parent f98d033f
definition module Regex
from Data.Maybe import :: Maybe (Just, Nothing)
import Regex.Match
import Regex.Print
:: Regex
= Literal [Char]
| CharacterClass [Char]
| Optional Regex
| Concat Regex Regex
| Concat [Regex]
| Any [Regex]
| Many Greediness Regex
| Some Greediness Regex
| Repeated Greediness Int (Maybe Int) Regex
| StartOfString
| EndOfString
| Empty
Many g :== Repeated g 0 Nothing
Some g :== Repeated g 1 Nothing
Optional g :== Repeated g 0 (Just 1)
:: Greediness :== Bool
:: Position :== Int
......@@ -4,7 +4,9 @@ import StdBool
from StdFunc import flip, id
from Data.Func import $
import Data.Functor
import Data.List
import Data.Maybe
import Regex
......@@ -31,22 +33,25 @@ match` (CharacterClass _) {unseen=[]}
match` r=:(CharacterClass cs) st=:{matched,unseen=[u:us]}
= matchAndContinue r st $
if (isMember u cs) [eat 1 st] []
match` (Optional r) st
= match` (Any [r, Empty]) st
match` (Concat r1 r2) st
= [st`` \\ st` <- match` r1 st, st`` <- match` r2 st`]
match` (Concat []) st
= [st]
match` (Concat [r]) st
= match` r st
match` (Concat [r:rs]) st
= [st`` \\ st` <- match` r st, st`` <- match` (Concat rs) st`]
match` (Any rs) st
= flatten $ map (flip match` st) rs
match` m=:(Many greedy r) st
= match` (Any $ if greedy id reverse [Concat r m, Empty]) st
match` (Some greedy r) st
= match` (Concat r $ Many greedy r) st
match` (Repeated g f (Just 0) r) st
= [st]
match` (Repeated g f t r) st
| isJust t && fromJust t <= f
= match` (Concat $ repeatn f r) st
= match` (Concat $ repeatn f r ++ [Any $ if g id reverse opts]) st
where opts = [Concat [r, Repeated g 0 (flip (-) (f+1) <$> t) r], Concat []]
match` StartOfString st
= if (isEmpty st.matched) [{st & can_skip=False}] []
match` EndOfString st
= if (isEmpty st.unseen) [st] []
match` Empty st
= [st]
skip :: Int MatchStatus -> MatchStatus
skip n st = {st & skipped=st.skipped ++ take n st.unseen, unseen=drop n st.unseen}
......
......@@ -6,6 +6,7 @@ import StdString
from Data.Func import $
import Data.List
import Data.Maybe
import Regex
......@@ -13,15 +14,27 @@ print :: Bool Regex -> String
print ps (Literal cs) = parens (ps && length cs > 1) cs
print ps (CharacterClass [c]) = {c}
print ps (CharacterClass cs) = "[" <+ cs <+ "]"
print ps (Optional r) = print True r <+ "?"
print ps (Concat r1 r2) = parens ps $ r1 <+ r2
print ps (Any rgxs) = "(?:" <+ foldl (\x s -> x +++ "|" +++ s) r rs <+ ")"
print ps (Concat rgxs) = parens ps $ foldl (\x s -> x +++ s) r rs
where [r:rs] = map (print True) rgxs
print ps (Many gr r) = parens ps $ print True r <+ "*" <+ if gr "" "?"
print ps (Some gr r) = parens ps $ print True r <+ "+" <+ if gr "" "?"
print ps (Any rgxs) = parens ps $ foldl (\x s -> x +++ "|" +++ s) r rs
where [r:rs] = map (print True) rgxs
print ps (Repeated g 0 (Just 1) r) = parens ps $ print True r <+ "?" <+ lz g
print ps (Repeated g 1 (Just 1) r) = parens ps $ print True r
print ps (Repeated g 0 Nothing r) = parens ps $ print True r <+ "*" <+ lz g
print ps (Repeated g 1 Nothing r) = parens ps $ print True r <+ "+" <+ lz g
print ps (Repeated g f t r) = parens ps $ print True r <+ q <+ lz g
where
q
| isNothing t = "{" <+ f <+ ",}"
| f == jt = "{" <+ f <+ "}"
| otherwise = "{" <+ f <+ "," <+ jt <+ "}"
where jt = fromJust t
print ps StartOfString = "^"
print ps EndOfString = "$"
print ps Empty = ""
lz :: Bool -> String // The lazy quantifier
lz True = ""
lz False = "?"
instance toString Regex where toString r = print False r
......
module test
from Data.Func import $
import Regex
Start = match rgx string
where
rgx = Concat StartOfString $ Concat (Some False (CharacterClass ['hel'])) $ CharacterClass ['l']
rgx = Concat [Repeated False 1 (Just 2) (CharacterClass ['hel']), CharacterClass ['l']]
string = ['hello world']
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment