Commit 7129bd73 authored by Camil Staps's avatar Camil Staps 🐧

WIP new match function using NFAs

parent 989664bc
......@@ -2,7 +2,7 @@ SHELL:=bash
CLM:=clm
CLMFLAGS:=\
-I $$CLEAN_HOME/lib/clean-platform/OS-Independent\
-I $$CLEAN_HOME/lib/Platform\
-I $$CLEAN_HOME/lib/Generics\
-nt
......
......@@ -5,3 +5,5 @@ from Regex import :: Regex, :: Position, :: GroupId
:: Match :== (Position, [Char], [(GroupId, [Char])])
match :: !Regex [Char] -> [Match]
//match_new :: !Regex [Char] -> [Match]
......@@ -11,6 +11,27 @@ import Data.Maybe
import Regex
:: NFA :== [State]
:: State = State Bool Char NFA
isFinal :: State -> Bool
isFinal (State b _ _) = b
new_match :: !Regex -> [Char] -> [Match]
new_match r = new_match` (compile [State True '~' []] r)
new_match` :: !NFA MatchStatus -> [Match]
new_match` [] _ = []
new_match` [State a c r:nfa] [i:is]
| i == c =
compile :: NFA !Regex -> NFA
compile next (Literal [c]) = [State (isEmpty next) c next]
compile next (Literal cs) = compile next (Concat $ map Literal [[c] \\ c <- cs])
compile next (Any rs) = flatten $ map (compile next) rs
compile next (Concat [r]) = compile next r
compile next (Concat [r:rs]) = compile (compile next $ Concat rs) r
:: MatchStatus
= { skipped :: [Char]
, matched :: [Char]
......@@ -45,8 +66,8 @@ match` (Any rs) st
= flatten $ map (flip match` st) rs
match` (Repeated _ 0 _ _) st=:{unseen=[]}
= [st]
match` tr=:(Repeated True 0 Nothing r) st
= flatten [match` tr st` \\ st` <- match` r st | st.matched <> st`.matched] ++ [st]
//match` tr=:(Repeated True 0 Nothing r) st
// = flatten [match` tr st` \\ st` <- match` r st | st.matched <> st`.matched] ++ [st]
match` (Repeated g f (Just 0) r) st
= [st]
match` (Repeated g f t r) st
......@@ -58,8 +79,8 @@ match` (Group id r) st
= [{st` & groups=put id (drop (length st.matched) st`.matched) st`.groups}
\\ st` <- match` r st]
where put id m gs = [(id,m):filter ((<>) id o fst) gs]
match` r=:StartOfString st
= matchOrSkip r st $ if (isEmpty st.matched) [{st & can_skip=False}] []
match` StartOfString st
= if (isEmpty st.matched) [{st & can_skip=False}] []
match` r=:EndOfString st
= matchOrSkip r st $ if (isEmpty st.unseen) [st] []
match` r=:(WordBreak n) st
......
......@@ -7,6 +7,22 @@ import Data.Functor
import Data.Tuple
import Regex
Start = flip match ['abc'] <$> compile ['(abc|)+']
Start = hd <$> flip match (repeatn 100 'a') <$> compile ['^(?:a+)*$']
Start = flip match ['abc'] <$> compile ['$']
Start = [(\r -> (toString r, r, '\n')) <$> compile r \\ r <- rgxs]
where
rgxs =
[ ['a|bc']
, ['ab|cd']
, ['a|b|c']
, ['(?:a|b)|c']
, ['a|(?:b|c)']
]
Start = map (appSnd3 toString o (appThd3 $ map (appSnd toString)))
<$> flip match input <$> compile rgx
where
......
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