Debian Codegraph @

texk/web2c/luatexdir/luapeg/lpeg.h

raw
1 /*
2 ** $Id: lptypes.h,v 1.16 2017/01/13 13:33:17 roberto Exp $
3 ** LPeg - PEG pattern matching for Lua
4 ** Copyright 2007-2017, Lua.org & PUC-Rio (see 'lpeg.html' for license)
5 ** written by Roberto Ierusalimschy
6 */
7
8 /*
9 "Amalgamated" version for Lua(jit)TeX written by Scarso Luigi.
10 */
11
12
13 #if !defined(lptypes_h)
14 #define lptypes_h
15
16 #define LPEG_DEBUG
17 #if !defined(LPEG_DEBUG)
18 #define NDEBUG
19 #endif
20
21 #include <assert.h>
22 #include <limits.h>
23 /* added */
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <string.h>
27
28
29 #include "lua.h"
30 #include "lauxlib.h"
31
32 #define VERSION "1.0.1"
33
34
35 #define PATTERN_T "lpeg-pattern"
36 #define MAXSTACKIDX "lpeg-maxstack"
37
38
39 /*
40 ** compatibility with Lua 5.1
41 */
42 #if (LUA_VERSION_NUM == 501)
43
44 #define lp_equal lua_equal
45
46 #define lua_getuservalue lua_getfenv
47 #define lua_setuservalue lua_setfenv
48
49 #define lua_rawlen lua_objlen
50
51 #define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f)
52 #if !defined(LuajitTeX)
53 #define luaL_newlib(L,f) luaL_register(L,"lpeg",f)
54 #endif
55 #endif
56
57
58 #if !defined(lp_equal)
59 #define lp_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
60 #endif
61
62
63 /* default maximum size for call/backtrack stack */
64 #if !defined(MAXBACK)
65 #define MAXBACK 400
66 #endif
67
68
69 /* maximum number of rules in a grammar */
70 #if !defined(MAXRULES)
71 #define MAXRULES 1000
72 #endif
73
74
75
76 /* initial size for capture's list */
77 #define INITCAPSIZE 32
78
79
80 /* index, on Lua stack, for subject */
81 #define SUBJIDX 2
82
83 /* number of fixed arguments to 'match' (before capture arguments) */
84 #define FIXEDARGS 3
85
86 /* index, on Lua stack, for capture list */
87 #define caplistidx(ptop) ((ptop) + 2)
88
89 /* index, on Lua stack, for pattern's ktable */
90 #define ktableidx(ptop) ((ptop) + 3)
91
92 /* index, on Lua stack, for backtracking stack */
93 #define stackidx(ptop) ((ptop) + 4)
94
95
96
97 typedef unsigned char byte;
98
99
100 #define BITSPERCHAR 8
101
102 #define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1)
103
104
105
106 typedef struct Charset {
107 byte cs[CHARSETSIZE];
108 } Charset;
109
110
111
112 #define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} }
113
114 /* access to charset */
115 #define treebuffer(t) ((byte *)((t) + 1))
116
117 /* number of slots needed for 'n' bytes */
118 #define bytes2slots(n) (((n) - 1) / sizeof(TTree) + 1)
119
120 /* set 'b' bit in charset 'cs' */
121 #define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7)))
122
123
124 /*
125 ** in capture instructions, 'kind' of capture and its offset are
126 ** packed in field 'aux', 4 bits for each
127 */
128 #define getkind(op) ((op)->i.aux & 0xF)
129 #define getoff(op) (((op)->i.aux >> 4) & 0xF)
130 #define joinkindoff(k,o) ((k) | ((o) << 4))
131
132 #define MAXOFF 0xF
133 #define MAXAUX 0xFF
134
135
136 /* maximum number of bytes to look behind */
137 #define MAXBEHIND MAXAUX
138
139
140 /* maximum size (in elements) for a pattern */
141 #define MAXPATTSIZE (SHRT_MAX - 10)
142
143
144 /* size (in elements) for an instruction plus extra l bytes */
145 #define instsize(l) (((l) + sizeof(Instruction) - 1)/sizeof(Instruction) + 1)
146
147
148 /* size (in elements) for a ISet instruction */
149 #define CHARSETINSTSIZE instsize(CHARSETSIZE)
150
151 /* size (in elements) for a IFunc instruction */
152 #define funcinstsize(p) ((p)->i.aux + 2)
153
154
155
156 #define testchar(st,c) (((int)(st)[((c) >> 3)] & (1 << ((c) & 7))))
157
158
159 #endif
160
161 /*
162 ** $Id: lpcap.h,v 1.3 2016/09/13 17:45:58 roberto Exp $
163 */
164
165 #if !defined(lpcap_h)
166 #define lpcap_h
167
168
169 /* #include "lptypes.h" */
170
171
172 /* kinds of captures */
173 typedef enum CapKind {
174 Cclose, /* not used in trees */
175 Cposition,
176 Cconst, /* ktable[key] is Lua constant */
177 Cbackref, /* ktable[key] is "name" of group to get capture */
178 Carg, /* 'key' is arg's number */
179 Csimple, /* next node is pattern */
180 Ctable, /* next node is pattern */
181 Cfunction, /* ktable[key] is function; next node is pattern */
182 Cquery, /* ktable[key] is table; next node is pattern */
183 Cstring, /* ktable[key] is string; next node is pattern */
184 Cnum, /* numbered capture; 'key' is number of value to return */
185 Csubst, /* substitution capture; next node is pattern */
186 Cfold, /* ktable[key] is function; next node is pattern */
187 Cruntime, /* not used in trees (is uses another type for tree) */
188 Cgroup /* ktable[key] is group's "name" */
189 } CapKind;
190
191
192 typedef struct Capture {
193 const char *s; /* subject position */
194 unsigned short idx; /* extra info (group name, arg index, etc.) */
195 byte kind; /* kind of capture */
196 byte siz; /* size of full capture + 1 (0 = not a full capture) */
197 } Capture;
198
199
200 typedef struct CapState {
201 Capture *cap; /* current capture */
202 Capture *ocap; /* (original) capture list */
203 lua_State *L;
204 int ptop; /* index of last argument to 'match' */
205 const char *s; /* original string */
206 int valuecached; /* value stored in cache slot */
207 } CapState;
208
209
210 int runtimecap (CapState *cs, Capture *close, const char *s, int *rem);
211 int getcaptures (lua_State *L, const char *s, const char *r, int ptop);
212 int finddyncap (Capture *cap, Capture *last);
213
214 #endif
215
216
217 /*
218 ** $Id: lptree.h,v 1.3 2016/09/13 18:07:51 roberto Exp $
219 */
220
221 #if !defined(lptree_h)
222 #define lptree_h
223
224
225 /* #include "lptypes.h" */
226
227
228 /*
229 ** types of trees
230 */
231 typedef enum TTag {
232 TChar = 0, /* 'n' = char */
233 TSet, /* the set is stored in next CHARSETSIZE bytes */
234 TAny,
235 TTrue,
236 TFalse,
237 TRep, /* 'sib1'* */
238 TSeq, /* 'sib1' 'sib2' */
239 TChoice, /* 'sib1' / 'sib2' */
240 TNot, /* !'sib1' */
241 TAnd, /* &'sib1' */
242 TCall, /* ktable[key] is rule's key; 'sib2' is rule being called */
243 TOpenCall, /* ktable[key] is rule's key */
244 TRule, /* ktable[key] is rule's key (but key == 0 for unused rules);
245 'sib1' is rule's pattern;
246 'sib2' is next rule; 'cap' is rule's sequential number */
247 TGrammar, /* 'sib1' is initial (and first) rule */
248 TBehind, /* 'sib1' is pattern, 'n' is how much to go back */
249 TCapture, /* captures: 'cap' is kind of capture (enum 'CapKind');
250 ktable[key] is Lua value associated with capture;
251 'sib1' is capture body */
252 TRunTime /* run-time capture: 'key' is Lua function;
253 'sib1' is capture body */
254 } TTag;
255
256
257 /*
258 ** Tree trees
259 ** The first child of a tree (if there is one) is immediately after
260 ** the tree. A reference to a second child (ps) is its position
261 ** relative to the position of the tree itself.
262 */
263 typedef struct TTree {
264 byte tag;
265 byte cap; /* kind of capture (if it is a capture) */
266 unsigned short key; /* key in ktable for Lua data (0 if no key) */
267 union {
268 int ps; /* occasional second child */
269 int n; /* occasional counter */
270 } u;
271 } TTree;
272
273
274 /*
275 ** A complete pattern has its tree plus, if already compiled,
276 ** its corresponding code
277 */
278 typedef struct Pattern {
279 union Instruction *code;
280 int codesize;
281 TTree tree[1];
282 } Pattern;
283
284
285 /* number of children for each tree */
286 extern const byte numsiblings[];
287
288 /* access to children */
289 #define sib1(t) ((t) + 1)
290 #define sib2(t) ((t) + (t)->u.ps)
291
292
293
294
295
296
297 #endif
298
299 /*
300 ** $Id: lpvm.h,v 1.3 2014/02/21 13:06:41 roberto Exp $
301 */
302
303 #if !defined(lpvm_h)
304 #define lpvm_h
305
306 /* #include "lpcap.h" */
307
308
309 /* Virtual Machine's instructions */
310 typedef enum Opcode {
311 IAny, /* if no char, fail */
312 IChar, /* if char != aux, fail */
313 ISet, /* if char not in buff, fail */
314 ITestAny, /* in no char, jump to 'offset' */
315 ITestChar, /* if char != aux, jump to 'offset' */
316 ITestSet, /* if char not in buff, jump to 'offset' */
317 ISpan, /* read a span of chars in buff */
318 IBehind, /* walk back 'aux' characters (fail if not possible) */
319 IRet, /* return from a rule */
320 IEnd, /* end of pattern */
321 IChoice, /* stack a choice; next fail will jump to 'offset' */
322 IJmp, /* jump to 'offset' */
323 ICall, /* call rule at 'offset' */
324 IOpenCall, /* call rule number 'key' (must be closed to a ICall) */
325 ICommit, /* pop choice and jump to 'offset' */
326 IPartialCommit, /* update top choice to current position and jump */
327 IBackCommit, /* "fails" but jump to its own 'offset' */
328 IFailTwice, /* pop one choice and then fail */
329 IFail, /* go back to saved state on choice and jump to saved offset */
330 IGiveup, /* internal use */
331 IFullCapture, /* complete capture of last 'off' chars */
332 IOpenCapture, /* start a capture */
333 ICloseCapture,
334 ICloseRunTime
335 } Opcode;
336
337
338
339 typedef union Instruction {
340 struct Inst {
341 byte code;
342 byte aux;
343 short key;
344 } i;
345 int offset;
346 byte buff[1];
347 } Instruction;
348
349
350 void printpatt (Instruction *p, int n);
351 const char *match (lua_State *L, const char *o, const char *s, const char *e,
352 Instruction *op, Capture *capture, int ptop);
353
354
355 #endif
356
357 /*
358 ** $Id: lpcode.h,v 1.8 2016/09/15 17:46:13 roberto Exp $
359 */
360
361 #if !defined(lpcode_h)
362 #define lpcode_h
363
364 /*#include "lua.h"*/
365
366 /* #include "lptypes.h" */
367 /* #include "lptree.h" */
368 /* #include "lpvm.h" */
369
370 int tocharset (TTree *tree, Charset *cs);
371 int checkaux (TTree *tree, int pred);
372 int fixedlen (TTree *tree);
373 int hascaptures (TTree *tree);
374 int lp_gc (lua_State *L);
375 Instruction *compile (lua_State *L, Pattern *p);
376 void realloccode (lua_State *L, Pattern *p, int nsize);
377 int sizei (const Instruction *i);
378
379
380 #define PEnullable 0
381 #define PEnofail 1
382
383 /*
384 ** nofail(t) implies that 't' cannot fail with any input
385 */
386 #define nofail(t) checkaux(t, PEnofail)
387
388 /*
389 ** (not nullable(t)) implies 't' cannot match without consuming
390 ** something
391 */
392 #define nullable(t) checkaux(t, PEnullable)
393
394
395
396 #endif
397 /*
398 ** $Id: lpprint.h,v 1.2 2015/06/12 18:18:08 roberto Exp $
399 */
400
401
402 #if !defined(lpprint_h)
403 #define lpprint_h
404
405
406 /* #include "lptree.h" */
407 /* #include "lpvm.h" */
408
409
410 #if defined(LPEG_DEBUG)
411
412 void printpatt (Instruction *p, int n);
413 void printtree (TTree *tree, int ident);
414 void printktable (lua_State *L, int idx);
415 void printcharset (const byte *st);
416 void printcaplist (Capture *cap, Capture *limit);
417 void printinst (const Instruction *op, const Instruction *p);
418
419 #else
420
421 #define printktable(L,idx) \
422 luaL_error(L, "function only implemented in debug mode")
423 #define printtree(tree,i) \
424 luaL_error(L, "function only implemented in debug mode")
425 #define printpatt(p,n) \
426 luaL_error(L, "function only implemented in debug mode")
427
428 #endif
429
430
431 #endif
432