robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1/* 2** LPeg - PEG pattern matching for Lua 3** Copyright 2007-2023, Lua.org & PUC-Rio (see 'lpeg.html' for license) 4** written by Roberto Ierusalimschy 5*/ 6 7#if !defined(lptypes_h ) 8#define lptypes_h 9 10 11#include <assert.h> 12#include <limits.h> 13#include <string.h> 14 15#include "lua.h" 16 17 18#define VERSION "1.1.0" 19 20 21#define PATTERN_T "lpeg-pattern" 22#define MAXSTACKIDX "lpeg-maxstack" 23 24 25/* 26** compatibility with Lua 5.1 27*/ 28#if (LUA_VERSION_NUM == 501 ) 29 30#define lp_equal lua_equal 31 32#define lua_getuservalue lua_getfenv 33#define lua_setuservalue lua_setfenv 34 35#define lua_rawlen lua_objlen 36 37#define luaL_setfuncs (L ,f ,n ) luaL_register(L,NULL,f) 38// #define luaL_newlib(L,f) luaL_register(L,"lpeg",f) // Nvim: conflicts with LuaJIT 39 40typedef size_t lua_Unsigned ; 41 42#endif 43 44 45#if !defined(lp_equal ) 46#define lp_equal (L ,idx1 ,idx2 ) lua_compare(L,(idx1),(idx2),LUA_OPEQ) 47#endif 48 49 50/* default maximum size for call/backtrack stack */ 51#if !defined(MAXBACK ) 52#define MAXBACK 400 53#endif 54 55 56/* maximum number of rules in a grammar (limited by 'unsigned short') */ 57#if !defined(MAXRULES ) 58#define MAXRULES 1000 59#endif 60 61 62 63/* initial size for capture's list */ 64#define INITCAPSIZE 32 65 66 67/* index, on Lua stack, for subject */ 68#define SUBJIDX 2 69 70/* number of fixed arguments to 'match' (before capture arguments) */ 71#define FIXEDARGS 3 72 73/* index, on Lua stack, for capture list */ 74#define caplistidx (ptop ) ((ptop) + 2) 75 76/* index, on Lua stack, for pattern's ktable */ 77#define ktableidx (ptop ) ((ptop) + 3) 78 79/* index, on Lua stack, for backtracking stack */ 80#define stackidx (ptop ) ((ptop) + 4) 81 82 83 84typedef unsigned char byte ; 85 86typedef unsigned int uint ; 87 88 89#define BITSPERCHAR 8 90 91#define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1) 92 93 94 95typedef struct Charset { 96byte cs [CHARSETSIZE ]; 97}Charset ; 98 99 100 101#define loopset (v ,b ) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} } 102 103#define fillset (s ,c ) memset(s,c,CHARSETSIZE) 104#define clearset (s ) fillset(s,0) 105 106/* number of slots needed for 'n' bytes */ 107#define bytes2slots (n ) (((n) - 1u) / (uint)sizeof(TTree) + 1u) 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.aux1 & 0xF) 118#define getoff (op ) (((op)->i.aux1 >> 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 instructions) for l bytes (l > 0) */ 134#define instsize (l ) ((int)(((l) + (uint)sizeof(Instruction) - 1u) \ 135 / (uint)sizeof(Instruction))) 136 137 138/* size (in elements) for a ISet instruction */ 139#define CHARSETINSTSIZE (1 + instsize(CHARSETSIZE)) 140 141/* size (in elements) for a IFunc instruction */ 142#define funcinstsize (p ) ((p)->i.aux + 2) 143 144 145 146#define testchar (st ,c ) ((((uint)(st)[((c) >> 3)]) >> ((c) & 7)) & 1) 147 148 149#endif 150