lpcap.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #if !defined(lpcap_h)
  2. #define lpcap_h
  3. #include "lptypes.h"
  4. /* kinds of captures */
  5. typedef enum CapKind {
  6. Cclose, /* not used in trees */
  7. Cposition,
  8. Cconst, /* ktable[key] is Lua constant */
  9. Cbackref, /* ktable[key] is "name" of group to get capture */
  10. Carg, /* 'key' is arg's number */
  11. Csimple, /* next node is pattern */
  12. Ctable, /* next node is pattern */
  13. Cfunction, /* ktable[key] is function; next node is pattern */
  14. Cacc, /* ktable[key] is function; next node is pattern */
  15. Cquery, /* ktable[key] is table; next node is pattern */
  16. Cstring, /* ktable[key] is string; next node is pattern */
  17. Cnum, /* numbered capture; 'key' is number of value to return */
  18. Csubst, /* substitution capture; next node is pattern */
  19. Cfold, /* ktable[key] is function; next node is pattern */
  20. Cruntime, /* not used in trees (is uses another type for tree) */
  21. Cgroup /* ktable[key] is group's "name" */
  22. } CapKind;
  23. /*
  24. ** An unsigned integer large enough to index any subject entirely.
  25. ** It can be size_t, but that will double the size of the array
  26. ** of captures in a 64-bit machine.
  27. */
  28. #if !defined(Index_t)
  29. typedef uint Index_t;
  30. #endif
  31. #define MAXINDT (~(Index_t)0)
  32. typedef struct Capture {
  33. Index_t index; /* subject position */
  34. unsigned short idx; /* extra info (group name, arg index, etc.) */
  35. byte kind; /* kind of capture */
  36. byte siz; /* size of full capture + 1 (0 = not a full capture) */
  37. } Capture;
  38. typedef struct CapState {
  39. Capture *cap; /* current capture */
  40. Capture *ocap; /* (original) capture list */
  41. lua_State *L;
  42. int ptop; /* stack index of last argument to 'match' */
  43. int firstcap; /* stack index of first capture pushed in the stack */
  44. const char *s; /* original string */
  45. int valuecached; /* value stored in cache slot */
  46. int reclevel; /* recursion level */
  47. } CapState;
  48. #define captype(cap) ((cap)->kind)
  49. #define isclosecap(cap) (captype(cap) == Cclose)
  50. #define isopencap(cap) ((cap)->siz == 0)
  51. /* true if c2 is (any number of levels) inside c1 */
  52. #define capinside(c1,c2) \
  53. (isopencap(c1) ? !isclosecap(c2) \
  54. : (c2)->index < (c1)->index + (c1)->siz - 1)
  55. /**
  56. ** Maximum number of captures to visit when looking for an 'open'.
  57. */
  58. #define MAXLOP 20
  59. int runtimecap (CapState *cs, Capture *close, const char *s, int *rem);
  60. int getcaptures (lua_State *L, const char *s, const char *r, int ptop);
  61. int finddyncap (Capture *cap, Capture *last);
  62. #endif