X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/7d07dbc24d4d5bd5fee59acddff905153f4258bd..c0888b2172d1f2ba9efb15c34b86c07026be9abe:/perly.y diff --git a/perly.y b/perly.y index 5996527..1d20b04 100644 --- a/perly.y +++ b/perly.y @@ -1,6 +1,6 @@ /* perly.y * - * Copyright (c) 1991-1997, Larry Wall + * Copyright (c) 1991-2002, 2003, 2004 Larry Wall * * You may distribute under the terms of either the GNU General Public * License or the Artistic License, as specified in the README file. @@ -12,17 +12,18 @@ * All that is gold does not glitter, not all those who wander are lost.' */ -%{ -#include "EXTERN.h" -#include "perl.h" +/* 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. + */ -static void -dep() -{ - deprecate("\"do\" to call subroutines"); -} +/* Make the parser re-entrant. */ -%} +%pure_parser %start prog @@ -33,7 +34,7 @@ dep() GV *gvval; } -%token '{' ')' +%token '{' %token WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF %token FUNC0SUB UNIOPSUB LSTOPSUB @@ -44,18 +45,24 @@ dep() %token FUNC0 FUNC1 FUNC UNIOP LSTOP %token RELOP EQOP MULOP ADDOP %token DOLSHARP DO HASHBRACK NOAMP -%token LOCAL MY +%token LOCAL MY MYSUB REQUIRE +%token COLONATTR -%type prog decl local format startsub startanonsub startformsub -%type remember mremember '&' +%type prog decl format startsub startanonsub startformsub mintro +%type progstart remember mremember '&' savescope %type block mblock lineseq line loop cond else -%type expr term scalar ary hsh arylen star amper sideff -%type argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr +%type expr term subscripted scalar ary hsh arylen star amper sideff +%type argexpr nexpr texpr iexpr mexpr mnexpr 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 -%left OROP +%nonassoc PREC_LOW +%nonassoc LOOPEX + +%left OROP DOROP %left ANDOP %right NOTOP %nonassoc LSTOP LSTOPSUB @@ -63,7 +70,7 @@ dep() %right ASSIGNOP %right '?' ':' %nonassoc DOTDOT -%left OROR +%left OROR DORDOR %left ANDAND %left BITOROP %left BITANDOP @@ -78,24 +85,22 @@ dep() %right POWOP %nonassoc PREINC PREDEC POSTINC POSTDEC %left ARROW +%nonassoc ')' %left '(' +%left '[' '{' %% /* RULES */ -prog : /* NULL */ - { -#if defined(YYDEBUG) && defined(DEBUGGING) - yydebug = (debug & 1); -#endif - expect = XSTATE; - } +/* The whole program */ +prog : progstart /*CONTINUED*/ lineseq - { newPROG($2); } + { $$ = $1; newPROG(block_end($1,$2)); } ; +/* An ordinary block */ block : '{' remember lineseq '}' - { if (copline > (line_t)$1) - copline = $1; + { if (PL_copline > (line_t)$1) + PL_copline = (line_t)$1; $$ = block_end($2, $3); } ; @@ -103,9 +108,16 @@ remember: /* NULL */ /* start a full lexical scope */ { $$ = block_start(TRUE); } ; +progstart: + { + PL_expect = XSTATE; $$ = block_start(TRUE); + } + ; + + mblock : '{' mremember lineseq '}' - { if (copline > (line_t)$1) - copline = $1; + { if (PL_copline > (line_t)$1) + PL_copline = (line_t)$1; $$ = block_end($2, $3); } ; @@ -113,17 +125,23 @@ mremember: /* NULL */ /* start a partial lexical scope */ { $$ = block_start(FALSE); } ; +savescope: /* NULL */ /* remember stack pos in case of error */ + { $$ = PL_savestack_ix; } + +/* A collection of "lines" in the program */ lineseq : /* NULL */ { $$ = Nullop; } | lineseq decl { $$ = $1; } - | lineseq line - { $$ = append_list(OP_LINESEQ, - (LISTOP*)$1, (LISTOP*)$2); - pad_reset_pending = TRUE; - if ($1 && $2) hints |= HINT_BLOCK_SCOPE; } + | lineseq savescope line + { LEAVE_SCOPE($2); + $$ = append_list(OP_LINESEQ, + (LISTOP*)$1, (LISTOP*)$3); + PL_pad_reset_pending = TRUE; + if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; } ; +/* A "line" in the program */ line : label cond { $$ = newSTATEOP(0, $1, $2); } | loop /* loops add their own labels */ @@ -133,14 +151,15 @@ line : label cond } else { $$ = Nullop; - copline = NOLINE; + PL_copline = NOLINE; } - expect = XSTATE; } + PL_expect = XSTATE; } | label sideff ';' { $$ = newSTATEOP(0, $1, $2); - expect = XSTATE; } + PL_expect = XSTATE; } ; +/* An expression which may have a side-effect */ sideff : error { $$ = Nullop; } | expr @@ -153,86 +172,110 @@ sideff : error { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); } | expr UNTIL iexpr { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);} + | expr FOR expr + { $$ = newFOROP(0, Nullch, (line_t)$2, + Nullop, $3, $1, Nullop); } ; +/* else and elsif blocks */ else : /* NULL */ { $$ = Nullop; } | ELSE mblock - { $$ = scope($2); } + { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); } | ELSIF '(' mexpr ')' mblock else - { copline = $1; - $$ = newSTATEOP(0, Nullch, - newCONDOP(0, $3, scope($5), $6)); - hints |= HINT_BLOCK_SCOPE; } + { 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 - { copline = $1; + { PL_copline = (line_t)$1; $$ = block_end($3, newCONDOP(0, $4, scope($6), $7)); } | UNLESS '(' remember miexpr ')' mblock else - { copline = $1; + { PL_copline = (line_t)$1; $$ = block_end($3, newCONDOP(0, $4, scope($6), $7)); } ; +/* Continue blocks */ cont : /* NULL */ { $$ = Nullop; } | CONTINUE block { $$ = scope($2); } ; -loop : label WHILE '(' remember mtexpr ')' mblock cont - { copline = $2; +/* Loops: while, until, for, and a bare block */ +loop : label WHILE '(' remember texpr ')' mintro mblock cont + { PL_copline = (line_t)$2; $$ = block_end($4, newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, - $5, $7, $8))); } - | label UNTIL '(' remember miexpr ')' mblock cont - { copline = $2; + $2, $5, $8, $9, $7))); } + | label UNTIL '(' remember iexpr ')' mintro mblock cont + { PL_copline = (line_t)$2; $$ = block_end($4, newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, - $5, $7, $8))); } + $2, $5, $8, $9, $7))); } | label FOR MY remember my_scalar '(' mexpr ')' mblock cont { $$ = block_end($4, - newFOROP(0, $1, $2, $5, $7, $9, $10)); } + newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); } | label FOR scalar '(' remember mexpr ')' mblock cont { $$ = block_end($5, - newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP), + 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, $2, Nullop, $5, $7, $8)); } - | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock + newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); } + | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')' + mblock /* basically fake up an initialize-while lineseq */ - { copline = $2; - $$ = block_end($4, - newSTATEOP(0, $1, - append_elem(OP_LINESEQ, scalar($5), - newWHILEOP(0, 1, (LOOP*)Nullop, - scalar($7), - $11, scalar($9))))); } + { OP *forop; + PL_copline = (line_t)$2; + forop = newSTATEOP(0, $1, + newWHILEOP(0, 1, (LOOP*)Nullop, + $2, scalar($7), + $12, $10, $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, - Nullop, $2, $3)); } + { $$ = newSTATEOP(0, $1, + newWHILEOP(0, 1, (LOOP*)Nullop, + NOLINE, Nullop, $2, $3, 0)); } ; +/* 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; } | sideff ; +/* Boolean expression */ texpr : /* NULL means true */ - { (void)scan_num("1"); $$ = yylval.opval; } + { (void)scan_num("1", &yylval); $$ = yylval.opval; } | expr ; +/* Inverted boolean expression */ iexpr : expr { $$ = invert(scalar($1)); } ; +/* Expression with its own lexical scope */ mexpr : expr { $$ = $1; intro_my(); } ; @@ -241,23 +284,23 @@ 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 @@ -272,8 +315,14 @@ formname: WORD { $$ = $1; } | /* NULL */ { $$ = Nullop; } ; -subrout : SUB startsub subname proto subbody - { newSUB($2, $3, $4, $5); } +/* 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); } ; startsub: /* NULL */ /* start a regular subroutine scope */ @@ -288,293 +337,382 @@ startformsub: /* NULL */ /* start a format subroutine scope */ { $$ = start_subparse(TRUE, 0); } ; -subname : WORD { char *name = SvPV(((SVOP*)$1)->op_sv, na); +/* Name of a subroutine - must be a bareword, could be special */ +subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv); if (strEQ(name, "BEGIN") || strEQ(name, "END") - || strEQ(name, "INIT")) - CvUNIQUE_on(compcv); + || strEQ(name, "INIT") || strEQ(name, "CHECK")) + CvSPECIAL_on(PL_compcv); $$ = $1; } ; +/* Subroutine prototype */ proto : /* NULL */ { $$ = Nullop; } | THING ; +/* Optional list of subroutine attributes */ +subattrlist: /* NULL */ + { $$ = Nullop; } + | COLONATTR THING + { $$ = $2; } + | COLONATTR + { $$ = Nullop; } + ; + +/* List of attributes for a "my" variable declaration */ +myattrlist: COLONATTR THING + { $$ = $2; } + | COLONATTR + { $$ = Nullop; } + ; + +/* Subroutine body - either null or a block */ subbody : block { $$ = $1; } - | ';' { $$ = Nullop; expect = XSTATE; } + | ';' { $$ = Nullop; PL_expect = XSTATE; } ; package : PACKAGE WORD ';' { package($2); } - | PACKAGE ';' - { package(Nullop); } ; use : USE startsub - { CvUNIQUE_on(compcv); /* It's a BEGIN {} */ } + { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ } WORD WORD listexpr ';' { utilize($1, $2, $4, $5, $6); } ; +/* Ordinary expressions; logical combinations */ expr : expr ANDOP expr { $$ = newLOGOP(OP_AND, 0, $1, $3); } | expr OROP expr { $$ = newLOGOP($2, 0, $1, $3); } - | argexpr + | expr DOROP expr + { $$ = newLOGOP(OP_DOR, 0, $1, $3); } + | argexpr %prec PREC_LOW ; +/* Expressions are a list of terms joined by commas */ argexpr : argexpr ',' { $$ = $1; } | argexpr ',' term { $$ = append_elem(OP_LIST, $1, $3); } - | term + | term %prec PREC_LOW ; -listop : LSTOP indirob argexpr +/* List operators */ +listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */ { $$ = convert($1, OPf_STACKED, prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); } - | FUNC '(' indirob expr ')' + | FUNC '(' indirob expr ')' /* print ($fh @args */ { $$ = convert($1, OPf_STACKED, prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); } - | term ARROW method '(' listexprcom ')' + | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */ { $$ = convert(OP_ENTERSUB, OPf_STACKED, append_elem(OP_LIST, prepend_elem(OP_LIST, scalar($1), $5), newUNOP(OP_METHOD, 0, $3))); } - | METHOD indirob listexpr + | 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 */ { $$ = convert(OP_ENTERSUB, OPf_STACKED, append_elem(OP_LIST, prepend_elem(OP_LIST, $2, $3), newUNOP(OP_METHOD, 0, $1))); } - | FUNCMETH indirob '(' listexprcom ')' + | FUNCMETH indirob '(' listexprcom ')' /* 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 + | LSTOP listexpr /* print @args */ { $$ = convert($1, 0, $2); } - | FUNC '(' listexprcom ')' + | FUNC '(' listexprcom ')' /* print (@args) */ { $$ = convert($1, 0, $3); } - | LSTOPSUB startanonsub block - { $3 = newANONSUB($2, 0, $3); } - listexpr %prec LSTOP + | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */ + { $3 = newANONATTRSUB($2, 0, Nullop, $3); } + listexpr %prec LSTOP /* ... @bar */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, append_elem(OP_LIST, prepend_elem(OP_LIST, $3, $5), $1)); } ; +/* Names of methods. May use $object->$methodname */ method : METHOD | scalar ; -term : term ASSIGNOP term +/* Some kind of subscripted expression */ +subscripted: star '{' 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; } + | scalar '[' expr ']' /* $array[$element] */ + { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); } + | term ARROW '[' expr ']' /* somearef->[$element] */ + { $$ = newBINOP(OP_AELEM, 0, + ref(newAVREF($1),OP_RV2AV), + scalar($4));} + | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */ + { $$ = newBINOP(OP_AELEM, 0, + ref(newAVREF($1),OP_RV2AV), + 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; } + | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */ + { $$ = newBINOP(OP_HELEM, 0, + ref(newHVREF($1),OP_RV2HV), + jmaybe($3)); + PL_expect = XOPERATOR; } + | 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, + newCVREF(0, scalar($1)))); } + + | subscripted '(' expr ')' /* $foo->{bar}->(@args) */ + { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, + 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); } + | '(' ')' '[' expr ']' /* empty list slice! */ + { $$ = newSLICEOP(0, $4, Nullop); } + ; + +/* Binary operators between terms */ +termbinop : term ASSIGNOP term /* $x = $y */ { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); } - | term POWOP term + | term POWOP term /* $x ** $y */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } - | term MULOP term + | term MULOP term /* $x * $y, $x x $y */ { if ($2 != OP_REPEAT) scalar($1); $$ = newBINOP($2, 0, $1, scalar($3)); } - | term ADDOP term + | term ADDOP term /* $x + $y */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } - | term SHIFTOP term + | term SHIFTOP term /* $x >> $y, $x << $y */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } - | term RELOP term + | term RELOP term /* $x > $y, etc. */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } - | term EQOP term + | term EQOP term /* $x == $y, $x eq $y */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } - | term BITANDOP term + | term BITANDOP term /* $x & $y */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } - | term BITOROP term + | term BITOROP term /* $x | $y */ { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } - | term DOTDOT term + | term DOTDOT term /* $x..$y, $x...$y */ { $$ = newRANGE($2, scalar($1), scalar($3));} - | term ANDAND term + | term ANDAND term /* $x && $y */ { $$ = newLOGOP(OP_AND, 0, $1, $3); } - | term OROR term + | term OROR term /* $x || $y */ { $$ = newLOGOP(OP_OR, 0, $1, $3); } - | term '?' term ':' term - { $$ = newCONDOP(0, $1, $3, $5); } - | term MATCHOP term + | term DORDOR term /* $x // $y */ + { $$ = newLOGOP(OP_DOR, 0, $1, $3); } + | term MATCHOP term /* $x =~ /$y/ */ { $$ = bind_match($2, $1, $3); } + ; - | '-' term %prec UMINUS +/* Unary operators and terms */ +termunop : '-' term %prec UMINUS /* -$x */ { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); } - | '+' term %prec UMINUS + | '+' term %prec UMINUS /* +$x */ { $$ = $2; } - | '!' term + | '!' term /* !$x */ { $$ = newUNOP(OP_NOT, 0, scalar($2)); } - | '~' term + | '~' term /* ~$x */ { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));} - | REFGEN term - { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); } - | term POSTINC + | term POSTINC /* $x++ */ { $$ = newUNOP(OP_POSTINC, 0, mod(scalar($1), OP_POSTINC)); } - | term POSTDEC + | term POSTDEC /* $x-- */ { $$ = newUNOP(OP_POSTDEC, 0, mod(scalar($1), OP_POSTDEC)); } - | PREINC term + | PREINC term /* ++$x */ { $$ = newUNOP(OP_PREINC, 0, mod(scalar($2), OP_PREINC)); } - | PREDEC term + | PREDEC term /* --$x */ { $$ = newUNOP(OP_PREDEC, 0, mod(scalar($2), OP_PREDEC)); } - | local term %prec UNIOP + + ; + +/* Constructors for anonymous data */ +anonymous: '[' expr ']' + { $$ = newANONLIST($2); } + | '[' ']' + { $$ = newANONLIST(Nullop); } + | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */ + { $$ = newANONHASH($2); } + | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */ + { $$ = newANONHASH(Nullop); } + | ANONSUB startanonsub proto subattrlist block %prec '(' + { $$ = newANONATTRSUB($2, $3, $4, $5); } + + ; + +/* Things called with "do" */ +termdo : DO term %prec UNIOP /* do $filename */ + { $$ = 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();} + + ; + +term : termbinop + | termunop + | anonymous + | termdo + | term '?' term ':' term + { $$ = newCONDOP(0, $1, $3, $5); } + | REFGEN term /* \$x, \@y, \%z */ + { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); } + | myattrterm %prec UNIOP + { $$ = $1; } + | LOCAL term %prec UNIOP { $$ = localize($2,$1); } | '(' expr ')' { $$ = sawparens($2); } | '(' ')' { $$ = sawparens(newNULLLIST()); } - | '[' expr ']' %prec '(' - { $$ = newANONLIST($2); } - | '[' ']' %prec '(' - { $$ = newANONLIST(Nullop); } - | HASHBRACK expr ';' '}' %prec '(' - { $$ = newANONHASH($2); } - | HASHBRACK ';' '}' %prec '(' - { $$ = newANONHASH(Nullop); } - | ANONSUB startanonsub proto block %prec '(' - { $$ = newANONSUB($2, $3, $4); } | scalar %prec '(' { $$ = $1; } - | star '{' expr ';' '}' - { $$ = newBINOP(OP_GELEM, 0, newGVREF(0,$1), $3); } | star %prec '(' { $$ = $1; } - | scalar '[' expr ']' %prec '(' - { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); } - | term ARROW '[' expr ']' %prec '(' - { $$ = newBINOP(OP_AELEM, 0, - ref(newAVREF($1),OP_RV2AV), - scalar($4));} - | term '[' expr ']' %prec '(' - { assertref($1); $$ = newBINOP(OP_AELEM, 0, - ref(newAVREF($1),OP_RV2AV), - scalar($3));} | hsh %prec '(' { $$ = $1; } | ary %prec '(' { $$ = $1; } - | arylen %prec '(' + | arylen %prec '(' /* $#x, $#{ something } */ { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));} - | scalar '{' expr ';' '}' %prec '(' - { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3)); - expect = XOPERATOR; } - | term ARROW '{' expr ';' '}' %prec '(' - { $$ = newBINOP(OP_HELEM, 0, - ref(newHVREF($1),OP_RV2HV), - jmaybe($4)); - expect = XOPERATOR; } - | term '{' expr ';' '}' %prec '(' - { assertref($1); $$ = newBINOP(OP_HELEM, 0, - ref(newHVREF($1),OP_RV2HV), - jmaybe($3)); - expect = XOPERATOR; } - | '(' expr ')' '[' expr ']' %prec '(' - { $$ = newSLICEOP(0, $5, $2); } - | '(' ')' '[' expr ']' %prec '(' - { $$ = newSLICEOP(0, $4, Nullop); } - | ary '[' expr ']' %prec '(' + | subscripted + { $$ = $1; } + | ary '[' expr ']' /* array slice */ { $$ = prepend_elem(OP_ASLICE, newOP(OP_PUSHMARK, 0), newLISTOP(OP_ASLICE, 0, list($3), ref($1, OP_ASLICE))); } - | ary '{' expr ';' '}' %prec '(' + | ary '{' expr ';' '}' /* @hash{@keys} */ { $$ = prepend_elem(OP_HSLICE, newOP(OP_PUSHMARK, 0), newLISTOP(OP_HSLICE, 0, list($3), ref(oopsHV($1), OP_HSLICE))); - expect = XOPERATOR; } + PL_expect = XOPERATOR; } | THING %prec '(' { $$ = $1; } - | amper + | amper /* &foo; */ { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); } - | amper '(' ')' + | amper '(' ')' /* &foo() */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } - | amper '(' expr ')' + | amper '(' expr ')' /* &foo(@args) */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, append_elem(OP_LIST, $3, scalar($1))); } - | NOAMP WORD listexpr + | NOAMP WORD listexpr /* foo(@args) */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, append_elem(OP_LIST, $3, scalar($2))); } - | DO term %prec UNIOP - { $$ = newUNOP(OP_DOFILE, 0, scalar($2)); } - | DO block %prec '(' - { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); } - | DO WORD '(' ')' - { $$ = newUNOP(OP_ENTERSUB, - OPf_SPECIAL|OPf_STACKED, - prepend_elem(OP_LIST, - scalar(newCVREF( - (OPpENTERSUB_AMPER<<8), - scalar($2) - )),Nullop)); dep();} - | DO WORD '(' expr ')' - { $$ = newUNOP(OP_ENTERSUB, - OPf_SPECIAL|OPf_STACKED, - append_elem(OP_LIST, - $4, - scalar(newCVREF( - (OPpENTERSUB_AMPER<<8), - scalar($2) - )))); dep();} - | DO scalar '(' ')' - { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED, - prepend_elem(OP_LIST, - scalar(newCVREF(0,scalar($2))), Nullop)); dep();} - | DO scalar '(' expr ')' - { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED, - prepend_elem(OP_LIST, - $4, - scalar(newCVREF(0,scalar($2))))); dep();} - | term ARROW '(' ')' %prec '(' - { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, - newCVREF(0, scalar($1))); } - | term ARROW '(' expr ')' %prec '(' - { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, - append_elem(OP_LIST, $4, - newCVREF(0, scalar($1)))); } - | LOOPEX + | LOOPEX /* loop exiting command (goto, last, dump, etc) */ { $$ = newOP($1, OPf_SPECIAL); - hints |= HINT_BLOCK_SCOPE; } + PL_hints |= HINT_BLOCK_SCOPE; } | LOOPEX term { $$ = newLOOPEX($1,$2); } - | NOTOP argexpr + | NOTOP argexpr /* not $foo */ { $$ = newUNOP(OP_NOT, 0, scalar($2)); } - | UNIOP + | UNIOP /* Unary op, $_ implied */ { $$ = newOP($1, 0); } - | UNIOP block + | UNIOP block /* eval { foo } */ { $$ = newUNOP($1, 0, $2); } - | UNIOP term + | UNIOP term /* Unary op */ { $$ = newUNOP($1, 0, $2); } - | UNIOPSUB term + | REQUIRE /* require, $_ implied */ + { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); } + | REQUIRE term /* require Foo */ + { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); } + | UNIOPSUB term /* Sub treated as unop */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, append_elem(OP_LIST, $2, scalar($1))); } - | FUNC0 + | FUNC0 /* Nullary operator */ { $$ = newOP($1, 0); } | FUNC0 '(' ')' { $$ = newOP($1, 0); } - | FUNC0SUB + | FUNC0SUB /* Sub treated as nullop */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } - | FUNC1 '(' ')' - { $$ = newOP($1, OPf_SPECIAL); } - | FUNC1 '(' expr ')' + | FUNC1 '(' ')' /* not () */ + { $$ = $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 ')' - { $$ = pmruntime($1, $3, Nullop); } - | PMFUNC '(' term ',' term ')' - { $$ = pmruntime($1, $3, $5); } + | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */ + { $$ = pmruntime($1, $3, 1); } | WORD | listop ; -listexpr: /* NULL */ +/* "my" declarations, with optional attributes */ +myattrterm: MY myterm myattrlist + { $$ = my_attrs($2,$3); } + | MY myterm + { $$ = localize($2,$1); } + ; + +/* Things that can be "my"'d */ +myterm : '(' expr ')' + { $$ = sawparens($2); } + | '(' ')' + { $$ = sawparens(newNULLLIST()); } + | scalar %prec '(' + { $$ = $1; } + | hsh %prec '(' + { $$ = $1; } + | ary %prec '(' + { $$ = $1; } + ; + +/* Basic list expressions */ +listexpr: /* NULL */ %prec PREC_LOW { $$ = Nullop; } - | argexpr + | argexpr %prec PREC_LOW { $$ = $1; } ; @@ -586,12 +724,10 @@ listexprcom: /* NULL */ { $$ = $1; } ; -local : LOCAL { $$ = 0; } - | MY { $$ = 1; } - ; - +/* A little bit of trickery to make "for my $foo (@bar)" actually be + lexical */ my_scalar: scalar - { in_my = 0; $$ = my($1); } + { PL_in_my = 0; $$ = my($1); } ; amper : '&' indirob @@ -618,9 +754,10 @@ star : '*' indirob { $$ = newGVREF(0,$2); } ; +/* Indirect objects */ indirob : WORD { $$ = scalar($1); } - | scalar + | scalar %prec PREC_LOW { $$ = scalar($1); } | block { $$ = scope($1); } @@ -628,5 +765,3 @@ indirob : WORD | PRIVATEREF { $$ = $1; } ; - -%% /* PROGRAM */