-
Notifications
You must be signed in to change notification settings - Fork 0
/
Regex.hs
180 lines (158 loc) · 4.5 KB
/
Regex.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
module Regex where
import Data.Char (isDigit, isLetter)
import RegexHelper (Match, firstMatch, thenMatch, matchChar)
match :: [Char] -> Bool
match input =
case regex input of
(True,[]) -> True
otherwise -> False
regex :: [Char] -> Match
regex [] = (True,[])
regex input = firstMatch [
(\i -> regexHL i `thenMatch` regexTS),
regexHL
] input
regexHL :: [Char] -> Match
regexHL input = firstMatch [
(\i -> regexH i `thenMatch` quantifier),
regexH,
regexL
] input
regexH :: [Char] -> Match
regexH input = firstMatch [
group,
set,
char,
matchChar '.'
] input
regexL :: [Char] -> Match
regexL ('^':xs) = (True,xs)
regexL ('$':xs) = (True,xs)
regexL input = (False,input)
regexTS :: [Char] -> Match
regexTS input = firstMatch [
(\i -> regexT i `thenMatch` regexTS),
regexT
] input
regexT :: [Char] -> Match
regexT input = firstMatch [
regexHL,
(\i -> (matchChar '|' i) `thenMatch` regexHL)
] input
group :: [Char] -> Match
group ('(':xs) =
case xs of
('?':r1) ->
case r1 of
('<':r2) ->
case name r2 of
(True,('>':r3)) -> go r3
otherwise -> (False, ('(':xs))
(':':r2) -> go r2
('=':r2) -> go r2
('!':r2) -> go r2
otherwise -> go r1
otherwise -> go xs
where
go input = case regex input of
(True,(')':rest)) -> (True,rest)
(False,(')':rest)) -> (True,rest)
otherwise -> (False,input)
group input = (False,input)
set :: [Char] -> Match
set ('[':']':xs) = (True,xs)
set ('[':'^':']':xs) = (True,xs)
set ('[':xs) =
case xs of
('^':xss) -> go xss
otherwise -> go xs
where
go input = case setItems input of
(_,(']':rest)) -> (True,rest)
otherwise -> (False,input)
set input = (False,input)
setItems :: [Char] -> Match
setItems input =
case setItem input of
(True,r1) ->
let (t,r2) = setItems r1 in
(True, if t then r2 else r1)
otherwise -> (False,input)
setItem :: [Char] -> Match
setItem input =
case setChar input of
(True,('-':r1)) ->
case setChar r1 of
(True,r2) -> (True,r2)
(False,_) -> (True,r1)
(True,rest) -> (True, rest)
otherwise -> (False, input)
quantifier :: [Char] -> Match
quantifier input =
firstMatch [
(\i -> quantifierH i `thenMatch` matchChar '?'),
quantifierH
] input
quantifierH :: [Char] -> Match
quantifierH ('?':xs) = (True,xs)
quantifierH ('+':xs) = (True,xs)
quantifierH ('*':xs) = (True,xs)
quantifierH ('{':xs) =
case number xs of
(True,('}':rest1)) -> (True, rest1)
(True,(',':rest1)) ->
case number rest1 of
(True,('}':rest2)) -> (True,rest2)
(False,('}':rest2)) -> (True,rest2)
otherwise -> (False, ('{':xs))
otherwise -> (False, ('{':xs))
quantifierH input = (False,input)
name :: [Char] -> Match
name input =
let
(t1,r1) = letter input
(t2,r2) = go r1
in if t1 then (True, if t2 then r2 else r1) else (False, input)
where
go i =
let
(t1,r1) = letter i
(t2,r2) = digit i
in case (t1,t2) of
(True,_) -> (True,r1)
(False,True) -> (True,r2)
otherwise -> (False,i)
setChar :: [Char] -> Match
setChar [] = (False,[])
setChar (']':xs) = (False,(']':xs))
setChar (x:xs) = (True,xs)
char :: [Char] -> Match
char [] = (False,[])
char ('\\':x:xs) = (True,xs)
char (x:xs) =
case metachar (x:xs) of
(True,_) -> (False,(x:xs))
otherwise -> (True,xs)
metachar :: [Char] -> Match
metachar ('.':xs) = (True,xs)
metachar ('+':xs) = (True,xs)
metachar ('(':xs) = (True,xs)
metachar (')':xs) = (True,xs)
metachar ('[':xs) = (True,xs)
metachar ('^':xs) = (True,xs)
metachar ('$':xs) = (True,xs)
metachar ('|':xs) = (True,xs)
metachar ('?':xs) = (True,xs)
metachar ('\\':xs) = (True,xs)
metachar input = (False, input)
letter :: [Char] -> Match
letter [] = (False, [])
letter (x:xs) = (isLetter x, xs)
number :: [Char] -> Match
number input = case digit input of
(True,r1) -> let (t2,r2) = number r1 in
(True, if t2 then r2 else r1)
otherwise -> (False, input)
digit :: [Char] -> Match
digit [] = (False, [])
digit (x:xs) = (isDigit x, xs)