Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathre.lua
More file actions
Latest commit
259 lines (203 loc) · 6.14 KB
/
Copy pathre.lua
File metadata and controls
259 lines (203 loc) · 6.14 KB
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
-- $Id: re.lua,v 1.44 2013/03/26 20:11:40 roberto Exp $
-- imported functions and modules
localtonumber, type, print, error=tonumber, type, print, error
localsetmetatable=setmetatable
localm=require"lpeg"
-- 'm' will be used to parse expressions, and 'mm' will be used to
-- create expressions; that is, 're' runs on 'm', creating patterns
-- on 'mm'
localmm=m
-- pattern's metatable
localmt=getmetatable(mm.P(0))
-- No more global accesses after this point
localversion=_VERSION
ifversion=="Lua 5.2" then_ENV=nilend
localany=m.P(1)
-- Pre-defined names
localPredef= { nl=m.P"\n" }
localmem
localfmem
localgmem
localfunctionupdatelocale ()
mm.locale(Predef)
Predef.a=Predef.alpha
Predef.c=Predef.cntrl
Predef.d=Predef.digit
Predef.g=Predef.graph
Predef.l=Predef.lower
Predef.p=Predef.punct
Predef.s=Predef.space
Predef.u=Predef.upper
Predef.w=Predef.alnum
Predef.x=Predef.xdigit
Predef.A=any-Predef.a
Predef.C=any-Predef.c
Predef.D=any-Predef.d
Predef.G=any-Predef.g
Predef.L=any-Predef.l
Predef.P=any-Predef.p
Predef.S=any-Predef.s
Predef.U=any-Predef.u
Predef.W=any-Predef.w
Predef.X=any-Predef.x
mem= {} -- restart memoization
fmem= {}
gmem= {}
localmt= {__mode="v"}
setmetatable(mem, mt)
setmetatable(fmem, mt)
setmetatable(gmem, mt)
end
updatelocale()
localI=m.P(function (s,i) print(i, s:sub(1, i-1)); returniend)
localfunctiongetdef (id, defs)
localc=defsanddefs[id]
ifnotcthenerror("undefined name: " ..id) end
returnc
end
localfunctionpatt_error (s, i)
localmsg= (#s<i+20) ands:sub(i)
ors:sub(i,i+20) .."..."
msg= ("pattern error near '%s'"):format(msg)
error(msg, 2)
end
localfunctionmult (p, n)
localnp=mm.P(true)
whilen>=1do
ifn%2>=1thennp=np*pend
p=p*p
n=n/2
end
returnnp
end
localfunctionequalcap (s, i, c)
iftype(c) ~="string" thenreturnnilend
locale=#c+i
ifs:sub(i, e-1) ==cthenreturneelsereturnnilend
end
localS= (Predef.space+"--" * (any-Predef.nl)^0)^0
localname=m.R("AZ", "az", "__") *m.R("AZ", "az", "__", "09")^0
localarrow=S*"<-"
localseq_follow=m.P"/" +")" +"}" +":}" +"~}" +"|}" + (name*arrow) +-1
name=m.C(name)
-- a defined name only have meaning in a given environment
localDef=name*m.Carg(1)
localnum=m.C(m.R"09"^1) *S/tonumber
localString="'" *m.C((any-"'")^0) *"'" +
'"' *m.C((any-'"')^0) *'"'
localdefined="%" *Def/function (c,Defs)
localcat=DefsandDefs[c] orPredef[c]
ifnotcatthenerror ("name '" ..c.."' undefined") end
returncat
end
localRange=m.Cs(any* (m.P"-"/"") * (any-"]")) /mm.R
localitem=defined+Range+m.C(any)
localClass=
"["
* (m.C(m.P"^"^-1)) -- optional complement symbol
*m.Cf(item* (item-"]")^0, mt.__add) /
function (c, p) returnc=="^" andany-porpend
*"]"
localfunctionadddef (t, k, exp)
ift[k] then
error("'"..k.."' already defined as a rule")
else
t[k] =exp
end
returnt
end
localfunctionfirstdef (n, r) returnadddef({n}, n, r) end
localfunctionNT (n, b)
ifnotbthen
error("rule '"..n.."' used outside a grammar")
elsereturnmm.V(n)
end
end
localexp=m.P{ "Exp",
Exp=S* ( m.V"Grammar"
+m.Cf(m.V"Seq" * ("/" *S*m.V"Seq")^0, mt.__add) );
Seq=m.Cf(m.Cc(m.P"") *m.V"Prefix"^0 , mt.__mul)
* (#seq_follow+patt_error);
Prefix="&" *S*m.V"Prefix" /mt.__len
+"!" *S*m.V"Prefix" /mt.__unm
+m.V"Suffix";
Suffix=m.Cf(m.V"Primary" *S*
( ( m.P"+" *m.Cc(1, mt.__pow)
+m.P"*" *m.Cc(0, mt.__pow)
+m.P"?" *m.Cc(-1, mt.__pow)
+"^" * ( m.Cg(num*m.Cc(mult))
+m.Cg(m.C(m.S"+-" *m.R"09"^1) *m.Cc(mt.__pow))
)
+"->" *S* ( m.Cg((String+num) *m.Cc(mt.__div))
+m.P"{}" *m.Cc(nil, m.Ct)
+m.Cg(Def/getdef*m.Cc(mt.__div))
)
+"=>" *S*m.Cg(Def/getdef*m.Cc(m.Cmt))
) *S
)^0, function (a,b,f) returnf(a,b) end );
Primary="(" *m.V"Exp" *")"
+String/mm.P
+Class
+defined
+"{:" * (name*":" +m.Cc(nil)) *m.V"Exp" *":}" /
function (n, p) returnmm.Cg(p, n) end
+"=" *name/function (n) returnmm.Cmt(mm.Cb(n), equalcap) end
+m.P"{}" /mm.Cp
+"{~" *m.V"Exp" *"~}" /mm.Cs
+"{|" *m.V"Exp" *"|}" /mm.Ct
+"{" *m.V"Exp" *"}" /mm.C
+m.P"." *m.Cc(any)
+ (name*-arrow+"<" *name*">") *m.Cb("G") /NT;
Definition=name*arrow*m.V"Exp";
Grammar=m.Cg(m.Cc(true), "G") *
m.Cf(m.V"Definition" /firstdef*m.Cg(m.V"Definition")^0,
adddef) /mm.P
}
localpattern=S*m.Cg(m.Cc(false), "G") *exp/mm.P* (-any+patt_error)
localfunctioncompile (p, defs)
ifmm.type(p) =="pattern" thenreturnpend-- already compiled
localcp=pattern:match(p, 1, defs)
ifnotcpthenerror("incorrect pattern", 3) end
returncp
end
localfunctionmatch (s, p, i)
localcp=mem[p]
ifnotcpthen
cp=compile(p)
mem[p] =cp
end
returncp:match(s, ior1)
end
localfunctionfind (s, p, i)
localcp=fmem[p]
ifnotcpthen
cp=compile(p) /0
cp=mm.P{ mm.Cp() *cp*mm.Cp() +1*mm.V(1) }
fmem[p] =cp
end
locali, e=cp:match(s, ior1)
ifithenreturni, e-1
elsereturni
end
end
localfunctiongsub (s, p, rep)
localg=gmem[p] or {} -- ensure gmem[p] is not collected while here
gmem[p] =g
localcp=g[rep]
ifnotcpthen
cp=compile(p)
cp=mm.Cs((cp/rep+1)^0)
g[rep] =cp
end
returncp:match(s)
end
-- exported names
localre= {
compile=compile,
match=match,
find=find,
gsub=gsub,
updatelocale=updatelocale,
}
ifversion=="Lua 5.1" then_G.re=reend
returnre