/* toke.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'It all comes from here, the stench and the peril.' --Frodo
*
* [p.719 of _The Lord of the Rings_, IV/ix: "Shelob's Lair"]
*/
/*
* This file is the lexer for Perl. It's closely linked to the
* parser, perly.y.
*
* The main routine is yylex(), which returns the next token.
*/
/*
=head1 Lexer interface
This is the lower layer of the Perl parser, managing characters and tokens.
=for apidoc AmU|yy_parser *|PL_parser
Pointer to a structure encapsulating the state of the parsing operation
currently in progress. The pointer can be locally changed to perform
a nested parse without interfering with the state of an outer parse.
Individual members of C have their own documentation.
=cut
*/
#include "EXTERN.h"
#define PERL_IN_TOKE_C
#include "perl.h"
#include "dquote_static.c"
#define new_constant(a,b,c,d,e,f,g) \
S_new_constant(aTHX_ a,b,STR_WITH_LEN(c),d,e,f, g)
#define pl_yylval (PL_parser->yylval)
/* XXX temporary backwards compatibility */
#define PL_lex_brackets (PL_parser->lex_brackets)
#define PL_lex_brackstack (PL_parser->lex_brackstack)
#define PL_lex_casemods (PL_parser->lex_casemods)
#define PL_lex_casestack (PL_parser->lex_casestack)
#define PL_lex_defer (PL_parser->lex_defer)
#define PL_lex_dojoin (PL_parser->lex_dojoin)
#define PL_lex_expect (PL_parser->lex_expect)
#define PL_lex_formbrack (PL_parser->lex_formbrack)
#define PL_lex_inpat (PL_parser->lex_inpat)
#define PL_lex_inwhat (PL_parser->lex_inwhat)
#define PL_lex_op (PL_parser->lex_op)
#define PL_lex_repl (PL_parser->lex_repl)
#define PL_lex_starts (PL_parser->lex_starts)
#define PL_lex_stuff (PL_parser->lex_stuff)
#define PL_multi_start (PL_parser->multi_start)
#define PL_multi_open (PL_parser->multi_open)
#define PL_multi_close (PL_parser->multi_close)
#define PL_pending_ident (PL_parser->pending_ident)
#define PL_preambled (PL_parser->preambled)
#define PL_sublex_info (PL_parser->sublex_info)
#define PL_linestr (PL_parser->linestr)
#define PL_expect (PL_parser->expect)
#define PL_copline (PL_parser->copline)
#define PL_bufptr (PL_parser->bufptr)
#define PL_oldbufptr (PL_parser->oldbufptr)
#define PL_oldoldbufptr (PL_parser->oldoldbufptr)
#define PL_linestart (PL_parser->linestart)
#define PL_bufend (PL_parser->bufend)
#define PL_last_uni (PL_parser->last_uni)
#define PL_last_lop (PL_parser->last_lop)
#define PL_last_lop_op (PL_parser->last_lop_op)
#define PL_lex_state (PL_parser->lex_state)
#define PL_rsfp (PL_parser->rsfp)
#define PL_rsfp_filters (PL_parser->rsfp_filters)
#define PL_in_my (PL_parser->in_my)
#define PL_in_my_stash (PL_parser->in_my_stash)
#define PL_tokenbuf (PL_parser->tokenbuf)
#define PL_multi_end (PL_parser->multi_end)
#define PL_error_count (PL_parser->error_count)
#ifdef PERL_MAD
# define PL_endwhite (PL_parser->endwhite)
# define PL_faketokens (PL_parser->faketokens)
# define PL_lasttoke (PL_parser->lasttoke)
# define PL_nextwhite (PL_parser->nextwhite)
# define PL_realtokenstart (PL_parser->realtokenstart)
# define PL_skipwhite (PL_parser->skipwhite)
# define PL_thisclose (PL_parser->thisclose)
# define PL_thismad (PL_parser->thismad)
# define PL_thisopen (PL_parser->thisopen)
# define PL_thisstuff (PL_parser->thisstuff)
# define PL_thistoken (PL_parser->thistoken)
# define PL_thiswhite (PL_parser->thiswhite)
# define PL_thiswhite (PL_parser->thiswhite)
# define PL_nexttoke (PL_parser->nexttoke)
# define PL_curforce (PL_parser->curforce)
#else
# define PL_nexttoke (PL_parser->nexttoke)
# define PL_nexttype (PL_parser->nexttype)
# define PL_nextval (PL_parser->nextval)
#endif
/* This can't be done with embed.fnc, because struct yy_parser contains a
member named pending_ident, which clashes with the generated #define */
static int
S_pending_ident(pTHX);
static const char ident_too_long[] = "Identifier too long";
#ifdef PERL_MAD
# define CURMAD(slot,sv) if (PL_madskills) { curmad(slot,sv); sv = 0; }
# define NEXTVAL_NEXTTOKE PL_nexttoke[PL_curforce].next_val
#else
# define CURMAD(slot,sv)
# define NEXTVAL_NEXTTOKE PL_nextval[PL_nexttoke]
#endif
#define XFAKEBRACK 128
#define XENUMMASK 127
#ifdef USE_UTF8_SCRIPTS
# define UTF (!IN_BYTES)
#else
# define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8))
#endif
/* The maximum number of characters preceding the unrecognized one to display */
#define UNRECOGNIZED_PRECEDE_COUNT 10
/* In variables named $^X, these are the legal values for X.
* 1999-02-27 mjd-perl-patch@plover.com */
#define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x)))
#define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t')
/* LEX_* are values for PL_lex_state, the state of the lexer.
* They are arranged oddly so that the guard on the switch statement
* 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 /* normal code (ie not within "...") */
#define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */
#define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */
#define LEX_INTERPPUSH 7 /* starting a new sublex parse level */
#define LEX_INTERPSTART 6 /* expecting the start of a $var */
/* at end of code, eg "$x" followed by: */
#define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */
#define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */
#define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of
string or after \E, $foo, etc */
#define LEX_INTERPCONST 2 /* NOT USED */
#define LEX_FORMLINE 1 /* expecting a format line */
#define LEX_KNOWNEXT 0 /* next token known; just return it */
#ifdef DEBUGGING
static const char* const lex_state_names[] = {
"KNOWNEXT",
"FORMLINE",
"INTERPCONST",
"INTERPCONCAT",
"INTERPENDMAYBE",
"INTERPEND",
"INTERPSTART",
"INTERPPUSH",
"INTERPCASEMOD",
"INTERPNORMAL",
"NORMAL"
};
#endif
#ifdef ff_next
#undef ff_next
#endif
#include "keywords.h"
/* CLINE is a macro that ensures PL_copline has a sane value */
#ifdef CLINE
#undef CLINE
#endif
#define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
#ifdef PERL_MAD
# define SKIPSPACE0(s) skipspace0(s)
# define SKIPSPACE1(s) skipspace1(s)
# define SKIPSPACE2(s,tsv) skipspace2(s,&tsv)
# define PEEKSPACE(s) skipspace2(s,0)
#else
# define SKIPSPACE0(s) skipspace(s)
# define SKIPSPACE1(s) skipspace(s)
# define SKIPSPACE2(s,tsv) skipspace(s)
# define PEEKSPACE(s) skipspace(s)
#endif
/*
* Convenience functions to return different tokens and prime the
* lexer for the next token. They all take an argument.
*
* TOKEN : generic token (used for '(', DOLSHARP, etc)
* OPERATOR : generic operator
* AOPERATOR : assignment operator
* PREBLOCK : beginning the block after an if, while, foreach, ...
* PRETERMBLOCK : beginning a non-code-defining {} block (eg, hash ref)
* PREREF : *EXPR where EXPR is not a simple identifier
* TERM : expression term
* LOOPX : loop exiting command (goto, last, dump, etc)
* FTST : file test operator
* FUN0 : zero-argument function
* FUN1 : not used, except for not, which isn't a UNIOP
* BOop : bitwise or or xor
* BAop : bitwise and
* SHop : shift operator
* PWop : power operator
* PMop : pattern-matching operator
* Aop : addition-level operator
* Mop : multiplication-level operator
* Eop : equality-testing operator
* Rop : relational operator <= != gt
*
* Also see LOP and lop() below.
*/
#ifdef DEBUGGING /* Serve -DT. */
# define REPORT(retval) tokereport((I32)retval, &pl_yylval)
#else
# define REPORT(retval) (retval)
#endif
#define TOKEN(retval) return ( PL_bufptr = s, REPORT(retval))
#define OPERATOR(retval) return (PL_expect = XTERM, PL_bufptr = s, REPORT(retval))
#define AOPERATOR(retval) return ao((PL_expect = XTERM, PL_bufptr = s, REPORT(retval)))
#define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s, REPORT(retval))
#define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s, REPORT(retval))
#define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s, REPORT(retval))
#define TERM(retval) return (CLINE, PL_expect = XOPERATOR, PL_bufptr = s, REPORT(retval))
#define LOOPX(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)LOOPEX))
#define FTST(f) return (pl_yylval.ival=f, PL_expect=XTERMORDORDOR, PL_bufptr=s, REPORT((int)UNIOP))
#define FUN0(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0))
#define FUN1(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC1))
#define BOop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITOROP)))
#define BAop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)BITANDOP)))
#define SHop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)SHIFTOP)))
#define PWop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)POWOP)))
#define PMop(f) return(pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MATCHOP))
#define Aop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)ADDOP)))
#define Mop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MULOP)))
#define Eop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)EQOP))
#define Rop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)RELOP))
/* This bit of chicanery makes a unary function followed by
* a parenthesis into a function with one argument, highest precedence.
* The UNIDOR macro is for unary functions that can be followed by the //
* operator (such as C).
*/
#define UNI2(f,x) { \
pl_yylval.ival = f; \
PL_expect = x; \
PL_bufptr = s; \
PL_last_uni = PL_oldbufptr; \
PL_last_lop_op = f; \
if (*s == '(') \
return REPORT( (int)FUNC1 ); \
s = PEEKSPACE(s); \
return REPORT( *s=='(' ? (int)FUNC1 : (int)UNIOP ); \
}
#define UNI(f) UNI2(f,XTERM)
#define UNIDOR(f) UNI2(f,XTERMORDORDOR)
#define UNIBRACK(f) { \
pl_yylval.ival = f; \
PL_bufptr = s; \
PL_last_uni = PL_oldbufptr; \
if (*s == '(') \
return REPORT( (int)FUNC1 ); \
s = PEEKSPACE(s); \
return REPORT( (*s == '(') ? (int)FUNC1 : (int)UNIOP ); \
}
/* grandfather return to old style */
#define OLDLOP(f) return(pl_yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)LSTOP)
#ifdef DEBUGGING
/* how to interpret the pl_yylval associated with the token */
enum token_type {
TOKENTYPE_NONE,
TOKENTYPE_IVAL,
TOKENTYPE_OPNUM, /* pl_yylval.ival contains an opcode number */
TOKENTYPE_PVAL,
TOKENTYPE_OPVAL,
TOKENTYPE_GVVAL
};
static struct debug_tokens {
const int token;
enum token_type type;
const char *name;
} const debug_tokens[] =
{
{ ADDOP, TOKENTYPE_OPNUM, "ADDOP" },
{ ANDAND, TOKENTYPE_NONE, "ANDAND" },
{ ANDOP, TOKENTYPE_NONE, "ANDOP" },
{ ANONSUB, TOKENTYPE_IVAL, "ANONSUB" },
{ ARROW, TOKENTYPE_NONE, "ARROW" },
{ ASSIGNOP, TOKENTYPE_OPNUM, "ASSIGNOP" },
{ BITANDOP, TOKENTYPE_OPNUM, "BITANDOP" },
{ BITOROP, TOKENTYPE_OPNUM, "BITOROP" },
{ COLONATTR, TOKENTYPE_NONE, "COLONATTR" },
{ CONTINUE, TOKENTYPE_NONE, "CONTINUE" },
{ DEFAULT, TOKENTYPE_NONE, "DEFAULT" },
{ DO, TOKENTYPE_NONE, "DO" },
{ DOLSHARP, TOKENTYPE_NONE, "DOLSHARP" },
{ DORDOR, TOKENTYPE_NONE, "DORDOR" },
{ DOROP, TOKENTYPE_OPNUM, "DOROP" },
{ DOTDOT, TOKENTYPE_IVAL, "DOTDOT" },
{ ELSE, TOKENTYPE_NONE, "ELSE" },
{ ELSIF, TOKENTYPE_IVAL, "ELSIF" },
{ EQOP, TOKENTYPE_OPNUM, "EQOP" },
{ FOR, TOKENTYPE_IVAL, "FOR" },
{ FORMAT, TOKENTYPE_NONE, "FORMAT" },
{ FUNC, TOKENTYPE_OPNUM, "FUNC" },
{ FUNC0, TOKENTYPE_OPNUM, "FUNC0" },
{ FUNC0SUB, TOKENTYPE_OPVAL, "FUNC0SUB" },
{ FUNC1, TOKENTYPE_OPNUM, "FUNC1" },
{ FUNCMETH, TOKENTYPE_OPVAL, "FUNCMETH" },
{ GIVEN, TOKENTYPE_IVAL, "GIVEN" },
{ HASHBRACK, TOKENTYPE_NONE, "HASHBRACK" },
{ IF, TOKENTYPE_IVAL, "IF" },
{ LABEL, TOKENTYPE_PVAL, "LABEL" },
{ LOCAL, TOKENTYPE_IVAL, "LOCAL" },
{ LOOPEX, TOKENTYPE_OPNUM, "LOOPEX" },
{ LSTOP, TOKENTYPE_OPNUM, "LSTOP" },
{ LSTOPSUB, TOKENTYPE_OPVAL, "LSTOPSUB" },
{ MATCHOP, TOKENTYPE_OPNUM, "MATCHOP" },
{ METHOD, TOKENTYPE_OPVAL, "METHOD" },
{ MULOP, TOKENTYPE_OPNUM, "MULOP" },
{ MY, TOKENTYPE_IVAL, "MY" },
{ MYSUB, TOKENTYPE_NONE, "MYSUB" },
{ NOAMP, TOKENTYPE_NONE, "NOAMP" },
{ NOTOP, TOKENTYPE_NONE, "NOTOP" },
{ OROP, TOKENTYPE_IVAL, "OROP" },
{ OROR, TOKENTYPE_NONE, "OROR" },
{ PACKAGE, TOKENTYPE_NONE, "PACKAGE" },
{ PLUGEXPR, TOKENTYPE_OPVAL, "PLUGEXPR" },
{ PLUGSTMT, TOKENTYPE_OPVAL, "PLUGSTMT" },
{ PMFUNC, TOKENTYPE_OPVAL, "PMFUNC" },
{ POSTDEC, TOKENTYPE_NONE, "POSTDEC" },
{ POSTINC, TOKENTYPE_NONE, "POSTINC" },
{ POWOP, TOKENTYPE_OPNUM, "POWOP" },
{ PREDEC, TOKENTYPE_NONE, "PREDEC" },
{ PREINC, TOKENTYPE_NONE, "PREINC" },
{ PRIVATEREF, TOKENTYPE_OPVAL, "PRIVATEREF" },
{ REFGEN, TOKENTYPE_NONE, "REFGEN" },
{ RELOP, TOKENTYPE_OPNUM, "RELOP" },
{ SHIFTOP, TOKENTYPE_OPNUM, "SHIFTOP" },
{ SUB, TOKENTYPE_NONE, "SUB" },
{ THING, TOKENTYPE_OPVAL, "THING" },
{ UMINUS, TOKENTYPE_NONE, "UMINUS" },
{ UNIOP, TOKENTYPE_OPNUM, "UNIOP" },
{ UNIOPSUB, TOKENTYPE_OPVAL, "UNIOPSUB" },
{ UNLESS, TOKENTYPE_IVAL, "UNLESS" },
{ UNTIL, TOKENTYPE_IVAL, "UNTIL" },
{ USE, TOKENTYPE_IVAL, "USE" },
{ WHEN, TOKENTYPE_IVAL, "WHEN" },
{ WHILE, TOKENTYPE_IVAL, "WHILE" },
{ WORD, TOKENTYPE_OPVAL, "WORD" },
{ YADAYADA, TOKENTYPE_IVAL, "YADAYADA" },
{ 0, TOKENTYPE_NONE, NULL }
};
/* dump the returned token in rv, plus any optional arg in pl_yylval */
STATIC int
S_tokereport(pTHX_ I32 rv, const YYSTYPE* lvalp)
{
dVAR;
PERL_ARGS_ASSERT_TOKEREPORT;
if (DEBUG_T_TEST) {
const char *name = NULL;
enum token_type type = TOKENTYPE_NONE;
const struct debug_tokens *p;
SV* const report = newSVpvs("<== ");
for (p = debug_tokens; p->token; p++) {
if (p->token == (int)rv) {
name = p->name;
type = p->type;
break;
}
}
if (name)
Perl_sv_catpv(aTHX_ report, name);
else if ((char)rv > ' ' && (char)rv < '~')
Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
else if (!rv)
sv_catpvs(report, "EOF");
else
Perl_sv_catpvf(aTHX_ report, "?? %"IVdf, (IV)rv);
switch (type) {
case TOKENTYPE_NONE:
case TOKENTYPE_GVVAL: /* doesn't appear to be used */
break;
case TOKENTYPE_IVAL:
Perl_sv_catpvf(aTHX_ report, "(ival=%"IVdf")", (IV)lvalp->ival);
break;
case TOKENTYPE_OPNUM:
Perl_sv_catpvf(aTHX_ report, "(ival=op_%s)",
PL_op_name[lvalp->ival]);
break;
case TOKENTYPE_PVAL:
Perl_sv_catpvf(aTHX_ report, "(pval=\"%s\")", lvalp->pval);
break;
case TOKENTYPE_OPVAL:
if (lvalp->opval) {
Perl_sv_catpvf(aTHX_ report, "(opval=op_%s)",
PL_op_name[lvalp->opval->op_type]);
if (lvalp->opval->op_type == OP_CONST) {
Perl_sv_catpvf(aTHX_ report, " %s",
SvPEEK(cSVOPx_sv(lvalp->opval)));
}
}
else
sv_catpvs(report, "(opval=null)");
break;
}
PerlIO_printf(Perl_debug_log, "### %s\n\n", SvPV_nolen_const(report));
};
return (int)rv;
}
/* print the buffer with suitable escapes */
STATIC void
S_printbuf(pTHX_ const char *const fmt, const char *const s)
{
SV* const tmp = newSVpvs("");
PERL_ARGS_ASSERT_PRINTBUF;
PerlIO_printf(Perl_debug_log, fmt, pv_display(tmp, s, strlen(s), 0, 60));
SvREFCNT_dec(tmp);
}
#endif
static int
S_deprecate_commaless_var_list(pTHX) {
PL_expect = XTERM;
deprecate("comma-less variable list");
return REPORT(','); /* grandfather non-comma-format format */
}
/*
* S_ao
*
* This subroutine detects &&=, ||=, and //= and turns an ANDAND, OROR or DORDOR
* into an OP_ANDASSIGN, OP_ORASSIGN, or OP_DORASSIGN
*/
STATIC int
S_ao(pTHX_ int toketype)
{
dVAR;
if (*PL_bufptr == '=') {
PL_bufptr++;
if (toketype == ANDAND)
pl_yylval.ival = OP_ANDASSIGN;
else if (toketype == OROR)
pl_yylval.ival = OP_ORASSIGN;
else if (toketype == DORDOR)
pl_yylval.ival = OP_DORASSIGN;
toketype = ASSIGNOP;
}
return toketype;
}
/*
* S_no_op
* When Perl expects an operator and finds something else, no_op
* prints the warning. It always prints " found where
* operator expected. It prints "Missing semicolon on previous line?"
* if the surprise occurs at the start of the line. "do you need to
* predeclare ..." is printed out for code like "sub bar; foo bar $x"
* where the compiler doesn't know if foo is a method call or a function.
* It prints "Missing operator before end of line" if there's nothing
* after the missing operator, or "... before <...>" if there is something
* after the missing operator.
*/
STATIC void
S_no_op(pTHX_ const char *const what, char *s)
{
dVAR;
char * const oldbp = PL_bufptr;
const bool is_first = (PL_oldbufptr == PL_linestart);
PERL_ARGS_ASSERT_NO_OP;
if (!s)
s = oldbp;
else
PL_bufptr = s;
yywarn(Perl_form(aTHX_ "%s found where operator expected", what));
if (ckWARN_d(WARN_SYNTAX)) {
if (is_first)
Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
"\t(Missing semicolon on previous line?)\n");
else if (PL_oldoldbufptr && isIDFIRST_lazy_if(PL_oldoldbufptr,UTF)) {
const char *t;
for (t = PL_oldoldbufptr; (isALNUM_lazy_if(t,UTF) || *t == ':'); t++)
NOOP;
if (t < PL_bufptr && isSPACE(*t))
Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
"\t(Do you need to predeclare %.*s?)\n",
(int)(t - PL_oldoldbufptr), PL_oldoldbufptr);
}
else {
assert(s >= oldbp);
Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
"\t(Missing operator before %.*s?)\n", (int)(s - oldbp), oldbp);
}
}
PL_bufptr = oldbp;
}
/*
* S_missingterm
* Complain about missing quote/regexp/heredoc terminator.
* If it's called with NULL then it cauterizes the line buffer.
* If we're in a delimited string and the delimiter is a control
* character, it's reformatted into a two-char sequence like ^C.
* This is fatal.
*/
STATIC void
S_missingterm(pTHX_ char *s)
{
dVAR;
char tmpbuf[3];
char q;
if (s) {
char * const nl = strrchr(s,'\n');
if (nl)
*nl = '\0';
}
else if (isCNTRL(PL_multi_close)) {
*tmpbuf = '^';
tmpbuf[1] = (char)toCTRL(PL_multi_close);
tmpbuf[2] = '\0';
s = tmpbuf;
}
else {
*tmpbuf = (char)PL_multi_close;
tmpbuf[1] = '\0';
s = tmpbuf;
}
q = strchr(s,'"') ? '\'' : '"';
Perl_croak(aTHX_ "Can't find string terminator %c%s%c anywhere before EOF",q,s,q);
}
#define FEATURE_IS_ENABLED(name) \
((0 != (PL_hints & HINT_LOCALIZE_HH)) \
&& S_feature_is_enabled(aTHX_ STR_WITH_LEN(name)))
/* The longest string we pass in. */
#define MAX_FEATURE_LEN (sizeof("unicode_strings")-1)
/*
* S_feature_is_enabled
* Check whether the named feature is enabled.
*/
STATIC bool
S_feature_is_enabled(pTHX_ const char *const name, STRLEN namelen)
{
dVAR;
HV * const hinthv = GvHV(PL_hintgv);
char he_name[8 + MAX_FEATURE_LEN] = "feature_";
PERL_ARGS_ASSERT_FEATURE_IS_ENABLED;
assert(namelen <= MAX_FEATURE_LEN);
memcpy(&he_name[8], name, namelen);
return (hinthv && hv_exists(hinthv, he_name, 8 + namelen));
}
/*
* experimental text filters for win32 carriage-returns, utf16-to-utf8 and
* utf16-to-utf8-reversed.
*/
#ifdef PERL_CR_FILTER
static void
strip_return(SV *sv)
{
register const char *s = SvPVX_const(sv);
register const char * const e = s + SvCUR(sv);
PERL_ARGS_ASSERT_STRIP_RETURN;
/* outer loop optimized to do nothing if there are no CR-LFs */
while (s < e) {
if (*s++ == '\r' && *s == '\n') {
/* hit a CR-LF, need to copy the rest */
register char *d = s - 1;
*d++ = *s++;
while (s < e) {
if (*s == '\r' && s[1] == '\n')
s++;
*d++ = *s++;
}
SvCUR(sv) -= s - d;
return;
}
}
}
STATIC I32
S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
{
const I32 count = FILTER_READ(idx+1, sv, maxlen);
if (count > 0 && !maxlen)
strip_return(sv);
return count;
}
#endif
/*
* Perl_lex_start
*
* Create a parser object and initialise its parser and lexer fields
*
* rsfp is the opened file handle to read from (if any),
*
* line holds any initial content already read from the file (or in
* the case of no file, such as an eval, the whole contents);
*
* new_filter indicates that this is a new file and it shouldn't inherit
* the filters from the current parser (ie require).
*/
void
Perl_lex_start(pTHX_ SV *line, PerlIO *rsfp, bool new_filter)
{
dVAR;
const char *s = NULL;
STRLEN len;
yy_parser *parser, *oparser;
/* create and initialise a parser */
Newxz(parser, 1, yy_parser);
parser->old_parser = oparser = PL_parser;
PL_parser = parser;
parser->stack = NULL;
parser->ps = NULL;
parser->stack_size = 0;
/* on scope exit, free this parser and restore any outer one */
SAVEPARSER(parser);
parser->saved_curcop = PL_curcop;
/* initialise lexer state */
#ifdef PERL_MAD
parser->curforce = -1;
#else
parser->nexttoke = 0;
#endif
parser->error_count = oparser ? oparser->error_count : 0;
parser->copline = NOLINE;
parser->lex_state = LEX_NORMAL;
parser->expect = XSTATE;
parser->rsfp = rsfp;
parser->rsfp_filters = (new_filter || !oparser) ? newAV()
: MUTABLE_AV(SvREFCNT_inc(oparser->rsfp_filters));
Newx(parser->lex_brackstack, 120, char);
Newx(parser->lex_casestack, 12, char);
*parser->lex_casestack = '\0';
if (line) {
s = SvPV_const(line, len);
} else {
len = 0;
}
if (!len) {
parser->linestr = newSVpvs("\n;");
} else if (SvREADONLY(line) || s[len-1] != ';' || !SvPOK(line)) {
/* avoid tie/overload weirdness */
parser->linestr = newSVpvn_flags(s, len, SvUTF8(line));
if (s[len-1] != ';')
sv_catpvs(parser->linestr, "\n;");
} else {
SvTEMP_off(line);
SvREFCNT_inc_simple_void_NN(line);
parser->linestr = line;
}
parser->oldoldbufptr =
parser->oldbufptr =
parser->bufptr =
parser->linestart = SvPVX(parser->linestr);
parser->bufend = parser->bufptr + SvCUR(parser->linestr);
parser->last_lop = parser->last_uni = NULL;
}
/* delete a parser object */
void
Perl_parser_free(pTHX_ const yy_parser *parser)
{
PERL_ARGS_ASSERT_PARSER_FREE;
PL_curcop = parser->saved_curcop;
SvREFCNT_dec(parser->linestr);
if (parser->rsfp == PerlIO_stdin())
PerlIO_clearerr(parser->rsfp);
else if (parser->rsfp && (!parser->old_parser ||
(parser->old_parser && parser->rsfp != parser->old_parser->rsfp)))
PerlIO_close(parser->rsfp);
SvREFCNT_dec(parser->rsfp_filters);
Safefree(parser->lex_brackstack);
Safefree(parser->lex_casestack);
PL_parser = parser->old_parser;
Safefree(parser);
}
/*
* Perl_lex_end
* Finalizer for lexing operations. Must be called when the parser is
* done with the lexer.
*/
void
Perl_lex_end(pTHX)
{
dVAR;
PL_doextract = FALSE;
}
/*
=for apidoc AmxU|SV *|PL_parser-Elinestr
Buffer scalar containing the chunk currently under consideration of the
text currently being lexed. This is always a plain string scalar (for
which C is true). It is not intended to be used as a scalar by
normal scalar means; instead refer to the buffer directly by the pointer
variables described below.
The lexer maintains various C pointers to things in the
Clinestr> buffer. If Clinestr> is ever
reallocated, all of these pointers must be updated. Don't attempt to
do this manually, but rather use L if you need to
reallocate the buffer.
The content of the text chunk in the buffer is commonly exactly one
complete line of input, up to and including a newline terminator,
but there are situations where it is otherwise. The octets of the
buffer may be intended to be interpreted as either UTF-8 or Latin-1.
The function L tells you which. Do not use the C
flag on this scalar, which may disagree with it.
For direct examination of the buffer, the variable
Lbufend> points to the end of the buffer. The current
lexing position is pointed to by Lbufptr>. Direct use
of these pointers is usually preferable to examination of the scalar
through normal scalar means.
=for apidoc AmxU|char *|PL_parser-Ebufend
Direct pointer to the end of the chunk of text currently being lexed, the
end of the lexer buffer. This is equal to Clinestr)
+ SvCUR(PL_parser-Elinestr)>. A NUL character (zero octet) is
always located at the end of the buffer, and does not count as part of
the buffer's contents.
=for apidoc AmxU|char *|PL_parser-Ebufptr
Points to the current position of lexing inside the lexer buffer.
Characters around this point may be freely examined, within
the range delimited by Clinestr>)> and
Lbufend>. The octets of the buffer may be intended to be
interpreted as either UTF-8 or Latin-1, as indicated by L.
Lexing code (whether in the Perl core or not) moves this pointer past
the characters that it consumes. It is also expected to perform some
bookkeeping whenever a newline character is consumed. This movement
can be more conveniently performed by the function L,
which handles newlines appropriately.
Interpretation of the buffer's octets can be abstracted out by
using the slightly higher-level functions L and
L.
=for apidoc AmxU|char *|PL_parser-Elinestart
Points to the start of the current line inside the lexer buffer.
This is useful for indicating at which column an error occurred, and
not much else. This must be updated by any lexing code that consumes
a newline; the function L handles this detail.
=cut
*/
/*
=for apidoc Amx|bool|lex_bufutf8
Indicates whether the octets in the lexer buffer
(Llinestr>) should be interpreted as the UTF-8 encoding
of Unicode characters. If not, they should be interpreted as Latin-1
characters. This is analogous to the C flag for scalars.
In UTF-8 mode, it is not guaranteed that the lexer buffer actually
contains valid UTF-8. Lexing code must be robust in the face of invalid
encoding.
The actual C flag of the Llinestr> scalar
is significant, but not the whole story regarding the input character
encoding. Normally, when a file is being read, the scalar contains octets
and its C flag is off, but the octets should be interpreted as
UTF-8 if the C et al) is correctly set to reflect the source
of the code to be parsed and the lexical context for the statement.
The op tree representing the statement is returned. This may be a
null pointer if the statement is null, for example if it was actually
a subroutine definition (which has compile-time side effects). If not
null, it will be the result of a L call, normally including
a C or equivalent op.
If an error occurs in parsing or compilation, in most cases a valid op
tree (most likely null) is returned anyway. The error is reflected in
the parser state, normally resulting in a single exception at the top
level of parsing which covers all the compilation errors that occurred.
Some compilation errors, however, will throw an exception immediately.
The I parameter is reserved for future use, and must always
be zero.
=cut
*/
OP *
Perl_parse_fullstmt(pTHX_ U32 flags)
{
OP *fullstmtop;
if (flags)
Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
ENTER;
SAVEVPTR(PL_eval_root);
PL_eval_root = NULL;
if(yyparse(GRAMFULLSTMT) && !PL_parser->error_count)
qerror(Perl_mess(aTHX_ "Parse error"));
fullstmtop = PL_eval_root;
LEAVE;
return fullstmtop;
}
/*
=for apidoc Amx|OP *|parse_stmtseq|U32 flags
Parse a sequence of zero or more Perl statements. These may be normal
imperative statements, including optional labels, or declarations
that have compile-time effect, or any mixture thereof. The statement
sequence ends when a closing brace or end-of-file is encountered in a
place where a new statement could have validly started. It is up to
the caller to ensure that the dynamic parser state (L et al)
is correctly set to reflect the source of the code to be parsed and the
lexical context for the statements.
The op tree representing the statement sequence is returned. This may
be a null pointer if the statements were all null, for example if there
were no statements or if there were only subroutine definitions (which
have compile-time side effects). If not null, it will be a C
list, normally including C or equivalent ops.
If an error occurs in parsing or compilation, in most cases a valid op
tree is returned anyway. The error is reflected in the parser state,
normally resulting in a single exception at the top level of parsing
which covers all the compilation errors that occurred. Some compilation
errors, however, will throw an exception immediately.
The I parameter is reserved for future use, and must always
be zero.
=cut
*/
OP *
Perl_parse_stmtseq(pTHX_ U32 flags)
{
OP *stmtseqop;
if (flags)
Perl_croak(aTHX_ "Parsing code internal error (%s)", "parse_fullstmt");
ENTER;
SAVEVPTR(PL_eval_root);
PL_eval_root = NULL;
if(yyparse(GRAMSTMTSEQ) && !PL_parser->error_count)
qerror(Perl_mess(aTHX_ "Parse error"));
stmtseqop = PL_eval_root;
LEAVE;
return stmtseqop;
}
void
Perl_munge_qwlist_to_paren_list(pTHX_ OP *qwlist)
{
PERL_ARGS_ASSERT_MUNGE_QWLIST_TO_PAREN_LIST;
deprecate("qw(...) as parentheses");
force_next(')');
if (qwlist->op_type == OP_STUB) {
op_free(qwlist);
}
else {
start_force(PL_curforce);
NEXTVAL_NEXTTOKE.opval = qwlist;
force_next(THING);
}
force_next('(');
}
/*
* Local variables:
* c-indentation-style: bsd
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*
* ex: set ts=8 sts=4 sw=4 noet:
*/