aboutsummaryrefslogtreecommitdiff
path: root/relabel.lua
diff options
context:
space:
mode:
authorUndecidable Robot <undecidabot@gmail.com>2016-05-07 21:30:30 +0800
committerUndecidable Robot <undecidabot@gmail.com>2016-05-07 21:31:19 +0800
commit0233268dd31b7d708d6123167c0d97dc1c836dd4 (patch)
treead914d437c3d8afe9452f04cfe3e8f407515b508 /relabel.lua
parent15bc429d1ca206131537dad8132b460f66e6ae89 (diff)
downloadlpeglabel-0233268dd31b7d708d6123167c0d97dc1c836dd4.tar.gz
lpeglabel-0233268dd31b7d708d6123167c0d97dc1c836dd4.tar.bz2
lpeglabel-0233268dd31b7d708d6123167c0d97dc1c836dd4.zip
Annotating relabel grammar with labels
Diffstat (limited to 'relabel.lua')
-rw-r--r--relabel.lua52
1 files changed, 27 insertions, 25 deletions
diff --git a/relabel.lua b/relabel.lua
index b66fc2e..12de21e 100644
--- a/relabel.lua
+++ b/relabel.lua
@@ -120,8 +120,8 @@ local Def = name * m.Carg(1)
120 120
121local num = m.C(m.R"09"^1) * S / tonumber 121local num = m.C(m.R"09"^1) * S / tonumber
122 122
123local String = "'" * m.C((any - "'")^0) * "'" + 123local String = "'" * m.C((any - "'")^0) * ("'" + m.T(31)) +
124 '"' * m.C((any - '"')^0) * '"' 124 '"' * m.C((any - '"')^0) * ('"' + m.T(30))
125 125
126 126
127local defined = "%" * Def / function (c,Defs) 127local defined = "%" * Def / function (c,Defs)
@@ -137,9 +137,9 @@ local item = defined + Range + m.C(any)
137local Class = 137local Class =
138 "[" 138 "["
139 * (m.C(m.P"^"^-1)) -- optional complement symbol 139 * (m.C(m.P"^"^-1)) -- optional complement symbol
140 * m.Cf(item * (item - "]")^0, mt.__add) / 140 * m.Cf((item + m.T(24)) * (item - "]")^0, mt.__add) /
141 function (c, p) return c == "^" and any - p or p end 141 function (c, p) return c == "^" and any - p or p end
142 * "]" 142 * ("]" + m.T(25))
143 143
144local function adddef (t, k, exp) 144local function adddef (t, k, exp)
145 if t[k] then 145 if t[k] then
@@ -176,13 +176,12 @@ end
176 176
177local exp = m.P{ "Exp", 177local exp = m.P{ "Exp",
178 Exp = S * ( m.V"Grammar" 178 Exp = S * ( m.V"Grammar"
179 + (m.V"Seq") * ("/" * m.V"Labels" * S * m.V"Seq")^1 / labchoice 179 + (m.V"Seq") * ("/" * m.V"Labels" * S * (m.V"Seq" + m.T(3)))^1 / labchoice
180 + m.Cf(m.V"Seq" * ("/" * S * m.V"Seq")^0, mt.__add) ); 180 + m.Cf(m.V"Seq" * ("/" * S * (m.V"Seq" + m.T(4)))^0, mt.__add) );
181 Labels = m.Ct(m.P"{" * S * m.V"Label" * (S * "," * S * m.V"Label")^0 * S * "}"); 181 Labels = m.Ct(m.P"{" * S * (m.V"Label" + m.T(27)) * (S * "," * S * (m.V"Label" + m.T(28)))^0 * S * ("}" + m.T(29)));
182 Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix"^0 , mt.__mul) 182 Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix"^1 , mt.__mul);
183 * (#seq_follow + patt_error); 183 Prefix = "&" * S * (m.V"Prefix" + m.T(5)) / mt.__len
184 Prefix = "&" * S * m.V"Prefix" / mt.__len 184 + "!" * S * (m.V"Prefix" + m.T(6)) / mt.__unm
185 + "!" * S * m.V"Prefix" / mt.__unm
186 + m.V"Suffix"; 185 + m.V"Suffix";
187 Suffix = m.Cf(m.V"Primary" * S * 186 Suffix = m.Cf(m.V"Primary" * S *
188 ( ( m.P"+" * m.Cc(1, mt.__pow) 187 ( ( m.P"+" * m.Cc(1, mt.__pow)
@@ -190,42 +189,45 @@ local exp = m.P{ "Exp",
190 + m.P"?" * m.Cc(-1, mt.__pow) 189 + m.P"?" * m.Cc(-1, mt.__pow)
191 + "^" * ( m.Cg(num * m.Cc(mult)) 190 + "^" * ( m.Cg(num * m.Cc(mult))
192 + m.Cg(m.C(m.S"+-" * m.R"09"^1) * m.Cc(mt.__pow)) 191 + m.Cg(m.C(m.S"+-" * m.R"09"^1) * m.Cc(mt.__pow))
192 + m.T(7)
193 ) 193 )
194 + "->" * S * ( m.Cg((String + num) * m.Cc(mt.__div)) 194 + "->" * S * ( m.Cg((String + num) * m.Cc(mt.__div))
195 + m.P"{}" * m.Cc(nil, m.Ct) 195 + m.P"{" * (m.P"}" + m.T(8)) * m.Cc(nil, m.Ct)
196 + m.Cg(Def / getdef * m.Cc(mt.__div)) 196 + m.Cg(Def / getdef * m.Cc(mt.__div))
197 + m.T(9)
197 ) 198 )
198 + "=>" * S * m.Cg(Def / getdef * m.Cc(m.Cmt)) 199 + "=>" * S * (m.Cg(Def / getdef * m.Cc(m.Cmt)) + m.T(10))
199 ) * S 200 ) * S
200 )^0, function (a,b,f) return f(a,b) end ); 201 )^0, function (a,b,f) return f(a,b) end );
201 Primary = "(" * m.V"Exp" * ")" 202 Primary = "(" * (m.V"Exp" + m.T(11)) * (")" + m.T(12))
202 + String / mm.P 203 + String / mm.P
203 + Class 204 + Class
204 + defined 205 + defined
205 + "%{" * S * m.V"Label" * (S * "," * S * m.V"Label")^0 * S * "}" / mm.T 206 + "%{" * S * (m.V"Label" + m.T(27)) * (S * "," * S * (m.V"Label" + m.T(28)))^0 * S * ("}" + m.T(29)) / mm.T
206 + "{:" * (name * ":" + m.Cc(nil)) * m.V"Exp" * ":}" / 207 + ("%" * m.T(13))
208 + "{:" * (name * ":" + m.Cc(nil)) * (m.V"Exp" + m.T(14)) * (":}" + m.T(15)) /
207 function (n, p) return mm.Cg(p, n) end 209 function (n, p) return mm.Cg(p, n) end
208 + "=" * name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end 210 + "=" * (name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end + m.T(16))
209 + m.P"{}" / mm.Cp 211 + m.P"{}" / mm.Cp
210 + "{~" * m.V"Exp" * "~}" / mm.Cs 212 + "{~" * (m.V"Exp" + m.T(17)) * ("~}" + m.T(18)) / mm.Cs
211 + "{|" * m.V"Exp" * "|}" / mm.Ct 213 + "{|" * (m.V"Exp" + m.T(32)) * ("|}" + m.T(33)) / mm.Ct
212 + "{" * m.V"Exp" * "}" / mm.C 214 + "{" * (m.V"Exp" + m.T(19)) * ("}" + m.T(20)) / mm.C
213 + m.P"." * m.Cc(any) 215 + m.P"." * m.Cc(any)
214 + (name * -arrow + "<" * name * ">") * m.Cb("G") / NT; 216 + (name * -arrow + "<" * (name + m.T(21)) * (">" + m.T(22))) * m.Cb("G") / NT;
215 Label = num + name / function (f) return tlabels[f] end; 217 Label = num + name / function (f) return tlabels[f] end;
216 Definition = name * arrow * m.V"Exp"; 218 Definition = name * arrow * (m.V"Exp" + m.T(23));
217 Grammar = m.Cg(m.Cc(true), "G") * 219 Grammar = m.Cg(m.Cc(true), "G") *
218 m.Cf(m.V"Definition" / firstdef * m.Cg(m.V"Definition")^0, 220 m.Cf(m.V"Definition" / firstdef * m.Cg(m.V"Definition")^0,
219 adddef) / mm.P 221 adddef) / mm.P
220} 222}
221 223
222local pattern = S * m.Cg(m.Cc(false), "G") * exp / mm.P * (-any + patt_error) 224local pattern = S * m.Cg(m.Cc(false), "G") * (exp + m.T(1)) / mm.P * (-any + m.T(2))
223 225
224 226
225local function compile (p, defs) 227local function compile (p, defs)
226 if mm.type(p) == "pattern" then return p end -- already compiled 228 if mm.type(p) == "pattern" then return p end -- already compiled
227 local cp = pattern:match(p, 1, defs) 229 local cp, label = pattern:match(p, 1, defs)
228 if not cp then error("incorrect pattern", 3) end 230 if not cp then error("incorrect pattern " .. label, 3) end
229 return cp 231 return cp
230end 232end
231 233