aboutsummaryrefslogtreecommitdiff
path: root/re.lua
diff options
context:
space:
mode:
Diffstat (limited to 're.lua')
-rw-r--r--re.lua276
1 files changed, 276 insertions, 0 deletions
diff --git a/re.lua b/re.lua
new file mode 100644
index 0000000..f45aad1
--- /dev/null
+++ b/re.lua
@@ -0,0 +1,276 @@
1-- $Id: re.lua,v 1.44 2013/03/26 20:11:40 roberto Exp $
2
3-- imported functions and modules
4local tonumber, type, print, error = tonumber, type, print, error
5local setmetatable = setmetatable
6local unpack = table.unpack
7local m = require"lpeglabel"
8
9-- 'm' will be used to parse expressions, and 'mm' will be used to
10-- create expressions; that is, 're' runs on 'm', creating patterns
11-- on 'mm'
12local mm = m
13
14-- pattern's metatable
15local mt = getmetatable(mm.P(0))
16
17
18
19-- No more global accesses after this point
20local version = _VERSION
21if version == "Lua 5.2" then _ENV = nil end
22
23
24local any = m.P(1)
25
26
27-- Pre-defined names
28local Predef = { nl = m.P"\n" }
29
30
31local mem
32local fmem
33local gmem
34
35
36local function updatelocale ()
37 mm.locale(Predef)
38 Predef.a = Predef.alpha
39 Predef.c = Predef.cntrl
40 Predef.d = Predef.digit
41 Predef.g = Predef.graph
42 Predef.l = Predef.lower
43 Predef.p = Predef.punct
44 Predef.s = Predef.space
45 Predef.u = Predef.upper
46 Predef.w = Predef.alnum
47 Predef.x = Predef.xdigit
48 Predef.A = any - Predef.a
49 Predef.C = any - Predef.c
50 Predef.D = any - Predef.d
51 Predef.G = any - Predef.g
52 Predef.L = any - Predef.l
53 Predef.P = any - Predef.p
54 Predef.S = any - Predef.s
55 Predef.U = any - Predef.u
56 Predef.W = any - Predef.w
57 Predef.X = any - Predef.x
58 mem = {} -- restart memoization
59 fmem = {}
60 gmem = {}
61 local mt = {__mode = "v"}
62 setmetatable(mem, mt)
63 setmetatable(fmem, mt)
64 setmetatable(gmem, mt)
65end
66
67
68updatelocale()
69
70
71
72local I = m.P(function (s,i) print(i, s:sub(1, i-1)); return i end)
73
74
75local function getdef (id, defs)
76 local c = defs and defs[id]
77 if not c then error("undefined name: " .. id) end
78 return c
79end
80
81
82local function patt_error (s, i)
83 local msg = (#s < i + 20) and s:sub(i)
84 or s:sub(i,i+20) .. "..."
85 msg = ("pattern error near '%s'"):format(msg)
86 error(msg, 2)
87end
88
89local function mult (p, n)
90 local np = mm.P(true)
91 while n >= 1 do
92 if n%2 >= 1 then np = np * p end
93 p = p * p
94 n = n/2
95 end
96 return np
97end
98
99local function equalcap (s, i, c)
100 if type(c) ~= "string" then return nil end
101 local e = #c + i
102 if s:sub(i, e - 1) == c then return e else return nil end
103end
104
105
106local S = (Predef.space + "--" * (any - Predef.nl)^0)^0
107
108local name = m.R("AZ", "az", "__") * m.R("AZ", "az", "__", "09")^0
109
110local arrow = S * "<-"
111
112local seq_follow = m.P"/" + ")" + "}" + ":}" + "~}" + "|}" + (name * arrow) + -1
113
114name = m.C(name)
115
116
117-- a defined name only have meaning in a given environment
118local Def = name * m.Carg(1)
119
120local num = m.C(m.R"09"^1) * S / tonumber
121
122local String = "'" * m.C((any - "'")^0) * "'" +
123 '"' * m.C((any - '"')^0) * '"'
124
125
126local defined = "%" * Def / function (c,Defs)
127 local cat = Defs and Defs[c] or Predef[c]
128 if not cat then error ("name '" .. c .. "' undefined") end
129 return cat
130end
131
132local Range = m.Cs(any * (m.P"-"/"") * (any - "]")) / mm.R
133
134local item = defined + Range + m.C(any)
135
136local Class =
137 "["
138 * (m.C(m.P"^"^-1)) -- optional complement symbol
139 * m.Cf(item * (item - "]")^0, mt.__add) /
140 function (c, p) return c == "^" and any - p or p end
141 * "]"
142
143local function adddef (t, k, exp)
144 if t[k] then
145 error("'"..k.."' already defined as a rule")
146 else
147 t[k] = exp
148 end
149 return t
150end
151
152local function firstdef (n, r) return adddef({n}, n, r) end
153
154
155local function NT (n, b)
156 if not b then
157 error("rule '"..n.."' used outside a grammar")
158 else return mm.V(n)
159 end
160end
161
162local function labchoice (...)
163 local t = { ... }
164 local n = #t
165 local p = t[1]
166 local i = 2
167 while i + 1 <= n do
168 p = mm.Lc(p, t[i+1], unpack(t[i]))
169 i = i + 2
170 end
171
172 return p
173end
174
175
176local exp = m.P{ "Exp",
177 Exp = S * ( m.V"Grammar"
178 + (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 Labels = m.Ct(m.P"{" * S * num * (S * "," * S * num)^0 * S * "}");
181 Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix"^0 , mt.__mul)
182 * (#seq_follow + patt_error);
183 Prefix = "&" * S * m.V"Prefix" / mt.__len
184 + "!" * S * m.V"Prefix" / mt.__unm
185 + m.V"Suffix";
186 Suffix = m.Cf(m.V"Primary" * S *
187 ( ( m.P"+" * m.Cc(1, mt.__pow)
188 + m.P"*" * m.Cc(0, mt.__pow)
189 + m.P"?" * m.Cc(-1, mt.__pow)
190 + "^" * ( m.Cg(num * m.Cc(mult))
191 + m.Cg(m.C(m.S"+-" * m.R"09"^1) * m.Cc(mt.__pow))
192 )
193 + "->" * S * ( m.Cg((String + num) * m.Cc(mt.__div))
194 + m.P"{}" * m.Cc(nil, m.Ct)
195 + m.Cg(Def / getdef * m.Cc(mt.__div))
196 )
197 + "=>" * S * m.Cg(Def / getdef * m.Cc(m.Cmt))
198 ) * S
199 )^0, function (a,b,f) return f(a,b) end );
200 Primary = "(" * m.V"Exp" * ")"
201 + String / mm.P
202 + Class
203 + defined
204 + "%{" * S * num * (S * "," * S * num)^0 * S * "}" / mm.T
205 + "{:" * (name * ":" + m.Cc(nil)) * m.V"Exp" * ":}" /
206 function (n, p) return mm.Cg(p, n) end
207 + "=" * name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end
208 + m.P"{}" / mm.Cp
209 + "{~" * m.V"Exp" * "~}" / mm.Cs
210 + "{|" * m.V"Exp" * "|}" / mm.Ct
211 + "{" * m.V"Exp" * "}" / mm.C
212 + m.P"." * m.Cc(any)
213 + (name * -arrow + "<" * name * ">") * m.Cb("G") / NT;
214 Definition = name * arrow * m.V"Exp";
215 Grammar = m.Cg(m.Cc(true), "G") *
216 m.Cf(m.V"Definition" / firstdef * m.Cg(m.V"Definition")^0,
217 adddef) / mm.P
218}
219
220local pattern = S * m.Cg(m.Cc(false), "G") * exp / mm.P * (-any + patt_error)
221
222
223local function compile (p, defs)
224 if mm.type(p) == "pattern" then return p end -- already compiled
225 local cp = pattern:match(p, 1, defs)
226 if not cp then error("incorrect pattern", 3) end
227 return cp
228end
229
230local function match (s, p, i)
231 local cp = mem[p]
232 if not cp then
233 cp = compile(p)
234 mem[p] = cp
235 end
236 return cp:match(s, i or 1)
237end
238
239local function find (s, p, i)
240 local cp = fmem[p]
241 if not cp then
242 cp = compile(p) / 0
243 cp = mm.P{ mm.Cp() * cp * mm.Cp() + 1 * mm.V(1) }
244 fmem[p] = cp
245 end
246 local i, e = cp:match(s, i or 1)
247 if i then return i, e - 1
248 else return i
249 end
250end
251
252local function gsub (s, p, rep)
253 local g = gmem[p] or {} -- ensure gmem[p] is not collected while here
254 gmem[p] = g
255 local cp = g[rep]
256 if not cp then
257 cp = compile(p)
258 cp = mm.Cs((cp / rep + 1)^0)
259 g[rep] = cp
260 end
261 return cp:match(s)
262end
263
264
265-- exported names
266local re = {
267 compile = compile,
268 match = match,
269 find = find,
270 gsub = gsub,
271 updatelocale = updatelocale,
272}
273
274if version == "Lua 5.1" then _G.re = re end
275
276return re