lptypes.h (2976B)
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 #if !defined(lptypes_h) 9 #define lptypes_h 10 11 12 #if !defined(LPEG_DEBUG) 13 #define NDEBUG 14 #endif 15 16 #include <assert.h> 17 #include <limits.h> 18 19 #include "lua.h" 20 21 22 #define VERSION "1.0.1" 23 24 25 #define PATTERN_T "lpeg-pattern" 26 #define MAXSTACKIDX "lpeg-maxstack" 27 28 29 /* 30 ** compatibility with Lua 5.1 31 */ 32 #if (LUA_VERSION_NUM == 501) 33 34 #define lp_equal lua_equal 35 36 #define lua_getuservalue lua_getfenv 37 #define lua_setuservalue lua_setfenv 38 39 #define lua_rawlen lua_objlen 40 41 #define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f) 42 #define luaL_newlib(L,f) luaL_register(L,"lpeg",f) 43 44 #endif 45 46 47 #if !defined(lp_equal) 48 #define lp_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) 49 #endif 50 51 52 /* default maximum size for call/backtrack stack */ 53 #if !defined(MAXBACK) 54 #define MAXBACK 400 55 #endif 56 57 58 /* maximum number of rules in a grammar (limited by 'unsigned char') */ 59 #if !defined(MAXRULES) 60 #define MAXRULES 250 61 #endif 62 63 64 65 /* initial size for capture's list */ 66 #define INITCAPSIZE 32 67 68 69 /* index, on Lua stack, for subject */ 70 #define SUBJIDX 2 71 72 /* number of fixed arguments to 'match' (before capture arguments) */ 73 #define FIXEDARGS 3 74 75 /* index, on Lua stack, for capture list */ 76 #define caplistidx(ptop) ((ptop) + 2) 77 78 /* index, on Lua stack, for pattern's ktable */ 79 #define ktableidx(ptop) ((ptop) + 3) 80 81 /* index, on Lua stack, for backtracking stack */ 82 #define stackidx(ptop) ((ptop) + 4) 83 84 85 86 typedef unsigned char byte; 87 88 89 #define BITSPERCHAR 8 90 91 #define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1) 92 93 94 95 typedef struct Charset { 96 byte cs[CHARSETSIZE]; 97 } Charset; 98 99 100 101 #define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} } 102 103 /* access to charset */ 104 #define treebuffer(t) ((byte *)((t) + 1)) 105 106 /* number of slots needed for 'n' bytes */ 107 #define bytes2slots(n) (((n) - 1) / sizeof(TTree) + 1) 108 109 /* set 'b' bit in charset 'cs' */ 110 #define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7))) 111 112 113 /* 114 ** in capture instructions, 'kind' of capture and its offset are 115 ** packed in field 'aux', 4 bits for each 116 */ 117 #define getkind(op) ((op)->i.aux & 0xF) 118 #define getoff(op) (((op)->i.aux >> 4) & 0xF) 119 #define joinkindoff(k,o) ((k) | ((o) << 4)) 120 121 #define MAXOFF 0xF 122 #define MAXAUX 0xFF 123 124 125 /* maximum number of bytes to look behind */ 126 #define MAXBEHIND MAXAUX 127 128 129 /* maximum size (in elements) for a pattern */ 130 #define MAXPATTSIZE (SHRT_MAX - 10) 131 132 133 /* size (in elements) for an instruction plus extra l bytes */ 134 #define instsize(l) (((l) + sizeof(Instruction) - 1)/sizeof(Instruction) + 1) 135 136 137 /* size (in elements) for a ISet instruction */ 138 #define CHARSETINSTSIZE instsize(CHARSETSIZE) 139 140 /* size (in elements) for a IFunc instruction */ 141 #define funcinstsize(p) ((p)->i.aux + 2) 142 143 144 145 #define testchar(st,c) (((int)(st)[((c) >> 3)] & (1 << ((c) & 7)))) 146 147 148 #endif 149