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 pathlpvm.c
More file actions
Latest commit
355 lines (325 loc) · 10.4 KB
/
Copy pathlpvm.c
File metadata and controls
355 lines (325 loc) · 10.4 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
** $Id: lpvm.c,v 1.5 2013/04/12 16:29:49 roberto Exp $
** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license)
*/
#include<limits.h>
#include<string.h>
#include"lua.h"
#include"lauxlib.h"
#include"lpcap.h"
#include"lptypes.h"
#include"lpvm.h"
#include"lpprint.h"
/* initial size for call/backtrack stack */
#if !defined(INITBACK)
#defineINITBACK 100
#endif
#definegetoffset(p) (((p) + 1)->offset)
staticconstInstructiongiveup= {{IGiveup, 0, 0}};
/*
** {======================================================
** Virtual Machine
** =======================================================
*/
typedefstructStack {
constchar*s; /* saved position (or NULL for calls) */
constInstruction*p; /* next instruction */
intcaplevel;
} Stack;
#definegetstackbase(L, ptop) ((Stack *)lua_touserdata(L, stackidx(ptop)))
/*
** Double the size of the array of captures
*/
staticCapture*doublecap (lua_State*L, Capture*cap, intcaptop, intptop) {
Capture*newc;
if (captop >= INT_MAX/((int)sizeof(Capture) *2))
luaL_error(L, "too many captures");
newc= (Capture*)lua_newuserdata(L, captop*2*sizeof(Capture));
memcpy(newc, cap, captop*sizeof(Capture));
lua_replace(L, caplistidx(ptop));
returnnewc;
}
/*
** Double the size of the stack
*/
staticStack*doublestack (lua_State*L, Stack**stacklimit, intptop) {
Stack*stack=getstackbase(L, ptop);
Stack*newstack;
intn=*stacklimit-stack; /* current stack size */
intmax, newn;
lua_getfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX);
max=lua_tointeger(L, -1); /* maximum allowed size */
lua_pop(L, 1);
if (n >= max) /* already at maximum size? */
luaL_error(L, "too many pending calls/choices");
newn=2*n; /* new size */
if (newn>max) newn=max;
newstack= (Stack*)lua_newuserdata(L, newn*sizeof(Stack));
memcpy(newstack, stack, n*sizeof(Stack));
lua_replace(L, stackidx(ptop));
*stacklimit=newstack+newn;
returnnewstack+n; /* return next position */
}
/*
** Interpret the result of a dynamic capture: false -> fail;
** true -> keep current position; number -> next position.
** Return new subject position. 'fr' is stack index where
** is the result; 'curr' is current subject position; 'limit'
** is subject's size.
*/
staticintresdyncaptures (lua_State*L, intfr, intcurr, intlimit) {
lua_Integerres;
if (!lua_toboolean(L, fr)) { /* false value? */
lua_settop(L, fr-1); /* remove results */
return-1; /* and fail */
}
elseif (lua_isboolean(L, fr)) /* true? */
res=curr; /* keep current position */
else {
res=lua_tointeger(L, fr) -1; /* new position */
if (res<curr||res>limit)
luaL_error(L, "invalid position returned by match-time capture");
}
lua_remove(L, fr); /* remove first result (offset) */
returnres;
}
/*
** Add capture values returned by a dynamic capture to the capture list
** 'base', nested inside a group capture. 'fd' indexes the first capture
** value, 'n' is the number of values (at least 1).
*/
staticvoidadddyncaptures (constchar*s, Capture*base, intn, intfd) {
inti;
/* Cgroup capture is already there */
assert(base[0].kind==Cgroup&&base[0].siz==0);
base[0].idx=0; /* make it an anonymous group */
for (i=1; i <= n; i++) { /* add runtime captures */
base[i].kind=Cruntime;
base[i].siz=1; /* mark it as closed */
base[i].idx=fd+i-1; /* stack index of capture value */
base[i].s=s;
}
base[i].kind=Cclose; /* close group */
base[i].siz=1;
base[i].s=s;
}
/*
** Remove dynamic captures from the Lua stack (called in case of failure)
*/
staticintremovedyncap (lua_State*L, Capture*capture,
intlevel, intlast) {
intid=finddyncap(capture+level, capture+last); /* index of 1st cap. */
inttop=lua_gettop(L);
if (id==0) return0; /* no dynamic captures? */
lua_settop(L, id-1); /* remove captures */
returntop-id+1; /* number of values removed */
}
/*
** Opcode interpreter
*/
constchar*match (lua_State*L, constchar*o, constchar*s, constchar*e,
Instruction*op, Capture*capture, intptop) {
Stackstackbase[INITBACK];
Stack*stacklimit=stackbase+INITBACK;
Stack*stack=stackbase; /* point to first empty slot in stack */
intcapsize=INITCAPSIZE;
intcaptop=0; /* point to first empty slot in captures */
intndyncap=0; /* number of dynamic captures (in Lua stack) */
constInstruction*p=op; /* current instruction */
stack->p=&giveup; stack->s=s; stack->caplevel=0; stack++;
lua_pushlightuserdata(L, stackbase);
for (;;) {
#if defined(DEBUG)
printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ",
s, stack-getstackbase(L, ptop), ndyncap, captop);
printinst(op, p);
printcaplist(capture, capture+captop);
#endif
assert(stackidx(ptop) +ndyncap==lua_gettop(L) &&ndyncap <= captop);
switch ((Opcode)p->i.code) {
caseIEnd: {
assert(stack==getstackbase(L, ptop) +1);
capture[captop].kind=Cclose;
capture[captop].s=NULL;
returns;
}
caseIGiveup: {
assert(stack==getstackbase(L, ptop));
returnNULL;
}
caseIRet: {
assert(stack>getstackbase(L, ptop) && (stack-1)->s==NULL);
p= (--stack)->p;
continue;
}
caseIAny: {
if (s<e) { p++; s++; }
else goto fail;
continue;
}
caseITestAny: {
if (s<e) p+=2;
elsep+=getoffset(p);
continue;
}
caseIChar: {
if ((byte)*s==p->i.aux&&s<e) { p++; s++; }
else goto fail;
continue;
}
caseITestChar: {
if ((byte)*s==p->i.aux&&s<e) p+=2;
elsep+=getoffset(p);
continue;
}
caseISet: {
intc= (byte)*s;
if (testchar((p+1)->buff, c) &&s<e)
{ p+=CHARSETINSTSIZE; s++; }
else goto fail;
continue;
}
caseITestSet: {
intc= (byte)*s;
if (testchar((p+2)->buff, c) &&s<e)
p+=1+CHARSETINSTSIZE;
elsep+=getoffset(p);
continue;
}
caseIBehind: {
intn=p->i.aux;
if (n>s-o) goto fail;
s-=n; p++;
continue;
}
caseISpan: {
for (; s<e; s++) {
intc= (byte)*s;
if (!testchar((p+1)->buff, c)) break;
}
p+=CHARSETINSTSIZE;
continue;
}
caseIJmp: {
p+=getoffset(p);
continue;
}
caseIChoice: {
if (stack==stacklimit)
stack=doublestack(L, &stacklimit, ptop);
stack->p=p+getoffset(p);
stack->s=s;
stack->caplevel=captop;
stack++;
p+=2;
continue;
}
caseICall: {
if (stack==stacklimit)
stack=doublestack(L, &stacklimit, ptop);
stack->s=NULL;
stack->p=p+2; /* save return address */
stack++;
p+=getoffset(p);
continue;
}
caseICommit: {
assert(stack>getstackbase(L, ptop) && (stack-1)->s!=NULL);
stack--;
p+=getoffset(p);
continue;
}
caseIPartialCommit: {
assert(stack>getstackbase(L, ptop) && (stack-1)->s!=NULL);
(stack-1)->s=s;
(stack-1)->caplevel=captop;
p+=getoffset(p);
continue;
}
caseIBackCommit: {
assert(stack>getstackbase(L, ptop) && (stack-1)->s!=NULL);
s= (--stack)->s;
captop=stack->caplevel;
p+=getoffset(p);
continue;
}
caseIFailTwice:
assert(stack>getstackbase(L, ptop));
stack--;
/* go through */
caseIFail:
fail: { /* pattern failed: try to backtrack */
do { /* remove pending calls */
assert(stack>getstackbase(L, ptop));
s= (--stack)->s;
} while (s==NULL);
if (ndyncap>0) /* is there matchtime captures? */
ndyncap-=removedyncap(L, capture, stack->caplevel, captop);
captop=stack->caplevel;
p=stack->p;
continue;
}
caseICloseRunTime: {
CapStatecs;
intrem, res, n;
intfr=lua_gettop(L) +1; /* stack index of first result */
cs.s=o; cs.L=L; cs.ocap=capture; cs.ptop=ptop;
n=runtimecap(&cs, capture+captop, s, &rem); /* call function */
captop-=n; /* remove nested captures */
fr-=rem; /* 'rem' items were popped from Lua stack */
res=resdyncaptures(L, fr, s-o, e-o); /* get result */
if (res==-1) /* fail? */
goto fail;
s=o+res; /* else update current position */
n=lua_gettop(L) -fr+1; /* number of new captures */
ndyncap+=n-rem; /* update number of dynamic captures */
if (n>0) { /* any new capture? */
if ((captop+=n+2) >= capsize) {
capture=doublecap(L, capture, captop, ptop);
capsize=2*captop;
}
/* add new captures to 'capture' list */
adddyncaptures(s, capture+captop-n-2, n, fr);
}
p++;
continue;
}
caseICloseCapture: {
constchar*s1=s;
assert(captop>0);
/* if possible, turn capture into a full capture */
if (capture[captop-1].siz==0&&
s1-capture[captop-1].s<UCHAR_MAX) {
capture[captop-1].siz=s1-capture[captop-1].s+1;
p++;
continue;
}
else {
capture[captop].siz=1; /* mark entry as closed */
capture[captop].s=s;
goto pushcapture;
}
}
caseIOpenCapture:
capture[captop].siz=0; /* mark entry as open */
capture[captop].s=s;
goto pushcapture;
caseIFullCapture:
capture[captop].siz=getoff(p) +1; /* save capture size */
capture[captop].s=s-getoff(p);
/* goto pushcapture; */
pushcapture: {
capture[captop].idx=p->i.key;
capture[captop].kind=getkind(p);
if (++captop >= capsize) {
capture=doublecap(L, capture, captop, ptop);
capsize=2*captop;
}
p++;
continue;
}
default: assert(0); returnNULL;
}
}
}
/* }====================================================== */