robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
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 { 11Cclose ,/* not used in trees */ 12Cposition , 13Cconst ,/* ktable[key] is Lua constant */ 14Cbackref ,/* ktable[key] is "name" of group to get capture */ 15Carg ,/* 'key' is arg's number */ 16Csimple ,/* next node is pattern */ 17Ctable ,/* next node is pattern */ 18Cfunction ,/* ktable[key] is function; next node is pattern */ 19Cacc ,/* ktable[key] is function; next node is pattern */ 20Cquery ,/* ktable[key] is table; next node is pattern */ 21Cstring ,/* ktable[key] is string; next node is pattern */ 22Cnum ,/* numbered capture; 'key' is number of value to return */ 23Csubst ,/* substitution capture; next node is pattern */ 24Cfold ,/* ktable[key] is function; next node is pattern */ 25Cruntime ,/* not used in trees (is uses another type for tree) */ 26Cgroup /* 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 { 43Index_t index ;/* subject position */ 44unsigned short idx ;/* extra info (group name, arg index, etc.) */ 45byte kind ;/* kind of capture */ 46byte siz ;/* size of full capture + 1 (0 = not a full capture) */ 47}Capture ; 48 49 50typedef struct CapState { 51Capture * cap ;/* current capture */ 52Capture * ocap ;/* (original) capture list */ 53lua_State * L ; 54int ptop ;/* stack index of last argument to 'match' */ 55int firstcap ;/* stack index of first capture pushed in the stack */ 56const char * s ;/* original string */ 57int valuecached ;/* value stored in cache slot */ 58int 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