lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


After playing with the lpeg user defined patterns, I found that I
needed a callback for when my patten was collected.
The patch below adds a __gc handler and the "GCFunc" callback type.

Hopefully this can make it into the next lpeg release.

Daurn


*** lpeg_gc.h	2012-06-21 23:46:40.853470989 +1000
--- lpeg.h	2011-02-17 02:03:25.000000000 +1100
***************
*** 14,21 ****
  #define KEYNEWPATT	"lpeg.newpf"


- typedef void (*GCFunc) ( const void *ud );
-
  /*
  ** type of extension functions that define new "patterns" for LPEG
  ** It should return the new current position or NULL if match fails
--- 14,19 ----
***************
*** 23,29 ****
  typedef const char *(*PattFunc) (const char *s,  /* current position */
                                   const char *e,  /* string end */
                                   const char *o,  /* string start */
!                                  const void *ud);/* user data */

  /*
  ** function to create new patterns based on 'PattFunc' functions.
--- 21,27 ----
  typedef const char *(*PattFunc) (const char *s,  /* current position */
                                   const char *e,  /* string end */
                                   const char *o,  /* string start */
!                                  const void *ud);  /* user data */

  /*
  ** function to create new patterns based on 'PattFunc' functions.
***************
*** 33,41 ****
  ** point to a function.)
  */
  typedef void (*Newpf) (lua_State *L,
!                        PattFunc f,      /* pattern */
                         const void *ud,  /* (user) data to be passed to 'f' */
!                        size_t l,        /* size of data to be passed to 'f' */
! 					   GCFunc g);

  #endif
--- 31,38 ----
  ** point to a function.)
  */
  typedef void (*Newpf) (lua_State *L,
!                        PattFunc f,  /* pattern */
                         const void *ud,  /* (user) data to be passed to 'f' */
!                        size_t l);  /* size of data to be passed to 'f' */

  #endif

*** lpeg_gc.c	2012-06-22 01:36:56.398032549 +1000
--- lpeg.c	2011-02-17 02:03:25.000000000 +1100
***************
*** 145,151 ****
      short offset;
    } i;
    PattFunc f;
-   GCFunc g;
    int iv;
    byte buff[1];
  } Instruction;
--- 145,150 ----
***************
*** 200,206 ****
  #define CHARSETINSTSIZE		instsize(CHARSETSIZE)

  /* size (in elements) for a IFunc instruction */
! #define funcinstsize(p)		((p)->i.aux + 3)


  #define loopset(v,b)	{ int v; for (v = 0; v < CHARSETSIZE; v++) b; }
--- 199,205 ----
  #define CHARSETINSTSIZE		instsize(CHARSETSIZE)

  /* size (in elements) for a IFunc instruction */
! #define funcinstsize(p)		((p)->i.aux + 2)


  #define loopset(v,b)	{ int v; for (v = 0; v < CHARSETSIZE; v++) b; }
***************
*** 504,510 ****
          continue;
        }
        case IFunc: {
!         const char *r = (p+1)->f(s, e, o, (p+3)->buff);
          if (r != NULL) { s = r; p += funcinstsize(p); }
          else condfailed(p);
          continue;
--- 503,509 ----
          continue;
        }
        case IFunc: {
!         const char *r = (p+1)->f(s, e, o, (p+2)->buff);
          if (r != NULL) { s = r; p += funcinstsize(p); }
          else condfailed(p);
          continue;
***************
*** 769,775 ****
          goto fail;  /* be liberal in this case */
        }
        case IFunc: {
!         const char *r = (p+1)->f(dummy, dummy, dummy, (p+3)->buff);
          if (r != NULL) { p += funcinstsize(p); }
          else condfailed(p);
          continue;
--- 768,774 ----
          goto fail;  /* be liberal in this case */
        }
        case IFunc: {
!         const char *r = (p+1)->f(dummy, dummy, dummy, (p+2)->buff);
          if (r != NULL) { p += funcinstsize(p); }
          else condfailed(p);
          continue;
***************
*** 963,969 ****
  ** 'p' keeps its original ktable.  If 'p' has no elements, it shares
  ** 'p1' ktable.  Otherwise, this function creates a new ktable for 'p'.
  ** Return the offset of original 'p' elements in the new ktable.
! */
  static int jointable (lua_State *L, int p1) {
    int n, n1, i;
    lua_getfenv(L, p1);
--- 962,968 ----
  ** 'p' keeps its original ktable.  If 'p' has no elements, it shares
  ** 'p1' ktable.  Otherwise, this function creates a new ktable for 'p'.
  ** Return the offset of original 'p' elements in the new ktable.
! */
  static int jointable (lua_State *L, int p1) {
    int n, n1, i;
    lua_getfenv(L, p1);
***************
*** 1811,1826 ****
  ** =======================================================
  */

! static void l_newpf (lua_State *L, PattFunc f, const void *ud,
size_t l, GCFunc g ) {
!   int n = instsize(l) + 2;
    Instruction *p = newpatt(L, n);
    if (n > MAXAUX) luaL_error(L, "pattern data too long");
    p[0].i.code = IFunc;
!   p[0].i.aux = n - 3;
    p[0].i.offset = 0;
    p[1].f = f;
!   p[2].g = g;
!   memcpy(p[3].buff, ud, l);
  }

  /* }====================================================== */
--- 1810,1824 ----
  ** =======================================================
  */

! static void l_newpf (lua_State *L, PattFunc f, const void *ud, size_t l) {
!   int n = instsize(l) + 1;
    Instruction *p = newpatt(L, n);
    if (n > MAXAUX) luaL_error(L, "pattern data too long");
    p[0].i.code = IFunc;
!   p[0].i.aux = n - 2;
    p[0].i.offset = 0;
    p[1].f = f;
!   memcpy(p[2].buff, ud, l);
  }

  /* }====================================================== */
***************
*** 2351,2370 ****
    return getcaptures(L, s, r, ptop);
  }

- static int gc_l ( lua_State *L ) {
-   Instruction *p = checkpattern(L, 1);
-   switch ((Opcode)p->i.code) {
-     case IFunc: {
-       GCFunc g = (p+2)->g;
-       if ( g != NULL) g((p+3)->buff);
-       break;
-     }
-     default:
-       break;
-   }
-   return 0;
- }
-

  static struct luaL_Reg pattreg[] = {
    {"match", matchl},
--- 2349,2354 ----
***************
*** 2400,2406 ****
    {"__div", rcapture_l},
    {"__unm", unm_l},
    {"__len", pattand_l},
-   {"__gc", gc_l},
    {NULL, NULL}
  };

--- 2384,2389 ----