-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnested_re.py
213 lines (169 loc) · 6.17 KB
/
nested_re.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
class NestedRE():
"""
Nested Regular expression. The goal is to simplify Regex patterns by
taking the intersection or unton of nested expressions and merging it
with other NestedRE instances.
TODO: This is work in progress, not completed yet. Only very basic
merge / join is possible for now.
:args:
exp - ether string or a (nested) list of RE instances
closure - one of: '*', '+', '?' or '' if none.
"""
def __init__(self, exp, closure=''):
assert type(exp) is str or type(exp) is list, 'Expected string or list.'
self.exp = exp
self.closure = closure
def __bool__(self):
"""
Check wether expression is empty.
"""
return bool(self.exp)
def __str__(self):
"""
Convert expression into "flat" string, i.e. merge nested expressions.
"""
return self.make_flat()
def is_flat(self):
return True if type(self.exp) == str else False
def make_flat(self):
"""
If we have simple expression, return just the concatenation of the
expression and closure. If we have nested expressions, recursively
make them flat and concatenate.
"""
if type(self.exp) == str:
if not self.closure or self.exp == 'ϵ':
return self.exp
elif len(self.exp) == 1:
return self.exp + self.closure
else:
return '(' + self.exp + ')' + self.closure
else:
flat_exp = ''.join( str(e) for e in self.exp )
if not self.closure or flat_exp == 'ϵ':
return flat_exp
elif len(flat_exp) == 1:
return flat_exp + self.closure
else:
return '(' + flat_exp + ')' + self.closure
def join_intersection(self, other):
"""
Joins the patterns of self and other by intersection.
If the patterns are the same, we merge them. Otherwise
just concatenate.
TODO: merge complex and nested patterns
:args:
other - NestedRE instance
:returns:
X - NestedRE with merged patterns
"""
assert type(self) is type(other), 'Expected NestedRE instance'
A = self.make_flat()
B = other.make_flat()
#print('JI, A=', A, 'B=', B)
if A == B and A !='ϵ':
return self.merge_by_intersection(A, [self.closure, other.closure])
elif A == 'ϵ' and B == 'ϵ':
return NestedRE('ϵ')
elif A == 'ϵ':
return other
elif B == 'ϵ':
return self
else:
return NestedRE(A+B)
def merge_by_intersection(self, p, C):
"""
Merge identical pattern with possibly different closures into one.
Merge by intersection, i.e. by the following rules:
p ^ p = pp
p ^ p? = pp?
p? ^ p? = a?a?
p ^ p+ = pp+
p ^ p* = p+
p? ^ p+ = p+
p+ ^ p* = p+
p+ ^ p+ = p+
p? ^ p* = p*
p* ^ p* = p*
:args:
p - string: pattern
C - list of two elements, the closures of the two instances,
should be one of: '*', '+', '?' or '' if none.
:returns:
X - NestedRE instance with merged pattern.
"""
if '' in C:
# At least one of two without clusure
if '?' in C:
return NestedRE(p+p, '?')
elif '*' in C:
return NestedRE(p, '+')
elif '+' in C:
return NestedRE(p+p, '+')
else:
# Both without closure
return NestedRE(p+p)
# At least one has "+"
elif '+' in C:
return NestedRE(p, '+')
elif '*' in C:
return NestedRE(p, '*')
# Only option left: both "?"
else:
return NestedRE(p+'?'+p+'?')
def join_union(self, other):
"""
Joins the patterns of self and other by union.
If the patterns are the same, simply return one. Otherwise
concatenate with '|'.
TODO: merge complex and nested patterns
:args:
other - NestedRE instance
:returns:
X - NestedRE with merged patterns
"""
assert type(self) is type(other), 'Expected NestedRE instance'
A = self.make_flat()
B = other.make_flat()
if A == B and A !='ϵ':
return self.merge_union(A, [self.closure, other.closure])
elif A == 'ϵ' and B == 'ϵ':
return NestedRE('ϵ')
elif A == 'ϵ':
return NestedRE(B, '?')
elif B == 'ϵ':
return NestedRE(A, '?')
else:
return NestedRE( '(' + A + '|' + B + ')' )
def merge_by_union(self, p, C):
"""
Merge identical pattern with possibly different closures into one.
Merge by union, i.e. by the following rules:
a + a = a
a + a? = a?
a + a+ = a+
a? + a+ = a*
a + a* = a? + a* = a* + a* = a*
:args:
p - string: pattern
C - list of two strings, the closures of the two instances,
should be one of: '*', '+', '?' or '' if none.
:returns:
X - NestedRE instance with merged pattern.
"""
assert len(A) == 2 and len(B) == 2, "[merge_disjunction] only simple re-s accepted"
if '*' in C:
# If any is *, then merged pattern is *
return NestedRE(p, '*')
elif '+' in C and '?' in C:
# One is mandatory, other optional, so merged pattern is * again
return NestedRE(p, '*')
elif '+' in C:
# One is mandatory, then merged pattern is mandatory
return NestedRE(p, '+')
elif '?' in C:
# One is optional, so merged pattern is optional
return NestedRE(p, '?')
else:
# Only option left: none has closure
return NestedRE(p)