lptypes.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #if !defined(lptypes_h)
  7. #define lptypes_h
  8. #include <assert.h>
  9. #include <limits.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #define VERSION "1.1.0"
  13. #define PATTERN_T "lpeg-pattern"
  14. #define MAXSTACKIDX "lpeg-maxstack"
  15. /*
  16. ** compatibility with Lua 5.1
  17. */
  18. #if (LUA_VERSION_NUM == 501)
  19. #define lp_equal lua_equal
  20. #define lua_getuservalue lua_getfenv
  21. #define lua_setuservalue lua_setfenv
  22. #define lua_rawlen lua_objlen
  23. #define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f)
  24. #define luaL_newlib(L,f) luaL_register(L,"lpeg",f)
  25. typedef size_t lua_Unsigned;
  26. #endif
  27. #if !defined(lp_equal)
  28. #define lp_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
  29. #endif
  30. /* default maximum size for call/backtrack stack */
  31. #if !defined(MAXBACK)
  32. #define MAXBACK 400
  33. #endif
  34. /* maximum number of rules in a grammar (limited by 'unsigned short') */
  35. #if !defined(MAXRULES)
  36. #define MAXRULES 1000
  37. #endif
  38. /* initial size for capture's list */
  39. #define INITCAPSIZE 32
  40. /* index, on Lua stack, for subject */
  41. #define SUBJIDX 2
  42. /* number of fixed arguments to 'match' (before capture arguments) */
  43. #define FIXEDARGS 3
  44. /* index, on Lua stack, for capture list */
  45. #define caplistidx(ptop) ((ptop) + 2)
  46. /* index, on Lua stack, for pattern's ktable */
  47. #define ktableidx(ptop) ((ptop) + 3)
  48. /* index, on Lua stack, for backtracking stack */
  49. #define stackidx(ptop) ((ptop) + 4)
  50. typedef unsigned char byte;
  51. typedef unsigned int uint;
  52. #define BITSPERCHAR 8
  53. #define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1)
  54. typedef struct Charset {
  55. byte cs[CHARSETSIZE];
  56. } Charset;
  57. #define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} }
  58. #define fillset(s,c) memset(s,c,CHARSETSIZE)
  59. #define clearset(s) fillset(s,0)
  60. /* number of slots needed for 'n' bytes */
  61. #define bytes2slots(n) (((n) - 1u) / (uint)sizeof(TTree) + 1u)
  62. /* set 'b' bit in charset 'cs' */
  63. #define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7)))
  64. /*
  65. ** in capture instructions, 'kind' of capture and its offset are
  66. ** packed in field 'aux', 4 bits for each
  67. */
  68. #define getkind(op) ((op)->i.aux1 & 0xF)
  69. #define getoff(op) (((op)->i.aux1 >> 4) & 0xF)
  70. #define joinkindoff(k,o) ((k) | ((o) << 4))
  71. #define MAXOFF 0xF
  72. #define MAXAUX 0xFF
  73. /* maximum number of bytes to look behind */
  74. #define MAXBEHIND MAXAUX
  75. /* maximum size (in elements) for a pattern */
  76. #define MAXPATTSIZE (SHRT_MAX - 10)
  77. /* size (in instructions) for l bytes (l > 0) */
  78. #define instsize(l) ((int)(((l) + (uint)sizeof(Instruction) - 1u) \
  79. / (uint)sizeof(Instruction)))
  80. /* size (in elements) for a ISet instruction */
  81. #define CHARSETINSTSIZE (1 + instsize(CHARSETSIZE))
  82. /* size (in elements) for a IFunc instruction */
  83. #define funcinstsize(p) ((p)->i.aux + 2)
  84. #define testchar(st,c) ((((uint)(st)[((c) >> 3)]) >> ((c) & 7)) & 1)
  85. #endif