X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/264e1af351acde2005eb4df085136043665b2b9c..32d439155644a16b717019874a8d41a0d7c2f0e0:/perly.y diff --git a/perly.y b/perly.y index 63210ae..77773fd 100644 --- a/perly.y +++ b/perly.y @@ -1,6 +1,7 @@ /* perly.y * - * Copyright (c) 1991-2002, Larry Wall + * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall + * Copyright (c) 2007, 2008, 2009, 2010, 2011 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. @@ -9,192 +10,439 @@ /* * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it? - * All that is gold does not glitter, not all those who wander are lost.' + * All that is gold does not glitter, not all those who wander are lost.' + * + * [p.171 of _The Lord of the Rings_, I/x: "Strider"] */ -%{ -#include "EXTERN.h" -#define PERL_IN_PERLY_C -#include "perl.h" -#ifdef EBCDIC -#undef YYDEBUG -#endif -#define dep() deprecate("\"do\" to call subroutines") - -/* stuff included here to make perly_c.diff apply better */ - -#define yydebug PL_yydebug -#define yynerrs PL_yynerrs -#define yyerrflag PL_yyerrflag -#define yychar PL_yychar -#define yyval PL_yyval -#define yylval PL_yylval - -struct ysv { - short* yyss; - YYSTYPE* yyvs; - int oldyydebug; - int oldyynerrs; - int oldyyerrflag; - int oldyychar; - YYSTYPE oldyyval; - YYSTYPE oldyylval; -}; - -static void yydestruct(pTHX_ void *ptr); - -%} - -%start prog - -%{ -#if 0 /* get this from perly.h instead */ -%} +/* + * This file holds the grammar for the Perl language. If edited, you need + * to run regen_perly.pl, which re-creates the files perly.h, perly.tab + * and perly.act which are derived from this. + * + * The main job of of this grammar is to call the various newFOO() + * functions in op.c to build a syntax tree of OP structs. + * It relies on the lexer in toke.c to do the tokenizing. + * + * Note: due to the way that the cleanup code works WRT to freeing ops on + * the parse stack, it is dangerous to assign to the $n variables within + * an action. + */ + +/* Make the parser re-entrant. */ + +%pure_parser + +%start grammar %union { - I32 ival; + I32 ival; /* __DEFAULT__ (marker for regen_perly.pl; + must always be 1st union member) */ char *pval; OP *opval; GV *gvval; } -%{ -#endif /* 0 */ - -#ifdef USE_PURE_BISON -#define YYLEX_PARAM (&yychar) -#define yylex yylex_r -#endif - -%} +%token GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ -%token '{' +%token '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';' '=' '.' -%token WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF -%token FUNC0SUB UNIOPSUB LSTOPSUB +%token WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST +%token FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB +%token PLUGEXPR PLUGSTMT %token LABEL %token FORMAT SUB ANONSUB PACKAGE USE %token WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR -%token LOOPEX DOTDOT +%token GIVEN WHEN DEFAULT +%token LOOPEX DOTDOT YADAYADA %token FUNC0 FUNC1 FUNC UNIOP LSTOP %token RELOP EQOP MULOP ADDOP %token DOLSHARP DO HASHBRACK NOAMP -%token LOCAL MY MYSUB -%token COLONATTR +%token LOCAL MY REQUIRE +%token COLONATTR FORMLBRACK FORMRBRACK -%type prog decl format startsub startanonsub startformsub -%type progstart remember mremember '&' -%type block mblock lineseq line loop cond else +%type grammar remember mremember +%type startsub startanonsub startformsub + +%type mintro + +%type stmtseq fullstmt labfullstmt barestmt block mblock else %type expr term subscripted scalar ary hsh arylen star amper sideff -%type argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr -%type listexpr listexprcom indirob listop method -%type formname subname proto subbody cont my_scalar -%type subattrlist myattrlist mysubrout myattrterm myterm -%type termbinop termunop anonymous termdo -%type label - -%nonassoc PREC_LOW +%type sliceme kvslice gelem +%type listexpr nexpr texpr iexpr mexpr mnexpr miexpr +%type optlistexpr optexpr indirob listop method +%type formname subname proto optsubbody cont my_scalar formblock +%type subattrlist myattrlist myattrterm myterm +%type realsubbody subsignature termbinop termunop anonymous termdo +%type formstmtseq formline formarg + +%nonassoc PREC_LOW %nonassoc LOOPEX %left OROP DOROP -%left ANDOP -%right NOTOP +%left ANDOP +%right NOTOP %nonassoc LSTOP LSTOPSUB -%left ',' +%left ',' %right ASSIGNOP -%right '?' ':' -%nonassoc DOTDOT -%left OROR DORDOR -%left ANDAND +%right '?' ':' +%nonassoc DOTDOT YADAYADA +%left OROR DORDOR +%left ANDAND %left BITOROP %left BITANDOP %nonassoc EQOP %nonassoc RELOP %nonassoc UNIOP UNIOPSUB +%nonassoc REQUIRE %left SHIFTOP %left ADDOP %left MULOP %left MATCHOP -%right '!' '~' UMINUS REFGEN +%right '!' '~' UMINUS REFGEN %right POWOP -%nonassoc PREINC PREDEC POSTINC POSTDEC -%left ARROW +%nonassoc PREINC PREDEC POSTINC POSTDEC POSTJOIN +%left ARROW %nonassoc ')' -%left '(' +%left '(' %left '[' '{' %% /* RULES */ -/* The whole program */ -prog : progstart - /*CONTINUED*/ lineseq - { $$ = $1; newPROG(block_end($1,$2)); } +/* Top-level choice of what kind of thing yyparse was called to parse */ +grammar : GRAMPROG + { + parser->expect = XSTATE; + } + remember stmtseq + { + newPROG(block_end($3,$4)); + $$ = 0; + } + | GRAMEXPR + { + parser->expect = XTERM; + } + optexpr + { + PL_eval_root = $3; + $$ = 0; + } + | GRAMBLOCK + { + parser->expect = XBLOCK; + } + block + { + PL_pad_reset_pending = TRUE; + PL_eval_root = $3; + $$ = 0; + yyunlex(); + parser->yychar = YYEOF; + } + | GRAMBARESTMT + { + parser->expect = XSTATE; + } + barestmt + { + PL_pad_reset_pending = TRUE; + PL_eval_root = $3; + $$ = 0; + yyunlex(); + parser->yychar = YYEOF; + } + | GRAMFULLSTMT + { + parser->expect = XSTATE; + } + fullstmt + { + PL_pad_reset_pending = TRUE; + PL_eval_root = $3; + $$ = 0; + yyunlex(); + parser->yychar = YYEOF; + } + | GRAMSTMTSEQ + { + parser->expect = XSTATE; + } + stmtseq + { + PL_eval_root = $3; + $$ = 0; + } ; /* An ordinary block */ -block : '{' remember lineseq '}' - { if (PL_copline > (line_t)$1) - PL_copline = (line_t)$1; - $$ = block_end($2, $3); } +block : '{' remember stmtseq '}' + { if (parser->copline > (line_t)$1) + parser->copline = (line_t)$1; + $$ = block_end($2, $3); + } + ; + +/* format body */ +formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.' + { if (parser->copline > (line_t)$1) + parser->copline = (line_t)$1; + $$ = block_end($2, $5); + } ; remember: /* NULL */ /* start a full lexical scope */ { $$ = block_start(TRUE); } ; -progstart: - { -#if defined(YYDEBUG) && defined(DEBUGGING) - yydebug = (DEBUG_p_TEST); -#endif - PL_expect = XSTATE; $$ = block_start(TRUE); - } +mblock : '{' mremember stmtseq '}' + { if (parser->copline > (line_t)$1) + parser->copline = (line_t)$1; + $$ = block_end($2, $3); + } ; +mremember: /* NULL */ /* start a partial lexical scope */ + { $$ = block_start(FALSE); } + ; -mblock : '{' mremember lineseq '}' - { if (PL_copline > (line_t)$1) - PL_copline = (line_t)$1; - $$ = block_end($2, $3); } +/* A sequence of statements in the program */ +stmtseq : /* NULL */ + { $$ = (OP*)NULL; } + | stmtseq fullstmt + { $$ = op_append_list(OP_LINESEQ, $1, $2); + PL_pad_reset_pending = TRUE; + if ($1 && $2) + PL_hints |= HINT_BLOCK_SCOPE; + } ; -mremember: /* NULL */ /* start a partial lexical scope */ - { $$ = block_start(FALSE); } +/* A sequence of format lines */ +formstmtseq: /* NULL */ + { $$ = (OP*)NULL; } + | formstmtseq formline + { $$ = op_append_list(OP_LINESEQ, $1, $2); + PL_pad_reset_pending = TRUE; + if ($1 && $2) + PL_hints |= HINT_BLOCK_SCOPE; + } ; -/* A collection of "lines" in the program */ -lineseq : /* NULL */ - { $$ = Nullop; } - | lineseq decl +/* A statement in the program, including optional labels */ +fullstmt: barestmt + { + $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL; + } + | labfullstmt { $$ = $1; } - | lineseq line - { $$ = append_list(OP_LINESEQ, - (LISTOP*)$1, (LISTOP*)$2); - PL_pad_reset_pending = TRUE; - if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; } - ; - -/* A "line" in the program */ -line : label cond - { $$ = newSTATEOP(0, $1, $2); } - | loop /* loops add their own labels */ - | label ';' - { if ($1 != Nullch) { - $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0)); - } - else { - $$ = Nullop; - PL_copline = NOLINE; - } - PL_expect = XSTATE; } - | label sideff ';' - { $$ = newSTATEOP(0, $1, $2); - PL_expect = XSTATE; } + ; + +labfullstmt: LABEL barestmt + { + $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2); + } + | LABEL labfullstmt + { + $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2); + } + ; + +/* A bare statement, lacking label and other aspects of state op */ +barestmt: PLUGSTMT + { $$ = $1; } + | FORMAT startformsub formname formblock + { + CV *fmtcv = PL_compcv; + newFORM($2, $3, $4); + $$ = (OP*)NULL; + if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) { + SvREFCNT_inc_simple_void(fmtcv); + pad_add_anon(fmtcv, OP_NULL); + } + } + | SUB subname startsub + { + if ($2->op_type == OP_CONST) { + const char *const name = + SvPV_nolen_const(((SVOP*)$2)->op_sv); + if (strEQ(name, "BEGIN") || strEQ(name, "END") + || strEQ(name, "INIT") || strEQ(name, "CHECK") + || strEQ(name, "UNITCHECK")) + CvSPECIAL_on(PL_compcv); + } + else + /* State subs inside anonymous subs need to be + clonable themselves. */ + if (CvANON(CvOUTSIDE(PL_compcv)) + || CvCLONE(CvOUTSIDE(PL_compcv)) + || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST( + CvOUTSIDE(PL_compcv) + ))[$2->op_targ])) + CvCLONE_on(PL_compcv); + parser->in_my = 0; + parser->in_my_stash = NULL; + } + proto subattrlist optsubbody + { + SvREFCNT_inc_simple_void(PL_compcv); + $2->op_type == OP_CONST + ? newATTRSUB($3, $2, $5, $6, $7) + : newMYSUB($3, $2, $5, $6, $7) + ; + $$ = (OP*)NULL; + intro_my(); + } + | PACKAGE WORD WORD ';' + { + package($3); + if ($2) + package_version($2); + $$ = (OP*)NULL; + } + | USE startsub + { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ } + WORD WORD optlistexpr ';' + { + SvREFCNT_inc_simple_void(PL_compcv); + utilize($1, $2, $4, $5, $6); + $$ = (OP*)NULL; + } + | IF '(' remember mexpr ')' mblock else + { + $$ = block_end($3, + newCONDOP(0, $4, op_scope($6), $7)); + parser->copline = (line_t)$1; + } + | UNLESS '(' remember miexpr ')' mblock else + { + $$ = block_end($3, + newCONDOP(0, $4, op_scope($6), $7)); + parser->copline = (line_t)$1; + } + | GIVEN '(' remember mexpr ')' mblock + { + const PADOFFSET offset = pad_findmy_pvs("$_", 0); + $$ = block_end($3, + newGIVENOP($4, op_scope($6), + offset == NOT_IN_PAD + || PAD_COMPNAME_FLAGS_isOUR(offset) + ? 0 + : offset)); + parser->copline = (line_t)$1; + } + | WHEN '(' remember mexpr ')' mblock + { $$ = block_end($3, newWHENOP($4, op_scope($6))); } + | DEFAULT block + { $$ = newWHENOP(0, op_scope($2)); } + | WHILE '(' remember texpr ')' mintro mblock cont + { + $$ = block_end($3, + newWHILEOP(0, 1, (LOOP*)(OP*)NULL, + $4, $7, $8, $6)); + parser->copline = (line_t)$1; + } + | UNTIL '(' remember iexpr ')' mintro mblock cont + { + $$ = block_end($3, + newWHILEOP(0, 1, (LOOP*)(OP*)NULL, + $4, $7, $8, $6)); + parser->copline = (line_t)$1; + } + | FOR '(' remember mnexpr ';' + { parser->expect = XTERM; } + texpr ';' + { parser->expect = XTERM; } + mintro mnexpr ')' + mblock + { + OP *initop = $4; + OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL, + scalar($7), $13, $11, $10); + if (initop) { + forop = op_prepend_elem(OP_LINESEQ, initop, + op_append_elem(OP_LINESEQ, + newOP(OP_UNSTACK, OPf_SPECIAL), + forop)); + } + $$ = block_end($3, forop); + parser->copline = (line_t)$1; + } + | FOR MY remember my_scalar '(' mexpr ')' mblock cont + { + $$ = block_end($3, newFOROP(0, $4, $6, $8, $9)); + parser->copline = (line_t)$1; + } + | FOR scalar '(' remember mexpr ')' mblock cont + { + $$ = block_end($4, newFOROP(0, + op_lvalue($2, OP_ENTERLOOP), $5, $7, $8)); + parser->copline = (line_t)$1; + } + | FOR '(' remember mexpr ')' mblock cont + { + $$ = block_end($3, + newFOROP(0, (OP*)NULL, $4, $6, $7)); + parser->copline = (line_t)$1; + } + | block cont + { + /* a block is a loop that happens once */ + $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL, + (OP*)NULL, $1, $2, 0); + } + | PACKAGE WORD WORD '{' remember + { + package($3); + if ($2) { + package_version($2); + } + } + stmtseq '}' + { + /* a block is a loop that happens once */ + $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL, + (OP*)NULL, block_end($5, $7), (OP*)NULL, 0); + if (parser->copline > (line_t)$4) + parser->copline = (line_t)$4; + } + | sideff ';' + { + $$ = $1; + } + | ';' + { + $$ = (OP*)NULL; + parser->copline = NOLINE; + } + ; + +/* Format line */ +formline: THING formarg + { OP *list; + if ($2) { + OP *term = $2; + list = op_append_elem(OP_LIST, $1, term); + } + else { + list = $1; + } + if (parser->copline == NOLINE) + parser->copline = CopLINE(PL_curcop)-1; + else parser->copline--; + $$ = newSTATEOP(0, NULL, + convert(OP_FORMLINE, 0, list)); + } + ; + +formarg : /* NULL */ + { $$ = NULL; } + | FORMLBRACK stmtseq FORMRBRACK + { $$ = op_unscope($2); } ; /* An expression which may have a side-effect */ sideff : error - { $$ = Nullop; } + { $$ = (OP*)NULL; } | expr { $$ = $1; } | expr IF expr @@ -204,95 +452,55 @@ sideff : error | expr WHILE expr { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); } | expr UNTIL iexpr - { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);} + { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); } | expr FOR expr - { $$ = newFOROP(0, Nullch, (line_t)$2, - Nullop, $3, $1, Nullop); } + { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL); + parser->copline = (line_t)$2; } + | expr WHEN expr + { $$ = newWHENOP($3, op_scope($1)); } ; /* else and elsif blocks */ else : /* NULL */ - { $$ = Nullop; } + { $$ = (OP*)NULL; } | ELSE mblock - { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); } + { + ($2)->op_flags |= OPf_PARENS; + $$ = op_scope($2); + } | ELSIF '(' mexpr ')' mblock else - { PL_copline = (line_t)$1; - $$ = newCONDOP(0, $3, scope($5), $6); - PL_hints |= HINT_BLOCK_SCOPE; } - ; - -/* Real conditional expressions */ -cond : IF '(' remember mexpr ')' mblock else - { PL_copline = (line_t)$1; - $$ = block_end($3, - newCONDOP(0, $4, scope($6), $7)); } - | UNLESS '(' remember miexpr ')' mblock else - { PL_copline = (line_t)$1; - $$ = block_end($3, - newCONDOP(0, $4, scope($6), $7)); } + { parser->copline = (line_t)$1; + $$ = newCONDOP(0, + newSTATEOP(OPf_SPECIAL,NULL,$3), + op_scope($5), $6); + PL_hints |= HINT_BLOCK_SCOPE; + } ; /* Continue blocks */ cont : /* NULL */ - { $$ = Nullop; } + { $$ = (OP*)NULL; } | CONTINUE block - { $$ = scope($2); } - ; - -/* Loops: while, until, for, and a bare block */ -loop : label WHILE '(' remember mtexpr ')' mblock cont - { PL_copline = (line_t)$2; - $$ = block_end($4, - newSTATEOP(0, $1, - newWHILEOP(0, 1, (LOOP*)Nullop, - $2, $5, $7, $8))); } - | label UNTIL '(' remember miexpr ')' mblock cont - { PL_copline = (line_t)$2; - $$ = block_end($4, - newSTATEOP(0, $1, - newWHILEOP(0, 1, (LOOP*)Nullop, - $2, $5, $7, $8))); } - | label FOR MY remember my_scalar '(' mexpr ')' mblock cont - { $$ = block_end($4, - newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); } - | label FOR scalar '(' remember mexpr ')' mblock cont - { $$ = block_end($5, - newFOROP(0, $1, (line_t)$2, mod($3, OP_ENTERLOOP), - $6, $8, $9)); } - | label FOR '(' remember mexpr ')' mblock cont - { $$ = block_end($4, - newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); } - | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock - /* basically fake up an initialize-while lineseq */ - { OP *forop; - PL_copline = (line_t)$2; - forop = newSTATEOP(0, $1, - newWHILEOP(0, 1, (LOOP*)Nullop, - $2, scalar($7), - $11, $9)); - if ($5) { - forop = append_elem(OP_LINESEQ, - newSTATEOP(0, ($1?savepv($1):Nullch), - $5), - forop); - } - - $$ = block_end($4, forop); } - | label block cont /* a block is a loop that happens once */ - { $$ = newSTATEOP(0, $1, - newWHILEOP(0, 1, (LOOP*)Nullop, - NOLINE, Nullop, $2, $3)); } + { $$ = op_scope($2); } ; +/* determine whether there are any new my declarations */ +mintro : /* NULL */ + { $$ = (PL_min_intro_pending && + PL_max_intro_pending >= PL_min_intro_pending); + intro_my(); } + /* Normal expression */ nexpr : /* NULL */ - { $$ = Nullop; } + { $$ = (OP*)NULL; } | sideff ; /* Boolean expression */ texpr : /* NULL means true */ - { (void)scan_num("1", &yylval); $$ = yylval.opval; } + { YYSTYPE tmplval; + (void)scan_num("1", &tmplval); + $$ = tmplval.opval; } | expr ; @@ -310,106 +518,90 @@ mnexpr : nexpr { $$ = $1; intro_my(); } ; -mtexpr : texpr - { $$ = $1; intro_my(); } - ; - miexpr : iexpr { $$ = $1; intro_my(); } ; -/* Optional "MAIN:"-style loop labels */ -label : /* empty */ - { $$ = Nullch; } - | LABEL - ; - -/* Some kind of declaration - does not take part in the parse tree */ -decl : format - { $$ = 0; } - | subrout - { $$ = 0; } - | mysubrout - { $$ = 0; } - | package - { $$ = 0; } - | use - { $$ = 0; } - ; - -format : FORMAT startformsub formname block - { newFORM($2, $3, $4); } - ; - formname: WORD { $$ = $1; } - | /* NULL */ { $$ = Nullop; } - ; - -/* Unimplemented "my sub foo { }" */ -mysubrout: MYSUB startsub subname proto subattrlist subbody - { newMYSUB($2, $3, $4, $5, $6); } - ; - -/* Subroutine definition */ -subrout : SUB startsub subname proto subattrlist subbody - { newATTRSUB($2, $3, $4, $5, $6); } + | /* NULL */ { $$ = (OP*)NULL; } ; startsub: /* NULL */ /* start a regular subroutine scope */ - { $$ = start_subparse(FALSE, 0); } + { $$ = start_subparse(FALSE, 0); + SAVEFREESV(PL_compcv); } + ; startanonsub: /* NULL */ /* start an anonymous subroutine scope */ - { $$ = start_subparse(FALSE, CVf_ANON); } + { $$ = start_subparse(FALSE, CVf_ANON); + SAVEFREESV(PL_compcv); } ; startformsub: /* NULL */ /* start a format subroutine scope */ - { $$ = start_subparse(TRUE, 0); } + { $$ = start_subparse(TRUE, 0); + SAVEFREESV(PL_compcv); } ; /* Name of a subroutine - must be a bareword, could be special */ -subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a); - if (strEQ(name, "BEGIN") || strEQ(name, "END") - || strEQ(name, "INIT") || strEQ(name, "CHECK")) - CvSPECIAL_on(PL_compcv); - $$ = $1; } +subname : WORD + | PRIVATEREF ; /* Subroutine prototype */ proto : /* NULL */ - { $$ = Nullop; } + { $$ = (OP*)NULL; } | THING ; /* Optional list of subroutine attributes */ subattrlist: /* NULL */ - { $$ = Nullop; } + { $$ = (OP*)NULL; } | COLONATTR THING { $$ = $2; } | COLONATTR - { $$ = Nullop; } + { $$ = (OP*)NULL; } ; /* List of attributes for a "my" variable declaration */ myattrlist: COLONATTR THING { $$ = $2; } | COLONATTR - { $$ = Nullop; } + { $$ = (OP*)NULL; } ; -/* Subroutine body - either null or a block */ -subbody : block { $$ = $1; } - | ';' { $$ = Nullop; PL_expect = XSTATE; } +/* Optional subroutine signature */ +subsignature: /* NULL */ { $$ = (OP*)NULL; } + | '(' + { + if (!FEATURE_SIGNATURES_IS_ENABLED) + Perl_croak(aTHX_ "Experimental " + "subroutine signatures not enabled"); + Perl_ck_warner_d(aTHX_ + packWARN(WARN_EXPERIMENTAL__SIGNATURES), + "The signatures feature is experimental"); + $$ = parse_subsignature(); + } + ')' + { + $$ = op_append_list(OP_LINESEQ, $2, + newSTATEOP(0, NULL, sawparens(newNULLLIST()))); + parser->expect = XBLOCK; + } ; -package : PACKAGE WORD ';' - { package($2); } +/* Subroutine body - block with optional signature */ +realsubbody: remember subsignature '{' stmtseq '}' + { + if (parser->copline > (line_t)$3) + parser->copline = (line_t)$3; + $$ = block_end($1, + op_append_list(OP_LINESEQ, $2, $4)); + } ; -use : USE startsub - { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ } - WORD WORD listexpr ';' - { utilize($1, $2, $4, $5, $6); } +/* Optional subroutine body, for named subroutine declaration */ +optsubbody: realsubbody { $$ = $1; } + | ';' { $$ = (OP*)NULL; } ; /* Ordinary expressions; logical combinations */ @@ -419,53 +611,64 @@ expr : expr ANDOP expr { $$ = newLOGOP($2, 0, $1, $3); } | expr DOROP expr { $$ = newLOGOP(OP_DOR, 0, $1, $3); } - | argexpr %prec PREC_LOW + | listexpr %prec PREC_LOW ; /* Expressions are a list of terms joined by commas */ -argexpr : argexpr ',' +listexpr: listexpr ',' { $$ = $1; } - | argexpr ',' term - { $$ = append_elem(OP_LIST, $1, $3); } + | listexpr ',' term + { + OP* term = $3; + $$ = op_append_elem(OP_LIST, $1, term); + } | term %prec PREC_LOW ; /* List operators */ -listop : LSTOP indirob argexpr /* print $fh @args */ +listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */ { $$ = convert($1, OPf_STACKED, - prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); } + op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); + } | FUNC '(' indirob expr ')' /* print ($fh @args */ { $$ = convert($1, OPf_STACKED, - prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); } - | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */ + op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); + } + | term ARROW method '(' optexpr ')' /* $foo->bar(list) */ { $$ = convert(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, - prepend_elem(OP_LIST, scalar($1), $5), - newUNOP(OP_METHOD, 0, $3))); } + op_append_elem(OP_LIST, + op_prepend_elem(OP_LIST, scalar($1), $5), + newUNOP(OP_METHOD, 0, $3))); + } | term ARROW method /* $foo->bar */ { $$ = convert(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, scalar($1), - newUNOP(OP_METHOD, 0, $3))); } - | METHOD indirob listexpr /* new Class @args */ + op_append_elem(OP_LIST, scalar($1), + newUNOP(OP_METHOD, 0, $3))); + } + | METHOD indirob optlistexpr /* new Class @args */ { $$ = convert(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, - prepend_elem(OP_LIST, $2, $3), - newUNOP(OP_METHOD, 0, $1))); } - | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */ + op_append_elem(OP_LIST, + op_prepend_elem(OP_LIST, $2, $3), + newUNOP(OP_METHOD, 0, $1))); + } + | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */ { $$ = convert(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, - prepend_elem(OP_LIST, $2, $4), - newUNOP(OP_METHOD, 0, $1))); } - | LSTOP listexpr /* print @args */ + op_append_elem(OP_LIST, + op_prepend_elem(OP_LIST, $2, $4), + newUNOP(OP_METHOD, 0, $1))); + } + | LSTOP optlistexpr /* print @args */ { $$ = convert($1, 0, $2); } - | FUNC '(' listexprcom ')' /* print (@args) */ + | FUNC '(' optexpr ')' /* print (@args) */ { $$ = convert($1, 0, $3); } - | LSTOPSUB startanonsub block /* map { foo } ... */ - { $3 = newANONATTRSUB($2, 0, Nullop, $3); } - listexpr %prec LSTOP /* ... @bar */ + | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */ + { SvREFCNT_inc_simple_void(PL_compcv); + $$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); } + optlistexpr %prec LSTOP /* ... @bar */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, - prepend_elem(OP_LIST, $3, $5), $1)); } + op_append_elem(OP_LIST, + op_prepend_elem(OP_LIST, $4, $5), $1)); + } ; /* Names of methods. May use $object->$methodname */ @@ -474,60 +677,67 @@ method : METHOD ; /* Some kind of subscripted expression */ -subscripted: star '{' expr ';' '}' /* *main::{something} */ +subscripted: gelem '{' expr ';' '}' /* *main::{something} */ /* In this and all the hash accessors, ';' is * provided by the tokeniser */ - { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); - PL_expect = XOPERATOR; } + { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); } | scalar '[' expr ']' /* $array[$element] */ - { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); } + { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); + } | term ARROW '[' expr ']' /* somearef->[$element] */ { $$ = newBINOP(OP_AELEM, 0, ref(newAVREF($1),OP_RV2AV), - scalar($4));} + scalar($4)); + } | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */ { $$ = newBINOP(OP_AELEM, 0, ref(newAVREF($1),OP_RV2AV), - scalar($3));} - | scalar '{' expr ';' '}' /* $foo->{bar();} */ + scalar($3)); + } + | scalar '{' expr ';' '}' /* $foo{bar();} */ { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3)); - PL_expect = XOPERATOR; } + } | term ARROW '{' expr ';' '}' /* somehref->{bar();} */ { $$ = newBINOP(OP_HELEM, 0, ref(newHVREF($1),OP_RV2HV), - jmaybe($4)); - PL_expect = XOPERATOR; } + jmaybe($4)); } | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */ { $$ = newBINOP(OP_HELEM, 0, ref(newHVREF($1),OP_RV2HV), - jmaybe($3)); - PL_expect = XOPERATOR; } + jmaybe($3)); } | term ARROW '(' ')' /* $subref->() */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, newCVREF(0, scalar($1))); } | term ARROW '(' expr ')' /* $subref->(@args) */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, $4, + op_append_elem(OP_LIST, $4, newCVREF(0, scalar($1)))); } | subscripted '(' expr ')' /* $foo->{bar}->(@args) */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, $3, + op_append_elem(OP_LIST, $3, newCVREF(0, scalar($1)))); } | subscripted '(' ')' /* $foo->{bar}->() */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, newCVREF(0, scalar($1))); } + | '(' expr ')' '[' expr ']' /* list slice */ + { $$ = newSLICEOP(0, $5, $2); } + | QWLIST '[' expr ']' /* list literal slice */ + { $$ = newSLICEOP(0, $3, $1); } + | '(' ')' '[' expr ']' /* empty list slice! */ + { $$ = newSLICEOP(0, $4, (OP*)NULL); } ; /* Binary operators between terms */ -termbinop : term ASSIGNOP term /* $x = $y */ +termbinop: term ASSIGNOP term /* $x = $y */ { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); } | term POWOP term /* $x ** $y */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } | term MULOP term /* $x * $y, $x x $y */ { if ($2 != OP_REPEAT) scalar($1); - $$ = newBINOP($2, 0, $1, scalar($3)); } + $$ = newBINOP($2, 0, $1, scalar($3)); + } | term ADDOP term /* $x + $y */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } | term SHIFTOP term /* $x >> $y, $x << $y */ @@ -541,7 +751,7 @@ termbinop : term ASSIGNOP term /* $x = $y */ | term BITOROP term /* $x | $y */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } | term DOTDOT term /* $x..$y, $x...$y */ - { $$ = newRANGE($2, scalar($1), scalar($3));} + { $$ = newRANGE($2, scalar($1), scalar($3)); } | term ANDAND term /* $x && $y */ { $$ = newLOGOP(OP_AND, 0, $1, $3); } | term OROR term /* $x || $y */ @@ -557,22 +767,34 @@ termunop : '-' term %prec UMINUS /* -$x */ { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); } | '+' term %prec UMINUS /* +$x */ { $$ = $2; } + | '!' term /* !$x */ { $$ = newUNOP(OP_NOT, 0, scalar($2)); } | '~' term /* ~$x */ - { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));} + { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2)); } | term POSTINC /* $x++ */ { $$ = newUNOP(OP_POSTINC, 0, - mod(scalar($1), OP_POSTINC)); } + op_lvalue(scalar($1), OP_POSTINC)); } | term POSTDEC /* $x-- */ { $$ = newUNOP(OP_POSTDEC, 0, - mod(scalar($1), OP_POSTDEC)); } + op_lvalue(scalar($1), OP_POSTDEC));} + | term POSTJOIN /* implicit join after interpolated ->@ */ + { $$ = convert(OP_JOIN, 0, + op_append_elem( + OP_LIST, + newSVREF(scalar( + newSVOP(OP_CONST,0, + newSVpvs("\"")) + )), + $1 + )); + } | PREINC term /* ++$x */ { $$ = newUNOP(OP_PREINC, 0, - mod(scalar($2), OP_PREINC)); } + op_lvalue(scalar($2), OP_PREINC)); } | PREDEC term /* --$x */ { $$ = newUNOP(OP_PREDEC, 0, - mod(scalar($2), OP_PREDEC)); } + op_lvalue(scalar($2), OP_PREDEC)); } ; @@ -580,48 +802,22 @@ termunop : '-' term %prec UMINUS /* -$x */ anonymous: '[' expr ']' { $$ = newANONLIST($2); } | '[' ']' - { $$ = newANONLIST(Nullop); } + { $$ = newANONLIST((OP*)NULL);} | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */ { $$ = newANONHASH($2); } | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */ - { $$ = newANONHASH(Nullop); } - | ANONSUB startanonsub proto subattrlist block %prec '(' - { $$ = newANONATTRSUB($2, $3, $4, $5); } + { $$ = newANONHASH((OP*)NULL); } + | ANONSUB startanonsub proto subattrlist realsubbody %prec '(' + { SvREFCNT_inc_simple_void(PL_compcv); + $$ = newANONATTRSUB($2, $3, $4, $5); } ; /* Things called with "do" */ termdo : DO term %prec UNIOP /* do $filename */ - { $$ = dofile($2); } + { $$ = dofile($2, $1);} | DO block %prec '(' /* do { code */ - { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); } - | DO WORD '(' ')' /* do somesub() */ - { $$ = newUNOP(OP_ENTERSUB, - OPf_SPECIAL|OPf_STACKED, - prepend_elem(OP_LIST, - scalar(newCVREF( - (OPpENTERSUB_AMPER<<8), - scalar($2) - )),Nullop)); dep();} - | DO WORD '(' expr ')' /* do somesub(@args) */ - { $$ = newUNOP(OP_ENTERSUB, - OPf_SPECIAL|OPf_STACKED, - append_elem(OP_LIST, - $4, - scalar(newCVREF( - (OPpENTERSUB_AMPER<<8), - scalar($2) - )))); dep();} - | DO scalar '(' ')' /* do $subref () */ - { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED, - prepend_elem(OP_LIST, - scalar(newCVREF(0,scalar($2))), Nullop)); dep();} - | DO scalar '(' expr ')' /* do $subref (@args) */ - { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED, - prepend_elem(OP_LIST, - $4, - scalar(newCVREF(0,scalar($2))))); dep();} - + { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));} ; term : termbinop @@ -631,13 +827,15 @@ term : termbinop | term '?' term ':' term { $$ = newCONDOP(0, $1, $3, $5); } | REFGEN term /* \$x, \@y, \%z */ - { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); } + { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN)); } | myattrterm %prec UNIOP { $$ = $1; } | LOCAL term %prec UNIOP { $$ = localize($2,$1); } | '(' expr ')' { $$ = sawparens($2); } + | QWLIST + { $$ = $1; } | '(' ')' { $$ = sawparens(newNULLLIST()); } | scalar %prec '(' @@ -652,68 +850,132 @@ term : termbinop { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));} | subscripted { $$ = $1; } - | '(' expr ')' '[' expr ']' /* list slice */ - { $$ = newSLICEOP(0, $5, $2); } - | '(' ')' '[' expr ']' /* empty list slice! */ - { $$ = newSLICEOP(0, $4, Nullop); } - | ary '[' expr ']' /* array slice */ - { $$ = prepend_elem(OP_ASLICE, + | sliceme '[' expr ']' /* array slice */ + { $$ = op_prepend_elem(OP_ASLICE, newOP(OP_PUSHMARK, 0), newLISTOP(OP_ASLICE, 0, list($3), - ref($1, OP_ASLICE))); } - | ary '{' expr ';' '}' /* @hash{@keys} */ - { $$ = prepend_elem(OP_HSLICE, + ref($1, OP_ASLICE))); + if ($$ && $1) + $$->op_private |= + $1->op_private & OPpSLICEWARNING; + } + | kvslice '[' expr ']' /* array key/value slice */ + { $$ = op_prepend_elem(OP_KVASLICE, + newOP(OP_PUSHMARK, 0), + newLISTOP(OP_KVASLICE, 0, + list($3), + ref(oopsAV($1), OP_KVASLICE))); + if ($$ && $1) + $$->op_private |= + $1->op_private & OPpSLICEWARNING; + } + | sliceme '{' expr ';' '}' /* @hash{@keys} */ + { $$ = op_prepend_elem(OP_HSLICE, newOP(OP_PUSHMARK, 0), newLISTOP(OP_HSLICE, 0, list($3), ref(oopsHV($1), OP_HSLICE))); - PL_expect = XOPERATOR; } + if ($$ && $1) + $$->op_private |= + $1->op_private & OPpSLICEWARNING; + } + | kvslice '{' expr ';' '}' /* %hash{@keys} */ + { $$ = op_prepend_elem(OP_KVHSLICE, + newOP(OP_PUSHMARK, 0), + newLISTOP(OP_KVHSLICE, 0, + list($3), + ref($1, OP_KVHSLICE))); + if ($$ && $1) + $$->op_private |= + $1->op_private & OPpSLICEWARNING; + } | THING %prec '(' { $$ = $1; } | amper /* &foo; */ { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); } - | amper '(' ')' /* &foo() */ - { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } - | amper '(' expr ')' /* &foo(@args) */ + | amper '(' ')' /* &foo() or foo() */ + { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); + } + | amper '(' expr ')' /* &foo(@args) or foo(@args) */ + { + $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, + op_append_elem(OP_LIST, $3, scalar($1))); + } + | NOAMP subname optlistexpr /* foo @args (no parens) */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, $3, scalar($1))); } - | NOAMP WORD listexpr /* foo(@args) */ - { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, $3, scalar($2))); } + op_append_elem(OP_LIST, $3, scalar($2))); + } + | term ARROW '$' '*' + { $$ = newSVREF($1); } + | term ARROW '@' '*' + { $$ = newAVREF($1); } + | term ARROW '%' '*' + { $$ = newHVREF($1); } + | term ARROW '&' '*' + { $$ = newUNOP(OP_ENTERSUB, 0, + scalar(newCVREF($3,$1))); } + | term ARROW '*' '*' %prec '(' + { $$ = newGVREF(0,$1); } | LOOPEX /* loop exiting command (goto, last, dump, etc) */ { $$ = newOP($1, OPf_SPECIAL); PL_hints |= HINT_BLOCK_SCOPE; } | LOOPEX term { $$ = newLOOPEX($1,$2); } - | NOTOP argexpr /* not $foo */ + | NOTOP listexpr /* not $foo */ { $$ = newUNOP(OP_NOT, 0, scalar($2)); } | UNIOP /* Unary op, $_ implied */ { $$ = newOP($1, 0); } - | UNIOP block /* eval { foo }, I *think* */ + | UNIOP block /* eval { foo }* */ { $$ = newUNOP($1, 0, $2); } | UNIOP term /* Unary op */ { $$ = newUNOP($1, 0, $2); } + | REQUIRE /* require, $_ implied */ + { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); } + | REQUIRE term /* require Foo */ + { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); } + | UNIOPSUB + { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } | UNIOPSUB term /* Sub treated as unop */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, $2, scalar($1))); } + op_append_elem(OP_LIST, $2, scalar($1))); } | FUNC0 /* Nullary operator */ { $$ = newOP($1, 0); } | FUNC0 '(' ')' - { $$ = newOP($1, 0); } + { $$ = newOP($1, 0);} + | FUNC0OP /* Same as above, but op created in toke.c */ + { $$ = $1; } + | FUNC0OP '(' ')' + { $$ = $1; } | FUNC0SUB /* Sub treated as nullop */ - { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, - scalar($1)); } + { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } | FUNC1 '(' ')' /* not () */ - { $$ = newOP($1, OPf_SPECIAL); } + { $$ = ($1 == OP_NOT) + ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0))) + : newOP($1, OPf_SPECIAL); } | FUNC1 '(' expr ')' /* not($foo) */ { $$ = newUNOP($1, 0, $3); } - | PMFUNC '(' term ')' /* split (/foo/) */ - { $$ = pmruntime($1, $3, Nullop); } - | PMFUNC '(' term ',' term ')' /* split (/foo/,$bar) */ - { $$ = pmruntime($1, $3, $5); } + | PMFUNC /* m//, s///, qr//, tr/// */ + { + if ( $1->op_type != OP_TRANS + && $1->op_type != OP_TRANSR + && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV)) + { + $$ = start_subparse(FALSE, CVf_ANON); + SAVEFREESV(PL_compcv); + } else + $$ = 0; + } + '(' listexpr ')' + { $$ = pmruntime($1, $4, 1, $2); } | WORD | listop + | YADAYADA + { + $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0), + newSVOP(OP_CONST, 0, newSVpvs("Unimplemented"))); + } + | PLUGEXPR ; /* "my" declarations, with optional attributes */ @@ -728,6 +990,7 @@ myterm : '(' expr ')' { $$ = sawparens($2); } | '(' ')' { $$ = sawparens(newNULLLIST()); } + | scalar %prec '(' { $$ = $1; } | hsh %prec '(' @@ -737,24 +1000,22 @@ myterm : '(' expr ')' ; /* Basic list expressions */ -listexpr: /* NULL */ %prec PREC_LOW - { $$ = Nullop; } - | argexpr %prec PREC_LOW +optlistexpr: /* NULL */ %prec PREC_LOW + { $$ = (OP*)NULL; } + | listexpr %prec PREC_LOW { $$ = $1; } ; -listexprcom: /* NULL */ - { $$ = Nullop; } +optexpr: /* NULL */ + { $$ = (OP*)NULL; } | expr { $$ = $1; } - | expr ',' - { $$ = $1; } ; /* A little bit of trickery to make "for my $foo (@bar)" actually be lexical */ my_scalar: scalar - { PL_in_my = 0; $$ = my($1); } + { parser->in_my = 0; $$ = my($1); } ; amper : '&' indirob @@ -766,39 +1027,50 @@ scalar : '$' indirob ; ary : '@' indirob - { $$ = newAVREF($2); } + { $$ = newAVREF($2); + if ($$) $$->op_private |= $1; + } ; hsh : '%' indirob - { $$ = newHVREF($2); } + { $$ = newHVREF($2); + if ($$) $$->op_private |= $1; + } ; arylen : DOLSHARP indirob { $$ = newAVREF($2); } + | term ARROW DOLSHARP '*' + { $$ = newAVREF($1); } ; star : '*' indirob { $$ = newGVREF(0,$2); } ; +sliceme : ary + | term ARROW '@' + { $$ = newAVREF($1); } + ; + +kvslice : hsh + | term ARROW '%' + { $$ = newHVREF($1); } + ; + +gelem : star + | term ARROW '*' + { $$ = newGVREF(0,$1); } + ; + /* Indirect objects */ indirob : WORD { $$ = scalar($1); } | scalar %prec PREC_LOW - { $$ = scalar($1); } + { $$ = scalar($1); } | block - { $$ = scope($1); } + { $$ = op_scope($1); } | PRIVATEREF { $$ = $1; } ; - -%% /* PROGRAM */ - -/* more stuff added to make perly_c.diff easier to apply */ - -#ifdef yyparse -#undef yyparse -#endif -#define yyparse() Perl_yyparse(pTHX) -