This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[win32] fix extra LEAVE when require fails
[perl5.git] / toke.c
diff --git a/toke.c b/toke.c
index 28ea26d..640ab67 100644 (file)
--- a/toke.c
+++ b/toke.c
@@ -49,6 +49,8 @@ static int uni _((I32 f, char *s));
 #endif
 static char * filter_gets _((SV *sv, PerlIO *fp, STRLEN append));
 static void restore_rsfp _((void *f));
+static void restore_expect _((void *e));
+static void restore_lex_expect _((void *e));
 
 static char ident_too_long[] = "Identifier too long";
 
@@ -187,7 +189,7 @@ missingterm(char *s)
     char q;
     if (s) {
        char *nl = strrchr(s,'\n');
-       if (nl)
+       if (nl) 
            *nl = '\0';
     }
     else if (multi_close < 32 || multi_close == 127) {
@@ -219,6 +221,19 @@ depcom(void)
     deprecate("comma-less variable list");
 }
 
+#ifdef WIN32
+
+static I32
+win32_textfilter(int idx, SV *sv, int maxlen)
+{
+ I32 count = FILTER_READ(idx+1, sv, maxlen);
+ if (count > 0 && !maxlen)
+  win32_strip_return(sv);
+ return count;
+}
+#endif
+
+
 void
 lex_start(SV *line)
 {
@@ -244,6 +259,11 @@ lex_start(SV *line)
     SAVEPPTR(lex_brackstack);
     SAVEPPTR(lex_casestack);
     SAVEDESTRUCTOR(restore_rsfp, rsfp);
+    SAVESPTR(lex_stuff);
+    SAVEI32(lex_defer);
+    SAVESPTR(lex_repl);
+    SAVEDESTRUCTOR(restore_expect, tokenbuf + expect); /* encode as pointer */
+    SAVEDESTRUCTOR(restore_lex_expect, tokenbuf + expect);
 
     lex_state = LEX_NORMAL;
     lex_defer = 0;
@@ -258,11 +278,7 @@ lex_start(SV *line)
     *lex_casestack = '\0';
     lex_dojoin = 0;
     lex_starts = 0;
-    if (lex_stuff)
-       SvREFCNT_dec(lex_stuff);
     lex_stuff = Nullsv;
-    if (lex_repl)
-       SvREFCNT_dec(lex_repl);
     lex_repl = Nullsv;
     lex_inpat = 0;
     lex_inwhat = 0;
@@ -302,6 +318,22 @@ restore_rsfp(void *f)
 }
 
 static void
+restore_expect(e)
+void *e;
+{
+    /* a safe way to store a small integer in a pointer */
+    expect = (expectation)((char *)e - tokenbuf);
+}
+
+static void
+restore_lex_expect(e)
+void *e;
+{
+    /* a safe way to store a small integer in a pointer */
+    lex_expect = (expectation)((char *)e - tokenbuf);
+}
+
+static void
 incline(char *s)
 {
     dTHR;
@@ -376,7 +408,7 @@ skipspace(register char *s)
            oldoldbufptr = oldbufptr = bufptr = s = linestart = SvPVX(linestr);
            bufend = SvPVX(linestr) + SvCUR(linestr);
            if (preprocess && !in_eval)
-               (void)my_pclose(rsfp);
+               (void)PerlProc_pclose(rsfp);
            else if ((PerlIO*)rsfp == PerlIO_stdin())
                PerlIO_clearerr(rsfp);
            else
@@ -487,7 +519,7 @@ force_next(I32 type)
 }
 
 static char *
-force_word(register char *start, int token, int check_keyword, int allow_pack, int allow_tick)
+force_word(register char *start, int token, int check_keyword, int allow_pack, int allow_initial_tick)
 {
     register char *s;
     STRLEN len;
@@ -496,7 +528,7 @@ force_word(register char *start, int token, int check_keyword, int allow_pack, i
     s = start;
     if (isIDFIRST(*s) ||
        (allow_pack && *s == ':') ||
-       (allow_tick && *s == '\'') )
+       (allow_initial_tick && *s == '\'') )
     {
        s = scan_word(s, tokenbuf, sizeof tokenbuf, allow_pack, &len);
        if (check_keyword && keyword(tokenbuf, len))
@@ -640,7 +672,7 @@ static I32
 sublex_push(void)
 {
     dTHR;
-    push_scope();
+    ENTER;
 
     lex_state = sublex_info.super_state;
     SAVEI32(lex_dojoin);
@@ -726,7 +758,7 @@ sublex_done(void)
        return ',';
     }
     else {
-       pop_scope();
+       LEAVE;
        bufend = SvPVX(linestr);
        bufend += SvCUR(linestr);
        expect = XOPERATOR;
@@ -770,9 +802,31 @@ scan_const(char *start)
                s++;
            }
        }
-       else if (*s == '(' && lex_inpat && s[1] == '?' && s[2] == '#') {
-           while (s < send && *s != ')')
-               *d++ = *s++;
+       else if (*s == '(' && lex_inpat && s[1] == '?') {
+           if (s[2] == '#') {
+               while (s < send && *s != ')')
+                   *d++ = *s++;
+           } else if (s[2] == '{') {   /* This should march regcomp.c */
+               I32 count = 1;
+               char *regparse = s + 3;
+               char c;
+
+               while (count && (c = *regparse)) {
+                   if (c == '\\' && regparse[1])
+                       regparse++;
+                   else if (c == '{') 
+                       count++;
+                   else if (c == '}') 
+                       count--;
+                   regparse++;
+               }
+               if (*regparse == ')')
+                   regparse++;
+               else
+                   yyerror("Sequence (?{...}) not terminated or not {}-balanced");
+               while (s < regparse && *s != ')')
+                   *d++ = *s++;
+           }
        }
        else if (*s == '#' && lex_inpat &&
          ((PMOP*)lex_inpat)->op_pmflags & PMf_EXTENDED) {
@@ -1010,9 +1064,18 @@ intuit_method(char *start, GV *gv)
     GV* indirgv;
 
     if (gv) {
+       CV *cv;
        if (GvIO(gv))
            return 0;
-       if (!GvCVu(gv))
+       if ((cv = GvCVu(gv))) {
+           char *proto = SvPVX(cv);
+           if (proto) {
+               if (*proto == ';')
+                   proto++;
+               if (*proto == '*')
+                   return 0;
+           }
+       } else
            gv = 0;
     }
     s = scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
@@ -1051,10 +1114,11 @@ static char*
 incl_perldb(void)
 {
     if (perldb) {
-       char *pdb = getenv("PERL5DB");
+       char *pdb = PerlEnv_getenv("PERL5DB");
 
        if (pdb)
            return pdb;
+       SETERRNO(0,SS$_NORMAL);
        return "BEGIN { require 'perl5db.pl' }";
     }
     return "";
@@ -1088,7 +1152,7 @@ filter_add(filter_t funcp, SV *datasv)
     if (!rsfp_filters)
        rsfp_filters = newAV();
     if (!datasv)
-       datasv = newSV(0);
+       datasv = NEWSV(255,0);
     if (!SvUPGRADE(datasv, SVt_PVIO))
         die("Can't upgrade filter_add data to SVt_PVIO");
     IoDIRP(datasv) = (DIR*)funcp; /* stash funcp into spare field */
@@ -1106,10 +1170,10 @@ filter_del(filter_t funcp)
 {
     if (filter_debug)
        warn("filter_del func %p", funcp);
-    if (!rsfp_filters || AvFILL(rsfp_filters)<0)
+    if (!rsfp_filters || AvFILLp(rsfp_filters)<0)
        return;
     /* if filter is on top of stack (usual case) just pop it off */
-    if (IoDIRP(FILTER_DATA(AvFILL(rsfp_filters))) == (void*)funcp){
+    if (IoDIRP(FILTER_DATA(AvFILLp(rsfp_filters))) == (void*)funcp){
        sv_free(av_pop(rsfp_filters));
 
         return;
@@ -1131,7 +1195,7 @@ filter_read(int idx, SV *buf_sv, int maxlen)
 
     if (!rsfp_filters)
        return -1;
-    if (idx > AvFILL(rsfp_filters)){       /* Any more filters?        */
+    if (idx > AvFILLp(rsfp_filters)){       /* Any more filters?       */
        /* Provide a default input filter to make life easy.    */
        /* Note that we append to the line. This is handy.      */
        if (filter_debug)
@@ -1158,6 +1222,7 @@ filter_read(int idx, SV *buf_sv, int maxlen)
                else
                    return 0 ;          /* end of file */
            }
+
        }
        return SvCUR(buf_sv);
     }
@@ -1178,9 +1243,15 @@ filter_read(int idx, SV *buf_sv, int maxlen)
     return (*funcp)(idx, buf_sv, maxlen);
 }
 
+
 static char *
-filter_gets(register SV *sv, register FILE *fp, STRLEN append)
+filter_gets(register SV *sv, register PerlIO *fp, STRLEN append)
 {
+#ifdef WIN32FILTER
+    if (!rsfp_filters) {
+       filter_add(win32_textfilter,NULL);
+    }
+#endif
     if (rsfp_filters) {
 
        if (!append)
@@ -1192,7 +1263,6 @@ filter_gets(register SV *sv, register FILE *fp, STRLEN append)
     }
     else 
         return (sv_gets(sv, fp, append));
-    
 }
 
 
@@ -1211,6 +1281,8 @@ yylex(void)
     register char *d;
     register I32 tmp;
     STRLEN len;
+    GV *gv = Nullgv;
+    GV **gvp = 0;
 
     if (pending_ident) {
        char pit = pending_ident;
@@ -1481,7 +1553,7 @@ yylex(void)
            if (SvCUR(linestr))
                sv_catpv(linestr,";");
            if (preambleav){
-               while(AvFILL(preambleav) >= 0) {
+               while(AvFILLp(preambleav) >= 0) {
                    SV *tmpsv = av_shift(preambleav);
                    sv_catsv(linestr, tmpsv);
                    sv_catpv(linestr, ";");
@@ -1538,7 +1610,7 @@ yylex(void)
              fake_eof:
                if (rsfp) {
                    if (preprocess && !in_eval)
-                       (void)my_pclose(rsfp);
+                       (void)PerlProc_pclose(rsfp);
                    else if ((PerlIO *)rsfp == PerlIO_stdin())
                        PerlIO_clearerr(rsfp);
                    else
@@ -1723,9 +1795,11 @@ yylex(void)
        }
        goto retry;
     case '\r':
+#ifndef WIN32CHEAT
        warn("Illegal character \\%03o (carriage return)", '\r');
        croak(
       "(Maybe you didn't strip carriage returns after a network transfer?)\n");
+#endif
     case ' ': case '\t': case '\f': case 013:
        s++;
        goto retry;
@@ -1990,8 +2064,13 @@ yylex(void)
                else
                    lex_brackstack[lex_brackets++] = XOPERATOR;
                s = skipspace(s);
-               if (*s == '}')
+               if (*s == '}') {
+                   if (expect == XSTATE) {
+                       lex_brackstack[lex_brackets-1] = XSTATE;
+                       break;
+                   }
                    OPERATOR(HASHBRACK);
+               }
                /* This hack serves to disambiguate a pair of curlies
                 * as being a block or an anon hash.  Normally, expectation
                 * determines that, but in cases where we're not in a
@@ -2524,8 +2603,8 @@ yylex(void)
     case 'z': case 'Z':
 
       keylookup: {
-       GV *gv = Nullgv;
-       GV **gvp = 0;
+       gv = Nullgv;
+       gvp = 0;
 
        bufptr = s;
        s = scan_word(s, tokenbuf, sizeof tokenbuf, FALSE, &len);
@@ -3519,7 +3598,7 @@ yylex(void)
            if (*s == ';' || *s == ')')         /* probably a close */
                croak("sort is now a reserved word");
            expect = XTERM;
-           s = force_word(s,WORD,TRUE,TRUE,TRUE);
+           s = force_word(s,WORD,TRUE,TRUE,FALSE);
            LOP(OP_SORT,XREF);
 
        case KEY_split:
@@ -4812,6 +4891,8 @@ scan_heredoc(register char *s)
        }
        sv_setpvn(tmpstr,d+1,s-d);
        s += len - 1;
+       curcop->cop_line++;     /* the preceding stmt passes a newline */
+
        sv_catpvn(herewas,s,bufend-s);
        sv_setsv(linestr,herewas);
        oldoldbufptr = oldbufptr = bufptr = s = linestart = SvPVX(linestr);
@@ -5360,3 +5441,4 @@ yyerror(char *s)
     return 0;
 }
 
+