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.

lpprint.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. ** $Id: lpprint.c,v 1.9 2015/06/15 16:09:57 roberto Exp $
  3. ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license)
  4. */
  5. #include <ctype.h>
  6. #include <limits.h>
  7. #include <stdio.h>
  8. #include "lptypes.h"
  9. #include "lpprint.h"
  10. #include "lpcode.h"
  11. #if defined(LPEG_DEBUG)
  12. /*
  13. ** {======================================================
  14. ** Printing patterns (for debugging)
  15. ** =======================================================
  16. */
  17. void printcharset (const byte *st) {
  18. int i;
  19. printf("[");
  20. for (i = 0; i <= UCHAR_MAX; i++) {
  21. int first = i;
  22. while (testchar(st, i) && i <= UCHAR_MAX) i++;
  23. if (i - 1 == first) /* unary range? */
  24. printf("(%02x)", first);
  25. else if (i - 1 > first) /* non-empty range? */
  26. printf("(%02x-%02x)", first, i - 1);
  27. }
  28. printf("]");
  29. }
  30. static void printcapkind (int kind) {
  31. const char *const modes[] = {
  32. "close", "position", "constant", "backref",
  33. "argument", "simple", "table", "function",
  34. "query", "string", "num", "substitution", "fold",
  35. "runtime", "group"};
  36. printf("%s", modes[kind]);
  37. }
  38. static void printjmp (const Instruction *op, const Instruction *p) {
  39. printf("-> %d", (int)(p + (p + 1)->offset - op));
  40. }
  41. void printinst (const Instruction *op, const Instruction *p) {
  42. const char *const names[] = {
  43. "any", "char", "set",
  44. "testany", "testchar", "testset",
  45. "span", "behind",
  46. "ret", "end",
  47. "choice", "jmp", "call", "open_call",
  48. "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup",
  49. "fullcapture", "opencapture", "closecapture", "closeruntime"
  50. };
  51. printf("%02ld: %s ", (long)(p - op), names[p->i.code]);
  52. switch ((Opcode)p->i.code) {
  53. case IChar: {
  54. printf("'%c'", p->i.aux);
  55. break;
  56. }
  57. case ITestChar: {
  58. printf("'%c'", p->i.aux); printjmp(op, p);
  59. break;
  60. }
  61. case IFullCapture: {
  62. printcapkind(getkind(p));
  63. printf(" (size = %d) (idx = %d)", getoff(p), p->i.key);
  64. break;
  65. }
  66. case IOpenCapture: {
  67. printcapkind(getkind(p));
  68. printf(" (idx = %d)", p->i.key);
  69. break;
  70. }
  71. case ISet: {
  72. printcharset((p+1)->buff);
  73. break;
  74. }
  75. case ITestSet: {
  76. printcharset((p+2)->buff); printjmp(op, p);
  77. break;
  78. }
  79. case ISpan: {
  80. printcharset((p+1)->buff);
  81. break;
  82. }
  83. case IOpenCall: {
  84. printf("-> %d", (p + 1)->offset);
  85. break;
  86. }
  87. case IBehind: {
  88. printf("%d", p->i.aux);
  89. break;
  90. }
  91. case IJmp: case ICall: case ICommit: case IChoice:
  92. case IPartialCommit: case IBackCommit: case ITestAny: {
  93. printjmp(op, p);
  94. break;
  95. }
  96. default: break;
  97. }
  98. printf("\n");
  99. }
  100. void printpatt (Instruction *p, int n) {
  101. Instruction *op = p;
  102. while (p < op + n) {
  103. printinst(op, p);
  104. p += sizei(p);
  105. }
  106. }
  107. #if defined(LPEG_DEBUG)
  108. static void printcap (Capture *cap) {
  109. printcapkind(cap->kind);
  110. printf(" (idx: %d - size: %d) -> %p\n", cap->idx, cap->siz, cap->s);
  111. }
  112. void printcaplist (Capture *cap, Capture *limit) {
  113. printf(">======\n");
  114. for (; cap->s && (limit == NULL || cap < limit); cap++)
  115. printcap(cap);
  116. printf("=======\n");
  117. }
  118. #endif
  119. /* }====================================================== */
  120. /*
  121. ** {======================================================
  122. ** Printing trees (for debugging)
  123. ** =======================================================
  124. */
  125. static const char *tagnames[] = {
  126. "char", "set", "any",
  127. "true", "false",
  128. "rep",
  129. "seq", "choice",
  130. "not", "and",
  131. "call", "opencall", "rule", "grammar",
  132. "behind",
  133. "capture", "run-time"
  134. };
  135. void printtree (TTree *tree, int ident) {
  136. int i;
  137. for (i = 0; i < ident; i++) printf(" ");
  138. printf("%s", tagnames[tree->tag]);
  139. switch (tree->tag) {
  140. case TChar: {
  141. int c = tree->u.n;
  142. if (isprint(c))
  143. printf(" '%c'\n", c);
  144. else
  145. printf(" (%02X)\n", c);
  146. break;
  147. }
  148. case TSet: {
  149. printcharset(treebuffer(tree));
  150. printf("\n");
  151. break;
  152. }
  153. case TOpenCall: case TCall: {
  154. printf(" key: %d\n", tree->key);
  155. break;
  156. }
  157. case TBehind: {
  158. printf(" %d\n", tree->u.n);
  159. printtree(sib1(tree), ident + 2);
  160. break;
  161. }
  162. case TCapture: {
  163. printf(" cap: %d key: %d n: %d\n", tree->cap, tree->key, tree->u.n);
  164. printtree(sib1(tree), ident + 2);
  165. break;
  166. }
  167. case TRule: {
  168. printf(" n: %d key: %d\n", tree->cap, tree->key);
  169. printtree(sib1(tree), ident + 2);
  170. break; /* do not print next rule as a sibling */
  171. }
  172. case TGrammar: {
  173. TTree *rule = sib1(tree);
  174. printf(" %d\n", tree->u.n); /* number of rules */
  175. for (i = 0; i < tree->u.n; i++) {
  176. printtree(rule, ident + 2);
  177. rule = sib2(rule);
  178. }
  179. assert(rule->tag == TTrue); /* sentinel */
  180. break;
  181. }
  182. default: {
  183. int sibs = numsiblings[tree->tag];
  184. printf("\n");
  185. if (sibs >= 1) {
  186. printtree(sib1(tree), ident + 2);
  187. if (sibs >= 2)
  188. printtree(sib2(tree), ident + 2);
  189. }
  190. break;
  191. }
  192. }
  193. }
  194. void printktable (lua_State *L, int idx) {
  195. int n, i;
  196. lua_getuservalue(L, idx);
  197. if (lua_isnil(L, -1)) /* no ktable? */
  198. return;
  199. n = lua_rawlen(L, -1);
  200. printf("[");
  201. for (i = 1; i <= n; i++) {
  202. printf("%d = ", i);
  203. lua_rawgeti(L, -1, i);
  204. if (lua_isstring(L, -1))
  205. printf("%s ", lua_tostring(L, -1));
  206. else
  207. printf("%s ", lua_typename(L, lua_type(L, -1)));
  208. lua_pop(L, 1);
  209. }
  210. printf("]\n");
  211. /* leave ktable at the stack */
  212. }
  213. /* }====================================================== */
  214. #endif