aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergio Medeiros <sqmedeiros@gmail.com>2014-11-13 16:49:23 -0300
committerSergio Medeiros <sqmedeiros@gmail.com>2014-11-13 16:49:23 -0300
commit4d9fd92a7fcd124b2dc62e149bf218a6d2f5d711 (patch)
tree1796f2df89a1990fc286e9708742ec2b36aa3eb0
parenta7d952491eab8839d5b0df056b13954c575d16e5 (diff)
downloadlpeglabel-4d9fd92a7fcd124b2dc62e149bf218a6d2f5d711.tar.gz
lpeglabel-4d9fd92a7fcd124b2dc62e149bf218a6d2f5d711.tar.bz2
lpeglabel-4d9fd92a7fcd124b2dc62e149bf218a6d2f5d711.zip
Now it's possible to give names to labels in 're'
-rw-r--r--re.lua10
1 files changed, 8 insertions, 2 deletions
diff --git a/re.lua b/re.lua
index f45aad1..ef1edae 100644
--- a/re.lua
+++ b/re.lua
@@ -26,6 +26,7 @@ local any = m.P(1)
26 26
27-- Pre-defined names 27-- Pre-defined names
28local Predef = { nl = m.P"\n" } 28local Predef = { nl = m.P"\n" }
29local tlabels = {}
29 30
30 31
31local mem 32local mem
@@ -177,7 +178,7 @@ local exp = m.P{ "Exp",
177 Exp = S * ( m.V"Grammar" 178 Exp = S * ( m.V"Grammar"
178 + (m.V"Seq") * ("/" * m.V"Labels" * S * m.V"Seq")^1 / labchoice 179 + (m.V"Seq") * ("/" * m.V"Labels" * S * m.V"Seq")^1 / labchoice
179 + m.Cf(m.V"Seq" * ("/" * S * m.V"Seq")^0, mt.__add) ); 180 + m.Cf(m.V"Seq" * ("/" * S * m.V"Seq")^0, mt.__add) );
180 Labels = m.Ct(m.P"{" * S * num * (S * "," * S * num)^0 * S * "}"); 181 Labels = m.Ct(m.P"{" * S * m.V"Label" * (S * "," * S * m.V"Label")^0 * S * "}");
181 Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix"^0 , mt.__mul) 182 Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix"^0 , mt.__mul)
182 * (#seq_follow + patt_error); 183 * (#seq_follow + patt_error);
183 Prefix = "&" * S * m.V"Prefix" / mt.__len 184 Prefix = "&" * S * m.V"Prefix" / mt.__len
@@ -201,7 +202,7 @@ local exp = m.P{ "Exp",
201 + String / mm.P 202 + String / mm.P
202 + Class 203 + Class
203 + defined 204 + defined
204 + "%{" * S * num * (S * "," * S * num)^0 * S * "}" / mm.T 205 + "%{" * S * m.V"Label" * (S * "," * S * m.V"Label")^0 * S * "}" / mm.T
205 + "{:" * (name * ":" + m.Cc(nil)) * m.V"Exp" * ":}" / 206 + "{:" * (name * ":" + m.Cc(nil)) * m.V"Exp" * ":}" /
206 function (n, p) return mm.Cg(p, n) end 207 function (n, p) return mm.Cg(p, n) end
207 + "=" * name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end 208 + "=" * name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end
@@ -211,6 +212,7 @@ local exp = m.P{ "Exp",
211 + "{" * m.V"Exp" * "}" / mm.C 212 + "{" * m.V"Exp" * "}" / mm.C
212 + m.P"." * m.Cc(any) 213 + m.P"." * m.Cc(any)
213 + (name * -arrow + "<" * name * ">") * m.Cb("G") / NT; 214 + (name * -arrow + "<" * name * ">") * m.Cb("G") / NT;
215 Label = num + name / function (f) return tlabels[f] end;
214 Definition = name * arrow * m.V"Exp"; 216 Definition = name * arrow * m.V"Exp";
215 Grammar = m.Cg(m.Cc(true), "G") * 217 Grammar = m.Cg(m.Cc(true), "G") *
216 m.Cf(m.V"Definition" / firstdef * m.Cg(m.V"Definition")^0, 218 m.Cf(m.V"Definition" / firstdef * m.Cg(m.V"Definition")^0,
@@ -261,6 +263,9 @@ local function gsub (s, p, rep)
261 return cp:match(s) 263 return cp:match(s)
262end 264end
263 265
266local function setlabels (t)
267 tlabels = t
268end
264 269
265-- exported names 270-- exported names
266local re = { 271local re = {
@@ -269,6 +274,7 @@ local re = {
269 find = find, 274 find = find,
270 gsub = gsub, 275 gsub = gsub,
271 updatelocale = updatelocale, 276 updatelocale = updatelocale,
277 setlabels = setlabels
272} 278}
273 279
274if version == "Lua 5.1" then _G.re = re end 280if version == "Lua 5.1" then _G.re = re end