robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
2.4 KiB86 linesraw
1
2#if !defined(lpcap_h)
3#define lpcap_h
4
5
6#include "lptypes.h"
7
8
9/* kinds of captures */
10typedef enum CapKind {
11  Cclose,  /* not used in trees */
12  Cposition,
13  Cconst,  /* ktable[key] is Lua constant */
14  Cbackref,  /* ktable[key] is "name" of group to get capture */
15  Carg,  /* 'key' is arg's number */
16  Csimple,  /* next node is pattern */
17  Ctable,  /* next node is pattern */
18  Cfunction,  /* ktable[key] is function; next node is pattern */
19  Cacc,  /* ktable[key] is function; next node is pattern */
20  Cquery,  /* ktable[key] is table; next node is pattern */
21  Cstring,  /* ktable[key] is string; next node is pattern */
22  Cnum,  /* numbered capture; 'key' is number of value to return */
23  Csubst,  /* substitution capture; next node is pattern */
24  Cfold,  /* ktable[key] is function; next node is pattern */
25  Cruntime,  /* not used in trees (is uses another type for tree) */
26  Cgroup  /* ktable[key] is group's "name" */
27} CapKind;
28
29
30/*
31** An unsigned integer large enough to index any subject entirely.
32** It can be size_t, but that will double the size of the array
33** of captures in a 64-bit machine.
34*/
35#if !defined(Index_t)
36typedef uint Index_t;
37#endif
38
39#define MAXINDT		(~(Index_t)0)
40
41
42typedef struct Capture {
43  Index_t index;  /* subject position */
44  unsigned short idx;  /* extra info (group name, arg index, etc.) */
45  byte kind;  /* kind of capture */
46  byte siz;  /* size of full capture + 1 (0 = not a full capture) */
47} Capture;
48
49
50typedef struct CapState {
51  Capture *cap;  /* current capture */
52  Capture *ocap;  /* (original) capture list */
53  lua_State *L;
54  int ptop;  /* stack index of last argument to 'match' */
55  int firstcap;  /* stack index of first capture pushed in the stack */
56  const char *s;  /* original string */
57  int valuecached;  /* value stored in cache slot */
58  int reclevel;  /* recursion level */
59} CapState;
60
61
62#define captype(cap)    ((cap)->kind)
63
64#define isclosecap(cap) (captype(cap) == Cclose)
65#define isopencap(cap)  ((cap)->siz == 0)
66
67/* true if c2 is (any number of levels) inside c1 */
68#define capinside(c1,c2)  \
69	(isopencap(c1) ? !isclosecap(c2) \
70                       : (c2)->index < (c1)->index + (c1)->siz - 1)
71
72
73/**
74** Maximum number of captures to visit when looking for an 'open'.
75*/
76#define MAXLOP		20
77
78
79
80int runtimecap (CapState *cs, Capture *close, const char *s, int *rem);
81int getcaptures (lua_State *L, const char *s, const char *r, int ptop);
82int finddyncap (Capture *cap, Capture *last);
83
84#endif
85
86