X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/0f602692adc580e73d2236976e9da7fec968ca93..20dbe1c7dfa1982373fd6506d893439d0de91db0:/perly.y diff --git a/perly.y b/perly.y index 6ba61ae..ff14780 100644 --- a/perly.y +++ b/perly.y @@ -31,7 +31,7 @@ /* Make the parser re-entrant. */ -%pure_parser +%pure-parser %start grammar @@ -45,9 +45,9 @@ %token GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ -%token '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';' '=' '.' +%token '{' '}' '[' ']' '-' '+' '@' '%' '&' '=' '.' -%token WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST +%token BAREWORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST %token FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB %token PLUGEXPR PLUGSTMT %token LABEL @@ -69,12 +69,15 @@ %type stmtseq fullstmt labfullstmt barestmt block mblock else %type expr term subscripted scalar ary hsh arylen star amper sideff %type sliceme kvslice gelem -%type listexpr nexpr texpr iexpr mexpr mnexpr miexpr -%type optlistexpr optexpr indirob listop method +%type listexpr nexpr texpr iexpr mexpr mnexpr +%type optlistexpr optexpr optrepl indirob listop method %type formname subname proto optsubbody cont my_scalar my_var %type refgen_topic formblock %type subattrlist myattrlist myattrterm myterm -%type realsubbody subsignature termbinop termunop anonymous termdo +%type termbinop termunop anonymous termdo +%type sigslurpsigil +%type sigvarname sigdefault sigscalarelem sigslurpelem +%type sigelem siglist siglistornull subsignature %type formstmtseq formline formarg %nonassoc PREC_LOW @@ -260,9 +263,9 @@ barestmt: PLUGSTMT newFORM($2, $3, $4); $$ = (OP*)NULL; if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) { - SvREFCNT_inc_simple_void(fmtcv); - pad_add_anon(fmtcv, OP_NULL); + pad_add_weakref(fmtcv); } + parser->parsed_sub = 1; } | SUB subname startsub { @@ -297,7 +300,46 @@ barestmt: PLUGSTMT intro_my(); parser->parsed_sub = 1; } - | PACKAGE WORD WORD ';' + | 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; + } + remember subsignature subattrlist '{' stmtseq '}' + { + OP *body; + if (parser->copline > (line_t)$8) + parser->copline = (line_t)$8; + body = block_end($5, + op_append_list(OP_LINESEQ, $6, $9)); + + SvREFCNT_inc_simple_void(PL_compcv); + $2->op_type == OP_CONST + ? newATTRSUB($3, $2, NULL, $7, body) + : newMYSUB($3, $2, NULL, $7, body) + ; + $$ = (OP*)NULL; + intro_my(); + parser->parsed_sub = 1; + } + | PACKAGE BAREWORD BAREWORD ';' { package($3); if ($2) @@ -306,7 +348,7 @@ barestmt: PLUGSTMT } | USE startsub { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ } - WORD WORD optlistexpr ';' + BAREWORD BAREWORD optlistexpr ';' { SvREFCNT_inc_simple_void(PL_compcv); utilize($1, $2, $4, $5, $6); @@ -319,21 +361,15 @@ barestmt: PLUGSTMT newCONDOP(0, $4, op_scope($6), $7)); parser->copline = (line_t)$1; } - | UNLESS '(' remember miexpr ')' mblock else + | UNLESS '(' remember mexpr ')' mblock else { $$ = block_end($3, - newCONDOP(0, $4, op_scope($6), $7)); + newCONDOP(0, $4, $7, op_scope($6))); 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)); + $$ = block_end($3, newGIVENOP($4, op_scope($6), 0)); parser->copline = (line_t)$1; } | WHEN '(' remember mexpr ')' mblock @@ -385,18 +421,18 @@ barestmt: PLUGSTMT op_lvalue($2, OP_ENTERLOOP), $5, $7, $8)); parser->copline = (line_t)$1; } - | FOR REFGEN MY remember my_var - { parser->in_my = 0; $$ = my($5); } + | FOR my_refgen remember my_var + { parser->in_my = 0; $$ = my($4); } '(' mexpr ')' mblock cont { $$ = block_end( - $4, + $3, newFOROP(0, op_lvalue( newUNOP(OP_REFGEN, 0, - $6), + $5), OP_ENTERLOOP), - $8, $10, $11) + $7, $9, $10) ); parser->copline = (line_t)$1; } @@ -420,7 +456,7 @@ barestmt: PLUGSTMT $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL, (OP*)NULL, $1, $2, 0); } - | PACKAGE WORD WORD '{' remember + | PACKAGE BAREWORD BAREWORD '{' remember { package($3); if ($2) { @@ -548,11 +584,7 @@ mnexpr : nexpr { $$ = $1; intro_my(); } ; -miexpr : iexpr - { $$ = $1; intro_my(); } - ; - -formname: WORD { $$ = $1; } +formname: BAREWORD { $$ = $1; } | /* NULL */ { $$ = (OP*)NULL; } ; @@ -573,7 +605,7 @@ startformsub: /* NULL */ /* start a format subroutine scope */ ; /* Name of a subroutine - must be a bareword, could be special */ -subname : WORD +subname : BAREWORD | PRIVATEREF ; @@ -599,38 +631,196 @@ myattrlist: COLONATTR THING { $$ = (OP*)NULL; } ; -/* 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(); - } - ')' + + +/* -------------------------------------- + * subroutine signature parsing + */ + +/* the '' or 'foo' part of a '$' or '@foo' etc signature variable */ +sigvarname: /* NULL */ + { parser->in_my = 0; $$ = (OP*)NULL; } + | PRIVATEREF + { parser->in_my = 0; $$ = $1; } + ; + +sigslurpsigil: + '@' + { $$ = '@'; } + | '%' + { $$ = '%'; } + +/* @, %, @foo, %foo */ +sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */ + { + I32 sigil = $1; + OP *var = $2; + OP *defexpr = $3; + + if (parser->sig_slurpy) + yyerror("Multiple slurpy parameters not allowed"); + parser->sig_slurpy = (char)sigil; + + if (defexpr) + yyerror("A slurpy parameter may not have " + "a default value"); + + $$ = var ? newSTATEOP(0, NULL, var) : (OP*)NULL; + } + ; + +/* default part of sub signature scalar element: i.e. '= default_expr' */ +sigdefault: /* NULL */ + { $$ = (OP*)NULL; } + | ASSIGNOP + { $$ = newOP(OP_NULL, 0); } + | ASSIGNOP term + { $$ = $2; } + + +/* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */ +sigscalarelem: + '$' sigvarname sigdefault + { + OP *var = $2; + OP *defexpr = $3; + + if (parser->sig_slurpy) + yyerror("Slurpy parameter not last"); + + parser->sig_elems++; + + if (defexpr) { + parser->sig_optelems++; + + if ( defexpr->op_type == OP_NULL + && !(defexpr->op_flags & OPf_KIDS)) + { + /* handle '$=' special case */ + if (var) + yyerror("Optional parameter " + "lacks default expression"); + op_free(defexpr); + } + else { + /* a normal '=default' expression */ + OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM, + defexpr, + LINKLIST(defexpr)); + /* re-purpose op_targ to hold @_ index */ + defop->op_targ = + (PADOFFSET)(parser->sig_elems - 1); + + if (var) { + var->op_flags |= OPf_STACKED; + (void)op_sibling_splice(var, + NULL, 0, defop); + scalar(defop); + } + else + var = newUNOP(OP_NULL, 0, defop); + + LINKLIST(var); + /* NB: normally the first child of a + * logop is executed before the logop, + * and it pushes a boolean result + * ready for the logop. For ARGDEFELEM, + * the op itself does the boolean + * calculation, so set the first op to + * it instead. + */ + var->op_next = defop; + defexpr->op_next = var; + } + } + else { + if (parser->sig_optelems) + yyerror("Mandatory parameter " + "follows optional parameter"); + } + + $$ = var ? newSTATEOP(0, NULL, var) : (OP*)NULL; + } + ; + + +/* subroutine signature element: e.g. '$x = $default' or '%h' */ +sigelem: sigscalarelem + { parser->in_my = KEY_sigvar; $$ = $1; } + | sigslurpelem + { parser->in_my = KEY_sigvar; $$ = $1; } + ; + +/* list of subroutine signature elements */ +siglist: + siglist ',' + { $$ = $1; } + | siglist ',' sigelem { - $$ = op_append_list(OP_LINESEQ, $2, - newSTATEOP(0, NULL, sawparens(newNULLLIST()))); - parser->expect = XBLOCK; + $$ = op_append_list(OP_LINESEQ, $1, $3); } + | sigelem %prec PREC_LOW + { $$ = $1; } ; -/* Subroutine body - block with optional signature */ -realsubbody: remember subsignature '{' stmtseq '}' +/* () or (....) */ +siglistornull: /* NULL */ + { $$ = (OP*)NULL; } + | siglist + { $$ = $1; } + +/* Subroutine signature */ +subsignature: '(' + { + ENTER; + SAVEIV(parser->sig_elems); + SAVEIV(parser->sig_optelems); + SAVEI8(parser->sig_slurpy); + parser->sig_elems = 0; + parser->sig_optelems = 0; + parser->sig_slurpy = 0; + parser->in_my = KEY_sigvar; + } + siglistornull + ')' { - if (parser->copline > (line_t)$3) - parser->copline = (line_t)$3; - $$ = block_end($1, - op_append_list(OP_LINESEQ, $2, $4)); + OP *sigops = $3; + UNOP_AUX_item *aux; + OP *check; + + assert(FEATURE_SIGNATURES_IS_ENABLED); + + /* We shouldn't get here otherwise */ + Perl_ck_warner_d(aTHX_ + packWARN(WARN_EXPERIMENTAL__SIGNATURES), + "The signatures feature is experimental"); + + aux = (UNOP_AUX_item*)PerlMemShared_malloc( + sizeof(UNOP_AUX_item) * 3); + aux[0].iv = parser->sig_elems; + aux[1].iv = parser->sig_optelems; + aux[2].iv = parser->sig_slurpy; + check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux); + sigops = op_prepend_elem(OP_LINESEQ, check, sigops); + sigops = op_prepend_elem(OP_LINESEQ, + newSTATEOP(0, NULL, NULL), + sigops); + /* a nextstate at the end handles context + * correctly for an empty sub body */ + $$ = op_append_elem(OP_LINESEQ, + sigops, + newSTATEOP(0, NULL, NULL)); + + parser->in_my = 0; + parser->expect = XATTRBLOCK; + LEAVE; } ; + + /* Optional subroutine body, for named subroutine declaration */ -optsubbody: realsubbody { $$ = $1; } +optsubbody: block | ';' { $$ = (OP*)NULL; } ; @@ -648,7 +838,7 @@ expr : expr ANDOP expr listexpr: listexpr ',' { $$ = $1; } | listexpr ',' term - { + { OP* term = $3; $$ = op_append_elem(OP_LIST, $1, term); } @@ -801,7 +991,7 @@ termunop : '-' term %prec UMINUS /* -$x */ | '!' term /* !$x */ { $$ = newUNOP(OP_NOT, 0, scalar($2)); } | '~' term /* ~$x */ - { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2)); } + { $$ = newUNOP($1, 0, scalar($2)); } | term POSTINC /* $x++ */ { $$ = newUNOP(OP_POSTINC, 0, op_lvalue(scalar($1), OP_POSTINC)); } @@ -837,9 +1027,19 @@ anonymous: '[' expr ']' { $$ = newANONHASH($2); } | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */ { $$ = newANONHASH((OP*)NULL); } - | ANONSUB startanonsub proto subattrlist realsubbody %prec '(' + | ANONSUB startanonsub proto subattrlist block %prec '(' { SvREFCNT_inc_simple_void(PL_compcv); $$ = newANONATTRSUB($2, $3, $4, $5); } + | ANONSUB startanonsub remember subsignature subattrlist '{' stmtseq '}' %prec '(' + { + OP *body; + if (parser->copline > (line_t)$6) + parser->copline = (line_t)$6; + body = block_end($3, + op_append_list(OP_LINESEQ, $4, $7)); + SvREFCNT_inc_simple_void(PL_compcv); + $$ = newANONATTRSUB($2, NULL, $5, body); + } ; @@ -857,11 +1057,13 @@ term : termbinop | term '?' term ':' term { $$ = newCONDOP(0, $1, $3, $5); } | REFGEN term /* \$x, \@y, \%z */ - { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN)); } + { $$ = newUNOP(OP_REFGEN, 0, $2); } + | MY REFGEN term + { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); } | myattrterm %prec UNIOP { $$ = $1; } | LOCAL term %prec UNIOP - { $$ = localize($2,$1); } + { $$ = localize($2,0); } | '(' expr ')' { $$ = sawparens($2); } | QWLIST @@ -996,9 +1198,9 @@ term : termbinop } else $$ = 0; } - '(' listexpr ')' - { $$ = pmruntime($1, $4, 1, $2); } - | WORD + '(' listexpr optrepl ')' + { $$ = pmruntime($1, $4, $5, 1, $2); } + | BAREWORD | listop | YADAYADA { @@ -1012,7 +1214,9 @@ term : termbinop myattrterm: MY myterm myattrlist { $$ = my_attrs($2,$3); } | MY myterm - { $$ = localize($2,$1); } + { $$ = localize($2,1); } + | MY REFGEN myterm myattrlist + { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); } ; /* Things that can be "my"'d */ @@ -1042,6 +1246,12 @@ optexpr: /* NULL */ { $$ = $1; } ; +optrepl: /* NULL */ + { $$ = (OP*)NULL; } + | '/' expr + { $$ = $2; } + ; + /* A little bit of trickery to make "for my $foo (@bar)" actually be lexical */ my_scalar: scalar @@ -1057,6 +1267,10 @@ refgen_topic: my_var | amper ; +my_refgen: MY REFGEN + | REFGEN MY + ; + amper : '&' indirob { $$ = newCVREF($1,$2); } ; @@ -1103,7 +1317,7 @@ gelem : star ; /* Indirect objects */ -indirob : WORD +indirob : BAREWORD { $$ = scalar($1); } | scalar %prec PREC_LOW { $$ = scalar($1); }