You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lpvm.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. ** $Id: lpvm.c,v 1.6 2015/09/28 17:01:25 roberto Exp $
  3. ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license)
  4. */
  5. #include "config.h"
  6. #include <limits.h>
  7. #include <string.h>
  8. #include "lua.h"
  9. #include "lauxlib.h"
  10. #include "lpcap.h"
  11. #include "lptypes.h"
  12. #include "lpvm.h"
  13. #include "lpprint.h"
  14. /* initial size for call/backtrack stack */
  15. #if !defined(INITBACK)
  16. #define INITBACK MAXBACK
  17. #endif
  18. #define getoffset(p) (((p) + 1)->offset)
  19. static const Instruction giveup = {{IGiveup, 0, 0}};
  20. /*
  21. ** {======================================================
  22. ** Virtual Machine
  23. ** =======================================================
  24. */
  25. typedef struct Stack {
  26. const char *s; /* saved position (or NULL for calls) */
  27. const Instruction *p; /* next instruction */
  28. int caplevel;
  29. } Stack;
  30. #define getstackbase(L, ptop) ((Stack *)lua_touserdata(L, stackidx(ptop)))
  31. /*
  32. ** Ensures the size of array 'capture' (with size '*capsize' and
  33. ** 'captop' elements being used) is enough to accomodate 'n' extra
  34. ** elements plus one. (Because several opcodes add stuff to the capture
  35. ** array, it is simpler to ensure the array always has at least one free
  36. ** slot upfront and check its size later.)
  37. */
  38. static Capture *growcap (lua_State *L, Capture *capture, int *capsize,
  39. int captop, int n, int ptop) {
  40. if (*capsize - captop > n)
  41. return capture; /* no need to grow array */
  42. else { /* must grow */
  43. Capture *newc;
  44. int newsize = captop + n + 1; /* minimum size needed */
  45. if (newsize < INT_MAX/((int)sizeof(Capture) * 2))
  46. newsize *= 2; /* twice that size, if not too big */
  47. else if (newsize >= INT_MAX/((int)sizeof(Capture)))
  48. luaL_error(L, "too many captures");
  49. newc = (Capture *)lua_newuserdata(L, newsize * sizeof(Capture));
  50. memcpy(newc, capture, captop * sizeof(Capture));
  51. *capsize = newsize;
  52. lua_replace(L, caplistidx(ptop));
  53. return newc;
  54. }
  55. }
  56. /*
  57. ** Double the size of the stack
  58. */
  59. static Stack *doublestack (lua_State *L, Stack **stacklimit, int ptop) {
  60. Stack *stack = getstackbase(L, ptop);
  61. Stack *newstack;
  62. int n = *stacklimit - stack; /* current stack size */
  63. int max, newn;
  64. lua_getfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX);
  65. max = lua_tointeger(L, -1); /* maximum allowed size */
  66. lua_pop(L, 1);
  67. if (n >= max) /* already at maximum size? */
  68. luaL_error(L, "backtrack stack overflow (current limit is %d)", max);
  69. newn = 2 * n; /* new size */
  70. if (newn > max) newn = max;
  71. newstack = (Stack *)lua_newuserdata(L, newn * sizeof(Stack));
  72. memcpy(newstack, stack, n * sizeof(Stack));
  73. lua_replace(L, stackidx(ptop));
  74. *stacklimit = newstack + newn;
  75. return newstack + n; /* return next position */
  76. }
  77. /*
  78. ** Interpret the result of a dynamic capture: false -> fail;
  79. ** true -> keep current position; number -> next position.
  80. ** Return new subject position. 'fr' is stack index where
  81. ** is the result; 'curr' is current subject position; 'limit'
  82. ** is subject's size.
  83. */
  84. static int resdyncaptures (lua_State *L, int fr, int curr, int limit) {
  85. lua_Integer res;
  86. if (!lua_toboolean(L, fr)) { /* false value? */
  87. lua_settop(L, fr - 1); /* remove results */
  88. return -1; /* and fail */
  89. }
  90. else if (lua_isboolean(L, fr)) /* true? */
  91. res = curr; /* keep current position */
  92. else {
  93. res = lua_tointeger(L, fr) - 1; /* new position */
  94. if (res < curr || res > limit)
  95. luaL_error(L, "invalid position returned by match-time capture");
  96. }
  97. lua_remove(L, fr); /* remove first result (offset) */
  98. return res;
  99. }
  100. /*
  101. ** Add capture values returned by a dynamic capture to the list
  102. ** 'capture', nested inside a group. 'fd' indexes the first capture
  103. ** value, 'n' is the number of values (at least 1). The open group
  104. ** capture is already in 'capture', before the place for the new entries.
  105. */
  106. static void adddyncaptures (const char *s, Capture *capture, int n, int fd) {
  107. int i;
  108. assert(capture[-1].kind == Cgroup && capture[-1].siz == 0);
  109. capture[-1].idx = 0; /* make group capture an anonymous group */
  110. for (i = 0; i < n; i++) { /* add runtime captures */
  111. capture[i].kind = Cruntime;
  112. capture[i].siz = 1; /* mark it as closed */
  113. capture[i].idx = fd + i; /* stack index of capture value */
  114. capture[i].s = s;
  115. }
  116. capture[n].kind = Cclose; /* close group */
  117. capture[n].siz = 1;
  118. capture[n].s = s;
  119. }
  120. /*
  121. ** Remove dynamic captures from the Lua stack (called in case of failure)
  122. */
  123. static int removedyncap (lua_State *L, Capture *capture,
  124. int level, int last) {
  125. int id = finddyncap(capture + level, capture + last); /* index of 1st cap. */
  126. int top = lua_gettop(L);
  127. if (id == 0) return 0; /* no dynamic captures? */
  128. lua_settop(L, id - 1); /* remove captures */
  129. return top - id + 1; /* number of values removed */
  130. }
  131. /*
  132. ** Opcode interpreter
  133. */
  134. const char *match (lua_State *L, const char *o, const char *s, const char *e,
  135. Instruction *op, Capture *capture, int ptop) {
  136. Stack stackbase[INITBACK];
  137. Stack *stacklimit = stackbase + INITBACK;
  138. Stack *stack = stackbase; /* point to first empty slot in stack */
  139. int capsize = INITCAPSIZE;
  140. int captop = 0; /* point to first empty slot in captures */
  141. int ndyncap = 0; /* number of dynamic captures (in Lua stack) */
  142. const Instruction *p = op; /* current instruction */
  143. stack->p = &giveup; stack->s = s; stack->caplevel = 0; stack++;
  144. lua_pushlightuserdata(L, stackbase);
  145. for (;;) {
  146. #if defined(DEBUG)
  147. printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ",
  148. s, (int)(stack - getstackbase(L, ptop)), ndyncap, captop);
  149. printinst(op, p);
  150. #endif
  151. assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop);
  152. switch ((Opcode)p->i.code) {
  153. case IEnd: {
  154. assert(stack == getstackbase(L, ptop) + 1);
  155. capture[captop].kind = Cclose;
  156. capture[captop].s = NULL;
  157. return s;
  158. }
  159. case IGiveup: {
  160. assert(stack == getstackbase(L, ptop));
  161. return NULL;
  162. }
  163. case IRet: {
  164. assert(stack > getstackbase(L, ptop) && (stack - 1)->s == NULL);
  165. p = (--stack)->p;
  166. continue;
  167. }
  168. case IAny: {
  169. if (s < e) { p++; s++; }
  170. else goto fail;
  171. continue;
  172. }
  173. case ITestAny: {
  174. if (s < e) p += 2;
  175. else p += getoffset(p);
  176. continue;
  177. }
  178. case IChar: {
  179. if (s < e && (byte)*s == p->i.aux) { p++; s++; }
  180. else goto fail;
  181. continue;
  182. }
  183. case ITestChar: {
  184. if (s < e && (byte)*s == p->i.aux) p += 2;
  185. else p += getoffset(p);
  186. continue;
  187. }
  188. case ISet: {
  189. if (s < e) {
  190. int c = (byte) *s;
  191. if (testchar((p + 1)->buff, c)) {
  192. p += CHARSETINSTSIZE;
  193. s++;
  194. }
  195. else goto fail;
  196. }
  197. else {
  198. goto fail;
  199. }
  200. continue;
  201. }
  202. case ITestSet: {
  203. if (s < e) {
  204. int c = (byte) *s;
  205. if (testchar((p + 2)->buff, c))
  206. p += 1 + CHARSETINSTSIZE;
  207. else p += getoffset(p);
  208. }
  209. else {
  210. p += getoffset(p);
  211. }
  212. continue;
  213. }
  214. case IBehind: {
  215. int n = p->i.aux;
  216. if (n > s - o) goto fail;
  217. s -= n; p++;
  218. continue;
  219. }
  220. case ISpan: {
  221. for (; s < e; s++) {
  222. int c = (byte)*s;
  223. if (!testchar((p+1)->buff, c)) break;
  224. }
  225. p += CHARSETINSTSIZE;
  226. continue;
  227. }
  228. case IJmp: {
  229. p += getoffset(p);
  230. continue;
  231. }
  232. case IChoice: {
  233. if (stack == stacklimit)
  234. stack = doublestack(L, &stacklimit, ptop);
  235. stack->p = p + getoffset(p);
  236. stack->s = s;
  237. stack->caplevel = captop;
  238. stack++;
  239. p += 2;
  240. continue;
  241. }
  242. case ICall: {
  243. if (stack == stacklimit)
  244. stack = doublestack(L, &stacklimit, ptop);
  245. stack->s = NULL;
  246. stack->p = p + 2; /* save return address */
  247. stack++;
  248. p += getoffset(p);
  249. continue;
  250. }
  251. case ICommit: {
  252. assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL);
  253. stack--;
  254. p += getoffset(p);
  255. continue;
  256. }
  257. case IPartialCommit: {
  258. assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL);
  259. (stack - 1)->s = s;
  260. (stack - 1)->caplevel = captop;
  261. p += getoffset(p);
  262. continue;
  263. }
  264. case IBackCommit: {
  265. assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL);
  266. s = (--stack)->s;
  267. captop = stack->caplevel;
  268. p += getoffset(p);
  269. continue;
  270. }
  271. case IFailTwice:
  272. assert(stack > getstackbase(L, ptop));
  273. stack--;
  274. /* go through */
  275. case IFail:
  276. fail: { /* pattern failed: try to backtrack */
  277. do { /* remove pending calls */
  278. assert(stack > getstackbase(L, ptop));
  279. s = (--stack)->s;
  280. } while (s == NULL);
  281. if (ndyncap > 0) /* is there matchtime captures? */
  282. ndyncap -= removedyncap(L, capture, stack->caplevel, captop);
  283. captop = stack->caplevel;
  284. p = stack->p;
  285. continue;
  286. }
  287. case ICloseRunTime: {
  288. CapState cs;
  289. int rem, res, n;
  290. int fr = lua_gettop(L) + 1; /* stack index of first result */
  291. cs.reclevel = 0; cs.L = L;
  292. cs.s = o; cs.ocap = capture; cs.ptop = ptop;
  293. n = runtimecap(&cs, capture + captop, s, &rem); /* call function */
  294. captop -= n; /* remove nested captures */
  295. ndyncap -= rem; /* update number of dynamic captures */
  296. fr -= rem; /* 'rem' items were popped from Lua stack */
  297. res = resdyncaptures(L, fr, s - o, e - o); /* get result */
  298. if (res == -1) /* fail? */
  299. goto fail;
  300. s = o + res; /* else update current position */
  301. n = lua_gettop(L) - fr + 1; /* number of new captures */
  302. ndyncap += n; /* update number of dynamic captures */
  303. if (n == 0) /* no new captures? */
  304. captop--; /* remove open group */
  305. else { /* new captures; keep original open group */
  306. if (fr + n >= SHRT_MAX)
  307. luaL_error(L, "too many results in match-time capture");
  308. /* add new captures + close group to 'capture' list */
  309. capture = growcap(L, capture, &capsize, captop, n + 1, ptop);
  310. adddyncaptures(s, capture + captop, n, fr);
  311. captop += n + 1; /* new captures + close group */
  312. }
  313. p++;
  314. continue;
  315. }
  316. case ICloseCapture: {
  317. const char *s1 = s;
  318. assert(captop > 0);
  319. /* if possible, turn capture into a full capture */
  320. if (capture[captop - 1].siz == 0 &&
  321. s1 - capture[captop - 1].s < UCHAR_MAX) {
  322. capture[captop - 1].siz = s1 - capture[captop - 1].s + 1;
  323. p++;
  324. continue;
  325. }
  326. else {
  327. capture[captop].siz = 1; /* mark entry as closed */
  328. capture[captop].s = s;
  329. goto pushcapture;
  330. }
  331. }
  332. case IOpenCapture:
  333. capture[captop].siz = 0; /* mark entry as open */
  334. capture[captop].s = s;
  335. goto pushcapture;
  336. case IFullCapture:
  337. capture[captop].siz = getoff(p) + 1; /* save capture size */
  338. capture[captop].s = s - getoff(p);
  339. /* goto pushcapture; */
  340. pushcapture: {
  341. capture[captop].idx = p->i.key;
  342. capture[captop].kind = getkind(p);
  343. captop++;
  344. capture = growcap(L, capture, &capsize, captop, 0, ptop);
  345. p++;
  346. continue;
  347. }
  348. default: assert(0); return NULL;
  349. }
  350. }
  351. }
  352. /* }====================================================== */