diff options
author | Sérgio Medeiros <sqmedeiros@gmail.com> | 2021-08-20 08:27:54 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-20 08:27:54 -0300 |
commit | ed183860289664af0f3727307653c9bf2bccdc80 (patch) | |
tree | c2d8e3bdd0d6d189572bd50d8d9a078097fcc3b5 /lpprint.c | |
parent | 635a51b5c88e958c27b26e049c639ea774946367 (diff) | |
parent | 7668d9076da6e2ae9e220eafb5c6ee1e933c088d (diff) | |
download | lpeglabel-ed183860289664af0f3727307653c9bf2bccdc80.tar.gz lpeglabel-ed183860289664af0f3727307653c9bf2bccdc80.tar.bz2 lpeglabel-ed183860289664af0f3727307653c9bf2bccdc80.zip |
Merge pull request #30 from logiceditor-com/avr/8279/eliminate-conflicts-with-original-lpeg
change prefix of source files from 'lp' to 'lpl'
Diffstat (limited to 'lpprint.c')
-rw-r--r-- | lpprint.c | 275 |
1 files changed, 0 insertions, 275 deletions
diff --git a/lpprint.c b/lpprint.c deleted file mode 100644 index af03edc..0000000 --- a/lpprint.c +++ /dev/null | |||
@@ -1,275 +0,0 @@ | |||
1 | /* | ||
2 | ** $Id: lpprint.c $ | ||
3 | ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) | ||
4 | */ | ||
5 | |||
6 | #include <ctype.h> | ||
7 | #include <limits.h> | ||
8 | #include <stdio.h> | ||
9 | |||
10 | |||
11 | #include "lptypes.h" | ||
12 | #include "lpprint.h" | ||
13 | #include "lpcode.h" | ||
14 | |||
15 | |||
16 | #if defined(LPEG_DEBUG) | ||
17 | |||
18 | /* | ||
19 | ** {====================================================== | ||
20 | ** Printing patterns (for debugging) | ||
21 | ** ======================================================= | ||
22 | */ | ||
23 | |||
24 | |||
25 | void printcharset (const byte *st) { | ||
26 | int i; | ||
27 | printf("["); | ||
28 | for (i = 0; i <= UCHAR_MAX; i++) { | ||
29 | int first = i; | ||
30 | while (testchar(st, i) && i <= UCHAR_MAX) i++; | ||
31 | if (i - 1 == first) /* unary range? */ | ||
32 | printf("(%02x)", first); | ||
33 | else if (i - 1 > first) /* non-empty range? */ | ||
34 | printf("(%02x-%02x)", first, i - 1); | ||
35 | } | ||
36 | printf("]"); | ||
37 | } | ||
38 | |||
39 | |||
40 | static const char *capkind (int kind) { | ||
41 | const char *const modes[] = { | ||
42 | "close", "position", "constant", "backref", | ||
43 | "argument", "simple", "table", "function", | ||
44 | "query", "string", "num", "substitution", "fold", | ||
45 | "runtime", "group"}; | ||
46 | return modes[kind]; | ||
47 | } | ||
48 | |||
49 | |||
50 | static void printjmp (const Instruction *op, const Instruction *p) { | ||
51 | printf("-> %d", (int)(p + (p + 1)->offset - op)); | ||
52 | } | ||
53 | |||
54 | |||
55 | void printinst (const Instruction *op, const Instruction *p) { | ||
56 | const char *const names[] = { | ||
57 | "any", "char", "set", | ||
58 | "testany", "testchar", "testset", | ||
59 | "span", "utf-range", "behind", | ||
60 | "ret", "end", | ||
61 | "choice", "pred_choice", "jmp", "call", "open_call", /* labeled failure */ | ||
62 | "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup", | ||
63 | "fullcapture", "opencapture", "closecapture", "closeruntime", | ||
64 | "throw", "throw_rec", /* labeled failure */ | ||
65 | "--" | ||
66 | }; | ||
67 | printf("%02ld: %s ", (long)(p - op), names[p->i.code]); | ||
68 | switch ((Opcode)p->i.code) { | ||
69 | case IChar: { | ||
70 | printf("'%c' (%02x)", p->i.aux, p->i.aux); | ||
71 | break; | ||
72 | } | ||
73 | case ITestChar: { | ||
74 | printf("'%c' (%02x)", p->i.aux, p->i.aux); printjmp(op, p); | ||
75 | break; | ||
76 | } | ||
77 | case IUTFR: { | ||
78 | printf("%d - %d", p[1].offset, utf_to(p)); | ||
79 | break; | ||
80 | } | ||
81 | case IFullCapture: { | ||
82 | printf("%s (size = %d) (idx = %d)", | ||
83 | capkind(getkind(p)), getoff(p), p->i.key); | ||
84 | break; | ||
85 | } | ||
86 | case IOpenCapture: { | ||
87 | printf("%s (idx = %d)", capkind(getkind(p)), p->i.key); | ||
88 | break; | ||
89 | } | ||
90 | case ISet: { | ||
91 | printcharset((p+1)->buff); | ||
92 | break; | ||
93 | } | ||
94 | case ITestSet: { | ||
95 | printcharset((p+2)->buff); printjmp(op, p); | ||
96 | break; | ||
97 | } | ||
98 | case ISpan: { | ||
99 | printcharset((p+1)->buff); | ||
100 | break; | ||
101 | } | ||
102 | case IOpenCall: { | ||
103 | printf("-> %d", (p + 1)->offset); | ||
104 | break; | ||
105 | } | ||
106 | case IBehind: { | ||
107 | printf("%d", p->i.aux); | ||
108 | break; | ||
109 | } | ||
110 | case IJmp: case ICall: case ICommit: case IChoice: | ||
111 | case IPartialCommit: case IBackCommit: case ITestAny: | ||
112 | case IPredChoice: { /* labeled failure */ | ||
113 | printjmp(op, p); | ||
114 | break; | ||
115 | } | ||
116 | case IThrow: { /* labeled failure */ | ||
117 | printf("(idx = %d)", (p + 1)->i.key); | ||
118 | break; | ||
119 | } | ||
120 | case IThrowRec: { /* labeled failure */ | ||
121 | printjmp(op, p); printf(" (idx = %d)", (p + 2)->i.key); | ||
122 | break; | ||
123 | } | ||
124 | default: break; | ||
125 | } | ||
126 | printf("\n"); | ||
127 | } | ||
128 | |||
129 | |||
130 | void printpatt (Instruction *p, int n) { | ||
131 | Instruction *op = p; | ||
132 | while (p < op + n) { | ||
133 | printinst(op, p); | ||
134 | p += sizei(p); | ||
135 | } | ||
136 | } | ||
137 | |||
138 | |||
139 | #if defined(LPEG_DEBUG) | ||
140 | static void printcap (Capture *cap) { | ||
141 | printf("%s (idx: %d - size: %d) -> %p\n", | ||
142 | capkind(cap->kind), cap->idx, cap->siz, cap->s); | ||
143 | } | ||
144 | |||
145 | |||
146 | void printcaplist (Capture *cap, Capture *limit) { | ||
147 | printf(">======\n"); | ||
148 | for (; cap->s && (limit == NULL || cap < limit); cap++) | ||
149 | printcap(cap); | ||
150 | printf("=======\n"); | ||
151 | } | ||
152 | #endif | ||
153 | |||
154 | /* }====================================================== */ | ||
155 | |||
156 | |||
157 | /* | ||
158 | ** {====================================================== | ||
159 | ** Printing trees (for debugging) | ||
160 | ** ======================================================= | ||
161 | */ | ||
162 | |||
163 | static const char *tagnames[] = { | ||
164 | "char", "set", "any", | ||
165 | "true", "false", "utf8.range", | ||
166 | "rep", | ||
167 | "seq", "choice", | ||
168 | "not", "and", | ||
169 | "call", "opencall", "rule", "xinfo", "grammar", | ||
170 | "behind", | ||
171 | "capture", "run-time", | ||
172 | "throw" /* labeled failure */ | ||
173 | }; | ||
174 | |||
175 | |||
176 | void printtree (TTree *tree, int ident) { | ||
177 | int i; | ||
178 | int sibs = numsiblings[tree->tag]; | ||
179 | for (i = 0; i < ident; i++) printf(" "); | ||
180 | printf("%s", tagnames[tree->tag]); | ||
181 | switch (tree->tag) { | ||
182 | case TChar: { | ||
183 | int c = tree->u.n; | ||
184 | if (isprint(c)) | ||
185 | printf(" '%c'\n", c); | ||
186 | else | ||
187 | printf(" (%02X)\n", c); | ||
188 | break; | ||
189 | } | ||
190 | case TSet: { | ||
191 | printcharset(treebuffer(tree)); | ||
192 | printf("\n"); | ||
193 | break; | ||
194 | } | ||
195 | case TUTFR: { | ||
196 | assert(sib1(tree)->tag == TXInfo); | ||
197 | printf(" %d (%02x %d) - %d (%02x %d) \n", | ||
198 | tree->u.n, tree->key, tree->cap, | ||
199 | sib1(tree)->u.n, sib1(tree)->key, sib1(tree)->cap); | ||
200 | break; | ||
201 | } | ||
202 | case TOpenCall: case TCall: { | ||
203 | assert(sib1(sib2(tree))->tag == TXInfo); | ||
204 | printf(" key: %d (rule: %d)\n", tree->key, sib1(sib2(tree))->u.n); | ||
205 | break; | ||
206 | } | ||
207 | case TBehind: { | ||
208 | printf(" %d\n", tree->u.n); | ||
209 | break; | ||
210 | } | ||
211 | case TCapture: { | ||
212 | printf(" kind: '%s' key: %d\n", capkind(tree->cap), tree->key); | ||
213 | break; | ||
214 | } | ||
215 | case TRule: { | ||
216 | printf(" key: %d\n", tree->key); | ||
217 | sibs = 1; /* do not print 'sib2' (next rule) as a sibling */ | ||
218 | break; | ||
219 | } | ||
220 | case TXInfo: { | ||
221 | printf(" n: %d\n", tree->u.n); | ||
222 | break; | ||
223 | } | ||
224 | case TGrammar: { | ||
225 | TTree *rule = sib1(tree); | ||
226 | printf(" %d\n", tree->u.n); /* number of rules */ | ||
227 | for (i = 0; i < tree->u.n; i++) { | ||
228 | printtree(rule, ident + 2); | ||
229 | rule = sib2(rule); | ||
230 | } | ||
231 | assert(rule->tag == TTrue); /* sentinel */ | ||
232 | sibs = 0; /* siblings already handled */ | ||
233 | break; | ||
234 | } | ||
235 | case TThrow: { /* labeled failure */ | ||
236 | if (tree->u.ps != 0) | ||
237 | assert(sib2(tree)->tag == TRule); | ||
238 | printf(" key: %d (rule: %d)\n", tree->key, sib2(tree)->cap); | ||
239 | break; | ||
240 | } | ||
241 | default: | ||
242 | printf("\n"); | ||
243 | break; | ||
244 | } | ||
245 | if (sibs >= 1) { | ||
246 | printtree(sib1(tree), ident + 2); | ||
247 | if (sibs >= 2) | ||
248 | printtree(sib2(tree), ident + 2); | ||
249 | } | ||
250 | } | ||
251 | |||
252 | |||
253 | void printktable (lua_State *L, int idx) { | ||
254 | int n, i; | ||
255 | lua_getuservalue(L, idx); | ||
256 | if (lua_isnil(L, -1)) /* no ktable? */ | ||
257 | return; | ||
258 | n = lua_rawlen(L, -1); | ||
259 | printf("["); | ||
260 | for (i = 1; i <= n; i++) { | ||
261 | printf("%d = ", i); | ||
262 | lua_rawgeti(L, -1, i); | ||
263 | if (lua_isstring(L, -1)) | ||
264 | printf("%s ", lua_tostring(L, -1)); | ||
265 | else | ||
266 | printf("%s ", lua_typename(L, lua_type(L, -1))); | ||
267 | lua_pop(L, 1); | ||
268 | } | ||
269 | printf("]\n"); | ||
270 | /* leave ktable at the stack */ | ||
271 | } | ||
272 | |||
273 | /* }====================================================== */ | ||
274 | |||
275 | #endif | ||