-
Notifications
You must be signed in to change notification settings - Fork 1
/
ban.lua
289 lines (262 loc) · 7.66 KB
/
ban.lua
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
local parser = require'pegparser.parser'
local first = require'pegparser.first'
local unique = require'pegparser.unique'
local label = require'pegparser.label'
local pretty = require'pegparser.pretty'
local newSeq = parser.newSeq
local newNot = parser.newNot
local newNode = parser.newNode
local newVar = parser.newVar
local calcfirst = first.calcfirst
local disjoint = first.disjoint
local matchEmpty = parser.matchEmpty
local calck = first.calck
local isempty = first.isempty
local inter = first.inter
local issubset = first.issubset
local union = first.union
local matchUPath = unique.matchUPath
local memo
local contAfter
-- UPath Deep Algorithm ------------------------------------------------------------
local function upathdeep_ban(g, p, ateTk, notll1, flw, afterU)
if p.tag == 'char' or (p.tag == 'var' and parser.isLexRule(p.p1)) then
if (ateTk and not afterU) or not disjoint(notll1, calck(g, p, flw)) then
p.ban = true
end
elseif p.tag == 'var' then
local s = calck(g, p, flw)
if ateTk then
if not afterU then
p.ban = true
else
return
end
if not issubset(notll1, memo[p.p1]) and not g.uniqueVar[p.p1] then
memo[p.p1] = union(memo[p.p1], notll1)
--print("Banir recursivamente ", p.p1)
upathdeep_ban(g, g.prules[p.p1], true, notll1, flw, afterU)
end
elseif not isempty(s) then
p.ban = true
if not issubset(s, memo[p.p1]) then
memo[p.p1] = union(memo[p.p1], s)
upathdeep_ban(g, g.prules[p.p1], false, s, flw, afterU)
end
end
elseif p.tag == 'ord' then
if ateTk then
if not afterU then
p.ban = true
upathdeep_ban(g, p.p1, true, notll1, flw, afterU)
upathdeep_ban(g, p.p2, true, notll1, flw, afterU)
end
else
local s1 = inter(notll1, calck(g, p.p1, flw))
local s2 = inter(notll1, calck(g, p.p2, flw))
if not isempty(s1) then
p.ban = true
upathdeep_ban(g, p.p1, false, s1, flw, afterU)
end
if not isempty(s2) then
p.ban = true
upathdeep_ban(g, p.p2, false, s2, flw, afterU)
end
end
elseif p.tag == 'con' then
if not afterU then
upathdeep_ban(g, p.p1, ateTk, notll1, calck(g, p.p2, flw), afterU)
end
if not ateTk then
ateTk = not disjoint(calcfirst(g, p.p1), notll1)
end
if not afterU then
upathdeep_ban(g, p.p2, ateTk, notll1, flw, afterU or matchUPath(p.p1))
end
elseif p.tag == 'star' or p.tag == 'plus' or p.tag == 'opt' then
if ateTk then
if not afterU then
p.ban = true
upathdeep_ban(g, p.p1, true, notll1, flw, afterU)
end
else
local s = inter(notll1, calck(g, p, flw))
if not isempty(s) then
p.ban = true
upathdeep_ban(g, p.p1, false, s, flw, afterU)
end
end
end
end
local function notannotate_upathdeep(g, p, flw)
if p.tag == 'ord' then
local s = inter(calcfirst(g, p.p1), calck(g, p.p2, flw))
if not isempty(s) then
upathdeep_ban(g, p.p1, false, s, flw, false)
end
notannotate_upathdeep(g, p.p1, flw)
notannotate_upathdeep(g, p.p2, flw)
elseif p.tag == 'con' then
notannotate_upathdeep(g, p.p1, calck(g, p.p2, flw))
notannotate_upathdeep(g, p.p2, flw)
elseif p.tag == 'star' or p.tag == 'plus' or p.tag == 'opt' then
local s = inter(calcfirst(g, p.p1), flw)
if not isempty(s) then
p.ban = true
upathdeep_ban(g, p.p1, false, s, flw, false, {})
end
notannotate_upathdeep(g, p.p1, flw)
end
end
------------------------------------------------------------------------------
-- Deep Algorithm ------------------------------------------------------------
local function deep_ban(g, p, ateTk, notll1, flw)
if p.tag == 'char' or (p.tag == 'var' and parser.isLexRule(p.p1)) then
if ateTk or not disjoint(notll1, calck(g, p, flw)) then
p.ban = true
end
elseif p.tag == 'var' then
local s = calck(g, p, flw)
if ateTk then
p.ban = true
if not issubset(notll1, memo[p.p1]) then
memo[p.p1] = union(memo[p.p1], notll1)
deep_ban(g, g.prules[p.p1], true, notll1, flw)
end
elseif not isempty(s) then
p.ban = true
if not issubset(s, memo[p.p1]) then
memo[p.p1] = union(memo[p.p1], s)
deep_ban(g, g.prules[p.p1], false, s, flw)
end
end
elseif p.tag == 'ord' then
if ateTk then
p.ban = true
deep_ban(g, p.p1, true, notll1, flw)
deep_ban(g, p.p2, true, notll1, flw)
else
local s1 = calck(g, p.p1, flw)
local s2 = calck(g, p.p2, flw)
if not isempty(s1) then
p.ban = true
deep_ban(g, p.p1, false, s1, flw)
end
if not isempty(s2) then
p.ban = true
deep_ban(g, p.p2, false, s2, flw)
end
end
elseif p.tag == 'con' then
deep_ban(g, p.p1, ateTk, notll1, calck(g, p.p2, flw))
if not ateTk then
ateTk = not disjoint(calcfirst(g, p.p1), notll1)
end
deep_ban(g, p.p2, ateTk, notll1, flw)
elseif p.tag == 'star' or p.tag == 'plus' or p.tag == 'opt' then
local s = calck(g, p, flw)
if ateTk then
p.ban = true
deep_ban(g, p.p1, true, notll1, flw)
elseif not isempty(s) then
p.ban = true
deep_ban(g, p.p1, false, s, flw)
end
end
end
local function notannotate_deep(g, p, flw)
if p.tag == 'ord' then
local s = inter(calcfirst(g, p.p1), calck(g, p.p2, flw))
if not isempty(s) then
p.ban = true
deep_ban(g, p.p1, false, s, flw)
end
notannotate_deep(g, p.p1, flw)
notannotate_deep(g, p.p2, flw)
elseif p.tag == 'con' then
notannotate_deep(g, p.p1, calck(g, p.p2, flw))
notannotate_deep(g, p.p2, flw)
elseif p.tag == 'star' or p.tag == 'plus' or p.tag == 'opt' then
local s = inter(calcfirst(g, p.p1), flw)
if not isempty(s) then
p.ban = true
deep_ban(g, p.p1, false, s, flw)
end
notannotate_deep(g, p.p1, flw)
end
end
------------------------------------------------------------------------------
local function addlab (g, p, seq, flw)
if ((p.tag == 'var' and not matchEmpty(p)) or p.tag == 'char' or p.tag == 'any') and seq and not p.ban then
return label.markerror(p, flw)
elseif p.tag == 'con' then
local newseq = seq or not matchEmpty(p.p1)
--return newSeq(addlab(g, p.p1, seq, calck(g, p.p2, flw)), addlab(g, p.p2, newseq, flw))
return newNode(p, addlab(g, p.p1, seq, calck(g, p.p2, flw)), addlab(g, p.p2, newseq, flw))
elseif p.tag == 'ord' then
local flagDisjoint = disjoint(calcfirst(g, p.p1), calck(g, p.p2, flw))
local p1 = p.p1
if flagDisjoint then
p1 = addlab(g, p.p1, false, flw)
end
local p2 = addlab(g, p.p2, false, flw)
if seq and not matchEmpty(p) and not p.ban then
return label.markerror(newNode(p, p1, p2), flw)
else
return newNode(p, p1, p2)
end
elseif (p.tag == 'star' or p.tag == 'opt' or p.tag == 'plus') and disjoint(calcfirst(g, p.p1), flw) then
if p.tag == 'star' or p.tag == 'plus' then
flw = union(calcfirst(g, p.p1), flw)
end
local newp = addlab(g, p.p1, false, flw)
if p.tag == 'star' or p.tag == 'opt' then
return newNode(p, newp)
else --plus
if seq and not p.ban then
return label.markerror(newNode(p, newp), flw)
else
return newNode(p, newp)
end
end
else
return p
end
end
local function ban_aux (g, f, flw)
for i, v in ipairs(g.plist) do
if not parser.isLexRule(v) then
f(g, g.prules[v], flw[v])
end
end
end
local function ban (g, flagBan)
local fst = first.calcFst(g)
local flw = first.calcFlw(g)
memo = {}
for i, v in ipairs(g.plist) do
memo[v] = {}
end
if flagBan == 'deep' then
ban_aux(g, notannotate_deep, flw)
elseif flagBan == 'upathdeep' then
ban_aux(g, notannotate_upathdeep, flw)
else
error("ban: Invalid flag " .. tostring(flagBan))
end
end
local function annotateBan (g)
local fst = first.calcFst(g)
local flw = first.calcFlw(g)
local newg = parser.initgrammar(g)
for i, v in ipairs(g.plist) do
if not parser.isLexRule(v) then
newg.prules[v] = addlab(g, g.prules[v], false, flw[v])
end
end
return newg
end
return {
ban = ban,
annotateBan = annotateBan
}