1 /* stolen_chunk_of_toke.c - from perl 5.8.8 toke.c
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "It all comes from here, the stench and the peril." --Frodo
16 * this is all blatantly stolen. I sincerely hopes it doesn't fuck anything
17 * up but if it does blame me (Matt S Trout), not the poor original authors
20 /* the following #defines are stolen from assorted headers, not toke.c */
22 #define DPTR2FPTR(t,p) ((t)PTR2nat(p)) /* data pointer to function pointer */
23 #define FPTR2DPTR(t,p) ((t)PTR2nat(p)) /* function pointer to data pointer */
24 #define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
25 #define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))))
28 #define LEX_NORMAL 10 /* normal code (ie not within "...") */
29 #define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */
30 #define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */
31 #define LEX_INTERPPUSH 7 /* starting a new sublex parse level */
32 #define LEX_INTERPSTART 6 /* expecting the start of a $var */
34 /* at end of code, eg "$x" followed by: */
35 #define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */
36 #define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */
38 #define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of
39 string or after \E, $foo, etc */
40 #define LEX_INTERPCONST 2 /* NOT USED */
41 #define LEX_FORMLINE 1 /* expecting a format line */
42 #define LEX_KNOWNEXT 0 /* next token known; just return it */
44 static const char ident_too_long[] =
45 "Identifier too long";
46 static const char c_without_g[] =
47 "Use of /c modifier is meaningless without /g";
48 static const char c_in_subst[] =
49 "Use of /c modifier is meaningless in s///";
51 #ifdef USE_UTF8_SCRIPTS
52 # define UTF (!IN_BYTES)
54 # define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8))
57 /* Invoke the idxth filter function for the current rsfp. */
58 /* maxlen 0 = read one text line */
60 Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen)
67 if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */
68 /* Provide a default input filter to make life easy. */
69 /* Note that we append to the line. This is handy. */
70 DEBUG_P(PerlIO_printf(Perl_debug_log,
71 "filter_read %d: from rsfp\n", idx));
75 const int old_len = SvCUR(buf_sv);
77 /* ensure buf_sv is large enough */
78 SvGROW(buf_sv, (STRLEN)(old_len + maxlen)) ;
79 if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len, maxlen)) <= 0){
80 if (PerlIO_error(PL_rsfp))
81 return -1; /* error */
83 return 0 ; /* end of file */
85 SvCUR_set(buf_sv, old_len + len) ;
88 if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) {
89 if (PerlIO_error(PL_rsfp))
90 return -1; /* error */
92 return 0 ; /* end of file */
97 /* Skip this filter slot if filter has been deleted */
98 if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) {
99 DEBUG_P(PerlIO_printf(Perl_debug_log,
100 "filter_read %d: skipped (filter deleted)\n",
102 return FILTER_READ(idx+1, buf_sv, maxlen); /* recurse */
104 /* Get function pointer hidden within datasv */
105 funcp = DPTR2FPTR(filter_t, IoANY(datasv));
106 DEBUG_P(PerlIO_printf(Perl_debug_log,
107 "filter_read %d: via function %p (%s)\n",
108 idx, datasv, SvPV_nolen_const(datasv)));
109 /* Call function. The function is expected to */
110 /* call "FILTER_READ(idx+1, buf_sv)" first. */
111 /* Return: <0:error, =0:eof, >0:not eof */
112 return (*funcp)(aTHX_ idx, buf_sv, maxlen);
116 S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append)
118 #ifdef PERL_CR_FILTER
119 if (!PL_rsfp_filters) {
120 filter_add(S_cr_textfilter,NULL);
123 if (PL_rsfp_filters) {
125 SvCUR_set(sv, 0); /* start with empty line */
126 if (FILTER_READ(0, sv, 0) > 0)
127 return ( SvPVX(sv) ) ;
132 return (sv_gets(sv, fp, append));
137 * Called to gobble the appropriate amount and type of whitespace.
138 * Skips comments as well.
142 S_skipspace(pTHX_ register char *s)
144 if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
145 while (s < PL_bufend && SPACE_OR_TAB(*s))
151 SSize_t oldprevlen, oldoldprevlen;
152 SSize_t oldloplen = 0, oldunilen = 0;
153 while (s < PL_bufend && isSPACE(*s)) {
154 if (*s++ == '\n' && PL_in_eval && !PL_rsfp)
159 if (s < PL_bufend && *s == '#') {
160 while (s < PL_bufend && *s != '\n')
164 if (PL_in_eval && !PL_rsfp) {
171 /* only continue to recharge the buffer if we're at the end
172 * of the buffer, we're not reading from a source filter, and
173 * we're in normal lexing mode
175 if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat ||
176 PL_lex_state == LEX_FORMLINE)
179 /* try to recharge the buffer */
180 if ((s = filter_gets(PL_linestr, PL_rsfp,
181 (prevlen = SvCUR(PL_linestr)))) == Nullch)
183 /* end of file. Add on the -p or -n magic */
186 ";}continue{print or die qq(-p destination: $!\\n);}");
187 PL_minus_n = PL_minus_p = 0;
189 else if (PL_minus_n) {
190 sv_setpvn(PL_linestr, ";}", 2);
194 sv_setpvn(PL_linestr,";", 1);
196 /* reset variables for next time we lex */
197 PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart
199 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
200 PL_last_lop = PL_last_uni = Nullch;
202 /* Close the filehandle. Could be from -P preprocessor,
203 * STDIN, or a regular file. If we were reading code from
204 * STDIN (because the commandline held no -e or filename)
205 * then we don't close it, we reset it so the code can
206 * read from STDIN too.
209 if (PL_preprocess && !PL_in_eval)
210 (void)PerlProc_pclose(PL_rsfp);
211 else if ((PerlIO*)PL_rsfp == PerlIO_stdin())
212 PerlIO_clearerr(PL_rsfp);
214 (void)PerlIO_close(PL_rsfp);
219 /* not at end of file, so we only read another line */
220 /* make corresponding updates to old pointers, for yyerror() */
221 oldprevlen = PL_oldbufptr - PL_bufend;
222 oldoldprevlen = PL_oldoldbufptr - PL_bufend;
224 oldunilen = PL_last_uni - PL_bufend;
226 oldloplen = PL_last_lop - PL_bufend;
227 PL_linestart = PL_bufptr = s + prevlen;
228 PL_bufend = s + SvCUR(PL_linestr);
230 PL_oldbufptr = s + oldprevlen;
231 PL_oldoldbufptr = s + oldoldprevlen;
233 PL_last_uni = s + oldunilen;
235 PL_last_lop = s + oldloplen;
238 /* debugger active and we're not compiling the debugger code,
239 * so store the line into the debugger's array of lines
241 if (PERLDB_LINE && PL_curstash != PL_debstash) {
242 SV * const sv = NEWSV(85,0);
244 sv_upgrade(sv, SVt_PVMG);
245 sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
248 av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
254 S_scan_word(pTHX_ register char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp)
256 register char *d = dest;
257 register char * const e = d + destlen - 3; /* two-character token, ending NUL */
260 Perl_croak(aTHX_ ident_too_long);
261 if (isALNUM(*s)) /* UTF handled below */
263 else if (*s == '\'' && allow_package && isIDFIRST_lazy_if(s+1,UTF)) {
268 else if (*s == ':' && s[1] == ':' && allow_package && s[2] != '$') {
272 else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) {
273 char *t = s + UTF8SKIP(s);
274 while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t))
277 Perl_croak(aTHX_ ident_too_long);
278 Copy(s, d, t - s, char);
292 * This subroutine has nothing to do with tilting, whether at windmills
293 * or pinball tables. Its name is short for "increment line". It
294 * increments the current line number in CopLINE(PL_curcop) and checks
295 * to see whether the line starts with a comment of the form
296 * # line 500 "foo.pm"
297 * If so, it sets the current line number and file to the values in the comment.
301 S_incline(pTHX_ char *s)
308 CopLINE_inc(PL_curcop);
311 while (SPACE_OR_TAB(*s)) s++;
312 if (strnEQ(s, "line", 4))
316 if (SPACE_OR_TAB(*s))
320 while (SPACE_OR_TAB(*s)) s++;
326 while (SPACE_OR_TAB(*s))
328 if (*s == '"' && (t = strchr(s+1, '"'))) {
333 for (t = s; !isSPACE(*t); t++) ;
336 while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
338 if (*e != '\n' && *e != '\0')
339 return; /* false alarm */
345 const char *cf = CopFILE(PL_curcop);
346 if (cf && strlen(cf) > 7 && strnEQ(cf, "(eval ", 6)) {
347 /* must copy *{"::_<(eval N)[oldfilename:L]"}
348 * to *{"::_<newfilename"} */
349 char smallbuf[256], smallbuf2[256];
350 char *tmpbuf, *tmpbuf2;
352 STRLEN tmplen = strlen(cf);
353 STRLEN tmplen2 = strlen(s);
354 if (tmplen + 3 < sizeof smallbuf)
357 Newx(tmpbuf, tmplen + 3, char);
358 if (tmplen2 + 3 < sizeof smallbuf2)
361 Newx(tmpbuf2, tmplen2 + 3, char);
362 tmpbuf[0] = tmpbuf2[0] = '_';
363 tmpbuf[1] = tmpbuf2[1] = '<';
364 memcpy(tmpbuf + 2, cf, ++tmplen);
365 memcpy(tmpbuf2 + 2, s, ++tmplen2);
367 gvp = (GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, FALSE);
369 gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE);
371 gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE);
372 /* adjust ${"::_<newfilename"} to store the new file name */
373 GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2);
374 GvHV(gv2) = (HV*)SvREFCNT_inc(GvHV(*gvp));
375 GvAV(gv2) = (AV*)SvREFCNT_inc(GvAV(*gvp));
377 if (tmpbuf != smallbuf) Safefree(tmpbuf);
378 if (tmpbuf2 != smallbuf2) Safefree(tmpbuf2);
381 CopFILE_free(PL_curcop);
382 CopFILE_set(PL_curcop, s);
385 CopLINE_set(PL_curcop, atoi(n)-1);