/
regexp2.hs
42 lines (35 loc) · 1.3 KB
/
regexp2.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE NoImplicitPrelude#-}
data Alphabet = A | B ;
data List a = Nil | Cons a (List a) ;
data Bool = True | False ;
data Result a = Fail | Parsed a ;
--match (rep (rep (rep (rep x)))) w
match (or x (rep x)) w
--match (concat (rep x) x) w
where
match = \p w -> p eow False False w;
a = \next f1 f w -> case w of {
Nil -> Fail;
Cons l w1 -> case l of { A -> next False False w1; B -> Fail;};
};
b = \next f1 f w -> case w of {
Nil -> Fail;
Cons l w1 -> case l of { A -> Fail; B -> next False False w1;};
};
eow = \f1 f w -> case w of {
Cons l w1 -> Fail;
Nil -> return f1 f Nil;
};
nil = \next f1 f w -> if f1 Fail (next False f w);
return = \f1 f y -> if f Fail (Parsed y);
concat = \p1 p2 next f1 f w -> if f1 (concatS p1 p2 next w) (concatS p1 p2 next w);
concatS = \p1 p2 next w -> or (\z -> p1 (p2 z)) (nullAnd p1 p2) next True True w;
nullAnd = \p1 p2 next f1 f2 w -> case p1 return False False Nil of {
Fail -> Fail;
Parsed y1 -> p2 next True True w;
};
rep0 = \p next f w -> pipe (p (rep p next) True True) (next False f) w;
rep = \p next f1 f w -> if f1 (p (rep p next) True True w) (rep0 p next f w);
or = \p1 p2 next f1 f -> pipe (p1 next f1 f) (p2 next f1 f);
pipe = \r1 r2 w -> case (r1 w) of { Parsed y -> Parsed y; Fail -> (r2 w);};
if = \cond a1 a2 -> case cond of {True -> a1; False -> a2;};