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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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)
  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. #define lua_rawlen lua_objlen
  26. #define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f)
  27. #define luaL_newlib(L,f) luaL_register(L,"lpeg",f)
  28. #endif
  29. #if !defined(lp_equal)
  30. #define lp_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
  31. #endif
  32. /* default maximum size for call/backtrack stack */
  33. #if !defined(MAXBACK)
  34. #define MAXBACK 400
  35. #endif
  36. /* maximum number of rules in a grammar */
  37. #if !defined(MAXRULES)
  38. #define MAXRULES 1000
  39. #endif
  40. /* initial size for capture's list */
  41. #define INITCAPSIZE 32
  42. /* index, on Lua stack, for subject */
  43. #define SUBJIDX 2
  44. /* number of fixed arguments to 'match' (before capture arguments) */
  45. #define FIXEDARGS 3
  46. /* index, on Lua stack, for capture list */
  47. #define caplistidx(ptop) ((ptop) + 2)
  48. /* index, on Lua stack, for pattern's ktable */
  49. #define ktableidx(ptop) ((ptop) + 3)
  50. /* index, on Lua stack, for backtracking stack */
  51. #define stackidx(ptop) ((ptop) + 4)
  52. typedef unsigned char byte;
  53. #define BITSPERCHAR 8
  54. #define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1)
  55. typedef struct Charset {
  56. byte cs[CHARSETSIZE];
  57. } Charset;
  58. #define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} }
  59. /* access to charset */
  60. #define treebuffer(t) ((byte *)((t) + 1))
  61. /* number of slots needed for 'n' bytes */
  62. #define bytes2slots(n) (((n) - 1) / sizeof(TTree) + 1)
  63. /* set 'b' bit in charset 'cs' */
  64. #define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7)))
  65. /*
  66. ** in capture instructions, 'kind' of capture and its offset are
  67. ** packed in field 'aux', 4 bits for each
  68. */
  69. #define getkind(op) ((op)->i.aux & 0xF)
  70. #define getoff(op) (((op)->i.aux >> 4) & 0xF)
  71. #define joinkindoff(k,o) ((k) | ((o) << 4))
  72. #define MAXOFF 0xF
  73. #define MAXAUX 0xFF
  74. /* maximum number of bytes to look behind */
  75. #define MAXBEHIND MAXAUX
  76. /* maximum size (in elements) for a pattern */
  77. #define MAXPATTSIZE (SHRT_MAX - 10)
  78. /* size (in elements) for an instruction plus extra l bytes */
  79. #define instsize(l) (((l) + sizeof(Instruction) - 1)/sizeof(Instruction) + 1)
  80. /* size (in elements) for a ISet instruction */
  81. #define CHARSETINSTSIZE instsize(CHARSETSIZE)
  82. /* size (in elements) for a IFunc instruction */
  83. #define funcinstsize(p) ((p)->i.aux + 2)
  84. #define testchar(st,c) (((int)(st)[((c) >> 3)] & (1 << ((c) & 7))))
  85. #endif