選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

lpvm.c 10KB

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