You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lptypes.h 3.0KB

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