Slightly more refined lock() keyword recognition (using %INC).
[p5sagit/p5-mst-13.2.git] / toke.c
diff --git a/toke.c b/toke.c
index a007fa4..7a37b7d 100644 (file)
--- a/toke.c
+++ b/toke.c
@@ -66,6 +66,8 @@ static struct {
  * can get by with a single comparison (if the compiler is smart enough).
  */
 
+/* #define LEX_NOTPARSING              11 is done in perl.h. */
+
 #define LEX_NORMAL             10
 #define LEX_INTERPNORMAL        9
 #define LEX_INTERPCASEMOD       8
@@ -226,6 +228,7 @@ void
 lex_start(line)
 SV *line;
 {
+    dTHR;
     char *s;
     STRLEN len;
 
@@ -309,6 +312,7 @@ static void
 incline(s)
 char *s;
 {
+    dTHR;
     char *t;
     char *n;
     char ch;
@@ -370,7 +374,9 @@ register char *s;
            return s;
        if ((s = filter_gets(linestr, rsfp, (prevlen = SvCUR(linestr)))) == Nullch) {
            if (minus_n || minus_p) {
-               sv_setpv(linestr,minus_p ? ";}continue{print" : "");
+               sv_setpv(linestr,minus_p ?
+                        ";}continue{print or die qq(-p destination: $!\\n)" :
+                        "");
                sv_catpv(linestr,";}");
                minus_n = minus_p = 0;
            }
@@ -384,6 +390,8 @@ register char *s;
                PerlIO_clearerr(rsfp);
            else
                (void)PerlIO_close(rsfp);
+           if (e_fp == rsfp)
+               e_fp = Nullfp;
            rsfp = Nullfp;
            return s;
        }
@@ -391,7 +399,7 @@ register char *s;
        bufend = s + SvCUR(linestr);
        s = bufptr;
        incline(s);
-       if (perldb && curstash != debstash) {
+       if (PERLDB_LINE && curstash != debstash) {
            SV *sv = NEWSV(85,0);
 
            sv_upgrade(sv, SVt_PVMG);
@@ -459,6 +467,7 @@ expectation x;
 char *s;
 #endif /* CAN_PROTOTYPE */
 {
+    dTHR;
     yylval.ival = f;
     CLINE;
     expect = x;
@@ -651,6 +660,7 @@ sublex_start()
 static I32
 sublex_push()
 {
+    dTHR;
     push_scope();
 
     lex_state = sublex_info.super_state;
@@ -755,7 +765,7 @@ char *start;
     register char *d = SvPVX(sv);
     bool dorange = FALSE;
     I32 len;
-    char *leave =
+    char *leaveit =
        lex_inpat
            ? "\\.^$@AGZdDwWsSbB+*?|()-nrtfeaxc0123456789[{]} \t\n\r\f\v#"
            : (lex_inwhat & OP_TRANS)
@@ -801,7 +811,7 @@ char *start;
        }
        if (*s == '\\' && s+1 < send) {
            s++;
-           if (*s && strchr(leave, *s)) {
+           if (*s && strchr(leaveit, *s)) {
                *d++ = '\\';
                *d++ = *s++;
                continue;
@@ -1246,27 +1256,39 @@ yylex()
            return PRIVATEREF;
        }
 
-       if (!strchr(tokenbuf,':')
-           && (tmp = pad_findmy(tokenbuf)) != NOT_IN_PAD) {
-           if (last_lop_op == OP_SORT &&
-               tokenbuf[0] == '$' &&
-               (tokenbuf[1] == 'a' || tokenbuf[1] == 'b')
-               && !tokenbuf[2])
+       if (!strchr(tokenbuf,':')) {
+#ifdef USE_THREADS
+           /* Check for single character per-thread magicals */
+           if (tokenbuf[0] == '$' && tokenbuf[2] == '\0'
+               && !isALPHA(tokenbuf[1]) /* Rule out obvious non-magicals */
+               && (tmp = find_thread_magical(&tokenbuf[1])) != NOT_IN_PAD)
            {
-               for (d = in_eval ? oldoldbufptr : linestart;
-                    d < bufend && *d != '\n';
-                    d++)
+               yylval.opval = newOP(OP_SPECIFIC, 0);
+               yylval.opval->op_targ = tmp;
+               return PRIVATEREF;
+           }
+#endif /* USE_THREADS */
+           if ((tmp = pad_findmy(tokenbuf)) != NOT_IN_PAD) {
+               if (last_lop_op == OP_SORT &&
+                   tokenbuf[0] == '$' &&
+                   (tokenbuf[1] == 'a' || tokenbuf[1] == 'b')
+                   && !tokenbuf[2])
                {
-                   if (strnEQ(d,"<=>",3) || strnEQ(d,"cmp",3)) {
-                       croak("Can't use \"my %s\" in sort comparison",
-                             tokenbuf);
+                   for (d = in_eval ? oldoldbufptr : linestart;
+                        d < bufend && *d != '\n';
+                        d++)
+                   {
+                       if (strnEQ(d,"<=>",3) || strnEQ(d,"cmp",3)) {
+                           croak("Can't use \"my %s\" in sort comparison",
+                                 tokenbuf);
+                       }
                    }
                }
-           }
 
-           yylval.opval = newOP(OP_PADANY, 0);
-           yylval.opval->op_targ = tmp;
-           return PRIVATEREF;
+               yylval.opval = newOP(OP_PADANY, 0);
+               yylval.opval->op_targ = tmp;
+               return PRIVATEREF;
+           }
        }
 
        /* Force them to make up their mind on "@foo". */
@@ -1381,7 +1403,13 @@ yylex()
        if (lex_dojoin) {
            nextval[nexttoke].ival = 0;
            force_next(',');
+#ifdef USE_THREADS
+           nextval[nexttoke].opval = newOP(OP_SPECIFIC, 0);
+           nextval[nexttoke].opval->op_targ = find_thread_magical("\"");
+           force_next(PRIVATEREF);
+#else
            force_ident("\"", '$');
+#endif /* USE_THREADS */
            nextval[nexttoke].ival = 0;
            force_next('$');
            nextval[nexttoke].ival = 0;
@@ -1528,7 +1556,7 @@ yylex()
            sv_catpv(linestr, "\n");
            oldoldbufptr = oldbufptr = s = linestart = SvPVX(linestr);
            bufend = SvPVX(linestr) + SvCUR(linestr);
-           if (perldb && curstash != debstash) {
+           if (PERLDB_LINE && curstash != debstash) {
                SV *sv = NEWSV(85,0);
 
                sv_upgrade(sv, SVt_PVMG);
@@ -1547,6 +1575,8 @@ yylex()
                        PerlIO_clearerr(rsfp);
                    else
                        (void)PerlIO_close(rsfp);
+                   if (e_fp == rsfp)
+                       e_fp = Nullfp;
                    rsfp = Nullfp;
                }
                if (!in_eval && (minus_n || minus_p)) {
@@ -1576,7 +1606,7 @@ yylex()
            incline(s);
        } while (doextract);
        oldoldbufptr = oldbufptr = bufptr = linestart = s;
-       if (perldb && curstash != debstash) {
+       if (PERLDB_LINE && curstash != debstash) {
            SV *sv = NEWSV(85,0);
 
            sv_upgrade(sv, SVt_PVMG);
@@ -1701,7 +1731,7 @@ yylex()
                            }
                            d = moreswitches(d);
                        } while (d);
-                       if (perldb && !oldpdb ||
+                       if (PERLDB_LINE && !oldpdb ||
                            ( minus_n || minus_p ) && !(oldn || oldp) )
                              /* if we have already added "LINE: while (<>) {",
                                 we must not do it again */
@@ -1710,7 +1740,7 @@ yylex()
                            oldoldbufptr = oldbufptr = s = linestart = SvPVX(linestr);
                            bufend = SvPVX(linestr) + SvCUR(linestr);
                            preambled = FALSE;
-                           if (perldb)
+                           if (PERLDB_LINE)
                                (void)gv_fetchfile(origfilename);
                            goto retry;
                        }
@@ -1994,19 +2024,73 @@ yylex()
                s = skipspace(s);
                if (*s == '}')
                    OPERATOR(HASHBRACK);
-               if (isALPHA(*s)) {
-                   for (t = s; t < bufend && isALNUM(*t); t++) ;
+               /* 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
+                * position to expect anything in particular (like inside
+                * eval"") we have to resolve the ambiguity.  This code
+                * covers the case where the first term in the curlies is a
+                * quoted string.  Most other cases need to be explicitly
+                * disambiguated by prepending a `+' before the opening
+                * curly in order to force resolution as an anon hash.
+                *
+                * XXX should probably propagate the outer expectation
+                * into eval"" to rely less on this hack, but that could
+                * potentially break current behavior of eval"".
+                * GSAR 97-07-21
+                */
+               t = s;
+               if (*s == '\'' || *s == '"' || *s == '`') {
+                   /* common case: get past first string, handling escapes */
+                   for (t++; t < bufend && *t != *s;)
+                       if (*t++ == '\\' && (*t == '\\' || *t == *s))
+                           t++;
+                   t++;
+               }
+               else if (*s == 'q') {
+                   if (++t < bufend
+                       && (!isALNUM(*t)
+                           || ((*t == 'q' || *t == 'x') && ++t < bufend
+                               && !isALNUM(*t)))) {
+                       char *tmps;
+                       char open, close, term;
+                       I32 brackets = 1;
+
+                       while (t < bufend && isSPACE(*t))
+                           t++;
+                       term = *t;
+                       open = term;
+                       if (term && (tmps = strchr("([{< )]}> )]}>",term)))
+                           term = tmps[5];
+                       close = term;
+                       if (open == close)
+                           for (t++; t < bufend; t++) {
+                               if (*t == '\\' && t+1 < bufend && open != '\\')
+                                   t++;
+                               else if (*t == open)
+                                   break;
+                           }
+                       else
+                           for (t++; t < bufend; t++) {
+                               if (*t == '\\' && t+1 < bufend)
+                                   t++;
+                               else if (*t == close && --brackets <= 0)
+                                   break;
+                               else if (*t == open)
+                                   brackets++;
+                           }
+                   }
+                   t++;
                }
-               else if (*s == '\'' || *s == '"') {
-                   t = strchr(s+1,*s);
-                   if (!t++)
-                       t = s;
+               else if (isALPHA(*s)) {
+                   for (t++; t < bufend && isALNUM(*t); t++) ;
                }
-               else
-                   t = s;
                while (t < bufend && isSPACE(*t))
                    t++;
-               if ((*t == ',' && !isLOWER(*s)) || (*t == '=' && t[1] == '>'))
+               /* if comma follows first term, call it an anon hash */
+               /* XXX it could be a comma expression with loop modifiers */
+               if (t < bufend && ((*t == ',' && (*s == 'q' || !isLOWER(*s)))
+                                  || (*t == '=' && t[1] == '>')))
                    OPERATOR(HASHBRACK);
                if (expect == XREF)
                    expect = XTERM;
@@ -2264,8 +2348,23 @@ yylex()
            else if (isIDFIRST(*s)) {
                char tmpbuf[sizeof tokenbuf];
                scan_word(s, tmpbuf, sizeof tmpbuf, TRUE, &len);
-               if (keyword(tmpbuf, len))
-                   expect = XTERM;     /* e.g. print $fh length() */
+               if (tmp = keyword(tmpbuf, len)) {
+                   /* binary operators exclude handle interpretations */
+                   switch (tmp) {
+                   case -KEY_x:
+                   case -KEY_eq:
+                   case -KEY_ne:
+                   case -KEY_gt:
+                   case -KEY_lt:
+                   case -KEY_ge:
+                   case -KEY_le:
+                   case -KEY_cmp:
+                       break;
+                   default:
+                       expect = XTERM; /* e.g. print $fh length() */
+                       break;
+                   }
+               }
                else {
                    GV *gv = gv_fetchpv(tmpbuf, FALSE, SVt_PVCV);
                    if (gv && GvCVu(gv))
@@ -2452,7 +2551,10 @@ yylex()
     case 'y': case 'Y':
     case 'z': case 'Z':
 
-      keylookup:
+      keylookup: {
+       GV *gv = Nullgv;
+       GV **gvp = 0;
+
        bufptr = s;
        s = scan_word(s, tokenbuf, sizeof tokenbuf, FALSE, &len);
 
@@ -2494,16 +2596,24 @@ yylex()
        }
 
        if (tmp < 0) {                  /* second-class keyword? */
-           GV* gv;
-           if (expect != XOPERATOR &&
-               (*s != ':' || s[1] != ':') &&
-               (gv = gv_fetchpv(tokenbuf, FALSE, SVt_PVCV)) &&
-               GvIMPORTED_CV(gv))
+           if (expect != XOPERATOR && (*s != ':' || s[1] != ':') &&
+               (((gv = gv_fetchpv(tokenbuf, FALSE, SVt_PVCV)) &&
+                 GvCVu(gv) && GvIMPORTED_CV(gv)) ||
+                ((gvp = (GV**)hv_fetch(globalstash,tokenbuf,len,FALSE)) &&
+                 (gv = *gvp) != (GV*)&sv_undef &&
+                 GvCVu(gv) && GvIMPORTED_CV(gv))))
            {
-               tmp = 0;
+               tmp = 0;                /* overridden by importation */
+           }
+           else if (gv && !gvp
+                    && -tmp==KEY_lock  /* XXX generalizable kludge */
+                    && !hv_fetch(GvHVn(incgv), "Thread.pm", 9, FALSE))
+           {
+               tmp = 0;                /* any sub overrides "weak" keyword */
+           }
+           else {
+               tmp = -tmp; gv = Nullgv; gvp = 0;
            }
-           else
-               tmp = -tmp;
        }
 
       reserved_word:
@@ -2511,7 +2621,6 @@ yylex()
 
        default:                        /* not a keyword */
          just_a_word: {
-               GV *gv;
                SV *sv;
                char lastchar = (bufptr == oldoldbufptr ? 0 : bufptr[-1]);
 
@@ -2536,12 +2645,19 @@ yylex()
 
                /* Look for a subroutine with this name in current package. */
 
-               gv = gv_fetchpv(tokenbuf,FALSE, SVt_PVCV);
+               if (gvp) {
+                   sv = newSVpv("CORE::GLOBAL::",14);
+                   sv_catpv(sv,tokenbuf);
+               }
+               else
+                   sv = newSVpv(tokenbuf,0);
+               if (!gv)
+                   gv = gv_fetchpv(tokenbuf,FALSE, SVt_PVCV);
 
                /* Presume this is going to be a bareword of some sort. */
 
                CLINE;
-               yylval.opval = (OP*)newSVOP(OP_CONST, 0, newSVpv(tokenbuf,0));
+               yylval.opval = (OP*)newSVOP(OP_CONST, 0, sv);
                yylval.opval->op_private = OPpCONST_BARE;
 
                /* See if it's the indirect object for a list operator. */
@@ -2735,7 +2851,7 @@ yylex()
        case KEY_DESTROY:
        case KEY_BEGIN:
        case KEY_END:
-       case KEY_RESTART:
+       case KEY_INIT:
            if (expect == XSTATE) {
                s = bufptr;
                goto really_sub;
@@ -3098,6 +3214,9 @@ yylex()
        case KEY_listen:
            LOP(OP_LISTEN,XTERM);
 
+       case KEY_lock:
+           UNI(OP_LOCK);
+
        case KEY_lstat:
            UNI(OP_LSTAT);
 
@@ -3674,7 +3793,7 @@ yylex()
            s = scan_trans(s);
            TERM(sublex_start());
        }
-    }
+    }}
 }
 
 I32
@@ -3914,7 +4033,7 @@ I32 len;
        case 4:
            if (strEQ(d,"grep"))                return KEY_grep;
            if (strEQ(d,"goto"))                return KEY_goto;
-           if (strEQ(d,"glob"))                return -KEY_glob;
+           if (strEQ(d,"glob"))                return KEY_glob;
            break;
        case 6:
            if (strEQ(d,"gmtime"))              return -KEY_gmtime;
@@ -3924,6 +4043,9 @@ I32 len;
     case 'h':
        if (strEQ(d,"hex"))                     return -KEY_hex;
        break;
+    case 'I':
+       if (strEQ(d,"INIT"))                    return KEY_INIT;
+       break;
     case 'i':
        switch (len) {
        case 2:
@@ -3966,6 +4088,7 @@ I32 len;
        case 4:
            if (strEQ(d,"last"))                return KEY_last;
            if (strEQ(d,"link"))                return -KEY_link;
+           if (strEQ(d,"lock"))                return -KEY_lock;
            break;
        case 5:
            if (strEQ(d,"local"))               return KEY_local;
@@ -4062,9 +4185,6 @@ I32 len;
        }
        else if (strEQ(d,"quotemeta"))          return -KEY_quotemeta;
        break;
-    case 'R':
-       if (strEQ(d,"RESTART"))                 return KEY_RESTART;
-       break;
     case 'r':
        switch (len) {
        case 3:
@@ -4312,7 +4432,7 @@ char *what;
        }
        if (*w)
            for (; *w && isSPACE(*w); w++) ;
-       if (!*w || !strchr(";|})]oa!=", *w))    /* an advisory hack only... */
+       if (!*w || !strchr(";|})]oaiuw!=", *w)) /* an advisory hack only... */
            warn("%s (...) interpreted as function",name);
     }
     while (s < bufend && isSPACE(*s))
@@ -4478,7 +4598,7 @@ I32 ck_uni;
                lex_state = LEX_INTERPEND;
            if (funny == '#')
                funny = '@';
-           if (dowarn &&
+           if (dowarn && lex_state == LEX_NORMAL &&
              (keyword(dest, d - dest) || perl_get_cv(dest, FALSE)))
                warn("Ambiguous use of %c{%s} resolved to %c%s",
                    funny, dest, funny, dest);
@@ -4796,7 +4916,7 @@ register char *s;
            missingterm(tokenbuf);
        }
        curcop->cop_line++;
-       if (perldb && curstash != debstash) {
+       if (PERLDB_LINE && curstash != debstash) {
            SV *sv = NEWSV(88,0);
 
            sv_upgrade(sv, SVt_PVMG);
@@ -4934,13 +5054,13 @@ char *start;
            for (; s < bufend; s++,to++) {
                if (*s == '\n' && !rsfp)
                    curcop->cop_line++;
-               if (*s == '\\' && s+1 < bufend && term != '\\') {
-                   if (s[1] == term)
+               if (*s == '\\' && s+1 < bufend) {
+                   if ((s[1] == multi_open) || (s[1] == multi_close))
                        s++;
                    else
                        *to++ = *s++;
                }
-               else if (*s == term && --brackets <= 0)
+               else if (*s == multi_close && --brackets <= 0)
                    break;
                else if (*s == multi_open)
                    brackets++;
@@ -4959,7 +5079,7 @@ char *start;
            return Nullch;
        }
        curcop->cop_line++;
-       if (perldb && curstash != debstash) {
+       if (PERLDB_LINE && curstash != debstash) {
            SV *sv = NEWSV(88,0);
 
            sv_upgrade(sv, SVt_PVMG);
@@ -5221,16 +5341,21 @@ U32 flags;
     CvFLAGS(compcv) |= flags;
 
     comppad = newAV();
+    av_push(comppad, Nullsv);
+    curpad = AvARRAY(comppad);
     comppad_name = newAV();
     comppad_name_fill = 0;
-#ifdef USE_THREADS
-    av_store(comppad_name, 0, newSVpv("@_", 2));
-#endif /* USE_THREADS */
     min_intro_pending = 0;
-    av_push(comppad, Nullsv);
-    curpad = AvARRAY(comppad);
     padix = 0;
     subline = curcop->cop_line;
+#ifdef USE_THREADS
+    av_store(comppad_name, 0, newSVpv("@_", 2));
+    curpad[0] = (SV*)newAV();
+    SvPADMY_on(curpad[0]);     /* XXX Needed? */
+    CvOWNER(compcv) = 0;
+    New(666, CvMUTEXP(compcv), 1, perl_mutex);
+    MUTEX_INIT(CvMUTEXP(compcv));
+#endif /* USE_THREADS */
 
     comppadlist = newAV();
     AvREAL_off(comppadlist);
@@ -5238,13 +5363,11 @@ U32 flags;
     av_store(comppadlist, 1, (SV*)comppad);
 
     CvPADLIST(compcv) = comppadlist;
-    CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)outsidecv);
+    CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(outsidecv);
 #ifdef USE_THREADS
     CvOWNER(compcv) = 0;
-    New(666, CvMUTEXP(compcv), 1, pthread_mutex_t);
+    New(666, CvMUTEXP(compcv), 1, perl_mutex);
     MUTEX_INIT(CvMUTEXP(compcv));
-    New(666, CvCONDP(compcv), 1, pthread_cond_t);
-    COND_INIT(CvCONDP(compcv));
 #endif /* USE_THREADS */
 
     return oldsavestack_ix;
@@ -5325,7 +5448,7 @@ char *s;
     if (in_eval & 2)
        warn("%_", msg);
     else if (in_eval)
-       sv_catsv(GvSV(errgv), msg);
+       sv_catsv(errsv, msg);
     else
        PerlIO_write(PerlIO_stderr(), SvPVX(msg), SvCUR(msg));
     if (++error_count >= 10)