robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

master
7.0 KiB298 linesraw
1
2#include <ctype.h>
3#include <limits.h>
4#include <stdio.h>
5
6
7#include "lptypes.h"
8#include "lpprint.h"
9#include "lpcode.h"
10
11
12#if defined(LPEG_DEBUG)
13
14/*
15** {======================================================
16** Printing patterns (for debugging)
17** =======================================================
18*/
19
20
21void printcharset (const byte *st) {
22  int i;
23  printf("[");
24  for (i = 0; i <= UCHAR_MAX; i++) {
25    int first = i;
26    while (i <= UCHAR_MAX && testchar(st, i)) i++;
27    if (i - 1 == first)  /* unary range? */
28      printf("(%02x)", first);
29    else if (i - 1 > first)  /* non-empty range? */
30      printf("(%02x-%02x)", first, i - 1);
31  }
32  printf("]");
33}
34
35
36static void printIcharset (const Instruction *inst, const byte *buff) {
37  byte cs[CHARSETSIZE];
38  int i;
39  printf("(%02x-%d) ", inst->i.aux2.set.offset, inst->i.aux2.set.size);
40  clearset(cs);
41  for (i = 0; i < CHARSETSIZE * 8; i++) {
42    if (charinset(inst, buff, i))
43      setchar(cs, i);
44  }
45  printcharset(cs);
46}
47
48
49static void printTcharset (TTree *tree) {
50  byte cs[CHARSETSIZE];
51  int i;
52  printf("(%02x-%d) ", tree->u.set.offset, tree->u.set.size);
53  fillset(cs, tree->u.set.deflt);
54  for (i = 0; i < tree->u.set.size; i++)
55    cs[tree->u.set.offset + i] = treebuffer(tree)[i];
56  printcharset(cs);
57}
58
59
60static const char *capkind (int kind) {
61  const char *const modes[] = {
62    "close", "position", "constant", "backref",
63    "argument", "simple", "table", "function", "accumulator",
64    "query", "string", "num", "substitution", "fold",
65    "runtime", "group"};
66  return modes[kind];
67}
68
69
70static void printjmp (const Instruction *op, const Instruction *p) {
71  printf("-> %d", (int)(p + (p + 1)->offset - op));
72}
73
74
75void printinst (const Instruction *op, const Instruction *p) {
76  const char *const names[] = {
77    "any", "char", "set",
78    "testany", "testchar", "testset",
79    "span", "utf-range", "behind",
80    "ret", "end",
81    "choice", "jmp", "call", "open_call",
82    "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup",
83     "fullcapture", "opencapture", "closecapture", "closeruntime",
84     "--"
85  };
86  printf("%02ld: %s ", (long)(p - op), names[p->i.code]);
87  switch ((Opcode)p->i.code) {
88    case IChar: {
89      printf("'%c' (%02x)", p->i.aux1, p->i.aux1);
90      break;
91    }
92    case ITestChar: {
93      printf("'%c' (%02x)", p->i.aux1, p->i.aux1); printjmp(op, p);
94      break;
95    }
96    case IUTFR: {
97      printf("%d - %d", p[1].offset, utf_to(p));
98      break;
99    }
100    case IFullCapture: {
101      printf("%s (size = %d)  (idx = %d)",
102             capkind(getkind(p)), getoff(p), p->i.aux2.key);
103      break;
104    }
105    case IOpenCapture: {
106      printf("%s (idx = %d)", capkind(getkind(p)), p->i.aux2.key);
107      break;
108    }
109    case ISet: {
110      printIcharset(p, (p+1)->buff);
111      break;
112    }
113    case ITestSet: {
114      printIcharset(p, (p+2)->buff); printjmp(op, p);
115      break;
116    }
117    case ISpan: {
118      printIcharset(p, (p+1)->buff);
119      break;
120    }
121    case IOpenCall: {
122      printf("-> %d", (p + 1)->offset);
123      break;
124    }
125    case IBehind: {
126      printf("%d", p->i.aux1);
127      break;
128    }
129    case IJmp: case ICall: case ICommit: case IChoice:
130    case IPartialCommit: case IBackCommit: case ITestAny: {
131      printjmp(op, p);
132      break;
133    }
134    default: break;
135  }
136  printf("\n");
137}
138
139
140void printpatt (Instruction *p) {
141  Instruction *op = p;
142  uint n = op[-1].codesize - 1;
143  while (p < op + n) {
144    printinst(op, p);
145    p += sizei(p);
146  }
147}
148
149
150static void printcap (Capture *cap, int ident) {
151  while (ident--) printf(" ");
152  printf("%s (idx: %d - size: %d) -> %lu  (%p)\n",
153         capkind(cap->kind), cap->idx, cap->siz, (long)cap->index, (void*)cap);
154}
155
156
157/*
158** Print a capture and its nested captures
159*/
160static Capture *printcap2close (Capture *cap, int ident) {
161  Capture *head = cap++;
162  printcap(head, ident);  /* print head capture */
163  while (capinside(head, cap))
164    cap = printcap2close(cap, ident + 2);  /* print nested captures */
165  if (isopencap(head)) {
166    assert(isclosecap(cap));
167    printcap(cap++, ident);  /* print and skip close capture */
168  }
169  return cap;
170}
171
172
173void printcaplist (Capture *cap) {
174  {  /* for debugging, print first a raw list of captures */
175    Capture *c = cap;
176    while (c->index != MAXINDT) { printcap(c, 0); c++; }
177  }
178  printf(">======\n");
179  while (!isclosecap(cap))
180    cap = printcap2close(cap, 0);
181  printf("=======\n");
182}
183
184/* }====================================================== */
185
186
187/*
188** {======================================================
189** Printing trees (for debugging)
190** =======================================================
191*/
192
193static const char *tagnames[] = {
194  "char", "set", "any",
195  "true", "false", "utf8.range",
196  "rep",
197  "seq", "choice",
198  "not", "and",
199  "call", "opencall", "rule", "xinfo", "grammar",
200  "behind",
201  "capture", "run-time"
202};
203
204
205void printtree (TTree *tree, int ident) {
206  int i;
207  int sibs = numsiblings[tree->tag];
208  for (i = 0; i < ident; i++) printf(" ");
209  printf("%s", tagnames[tree->tag]);
210  switch (tree->tag) {
211    case TChar: {
212      int c = tree->u.n;
213      if (isprint(c))
214        printf(" '%c'\n", c);
215      else
216        printf(" (%02X)\n", c);
217      break;
218    }
219    case TSet: {
220      printTcharset(tree);
221      printf("\n");
222      break;
223    }
224    case TUTFR: {
225      assert(sib1(tree)->tag == TXInfo);
226      printf(" %d (%02x %d) - %d (%02x %d) \n",
227        tree->u.n, tree->key, tree->cap,
228        sib1(tree)->u.n, sib1(tree)->key, sib1(tree)->cap);
229      break;
230    }
231    case TOpenCall: case TCall: {
232      assert(sib1(sib2(tree))->tag == TXInfo);
233      printf(" key: %d  (rule: %d)\n", tree->key, sib1(sib2(tree))->u.n);
234      break;
235    }
236    case TBehind: {
237      printf(" %d\n", tree->u.n);
238      break;
239    }
240    case TCapture: {
241      printf(" kind: '%s'  key: %d\n", capkind(tree->cap), tree->key);
242      break;
243    }
244    case TRule: {
245      printf(" key: %d\n", tree->key);
246      sibs = 1;  /* do not print 'sib2' (next rule) as a sibling */
247      break;
248    }
249    case TXInfo: {
250      printf(" n: %d\n", tree->u.n);
251      break;
252    }
253    case TGrammar: {
254      TTree *rule = sib1(tree);
255      printf(" %d\n", tree->u.n);  /* number of rules */
256      for (i = 0; i < tree->u.n; i++) {
257        printtree(rule, ident + 2);
258        rule = sib2(rule);
259      }
260      assert(rule->tag == TTrue);  /* sentinel */
261      sibs = 0;  /* siblings already handled */
262      break;
263    }
264    default:
265      printf("\n");
266      break;
267  }
268  if (sibs >= 1) {
269    printtree(sib1(tree), ident + 2);
270    if (sibs >= 2)
271      printtree(sib2(tree), ident + 2);
272  }
273}
274
275
276void printktable (lua_State *L, int idx) {
277  int n, i;
278  lua_getuservalue(L, idx);
279  if (lua_isnil(L, -1))  /* no ktable? */
280    return;
281  n = lua_rawlen(L, -1);
282  printf("[");
283  for (i = 1; i <= n; i++) {
284    printf("%d = ", i);
285    lua_rawgeti(L, -1, i);
286    if (lua_isstring(L, -1))
287      printf("%s  ", lua_tostring(L, -1));
288    else
289      printf("%s  ", lua_typename(L, lua_type(L, -1)));
290    lua_pop(L, 1);
291  }
292  printf("]\n");
293  /* leave ktable at the stack */
294}
295
296/* }====================================================== */
297
298#endif