Verified Commit f98d033f authored by Camil Staps's avatar Camil Staps 🙂

Move match to separate module

parent 4da6dec8
......@@ -4,7 +4,7 @@ CLMFLAGS:=\
-I $$CLEAN_HOME/lib/Generics\
-nt
DEPMODS:=Regex Regex/Print
DEPMODS:=Regex Regex/Match Regex/Print
DEPS:=$(addsuffix .dcl,$(DEPMODS)) $(addsuffix .icl,$(DEPMODS))
EXE:=test
......
definition module Regex
import Regex.Match
import Regex.Print
:: Regex
......@@ -16,5 +17,3 @@ import Regex.Print
:: Greediness :== Bool
:: Position :== Int
match :: Regex [Char] -> [(Position, [Char])]
implementation module Regex
import StdBool
from StdFunc import flip, id
from Data.Func import $
import Data.List
:: MatchStatus
= { skipped :: [Char]
, matched :: [Char]
, unseen :: [Char]
, can_skip :: Bool
}
instance zero MatchStatus
where zero = {skipped=[], matched=[], unseen=[], can_skip=True}
match :: Regex [Char] -> [(Position, [Char])]
match r s = [(length st.skipped, st.matched) \\ st <- match` r status]
where status = {zero & unseen=s}
match` :: Regex MatchStatus -> [MatchStatus]
match` r=:(Literal cs) st
= matchAndContinue r st $
if (isPrefixOf cs st.unseen) [eat (length cs) st] []
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` (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` 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}
eat :: Int MatchStatus -> MatchStatus
eat n st = {st & matched=st.matched ++ take n st.unseen, unseen=drop n st.unseen}
matchAndContinue :: Regex MatchStatus [MatchStatus] -> [MatchStatus]
matchAndContinue r st sts = sts ++
if can_skip (match` r $ skip 1 st) []
where
can_skip = st.can_skip && not (isEmpty st.unseen) && isEmpty st.matched
definition module Regex.Match
from Regex import :: Regex, :: Position
match :: Regex [Char] -> [(Position, [Char])]
implementation module Regex.Match
import StdBool
from StdFunc import flip, id
from Data.Func import $
import Data.List
import Regex
:: MatchStatus
= { skipped :: [Char]
, matched :: [Char]
, unseen :: [Char]
, can_skip :: Bool
}
instance zero MatchStatus
where zero = {skipped=[], matched=[], unseen=[], can_skip=True}
match :: Regex [Char] -> [(Position, [Char])]
match r s = [(length st.skipped, st.matched) \\ st <- match` r status]
where status = {zero & unseen=s}
match` :: Regex MatchStatus -> [MatchStatus]
match` r=:(Literal cs) st
= matchAndContinue r st $
if (isPrefixOf cs st.unseen) [eat (length cs) st] []
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` (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` 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}
eat :: Int MatchStatus -> MatchStatus
eat n st = {st & matched=st.matched ++ take n st.unseen, unseen=drop n st.unseen}
matchAndContinue :: Regex MatchStatus [MatchStatus] -> [MatchStatus]
matchAndContinue r st sts = sts ++
if can_skip (match` r $ skip 1 st) []
where
can_skip = st.can_skip && not (isEmpty st.unseen) && isEmpty st.matched
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