/* 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_inline.h"
#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_allbrackets (PL_parser->lex_allbrackets)
#define PL_lex_fakeeof (PL_parser->lex_fakeeof)
#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_dojoin (PL_parser->lex_dojoin)
#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_preambled (PL_parser->preambled)
#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)
# define PL_nexttoke (PL_parser->nexttoke)
# define PL_nexttype (PL_parser->nexttype)
# define PL_nextval (PL_parser->nextval)
#define SvEVALED(sv) \
(SvTYPE(sv) >= SVt_PVNV \
&& ((XPVIV*)SvANY(sv))->xiv_u.xivu_eval_seen)
static const char* const ident_too_long = "Identifier too long";
# define NEXTVAL_NEXTTOKE PL_nextval[PL_nexttoke]
#define XENUMMASK 0x3f
#define XFAKEEOF 0x40
#define XFAKEBRACK 0x80
#ifdef USE_UTF8_SCRIPTS
# define UTF cBOOL(!IN_BYTES)
#else
# define UTF cBOOL((PL_linestr && DO_UTF8(PL_linestr)) || ( !(PL_parser->lex_flags & LEX_IGNORE_UTF8_HINTS) && (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) isBLANK_A(c)
#define HEXFP_PEEK(s) \
(((s[0] == '.') && \
(isXDIGIT(s[1]) || isALPHA_FOLD_EQ(s[1], 'p'))) || \
isALPHA_FOLD_EQ(s[0], 'p'))
/* 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).
*
* These values refer to the various states within a sublex parse,
* i.e. within a double quotish string
*/
/* #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 */
#ifdef DEBUGGING
static const char* const lex_state_names[] = {
"KNOWNEXT",
"FORMLINE",
"INTERPCONST",
"INTERPCONCAT",
"INTERPENDMAYBE",
"INTERPEND",
"INTERPSTART",
"INTERPPUSH",
"INTERPCASEMOD",
"INTERPNORMAL",
"NORMAL"
};
#endif
#include "keywords.h"
/* CLINE is a macro that ensures PL_copline has a sane value */
#define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
/*
* 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
* POSTDEREF : postfix dereference (->$* ->@[...] etc.)
* LOOPX : loop exiting command (goto, last, dump, etc)
* FTST : file test operator
* FUN0 : zero-argument function
* FUN0OP : zero-argument function, with its op created in this file
* FUN1 : not used, except for not, which isn't a UNIOP
* BOop : bitwise or or xor
* BAop : bitwise and
* BCop : bitwise complement
* SHop : shift operator
* PWop : power operator
* PMop : pattern-matching operator
* Aop : addition-level operator
* AopNOASSIGN : addition-level operator that is never part of .=
* 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, 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 POSTDEREF(f) return (PL_bufptr = s, S_postderef(aTHX_ REPORT(f),s[1]))
#define LOOPX(f) return (PL_bufptr = force_word(s,BAREWORD,TRUE,FALSE), \
pl_yylval.ival=f, \
PL_expect = PL_nexttoke ? XOPERATOR : XTERM, \
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 FUN0OP(f) return (pl_yylval.opval=f, CLINE, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0OP))
#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, (int)BITOROP))
#define BAop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)BITANDOP))
#define BCop(f) return pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr = s, \
REPORT('~')
#define SHop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)SHIFTOP))
#define PWop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (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, (int)ADDOP))
#define AopNOASSIGN(f) return (pl_yylval.ival=f, PL_bufptr=s, REPORT((int)ADDOP))
#define Mop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (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 UNI3(f,x,have_x) { \
pl_yylval.ival = f; \
if (have_x) PL_expect = x; \
PL_bufptr = s; \
PL_last_uni = PL_oldbufptr; \
PL_last_lop_op = (f) < 0 ? -(f) : (f); \
if (*s == '(') \
return REPORT( (int)FUNC1 ); \
s = skipspace(s); \
return REPORT( *s=='(' ? (int)FUNC1 : (int)UNIOP ); \
}
#define UNI(f) UNI3(f,XTERM,1)
#define UNIDOR(f) UNI3(f,XTERMORDORDOR,1)
#define UNIPROTO(f,optional) { \
if (optional) PL_last_uni = PL_oldbufptr; \
OPERATOR(f); \
}
#define UNIBRACK(f) UNI3(f,0,0)
/* grandfather return to old style */
#define OLDLOP(f) \
do { \
if (!PL_lex_allbrackets && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC) \
PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC; \
pl_yylval.ival = (f); \
PL_expect = XTERM; \
PL_bufptr = s; \
return (int)LSTOP; \
} while(0)
#define COPLINE_INC_WITH_HERELINES \
STMT_START { \
CopLINE_inc(PL_curcop); \
if (PL_parser->herelines) \
CopLINE(PL_curcop) += PL_parser->herelines, \
PL_parser->herelines = 0; \
} STMT_END
/* Called after scan_str to update CopLINE(PL_curcop), but only when there
* is no sublex_push to follow. */
#define COPLINE_SET_FROM_MULTI_END \
STMT_START { \
CopLINE_set(PL_curcop, PL_multi_end); \
if (PL_multi_end != PL_multi_start) \
PL_parser->herelines = 0; \
} STMT_END
#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
};
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" },
{ FORMLBRACK, TOKENTYPE_NONE, "FORMLBRACK" },
{ FORMRBRACK, TOKENTYPE_NONE, "FORMRBRACK" },
{ FUNC, TOKENTYPE_OPNUM, "FUNC" },
{ FUNC0, TOKENTYPE_OPNUM, "FUNC0" },
{ FUNC0OP, TOKENTYPE_OPVAL, "FUNC0OP" },
{ 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" },
{ 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" },
{ POSTJOIN, TOKENTYPE_NONE, "POSTJOIN" },
{ POSTDEC, TOKENTYPE_NONE, "POSTDEC" },
{ POSTINC, TOKENTYPE_NONE, "POSTINC" },
{ POWOP, TOKENTYPE_OPNUM, "POWOP" },
{ PREDEC, TOKENTYPE_NONE, "PREDEC" },
{ PREINC, TOKENTYPE_NONE, "PREINC" },
{ PRIVATEREF, TOKENTYPE_OPVAL, "PRIVATEREF" },
{ QWLIST, TOKENTYPE_OPVAL, "QWLIST" },
{ REFGEN, TOKENTYPE_NONE, "REFGEN" },
{ RELOP, TOKENTYPE_OPNUM, "RELOP" },
{ REQUIRE, TOKENTYPE_NONE, "REQUIRE" },
{ 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" },
{ BAREWORD, TOKENTYPE_OPVAL, "BAREWORD" },
{ 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)
{
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 (isGRAPH(rv))
{
Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
if ((char)rv == 'p')
sv_catpvs(report, " (pending identifier)");
}
else if (!rv)
sv_catpvs(report, "EOF");
else
Perl_sv_catpvf(aTHX_ report, "?? %" IVdf, (IV)rv);
switch (type) {
case TOKENTYPE_NONE:
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;
GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); /* fmt checked by caller */
PerlIO_printf(Perl_debug_log, fmt, pv_display(tmp, s, strlen(s), 0, 60));
GCC_DIAG_RESTORE_STMT;
SvREFCNT_dec(tmp);
}
#endif
/*
* S_ao
*
* This subroutine looks for an '=' next to the operator that has just been
* parsed and turns it into an ASSIGNOP if it finds one.
*/
STATIC int
S_ao(pTHX_ int toketype)
{
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 REPORT(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.
*
* PL_bufptr is expected to point to the start of the thing that was found,
* and s after the next token or partial token.
*/
STATIC void
S_no_op(pTHX_ const char *const what, char *s)
{
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), UTF ? SVf_UTF8 : 0);
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_safe(PL_oldoldbufptr,
PL_bufend,
UTF))
{
const char *t;
for (t = PL_oldoldbufptr;
(isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF) || *t == ':');
t += UTF ? UTF8SKIP(t) : 1)
{
NOOP;
}
if (t < PL_bufptr && isSPACE(*t))
Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
"\t(Do you need to predeclare %" UTF8f "?)\n",
UTF8fARG(UTF, t - PL_oldoldbufptr, PL_oldoldbufptr));
}
else {
assert(s >= oldbp);
Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
"\t(Missing operator before %" UTF8f "?)\n",
UTF8fARG(UTF, 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, STRLEN len)
{
char tmpbuf[UTF8_MAXBYTES + 1];
char q;
bool uni = FALSE;
SV *sv;
if (s) {
char * const nl = (char *) my_memrchr(s, '\n', len);
if (nl) {
*nl = '\0';
len = nl - s;
}
uni = UTF;
}
else if (PL_multi_close < 32) {
*tmpbuf = '^';
tmpbuf[1] = (char)toCTRL(PL_multi_close);
tmpbuf[2] = '\0';
s = tmpbuf;
len = 2;
}
else {
if (LIKELY(PL_multi_close < 256)) {
*tmpbuf = (char)PL_multi_close;
tmpbuf[1] = '\0';
len = 1;
}
else {
char *end = (char *)uvchr_to_utf8((U8 *)tmpbuf, PL_multi_close);
*end = '\0';
len = end - tmpbuf;
uni = TRUE;
}
s = tmpbuf;
}
q = memchr(s, '"', len) ? '\'' : '"';
sv = sv_2mortal(newSVpvn(s, len));
if (uni)
SvUTF8_on(sv);
Perl_croak(aTHX_ "Can't find string terminator %c%" SVf "%c"
" anywhere before EOF", q, SVfARG(sv), q);
}
#include "feature.h"
/*
* Check whether the named feature is enabled.
*/
bool
Perl_feature_is_enabled(pTHX_ const char *const name, STRLEN namelen)
{
char he_name[8 + MAX_FEATURE_LEN] = "feature_";
PERL_ARGS_ASSERT_FEATURE_IS_ENABLED;
assert(CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM);
if (namelen > MAX_FEATURE_LEN)
return FALSE;
memcpy(&he_name[8], name, namelen);
return cBOOL(cop_hints_fetch_pvn(PL_curcop, he_name, 8 + namelen, 0,
REFCOUNTED_HE_EXISTS));
}
/*
* 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)
{
const char *s = SvPVX_const(sv);
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 */
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
/*
=for apidoc Amx|void|lex_start|SV *line|PerlIO *rsfp|U32 flags
Creates and initialises a new lexer/parser state object, supplying
a context in which to lex and parse from a new source of Perl code.
A pointer to the new state object is placed in L. An entry
is made on the save stack so that upon unwinding, the new state object
will be destroyed and the former value of L will be restored.
Nothing else need be done to clean up the parsing context.
The code to be parsed comes from C and C. C, if
non-null, provides a string (in SV form) containing code to be parsed.
A copy of the string is made, so subsequent modification of C
does not affect parsing. C, if non-null, provides an input stream
from which code will be read to be parsed. If both are non-null, the
code in C comes first and must consist of complete lines of input,
and C supplies the remainder of the source.
The C parameter is reserved for future use. Currently it is only
used by perl internally, so extensions should always pass zero.
=cut
*/
/* LEX_START_SAME_FILTER indicates that this is not a new file, so it
can share filters with the current parser.
LEX_START_DONT_CLOSE indicates that the file handle wasn't opened by the
caller, hence isn't owned by the parser, so shouldn't be closed on parser
destruction. This is used to handle the case of defaulting to reading the
script from the standard input because no filename was given on the command
line (without getting confused by situation where STDIN has been closed, so
the script handle is opened on fd 0) */
void
Perl_lex_start(pTHX_ SV *line, PerlIO *rsfp, U32 flags)
{
const char *s = NULL;
yy_parser *parser, *oparser;
if (flags && flags & ~LEX_START_FLAGS)
Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_start");
/* create and initialise a parser */
Newxz(parser, 1, yy_parser);
parser->old_parser = oparser = PL_parser;
PL_parser = parser;
parser->stack = NULL;
parser->stack_max1 = NULL;
parser->ps = NULL;
/* on scope exit, free this parser and restore any outer one */
SAVEPARSER(parser);
parser->saved_curcop = PL_curcop;
/* initialise lexer state */
parser->nexttoke = 0;
parser->error_count = oparser ? oparser->error_count : 0;
parser->copline = parser->preambling = NOLINE;
parser->lex_state = LEX_NORMAL;
parser->expect = XSTATE;
parser->rsfp = rsfp;
parser->recheck_utf8_validity = FALSE;
parser->rsfp_filters =
!(flags & LEX_START_SAME_FILTER) || !oparser
? NULL
: MUTABLE_AV(SvREFCNT_inc(
oparser->rsfp_filters
? oparser->rsfp_filters
: (oparser->rsfp_filters = newAV())
));
Newx(parser->lex_brackstack, 120, char);
Newx(parser->lex_casestack, 12, char);
*parser->lex_casestack = '\0';
Newxz(parser->lex_shared, 1, LEXSHARED);
if (line) {
STRLEN len;
const U8* first_bad_char_loc;
s = SvPV_const(line, len);
if ( SvUTF8(line)
&& UNLIKELY(! is_utf8_string_loc((U8 *) s,
SvCUR(line),
&first_bad_char_loc)))
{
_force_out_malformed_utf8_message(first_bad_char_loc,
(U8 *) s + SvCUR(line),
0,
1 /* 1 means die */ );
NOT_REACHED; /* NOTREACHED */
}
parser->linestr = flags & LEX_START_COPIED
? SvREFCNT_inc_simple_NN(line)
: newSVpvn_flags(s, len, SvUTF8(line));
if (!rsfp)
sv_catpvs(parser->linestr, "\n;");
} else {
parser->linestr = newSVpvn("\n;", rsfp ? 1 : 2);
}
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;
STATIC_ASSERT_STMT(FITS_IN_8_BITS(LEX_IGNORE_UTF8_HINTS|LEX_EVALBYTES
|LEX_DONT_CLOSE_RSFP));
parser->lex_flags = (U8) (flags & (LEX_IGNORE_UTF8_HINTS|LEX_EVALBYTES
|LEX_DONT_CLOSE_RSFP));
parser->in_pod = parser->filtered = 0;
}
/* 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 (PL_parser->lex_flags & LEX_DONT_CLOSE_RSFP)
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);
SvREFCNT_dec(parser->lex_stuff);
SvREFCNT_dec(parser->lex_sub_repl);
Safefree(parser->lex_brackstack);
Safefree(parser->lex_casestack);
Safefree(parser->lex_shared);
PL_parser = parser->old_parser;
Safefree(parser);
}
void
Perl_parser_free_nexttoke_ops(pTHX_ yy_parser *parser, OPSLAB *slab)
{
I32 nexttoke = parser->nexttoke;
PERL_ARGS_ASSERT_PARSER_FREE_NEXTTOKE_OPS;
while (nexttoke--) {
if (S_is_opval_token(parser->nexttype[nexttoke] & 0xffff)
&& parser->nextval[nexttoke].opval
&& parser->nextval[nexttoke].opval->op_slabbed
&& OpSLAB(parser->nextval[nexttoke].opval) == slab) {
op_free(parser->nextval[nexttoke].opval);
parser->nextval[nexttoke].opval = NULL;
}
}
}
/*
=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 C 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