Port to B::Hooks::Parser.
[p5sagit/Devel-Declare.git] / Declare.xs
index 68f070d..83ebc1d 100644 (file)
-#define PERL_IN_TOKE_C
-#define PERL_CORE
-#define PERL_NO_GET_CONTEXT
 #include "EXTERN.h"
 #include "perl.h"
 #include "XSUB.h"
+#include "hook_op_check.h"
 #undef printf
 #include "stolen_chunk_of_toke.c"
 #include <stdio.h>
 #include <string.h>
 
-#define LEX_NORMAL    10
-#define LEX_INTERPNORMAL   9
+#ifndef Newx
+# define Newx(v,n,t) New(0,v,n,t)
+#endif /* !Newx */
 
-/* placeholders for PL_check entries we wrap */
+static int dd_debug = 0;
 
-STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
-STATIC OP *(*dd_old_ck_nextstate)(pTHX_ OP *op);
+#define LEX_NORMAL    10
+#define LEX_INTERPNORMAL   9
 
 /* flag to trigger removal of temporary declaree sub */
 
 static int in_declare = 0;
 
-/* replacement PL_check rv2cv entry */
+/* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff
+   is a lookup into it - so if anything else we can use to tell, so we
+   need to be a bit more careful if PL_parser exists */
 
-STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
-  OP* kid;
-  char* s;
-  char tmpbuf[sizeof PL_tokenbuf];
-  STRLEN len;
-  HV *stash;
+#define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)
+
+#if defined(PL_parser) || defined(PERL_5_9_PLUS)
+#define DD_HAVE_PARSER PL_parser
+#define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff)
+#define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK)
+#else
+#define DD_HAVE_PARSER 1
+#define DD_HAVE_LEX_STUFF PL_lex_stuff
+#define DD_AM_LEXING DD_AM_LEXING_CHECK
+#endif
+
+/* thing that decides whether we're dealing with a declarator */
+
+int dd_is_declarator(pTHX_ char* name) {
   HV* is_declarator;
   SV** is_declarator_pack_ref;
   HV* is_declarator_pack_hash;
   SV** is_declarator_flag_ref;
-  char* cb_args[4];
+  int dd_flags;
+
+  is_declarator = get_hv("Devel::Declare::declarators", FALSE);
+
+  if (!is_declarator)
+    return -1;
+
+  /* $declarators{$current_package_name} */
+
+  if (!HvNAME(PL_curstash))
+         return -1;
+
+  is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
+                             strlen(HvNAME(PL_curstash)), FALSE);
+
+  if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
+    return -1; /* not a hashref */
+
+  is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
+
+  /* $declarators{$current_package_name}{$name} */
+
+  is_declarator_flag_ref = hv_fetch(
+    is_declarator_pack_hash, name,
+    strlen(name), FALSE
+  );
+
+  /* requires SvIOK as well as TRUE since flags not being an int is useless */
+
+  if (!is_declarator_flag_ref
+        || !SvIOK(*is_declarator_flag_ref) 
+        || !SvTRUE(*is_declarator_flag_ref))
+    return -1;
+
+  dd_flags = SvIVX(*is_declarator_flag_ref);
 
-  o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
+  return dd_flags;
+}
+
+/* callback thingy */
+
+void dd_linestr_callback (pTHX_ char* type, char* name) {
+
+  char* linestr = SvPVX(PL_linestr);
+  int offset = PL_bufptr - linestr;
+
+  dSP;
+
+  ENTER;
+  SAVETMPS;
+
+  PUSHMARK(SP);
+  XPUSHs(sv_2mortal(newSVpv(type, 0)));
+  XPUSHs(sv_2mortal(newSVpv(name, 0)));
+  XPUSHs(sv_2mortal(newSViv(offset)));
+  PUTBACK;
+
+  call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);
+
+  FREETMPS;
+  LEAVE;
+}
+
+char* dd_get_lex_stuff(pTHX) {
+  return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
+}
+
+void dd_clear_lex_stuff(pTHX) {
+  if (DD_HAVE_PARSER)
+    PL_lex_stuff = (SV*)NULL;
+}
+
+char* dd_get_curstash_name(pTHX) {
+  return HvNAME(PL_curstash);
+}
+
+char* dd_move_past_token (pTHX_ char* s) {
+
+  /*
+   *   buffer will be at the beginning of the declarator, -unless- the
+   *   declarator is at EOL in which case it'll be the next useful line
+   *   so we don't short-circuit out if we don't find the declarator
+   */
+
+  while (s < PL_bufend && isSPACE(*s)) s++;
+  if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
+    s += strlen(PL_tokenbuf);
+  return s;
+}
+
+int dd_toke_move_past_token (pTHX_ int offset) {
+  char* base_s = SvPVX(PL_linestr) + offset;
+  char* s = dd_move_past_token(aTHX_ base_s);
+  return s - base_s;
+}
+
+int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
+  char tmpbuf[sizeof PL_tokenbuf];
+  char* base_s = SvPVX(PL_linestr) + offset;
+  STRLEN len;
+  char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
+  return s - base_s;
+}
+
+int dd_toke_scan_str(pTHX_ int offset) {
+  char* base_s = SvPVX(PL_linestr) + offset;
+  char* s = scan_str(base_s, FALSE, FALSE);
+  return s - base_s;
+}
+
+int dd_toke_skipspace(pTHX_ int offset) {
+  char* base_s = SvPVX(PL_linestr) + offset;
+  char* s = skipspace(base_s);
+  return s - base_s;
+}
+
+/* replacement PL_check rv2cv entry */
+
+STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
+  OP* kid;
+  int dd_flags;
 
   if (in_declare) {
-    cb_args[0] = NULL;
-    call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
-    in_declare = 0;
+    if (dd_debug) {
+      printf("Deconstructing declare\n");
+      printf("PL_bufptr: %s\n", PL_bufptr);
+      printf("bufend at: %i\n", PL_bufend - PL_bufptr);
+      printf("linestr: %s\n", SvPVX(PL_linestr));
+      printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
+    }
+
+    dSP;
+  
+    ENTER;
+    SAVETMPS;
+  
+    PUSHMARK(SP);
+  
+    call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
+
+    FREETMPS;
+    LEAVE;
+
+    if (dd_debug) {
+      printf("PL_bufptr: %s\n", PL_bufptr);
+      printf("bufend at: %i\n", PL_bufend - PL_bufptr);
+      printf("linestr: %s\n", SvPVX(PL_linestr));
+      printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
+      printf("actual len: %i\n", strlen(PL_bufptr));
+    }
     return o;
   }
 
@@ -49,58 +201,90 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
   if (kid->op_type != OP_GV) /* not a GV so ignore */
     return o;
 
-  if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
+  if (!DD_AM_LEXING)
     return o; /* not lexing? */
 
-  stash = GvSTASH(kGVOP_gv);
-
-  /* printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv)); */
+  if (dd_debug) {
+    printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
+  }
 
-  is_declarator = get_hv("Devel::Declare::declarators", FALSE);
+  dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
 
-  if (!is_declarator)
+  if (dd_flags == -1)
     return o;
 
-  is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash),
-                             strlen(HvNAME(stash)), FALSE);
+  if (dd_debug) {
+    printf("dd_flags are: %i\n", dd_flags);
+    printf("PL_tokenbuf: %s\n", PL_tokenbuf);
+  }
 
-  if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
-    return o; /* not a hashref */
+  dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
 
-  is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
+  return o;
+}
 
-  is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv),
-                                strlen(GvNAME(kGVOP_gv)), FALSE);
+OP* dd_pp_entereval(pTHX) {
+  dSP;
+  dPOPss;
+  STRLEN len;
+  const char* s;
+  if (SvPOK(sv)) {
+    if (dd_debug) {
+      printf("mangling eval sv\n");
+    }
+    if (SvREADONLY(sv))
+      sv = sv_2mortal(newSVsv(sv));
+    s = SvPVX(sv);
+    len = SvCUR(sv);
+    if (!len || s[len-1] != ';') {
+      if (!(SvFLAGS(sv) & SVs_TEMP))
+        sv = sv_2mortal(newSVsv(sv));
+      sv_catpvn(sv, "\n;", 2);
+    }
+    SvGROW(sv, 8192);
+  }
+  PUSHs(sv);
+  return PL_ppaddr[OP_ENTEREVAL](aTHX);
+}
 
-  if (!is_declarator_flag_ref || !SvTRUE(*is_declarator_flag_ref))
-    return o;
+STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
+  if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
+    o->op_ppaddr = dd_pp_entereval;
+  return o;
+}
 
-  s = PL_bufptr; /* copy the current buffer pointer */
+static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
+{
+  const I32 count = FILTER_READ(idx+1, sv, maxlen);
+  SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
+  /* filter_del(dd_filter_realloc); */
+  return count;
+}
 
-  while (s < PL_bufend && isSPACE(*s)) s++;
-  if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
-    s += strlen(PL_tokenbuf);
-  else
+STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
+  int dd_flags;
+  char* name;
+
+  /* if this is set, we just grabbed a delimited string or something,
+     not a bareword, so NO TOUCHY */
+
+  if (DD_HAVE_LEX_STUFF)
     return o;
 
-  /* find next word */
+  /* don't try and look this up if it's not a string const */
+  if (!SvPOK(cSVOPo->op_sv))
+    return o;
 
-  s = skipspace(s);
+  name = SvPVX(cSVOPo->op_sv);
 
-  /* 0 in arg 4 is allow_package - not trying that yet :) */
+  dd_flags = dd_is_declarator(aTHX_ name);
 
-  s = scan_word(s, tmpbuf, sizeof tmpbuf, 0, &len);
+  if (dd_flags == -1)
+    return o;
 
-  if (len) {
-    cb_args[0] = HvNAME(stash);
-    cb_args[1] = GvNAME(kGVOP_gv);
-    cb_args[2] = tmpbuf;
-    cb_args[3] = NULL;
-    call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
-    in_declare = 1;
-  }
+  dd_linestr_callback(aTHX_ "const", name);
 
-  return o;
+  return o;  
 }
 
 static int initialized = 0;
@@ -113,15 +297,72 @@ void
 setup()
   CODE:
   if (!initialized++) {
-    dd_old_ck_rv2cv = PL_check[OP_RV2CV];
-    PL_check[OP_RV2CV] = dd_ck_rv2cv;
+    hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
+    hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
+    hook_op_check(OP_CONST, dd_ck_const, NULL);
   }
+  filter_add(dd_filter_realloc, NULL);
+
+char*
+get_lex_stuff()
+  CODE:
+    RETVAL = dd_get_lex_stuff(aTHX);
+  OUTPUT:
+    RETVAL
+
+void
+clear_lex_stuff()
+  CODE:
+    dd_clear_lex_stuff(aTHX);
+
+char*
+get_curstash_name()
+  CODE:
+    RETVAL = dd_get_curstash_name(aTHX);
+  OUTPUT:
+    RETVAL
+
+int
+toke_scan_word(int offset, int handle_package)
+  CODE:
+    RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
+  OUTPUT:
+    RETVAL
+
+int
+toke_move_past_token(int offset);
+  CODE:
+    RETVAL = dd_toke_move_past_token(aTHX_ offset);
+  OUTPUT:
+    RETVAL
+
+int
+toke_scan_str(int offset);
+  CODE:
+    RETVAL = dd_toke_scan_str(aTHX_ offset);
+  OUTPUT:
+    RETVAL
+
+int
+toke_skipspace(int offset)
+  CODE:
+    RETVAL = dd_toke_skipspace(aTHX_ offset);
+  OUTPUT:
+    RETVAL
+
+int
+get_in_declare()
+  CODE:
+    RETVAL = in_declare;
+  OUTPUT:
+    RETVAL
 
 void
-teardown()
+set_in_declare(int value)
   CODE:
-  /* ensure we only uninit when number of teardown calls matches 
-     number of setup calls */
-  if (initialized && !--initialized) {
-    PL_check[OP_RV2CV] = dd_old_ck_rv2cv;
+    in_declare = value;
+
+BOOT:
+  if (getenv ("DD_DEBUG")) {
+    dd_debug = 1;
   }