-
Notifications
You must be signed in to change notification settings - Fork 1
/
label.lua
142 lines (120 loc) · 3.24 KB
/
label.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
local first = require'pegparser.first'
local parser = require'pegparser.parser'
local newNode = parser.newNode
local set2choice = first.set2choice
local ierr
-- recovery rule for expression p
-- (!FOLLOW(p) eatToken space)*
-- eatToken <- token / (!space .)+
local function adderror (g, p, rec)
local s = 'Err_' .. string.format("%03d", ierr)
ierr = ierr + 1
if rec then
local pred = parser.newNot(set2choice(p.flw))
local seq = newNode('var', 'EatToken')
table.insert(g.plist, s)
g.prules[s] = newNode('star', parser.newSeq(pred, seq))
end
return parser.newOrd(p, parser.newThrow(s))
end
local function adderrorstar (g, p, rec)
local s = 'Err_' .. string.format("%03d", ierr)
ierr = ierr + 1
--local pred = parser.newNot(set2choice(first.setdiff(p.flw, first.calcfirst(g, p.p1))))
local pred = parser.newNot(set2choice(p.flw))
if rec then
local seq = newNode('var', 'EatToken')
table.insert(g.plist, s)
g.prules[s] = newNode('star', parser.newSeq(pred, seq))
end
local p2 = parser.newSeq(pred, parser.newSeq(parser.newThrow(s), parser.newAny()))
local choice = parser.newOrd(p.p1, p2)
return newNode(p, choice)
end
local function markerror (p, flw)
p.throw = true
p.flw = flw
return p
end
local function putlabel (g, p, rec)
if p.throw then
io.write("Err_" .. ierr .. ", ")
if p.tag == 'star' or p.tag == 'opt' or p.tag == 'plus' then
return adderrorstar(g, p, rec)
else
return adderror(g, p, rec)
end
else
return p
end
end
local function labelexp (g, p, rec)
if p.tag == 'char' or p.tag == 'var' or p.tag == 'any' then
return putlabel(g, p, rec)
elseif p.tag == 'ord' then
local p1 = labelexp(g, p.p1, rec)
local p2 = labelexp(g, p.p2, rec)
return putlabel(g, newNode(p, p1, p2), rec)
elseif p.tag == 'con' then
local p1 = labelexp(g, p.p1, rec)
local p2 = labelexp(g, p.p2, rec)
return newNode(p, p1, p2, rec)
elseif p.tag == 'star' or p.tag == 'plus' or p.tag == 'opt' then
local p1 = labelexp(g, p.p1, rec)
return putlabel(g, newNode(p, p1), rec)
else
return p
end
end
local function getLexRules (g)
local t = {}
for i, v in ipairs(g.plist) do
if parser.isLexRule(v) and v ~= 'SKIP' and v ~= 'SPACE' then
t['__' .. v] = true
end
end
return t
end
local function addTkRule (g)
local t = getLexRules(g)
-- add literal tokens to t
for k, v in pairs(g.tokens) do
t[k] = v
end
local p = set2choice(t)
g.prules['Token'] = p
table.insert(g.plist, 'Token')
end
local function addEatTkRule (g)
-- (!FOLLOW(p) eatToken space)*
-- eatToken <- token / (!space .)+ skip
local newSeq = parser.newSeq
local any = parser.newAny()
local tk = newNode('var', 'Token')
local notspace = parser.newNot(newNode('var', 'SPACE'))
local eatToken = parser.newOrd(tk, newNode('plus', newSeq(notspace, any)))
g.prules['EatToken'] = newSeq(eatToken, newNode('var', 'SKIP'))
table.insert(g.plist, 'EatToken')
end
local function labelgrammar (g, rec)
if rec then
addTkRule(g)
addEatTkRule(g)
end
io.write("Adding labels: ")
ierr = 1
for i, v in ipairs(g.plist) do
if not parser.isLexRule(v) then
g.prules[v] = labelexp(g, g.prules[v], rec)
else
g.prules[v] = g.prules[v]
end
g.plist[i] = v
end
io.write("\n\n")
return g
end
return {
labelgrammar = labelgrammar,
markerror = markerror
}