| 1 | /* perly.y |
| 2 | * |
| 3 | * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall |
| 4 | * Copyright (c) 2007, 2008, 2009, 2010, 2011 by Larry Wall and others |
| 5 | * |
| 6 | * You may distribute under the terms of either the GNU General Public |
| 7 | * License or the Artistic License, as specified in the README file. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it? |
| 13 | * All that is gold does not glitter, not all those who wander are lost.' |
| 14 | * |
| 15 | * [p.171 of _The Lord of the Rings_, I/x: "Strider"] |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * This file holds the grammar for the Perl language. If edited, you need |
| 20 | * to run regen_perly.pl, which re-creates the files perly.h, perly.tab |
| 21 | * and perly.act which are derived from this. |
| 22 | * |
| 23 | * The main job of of this grammar is to call the various newFOO() |
| 24 | * functions in op.c to build a syntax tree of OP structs. |
| 25 | * It relies on the lexer in toke.c to do the tokenizing. |
| 26 | * |
| 27 | * Note: due to the way that the cleanup code works WRT to freeing ops on |
| 28 | * the parse stack, it is dangerous to assign to the $n variables within |
| 29 | * an action. |
| 30 | */ |
| 31 | |
| 32 | /* Make the parser re-entrant. */ |
| 33 | |
| 34 | %pure-parser |
| 35 | |
| 36 | %start grammar |
| 37 | |
| 38 | %union { |
| 39 | I32 ival; /* __DEFAULT__ (marker for regen_perly.pl; |
| 40 | must always be 1st union member) */ |
| 41 | char *pval; |
| 42 | OP *opval; |
| 43 | GV *gvval; |
| 44 | } |
| 45 | |
| 46 | %token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ GRAMSUBSIGNATURE |
| 47 | |
| 48 | %token <ival> '{' '}' '[' ']' '-' '+' '@' '%' '&' '=' '.' |
| 49 | |
| 50 | %token <opval> BAREWORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST |
| 51 | %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB |
| 52 | %token <opval> PLUGEXPR PLUGSTMT |
| 53 | %token <opval> LABEL |
| 54 | %token <ival> FORMAT SUB SIGSUB ANONSUB ANON_SIGSUB PACKAGE USE |
| 55 | %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR |
| 56 | %token <ival> GIVEN WHEN DEFAULT |
| 57 | %token <ival> LOOPEX DOTDOT YADAYADA |
| 58 | %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP |
| 59 | %token <ival> RELOP EQOP MULOP ADDOP |
| 60 | %token <ival> DOLSHARP DO HASHBRACK NOAMP |
| 61 | %token <ival> LOCAL MY REQUIRE |
| 62 | %token <ival> COLONATTR FORMLBRACK FORMRBRACK |
| 63 | %token <ival> SUBLEXSTART SUBLEXEND |
| 64 | |
| 65 | %type <ival> grammar remember mremember |
| 66 | %type <ival> startsub startanonsub startformsub |
| 67 | |
| 68 | %type <ival> mintro |
| 69 | |
| 70 | %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else |
| 71 | %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff |
| 72 | %type <opval> sliceme kvslice gelem |
| 73 | %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr |
| 74 | %type <opval> optlistexpr optexpr optrepl indirob listop method |
| 75 | %type <opval> formname subname proto cont my_scalar my_var |
| 76 | %type <opval> refgen_topic formblock |
| 77 | %type <opval> subattrlist myattrlist myattrterm myterm |
| 78 | %type <opval> termbinop termunop anonymous termdo |
| 79 | %type <ival> sigslurpsigil |
| 80 | %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem |
| 81 | %type <opval> sigelem siglist siglistornull subsigguts subsignature optsubsignature |
| 82 | %type <opval> subbody optsubbody sigsubbody optsigsubbody |
| 83 | %type <opval> formstmtseq formline formarg |
| 84 | |
| 85 | %nonassoc <ival> PREC_LOW |
| 86 | %nonassoc LOOPEX |
| 87 | |
| 88 | %left <ival> OROP DOROP |
| 89 | %left <ival> ANDOP |
| 90 | %right <ival> NOTOP |
| 91 | %nonassoc LSTOP LSTOPSUB |
| 92 | %left <ival> ',' |
| 93 | %right <ival> ASSIGNOP |
| 94 | %right <ival> '?' ':' |
| 95 | %nonassoc DOTDOT |
| 96 | %left <ival> OROR DORDOR |
| 97 | %left <ival> ANDAND |
| 98 | %left <ival> BITOROP |
| 99 | %left <ival> BITANDOP |
| 100 | %nonassoc EQOP |
| 101 | %nonassoc RELOP |
| 102 | %nonassoc UNIOP UNIOPSUB |
| 103 | %nonassoc REQUIRE |
| 104 | %left <ival> SHIFTOP |
| 105 | %left ADDOP |
| 106 | %left MULOP |
| 107 | %left <ival> MATCHOP |
| 108 | %right <ival> '!' '~' UMINUS REFGEN |
| 109 | %right <ival> POWOP |
| 110 | %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN |
| 111 | %left <ival> ARROW |
| 112 | %nonassoc <ival> ')' |
| 113 | %left <ival> '(' |
| 114 | %left '[' '{' |
| 115 | |
| 116 | %% /* RULES */ |
| 117 | |
| 118 | /* Top-level choice of what kind of thing yyparse was called to parse */ |
| 119 | grammar : GRAMPROG |
| 120 | { |
| 121 | parser->expect = XSTATE; |
| 122 | $<ival>$ = 0; |
| 123 | } |
| 124 | remember stmtseq |
| 125 | { |
| 126 | newPROG(block_end($3,$4)); |
| 127 | PL_compiling.cop_seq = 0; |
| 128 | $$ = 0; |
| 129 | } |
| 130 | | GRAMEXPR |
| 131 | { |
| 132 | parser->expect = XTERM; |
| 133 | $<ival>$ = 0; |
| 134 | } |
| 135 | optexpr |
| 136 | { |
| 137 | PL_eval_root = $3; |
| 138 | $$ = 0; |
| 139 | } |
| 140 | | GRAMBLOCK |
| 141 | { |
| 142 | parser->expect = XBLOCK; |
| 143 | $<ival>$ = 0; |
| 144 | } |
| 145 | block |
| 146 | { |
| 147 | PL_pad_reset_pending = TRUE; |
| 148 | PL_eval_root = $3; |
| 149 | $$ = 0; |
| 150 | yyunlex(); |
| 151 | parser->yychar = yytoken = YYEOF; |
| 152 | } |
| 153 | | GRAMBARESTMT |
| 154 | { |
| 155 | parser->expect = XSTATE; |
| 156 | $<ival>$ = 0; |
| 157 | } |
| 158 | barestmt |
| 159 | { |
| 160 | PL_pad_reset_pending = TRUE; |
| 161 | PL_eval_root = $3; |
| 162 | $$ = 0; |
| 163 | yyunlex(); |
| 164 | parser->yychar = yytoken = YYEOF; |
| 165 | } |
| 166 | | GRAMFULLSTMT |
| 167 | { |
| 168 | parser->expect = XSTATE; |
| 169 | $<ival>$ = 0; |
| 170 | } |
| 171 | fullstmt |
| 172 | { |
| 173 | PL_pad_reset_pending = TRUE; |
| 174 | PL_eval_root = $3; |
| 175 | $$ = 0; |
| 176 | yyunlex(); |
| 177 | parser->yychar = yytoken = YYEOF; |
| 178 | } |
| 179 | | GRAMSTMTSEQ |
| 180 | { |
| 181 | parser->expect = XSTATE; |
| 182 | $<ival>$ = 0; |
| 183 | } |
| 184 | stmtseq |
| 185 | { |
| 186 | PL_eval_root = $3; |
| 187 | $$ = 0; |
| 188 | } |
| 189 | | GRAMSUBSIGNATURE |
| 190 | { |
| 191 | parser->expect = XSTATE; |
| 192 | $<ival>$ = 0; |
| 193 | } |
| 194 | subsigguts |
| 195 | { |
| 196 | PL_eval_root = $3; |
| 197 | $$ = 0; |
| 198 | } |
| 199 | ; |
| 200 | |
| 201 | /* An ordinary block */ |
| 202 | block : '{' remember stmtseq '}' |
| 203 | { if (parser->copline > (line_t)$1) |
| 204 | parser->copline = (line_t)$1; |
| 205 | $$ = block_end($2, $3); |
| 206 | } |
| 207 | ; |
| 208 | |
| 209 | /* format body */ |
| 210 | formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.' |
| 211 | { if (parser->copline > (line_t)$1) |
| 212 | parser->copline = (line_t)$1; |
| 213 | $$ = block_end($2, $5); |
| 214 | } |
| 215 | ; |
| 216 | |
| 217 | remember: /* NULL */ /* start a full lexical scope */ |
| 218 | { $$ = block_start(TRUE); |
| 219 | parser->parsed_sub = 0; } |
| 220 | ; |
| 221 | |
| 222 | mblock : '{' mremember stmtseq '}' |
| 223 | { if (parser->copline > (line_t)$1) |
| 224 | parser->copline = (line_t)$1; |
| 225 | $$ = block_end($2, $3); |
| 226 | } |
| 227 | ; |
| 228 | |
| 229 | mremember: /* NULL */ /* start a partial lexical scope */ |
| 230 | { $$ = block_start(FALSE); |
| 231 | parser->parsed_sub = 0; } |
| 232 | ; |
| 233 | |
| 234 | /* A sequence of statements in the program */ |
| 235 | stmtseq : /* NULL */ |
| 236 | { $$ = NULL; } |
| 237 | | stmtseq fullstmt |
| 238 | { $$ = op_append_list(OP_LINESEQ, $1, $2); |
| 239 | PL_pad_reset_pending = TRUE; |
| 240 | if ($1 && $2) |
| 241 | PL_hints |= HINT_BLOCK_SCOPE; |
| 242 | } |
| 243 | ; |
| 244 | |
| 245 | /* A sequence of format lines */ |
| 246 | formstmtseq: /* NULL */ |
| 247 | { $$ = NULL; } |
| 248 | | formstmtseq formline |
| 249 | { $$ = op_append_list(OP_LINESEQ, $1, $2); |
| 250 | PL_pad_reset_pending = TRUE; |
| 251 | if ($1 && $2) |
| 252 | PL_hints |= HINT_BLOCK_SCOPE; |
| 253 | } |
| 254 | ; |
| 255 | |
| 256 | /* A statement in the program, including optional labels */ |
| 257 | fullstmt: barestmt |
| 258 | { |
| 259 | $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL; |
| 260 | } |
| 261 | | labfullstmt |
| 262 | { $$ = $1; } |
| 263 | ; |
| 264 | |
| 265 | labfullstmt: LABEL barestmt |
| 266 | { |
| 267 | SV *label = cSVOPx_sv($1); |
| 268 | $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8, |
| 269 | savepv(SvPVX_const(label)), $2); |
| 270 | op_free($1); |
| 271 | } |
| 272 | | LABEL labfullstmt |
| 273 | { |
| 274 | SV *label = cSVOPx_sv($1); |
| 275 | $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8, |
| 276 | savepv(SvPVX_const(label)), $2); |
| 277 | op_free($1); |
| 278 | } |
| 279 | ; |
| 280 | |
| 281 | /* A bare statement, lacking label and other aspects of state op */ |
| 282 | barestmt: PLUGSTMT |
| 283 | { $$ = $1; } |
| 284 | | FORMAT startformsub formname formblock |
| 285 | { |
| 286 | CV *fmtcv = PL_compcv; |
| 287 | newFORM($2, $3, $4); |
| 288 | $$ = NULL; |
| 289 | if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) { |
| 290 | pad_add_weakref(fmtcv); |
| 291 | } |
| 292 | parser->parsed_sub = 1; |
| 293 | } |
| 294 | | SUB subname startsub |
| 295 | /* sub declaration or definition not within scope |
| 296 | of 'use feature "signatures"'*/ |
| 297 | { |
| 298 | init_named_cv(PL_compcv, $2); |
| 299 | parser->in_my = 0; |
| 300 | parser->in_my_stash = NULL; |
| 301 | } |
| 302 | proto subattrlist optsubbody |
| 303 | { |
| 304 | SvREFCNT_inc_simple_void(PL_compcv); |
| 305 | $2->op_type == OP_CONST |
| 306 | ? newATTRSUB($3, $2, $5, $6, $7) |
| 307 | : newMYSUB($3, $2, $5, $6, $7) |
| 308 | ; |
| 309 | $$ = NULL; |
| 310 | intro_my(); |
| 311 | parser->parsed_sub = 1; |
| 312 | } |
| 313 | | SIGSUB subname startsub |
| 314 | /* sub declaration or definition under 'use feature |
| 315 | * "signatures"'. (Note that a signature isn't |
| 316 | * allowed in a declaration) |
| 317 | */ |
| 318 | { |
| 319 | init_named_cv(PL_compcv, $2); |
| 320 | parser->in_my = 0; |
| 321 | parser->in_my_stash = NULL; |
| 322 | } |
| 323 | subattrlist optsigsubbody |
| 324 | { |
| 325 | SvREFCNT_inc_simple_void(PL_compcv); |
| 326 | $2->op_type == OP_CONST |
| 327 | ? newATTRSUB($3, $2, NULL, $5, $6) |
| 328 | : newMYSUB( $3, $2, NULL, $5, $6) |
| 329 | ; |
| 330 | $$ = NULL; |
| 331 | intro_my(); |
| 332 | parser->parsed_sub = 1; |
| 333 | } |
| 334 | | PACKAGE BAREWORD BAREWORD ';' |
| 335 | { |
| 336 | package($3); |
| 337 | if ($2) |
| 338 | package_version($2); |
| 339 | $$ = NULL; |
| 340 | } |
| 341 | | USE startsub |
| 342 | { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ } |
| 343 | BAREWORD BAREWORD optlistexpr ';' |
| 344 | { |
| 345 | SvREFCNT_inc_simple_void(PL_compcv); |
| 346 | utilize($1, $2, $4, $5, $6); |
| 347 | parser->parsed_sub = 1; |
| 348 | $$ = NULL; |
| 349 | } |
| 350 | | IF '(' remember mexpr ')' mblock else |
| 351 | { |
| 352 | $$ = block_end($3, |
| 353 | newCONDOP(0, $4, op_scope($6), $7)); |
| 354 | parser->copline = (line_t)$1; |
| 355 | } |
| 356 | | UNLESS '(' remember mexpr ')' mblock else |
| 357 | { |
| 358 | $$ = block_end($3, |
| 359 | newCONDOP(0, $4, $7, op_scope($6))); |
| 360 | parser->copline = (line_t)$1; |
| 361 | } |
| 362 | | GIVEN '(' remember mexpr ')' mblock |
| 363 | { |
| 364 | $$ = block_end($3, newGIVENOP($4, op_scope($6), 0)); |
| 365 | parser->copline = (line_t)$1; |
| 366 | } |
| 367 | | WHEN '(' remember mexpr ')' mblock |
| 368 | { $$ = block_end($3, newWHENOP($4, op_scope($6))); } |
| 369 | | DEFAULT block |
| 370 | { $$ = newWHENOP(0, op_scope($2)); } |
| 371 | | WHILE '(' remember texpr ')' mintro mblock cont |
| 372 | { |
| 373 | $$ = block_end($3, |
| 374 | newWHILEOP(0, 1, NULL, |
| 375 | $4, $7, $8, $6)); |
| 376 | parser->copline = (line_t)$1; |
| 377 | } |
| 378 | | UNTIL '(' remember iexpr ')' mintro mblock cont |
| 379 | { |
| 380 | $$ = block_end($3, |
| 381 | newWHILEOP(0, 1, NULL, |
| 382 | $4, $7, $8, $6)); |
| 383 | parser->copline = (line_t)$1; |
| 384 | } |
| 385 | | FOR '(' remember mnexpr ';' |
| 386 | { parser->expect = XTERM; } |
| 387 | texpr ';' |
| 388 | { parser->expect = XTERM; } |
| 389 | mintro mnexpr ')' |
| 390 | mblock |
| 391 | { |
| 392 | OP *initop = $4; |
| 393 | OP *forop = newWHILEOP(0, 1, NULL, |
| 394 | scalar($7), $13, $11, $10); |
| 395 | if (initop) { |
| 396 | forop = op_prepend_elem(OP_LINESEQ, initop, |
| 397 | op_append_elem(OP_LINESEQ, |
| 398 | newOP(OP_UNSTACK, OPf_SPECIAL), |
| 399 | forop)); |
| 400 | } |
| 401 | PL_hints |= HINT_BLOCK_SCOPE; |
| 402 | $$ = block_end($3, forop); |
| 403 | parser->copline = (line_t)$1; |
| 404 | } |
| 405 | | FOR MY remember my_scalar '(' mexpr ')' mblock cont |
| 406 | { |
| 407 | $$ = block_end($3, newFOROP(0, $4, $6, $8, $9)); |
| 408 | parser->copline = (line_t)$1; |
| 409 | } |
| 410 | | FOR scalar '(' remember mexpr ')' mblock cont |
| 411 | { |
| 412 | $$ = block_end($4, newFOROP(0, |
| 413 | op_lvalue($2, OP_ENTERLOOP), $5, $7, $8)); |
| 414 | parser->copline = (line_t)$1; |
| 415 | } |
| 416 | | FOR my_refgen remember my_var |
| 417 | { parser->in_my = 0; $<opval>$ = my($4); } |
| 418 | '(' mexpr ')' mblock cont |
| 419 | { |
| 420 | $$ = block_end( |
| 421 | $3, |
| 422 | newFOROP(0, |
| 423 | op_lvalue( |
| 424 | newUNOP(OP_REFGEN, 0, |
| 425 | $<opval>5), |
| 426 | OP_ENTERLOOP), |
| 427 | $7, $9, $10) |
| 428 | ); |
| 429 | parser->copline = (line_t)$1; |
| 430 | } |
| 431 | | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont |
| 432 | { |
| 433 | $$ = block_end($5, newFOROP( |
| 434 | 0, op_lvalue(newUNOP(OP_REFGEN, 0, |
| 435 | $3), |
| 436 | OP_ENTERLOOP), $6, $8, $9)); |
| 437 | parser->copline = (line_t)$1; |
| 438 | } |
| 439 | | FOR '(' remember mexpr ')' mblock cont |
| 440 | { |
| 441 | $$ = block_end($3, |
| 442 | newFOROP(0, NULL, $4, $6, $7)); |
| 443 | parser->copline = (line_t)$1; |
| 444 | } |
| 445 | | block cont |
| 446 | { |
| 447 | /* a block is a loop that happens once */ |
| 448 | $$ = newWHILEOP(0, 1, NULL, |
| 449 | NULL, $1, $2, 0); |
| 450 | } |
| 451 | | PACKAGE BAREWORD BAREWORD '{' remember |
| 452 | { |
| 453 | package($3); |
| 454 | if ($2) { |
| 455 | package_version($2); |
| 456 | } |
| 457 | } |
| 458 | stmtseq '}' |
| 459 | { |
| 460 | /* a block is a loop that happens once */ |
| 461 | $$ = newWHILEOP(0, 1, NULL, |
| 462 | NULL, block_end($5, $7), NULL, 0); |
| 463 | if (parser->copline > (line_t)$4) |
| 464 | parser->copline = (line_t)$4; |
| 465 | } |
| 466 | | sideff ';' |
| 467 | { |
| 468 | $$ = $1; |
| 469 | } |
| 470 | | YADAYADA ';' |
| 471 | { |
| 472 | $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0), |
| 473 | newSVOP(OP_CONST, 0, newSVpvs("Unimplemented"))); |
| 474 | } |
| 475 | | ';' |
| 476 | { |
| 477 | $$ = NULL; |
| 478 | parser->copline = NOLINE; |
| 479 | } |
| 480 | ; |
| 481 | |
| 482 | /* Format line */ |
| 483 | formline: THING formarg |
| 484 | { OP *list; |
| 485 | if ($2) { |
| 486 | OP *term = $2; |
| 487 | list = op_append_elem(OP_LIST, $1, term); |
| 488 | } |
| 489 | else { |
| 490 | list = $1; |
| 491 | } |
| 492 | if (parser->copline == NOLINE) |
| 493 | parser->copline = CopLINE(PL_curcop)-1; |
| 494 | else parser->copline--; |
| 495 | $$ = newSTATEOP(0, NULL, |
| 496 | op_convert_list(OP_FORMLINE, 0, list)); |
| 497 | } |
| 498 | ; |
| 499 | |
| 500 | formarg : /* NULL */ |
| 501 | { $$ = NULL; } |
| 502 | | FORMLBRACK stmtseq FORMRBRACK |
| 503 | { $$ = op_unscope($2); } |
| 504 | ; |
| 505 | |
| 506 | /* An expression which may have a side-effect */ |
| 507 | sideff : error |
| 508 | { $$ = NULL; } |
| 509 | | expr |
| 510 | { $$ = $1; } |
| 511 | | expr IF expr |
| 512 | { $$ = newLOGOP(OP_AND, 0, $3, $1); } |
| 513 | | expr UNLESS expr |
| 514 | { $$ = newLOGOP(OP_OR, 0, $3, $1); } |
| 515 | | expr WHILE expr |
| 516 | { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); } |
| 517 | | expr UNTIL iexpr |
| 518 | { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); } |
| 519 | | expr FOR expr |
| 520 | { $$ = newFOROP(0, NULL, $3, $1, NULL); |
| 521 | parser->copline = (line_t)$2; } |
| 522 | | expr WHEN expr |
| 523 | { $$ = newWHENOP($3, op_scope($1)); } |
| 524 | ; |
| 525 | |
| 526 | /* else and elsif blocks */ |
| 527 | else : /* NULL */ |
| 528 | { $$ = NULL; } |
| 529 | | ELSE mblock |
| 530 | { |
| 531 | ($2)->op_flags |= OPf_PARENS; |
| 532 | $$ = op_scope($2); |
| 533 | } |
| 534 | | ELSIF '(' mexpr ')' mblock else |
| 535 | { parser->copline = (line_t)$1; |
| 536 | $$ = newCONDOP(0, |
| 537 | newSTATEOP(OPf_SPECIAL,NULL,$3), |
| 538 | op_scope($5), $6); |
| 539 | PL_hints |= HINT_BLOCK_SCOPE; |
| 540 | } |
| 541 | ; |
| 542 | |
| 543 | /* Continue blocks */ |
| 544 | cont : /* NULL */ |
| 545 | { $$ = NULL; } |
| 546 | | CONTINUE block |
| 547 | { $$ = op_scope($2); } |
| 548 | ; |
| 549 | |
| 550 | /* determine whether there are any new my declarations */ |
| 551 | mintro : /* NULL */ |
| 552 | { $$ = (PL_min_intro_pending && |
| 553 | PL_max_intro_pending >= PL_min_intro_pending); |
| 554 | intro_my(); } |
| 555 | |
| 556 | /* Normal expression */ |
| 557 | nexpr : /* NULL */ |
| 558 | { $$ = NULL; } |
| 559 | | sideff |
| 560 | ; |
| 561 | |
| 562 | /* Boolean expression */ |
| 563 | texpr : /* NULL means true */ |
| 564 | { YYSTYPE tmplval; |
| 565 | (void)scan_num("1", &tmplval); |
| 566 | $$ = tmplval.opval; } |
| 567 | | expr |
| 568 | ; |
| 569 | |
| 570 | /* Inverted boolean expression */ |
| 571 | iexpr : expr |
| 572 | { $$ = invert(scalar($1)); } |
| 573 | ; |
| 574 | |
| 575 | /* Expression with its own lexical scope */ |
| 576 | mexpr : expr |
| 577 | { $$ = $1; intro_my(); } |
| 578 | ; |
| 579 | |
| 580 | mnexpr : nexpr |
| 581 | { $$ = $1; intro_my(); } |
| 582 | ; |
| 583 | |
| 584 | formname: BAREWORD { $$ = $1; } |
| 585 | | /* NULL */ { $$ = NULL; } |
| 586 | ; |
| 587 | |
| 588 | startsub: /* NULL */ /* start a regular subroutine scope */ |
| 589 | { $$ = start_subparse(FALSE, 0); |
| 590 | SAVEFREESV(PL_compcv); } |
| 591 | |
| 592 | ; |
| 593 | |
| 594 | startanonsub: /* NULL */ /* start an anonymous subroutine scope */ |
| 595 | { $$ = start_subparse(FALSE, CVf_ANON); |
| 596 | SAVEFREESV(PL_compcv); } |
| 597 | ; |
| 598 | |
| 599 | startformsub: /* NULL */ /* start a format subroutine scope */ |
| 600 | { $$ = start_subparse(TRUE, 0); |
| 601 | SAVEFREESV(PL_compcv); } |
| 602 | ; |
| 603 | |
| 604 | /* Name of a subroutine - must be a bareword, could be special */ |
| 605 | subname : BAREWORD |
| 606 | | PRIVATEREF |
| 607 | ; |
| 608 | |
| 609 | /* Subroutine prototype */ |
| 610 | proto : /* NULL */ |
| 611 | { $$ = NULL; } |
| 612 | | THING |
| 613 | ; |
| 614 | |
| 615 | /* Optional list of subroutine attributes */ |
| 616 | subattrlist: /* NULL */ |
| 617 | { $$ = NULL; } |
| 618 | | COLONATTR THING |
| 619 | { $$ = $2; } |
| 620 | | COLONATTR |
| 621 | { $$ = NULL; } |
| 622 | ; |
| 623 | |
| 624 | /* List of attributes for a "my" variable declaration */ |
| 625 | myattrlist: COLONATTR THING |
| 626 | { $$ = $2; } |
| 627 | | COLONATTR |
| 628 | { $$ = NULL; } |
| 629 | ; |
| 630 | |
| 631 | |
| 632 | |
| 633 | /* -------------------------------------- |
| 634 | * subroutine signature parsing |
| 635 | */ |
| 636 | |
| 637 | /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */ |
| 638 | sigvarname: /* NULL */ |
| 639 | { parser->in_my = 0; $$ = NULL; } |
| 640 | | PRIVATEREF |
| 641 | { parser->in_my = 0; $$ = $1; } |
| 642 | ; |
| 643 | |
| 644 | sigslurpsigil: |
| 645 | '@' |
| 646 | { $$ = '@'; } |
| 647 | | '%' |
| 648 | { $$ = '%'; } |
| 649 | |
| 650 | /* @, %, @foo, %foo */ |
| 651 | sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */ |
| 652 | { |
| 653 | I32 sigil = $1; |
| 654 | OP *var = $2; |
| 655 | OP *defexpr = $3; |
| 656 | |
| 657 | if (parser->sig_slurpy) |
| 658 | yyerror("Multiple slurpy parameters not allowed"); |
| 659 | parser->sig_slurpy = (char)sigil; |
| 660 | |
| 661 | if (defexpr) |
| 662 | yyerror("A slurpy parameter may not have " |
| 663 | "a default value"); |
| 664 | |
| 665 | $$ = var ? newSTATEOP(0, NULL, var) : NULL; |
| 666 | } |
| 667 | ; |
| 668 | |
| 669 | /* default part of sub signature scalar element: i.e. '= default_expr' */ |
| 670 | sigdefault: /* NULL */ |
| 671 | { $$ = NULL; } |
| 672 | | ASSIGNOP |
| 673 | { $$ = newOP(OP_NULL, 0); } |
| 674 | | ASSIGNOP term |
| 675 | { $$ = $2; } |
| 676 | |
| 677 | |
| 678 | /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */ |
| 679 | sigscalarelem: |
| 680 | '$' sigvarname sigdefault |
| 681 | { |
| 682 | OP *var = $2; |
| 683 | OP *defexpr = $3; |
| 684 | |
| 685 | if (parser->sig_slurpy) |
| 686 | yyerror("Slurpy parameter not last"); |
| 687 | |
| 688 | parser->sig_elems++; |
| 689 | |
| 690 | if (defexpr) { |
| 691 | parser->sig_optelems++; |
| 692 | |
| 693 | if ( defexpr->op_type == OP_NULL |
| 694 | && !(defexpr->op_flags & OPf_KIDS)) |
| 695 | { |
| 696 | /* handle '$=' special case */ |
| 697 | if (var) |
| 698 | yyerror("Optional parameter " |
| 699 | "lacks default expression"); |
| 700 | op_free(defexpr); |
| 701 | } |
| 702 | else { |
| 703 | /* a normal '=default' expression */ |
| 704 | OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM, |
| 705 | defexpr, |
| 706 | LINKLIST(defexpr)); |
| 707 | /* re-purpose op_targ to hold @_ index */ |
| 708 | defop->op_targ = |
| 709 | (PADOFFSET)(parser->sig_elems - 1); |
| 710 | |
| 711 | if (var) { |
| 712 | var->op_flags |= OPf_STACKED; |
| 713 | (void)op_sibling_splice(var, |
| 714 | NULL, 0, defop); |
| 715 | scalar(defop); |
| 716 | } |
| 717 | else |
| 718 | var = newUNOP(OP_NULL, 0, defop); |
| 719 | |
| 720 | LINKLIST(var); |
| 721 | /* NB: normally the first child of a |
| 722 | * logop is executed before the logop, |
| 723 | * and it pushes a boolean result |
| 724 | * ready for the logop. For ARGDEFELEM, |
| 725 | * the op itself does the boolean |
| 726 | * calculation, so set the first op to |
| 727 | * it instead. |
| 728 | */ |
| 729 | var->op_next = defop; |
| 730 | defexpr->op_next = var; |
| 731 | } |
| 732 | } |
| 733 | else { |
| 734 | if (parser->sig_optelems) |
| 735 | yyerror("Mandatory parameter " |
| 736 | "follows optional parameter"); |
| 737 | } |
| 738 | |
| 739 | $$ = var ? newSTATEOP(0, NULL, var) : NULL; |
| 740 | } |
| 741 | ; |
| 742 | |
| 743 | |
| 744 | /* subroutine signature element: e.g. '$x = $default' or '%h' */ |
| 745 | sigelem: sigscalarelem |
| 746 | { parser->in_my = KEY_sigvar; $$ = $1; } |
| 747 | | sigslurpelem |
| 748 | { parser->in_my = KEY_sigvar; $$ = $1; } |
| 749 | ; |
| 750 | |
| 751 | /* list of subroutine signature elements */ |
| 752 | siglist: |
| 753 | siglist ',' |
| 754 | { $$ = $1; } |
| 755 | | siglist ',' sigelem |
| 756 | { |
| 757 | $$ = op_append_list(OP_LINESEQ, $1, $3); |
| 758 | } |
| 759 | | sigelem %prec PREC_LOW |
| 760 | { $$ = $1; } |
| 761 | ; |
| 762 | |
| 763 | /* () or (....) */ |
| 764 | siglistornull: /* NULL */ |
| 765 | { $$ = NULL; } |
| 766 | | siglist |
| 767 | { $$ = $1; } |
| 768 | |
| 769 | /* optional subroutine signature */ |
| 770 | optsubsignature: /* NULL */ |
| 771 | { $$ = NULL; } |
| 772 | | subsignature |
| 773 | { $$ = $1; } |
| 774 | |
| 775 | /* Subroutine signature */ |
| 776 | subsignature: '(' subsigguts ')' |
| 777 | { $$ = $2; } |
| 778 | |
| 779 | subsigguts: |
| 780 | { |
| 781 | ENTER; |
| 782 | SAVEIV(parser->sig_elems); |
| 783 | SAVEIV(parser->sig_optelems); |
| 784 | SAVEI8(parser->sig_slurpy); |
| 785 | parser->sig_elems = 0; |
| 786 | parser->sig_optelems = 0; |
| 787 | parser->sig_slurpy = 0; |
| 788 | parser->in_my = KEY_sigvar; |
| 789 | } |
| 790 | siglistornull |
| 791 | { |
| 792 | OP *sigops = $2; |
| 793 | struct op_argcheck_aux *aux; |
| 794 | OP *check; |
| 795 | |
| 796 | if (!FEATURE_SIGNATURES_IS_ENABLED) |
| 797 | Perl_croak(aTHX_ "Experimental " |
| 798 | "subroutine signatures not enabled"); |
| 799 | |
| 800 | /* We shouldn't get here otherwise */ |
| 801 | Perl_ck_warner_d(aTHX_ |
| 802 | packWARN(WARN_EXPERIMENTAL__SIGNATURES), |
| 803 | "The signatures feature is experimental"); |
| 804 | |
| 805 | aux = (struct op_argcheck_aux*) |
| 806 | PerlMemShared_malloc( |
| 807 | sizeof(struct op_argcheck_aux)); |
| 808 | aux->params = parser->sig_elems; |
| 809 | aux->opt_params = parser->sig_optelems; |
| 810 | aux->slurpy = parser->sig_slurpy; |
| 811 | check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, |
| 812 | (UNOP_AUX_item *)aux); |
| 813 | sigops = op_prepend_elem(OP_LINESEQ, check, sigops); |
| 814 | sigops = op_prepend_elem(OP_LINESEQ, |
| 815 | newSTATEOP(0, NULL, NULL), |
| 816 | sigops); |
| 817 | /* a nextstate at the end handles context |
| 818 | * correctly for an empty sub body */ |
| 819 | $$ = op_append_elem(OP_LINESEQ, |
| 820 | sigops, |
| 821 | newSTATEOP(0, NULL, NULL)); |
| 822 | |
| 823 | parser->in_my = 0; |
| 824 | /* tell the toker that attrributes can follow |
| 825 | * this sig, but only so that the toker |
| 826 | * can skip through any (illegal) trailing |
| 827 | * attribute text then give a useful error |
| 828 | * message about "attributes before sig", |
| 829 | * rather than falling over ina mess at |
| 830 | * unrecognised syntax. |
| 831 | */ |
| 832 | parser->expect = XATTRBLOCK; |
| 833 | parser->sig_seen = TRUE; |
| 834 | LEAVE; |
| 835 | } |
| 836 | ; |
| 837 | |
| 838 | /* Optional subroutine body (for named subroutine declaration) */ |
| 839 | optsubbody: subbody { $$ = $1; } |
| 840 | | ';' { $$ = NULL; } |
| 841 | ; |
| 842 | |
| 843 | |
| 844 | /* Subroutine body (without signature) */ |
| 845 | subbody: remember '{' stmtseq '}' |
| 846 | { |
| 847 | if (parser->copline > (line_t)$2) |
| 848 | parser->copline = (line_t)$2; |
| 849 | $$ = block_end($1, $3); |
| 850 | } |
| 851 | ; |
| 852 | |
| 853 | |
| 854 | /* optional [ Subroutine body with optional signature ] (for named |
| 855 | * subroutine declaration) */ |
| 856 | optsigsubbody: sigsubbody { $$ = $1; } |
| 857 | | ';' { $$ = NULL; } |
| 858 | |
| 859 | /* Subroutine body with optional signature */ |
| 860 | sigsubbody: remember optsubsignature '{' stmtseq '}' |
| 861 | { |
| 862 | if (parser->copline > (line_t)$3) |
| 863 | parser->copline = (line_t)$3; |
| 864 | $$ = block_end($1, |
| 865 | op_append_list(OP_LINESEQ, $2, $4)); |
| 866 | } |
| 867 | ; |
| 868 | |
| 869 | |
| 870 | /* Ordinary expressions; logical combinations */ |
| 871 | expr : expr ANDOP expr |
| 872 | { $$ = newLOGOP(OP_AND, 0, $1, $3); } |
| 873 | | expr OROP expr |
| 874 | { $$ = newLOGOP($2, 0, $1, $3); } |
| 875 | | expr DOROP expr |
| 876 | { $$ = newLOGOP(OP_DOR, 0, $1, $3); } |
| 877 | | listexpr %prec PREC_LOW |
| 878 | ; |
| 879 | |
| 880 | /* Expressions are a list of terms joined by commas */ |
| 881 | listexpr: listexpr ',' |
| 882 | { $$ = $1; } |
| 883 | | listexpr ',' term |
| 884 | { |
| 885 | OP* term = $3; |
| 886 | $$ = op_append_elem(OP_LIST, $1, term); |
| 887 | } |
| 888 | | term %prec PREC_LOW |
| 889 | ; |
| 890 | |
| 891 | /* List operators */ |
| 892 | listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */ |
| 893 | { $$ = op_convert_list($1, OPf_STACKED, |
| 894 | op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); |
| 895 | } |
| 896 | | FUNC '(' indirob expr ')' /* print ($fh @args */ |
| 897 | { $$ = op_convert_list($1, OPf_STACKED, |
| 898 | op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); |
| 899 | } |
| 900 | | term ARROW method '(' optexpr ')' /* $foo->bar(list) */ |
| 901 | { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED, |
| 902 | op_append_elem(OP_LIST, |
| 903 | op_prepend_elem(OP_LIST, scalar($1), $5), |
| 904 | newMETHOP(OP_METHOD, 0, $3))); |
| 905 | } |
| 906 | | term ARROW method /* $foo->bar */ |
| 907 | { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED, |
| 908 | op_append_elem(OP_LIST, scalar($1), |
| 909 | newMETHOP(OP_METHOD, 0, $3))); |
| 910 | } |
| 911 | | METHOD indirob optlistexpr /* new Class @args */ |
| 912 | { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED, |
| 913 | op_append_elem(OP_LIST, |
| 914 | op_prepend_elem(OP_LIST, $2, $3), |
| 915 | newMETHOP(OP_METHOD, 0, $1))); |
| 916 | } |
| 917 | | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */ |
| 918 | { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED, |
| 919 | op_append_elem(OP_LIST, |
| 920 | op_prepend_elem(OP_LIST, $2, $4), |
| 921 | newMETHOP(OP_METHOD, 0, $1))); |
| 922 | } |
| 923 | | LSTOP optlistexpr /* print @args */ |
| 924 | { $$ = op_convert_list($1, 0, $2); } |
| 925 | | FUNC '(' optexpr ')' /* print (@args) */ |
| 926 | { $$ = op_convert_list($1, 0, $3); } |
| 927 | | FUNC SUBLEXSTART optexpr SUBLEXEND /* uc($arg) from "\U..." */ |
| 928 | { $$ = op_convert_list($1, 0, $3); } |
| 929 | | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */ |
| 930 | { SvREFCNT_inc_simple_void(PL_compcv); |
| 931 | $<opval>$ = newANONATTRSUB($2, 0, NULL, $3); } |
| 932 | optlistexpr %prec LSTOP /* ... @bar */ |
| 933 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 934 | op_append_elem(OP_LIST, |
| 935 | op_prepend_elem(OP_LIST, $<opval>4, $5), $1)); |
| 936 | } |
| 937 | ; |
| 938 | |
| 939 | /* Names of methods. May use $object->$methodname */ |
| 940 | method : METHOD |
| 941 | | scalar |
| 942 | ; |
| 943 | |
| 944 | /* Some kind of subscripted expression */ |
| 945 | subscripted: gelem '{' expr ';' '}' /* *main::{something} */ |
| 946 | /* In this and all the hash accessors, ';' is |
| 947 | * provided by the tokeniser */ |
| 948 | { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); } |
| 949 | | scalar '[' expr ']' /* $array[$element] */ |
| 950 | { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); |
| 951 | } |
| 952 | | term ARROW '[' expr ']' /* somearef->[$element] */ |
| 953 | { $$ = newBINOP(OP_AELEM, 0, |
| 954 | ref(newAVREF($1),OP_RV2AV), |
| 955 | scalar($4)); |
| 956 | } |
| 957 | | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */ |
| 958 | { $$ = newBINOP(OP_AELEM, 0, |
| 959 | ref(newAVREF($1),OP_RV2AV), |
| 960 | scalar($3)); |
| 961 | } |
| 962 | | scalar '{' expr ';' '}' /* $foo{bar();} */ |
| 963 | { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3)); |
| 964 | } |
| 965 | | term ARROW '{' expr ';' '}' /* somehref->{bar();} */ |
| 966 | { $$ = newBINOP(OP_HELEM, 0, |
| 967 | ref(newHVREF($1),OP_RV2HV), |
| 968 | jmaybe($4)); } |
| 969 | | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */ |
| 970 | { $$ = newBINOP(OP_HELEM, 0, |
| 971 | ref(newHVREF($1),OP_RV2HV), |
| 972 | jmaybe($3)); } |
| 973 | | term ARROW '(' ')' /* $subref->() */ |
| 974 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 975 | newCVREF(0, scalar($1))); |
| 976 | if (parser->expect == XBLOCK) |
| 977 | parser->expect = XOPERATOR; |
| 978 | } |
| 979 | | term ARROW '(' expr ')' /* $subref->(@args) */ |
| 980 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 981 | op_append_elem(OP_LIST, $4, |
| 982 | newCVREF(0, scalar($1)))); |
| 983 | if (parser->expect == XBLOCK) |
| 984 | parser->expect = XOPERATOR; |
| 985 | } |
| 986 | |
| 987 | | subscripted '(' expr ')' /* $foo->{bar}->(@args) */ |
| 988 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 989 | op_append_elem(OP_LIST, $3, |
| 990 | newCVREF(0, scalar($1)))); |
| 991 | if (parser->expect == XBLOCK) |
| 992 | parser->expect = XOPERATOR; |
| 993 | } |
| 994 | | subscripted '(' ')' /* $foo->{bar}->() */ |
| 995 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 996 | newCVREF(0, scalar($1))); |
| 997 | if (parser->expect == XBLOCK) |
| 998 | parser->expect = XOPERATOR; |
| 999 | } |
| 1000 | | '(' expr ')' '[' expr ']' /* list slice */ |
| 1001 | { $$ = newSLICEOP(0, $5, $2); } |
| 1002 | | QWLIST '[' expr ']' /* list literal slice */ |
| 1003 | { $$ = newSLICEOP(0, $3, $1); } |
| 1004 | | '(' ')' '[' expr ']' /* empty list slice! */ |
| 1005 | { $$ = newSLICEOP(0, $4, NULL); } |
| 1006 | ; |
| 1007 | |
| 1008 | /* Binary operators between terms */ |
| 1009 | termbinop: term ASSIGNOP term /* $x = $y, $x += $y */ |
| 1010 | { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); } |
| 1011 | | term POWOP term /* $x ** $y */ |
| 1012 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
| 1013 | | term MULOP term /* $x * $y, $x x $y */ |
| 1014 | { if ($2 != OP_REPEAT) |
| 1015 | scalar($1); |
| 1016 | $$ = newBINOP($2, 0, $1, scalar($3)); |
| 1017 | } |
| 1018 | | term ADDOP term /* $x + $y */ |
| 1019 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
| 1020 | | term SHIFTOP term /* $x >> $y, $x << $y */ |
| 1021 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
| 1022 | | term RELOP term /* $x > $y, etc. */ |
| 1023 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
| 1024 | | term EQOP term /* $x == $y, $x eq $y */ |
| 1025 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
| 1026 | | term BITANDOP term /* $x & $y */ |
| 1027 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
| 1028 | | term BITOROP term /* $x | $y */ |
| 1029 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
| 1030 | | term DOTDOT term /* $x..$y, $x...$y */ |
| 1031 | { $$ = newRANGE($2, scalar($1), scalar($3)); } |
| 1032 | | term ANDAND term /* $x && $y */ |
| 1033 | { $$ = newLOGOP(OP_AND, 0, $1, $3); } |
| 1034 | | term OROR term /* $x || $y */ |
| 1035 | { $$ = newLOGOP(OP_OR, 0, $1, $3); } |
| 1036 | | term DORDOR term /* $x // $y */ |
| 1037 | { $$ = newLOGOP(OP_DOR, 0, $1, $3); } |
| 1038 | | term MATCHOP term /* $x =~ /$y/ */ |
| 1039 | { $$ = bind_match($2, $1, $3); } |
| 1040 | ; |
| 1041 | |
| 1042 | /* Unary operators and terms */ |
| 1043 | termunop : '-' term %prec UMINUS /* -$x */ |
| 1044 | { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); } |
| 1045 | | '+' term %prec UMINUS /* +$x */ |
| 1046 | { $$ = $2; } |
| 1047 | |
| 1048 | | '!' term /* !$x */ |
| 1049 | { $$ = newUNOP(OP_NOT, 0, scalar($2)); } |
| 1050 | | '~' term /* ~$x */ |
| 1051 | { $$ = newUNOP($1, 0, scalar($2)); } |
| 1052 | | term POSTINC /* $x++ */ |
| 1053 | { $$ = newUNOP(OP_POSTINC, 0, |
| 1054 | op_lvalue(scalar($1), OP_POSTINC)); } |
| 1055 | | term POSTDEC /* $x-- */ |
| 1056 | { $$ = newUNOP(OP_POSTDEC, 0, |
| 1057 | op_lvalue(scalar($1), OP_POSTDEC));} |
| 1058 | | term POSTJOIN /* implicit join after interpolated ->@ */ |
| 1059 | { $$ = op_convert_list(OP_JOIN, 0, |
| 1060 | op_append_elem( |
| 1061 | OP_LIST, |
| 1062 | newSVREF(scalar( |
| 1063 | newSVOP(OP_CONST,0, |
| 1064 | newSVpvs("\"")) |
| 1065 | )), |
| 1066 | $1 |
| 1067 | )); |
| 1068 | } |
| 1069 | | PREINC term /* ++$x */ |
| 1070 | { $$ = newUNOP(OP_PREINC, 0, |
| 1071 | op_lvalue(scalar($2), OP_PREINC)); } |
| 1072 | | PREDEC term /* --$x */ |
| 1073 | { $$ = newUNOP(OP_PREDEC, 0, |
| 1074 | op_lvalue(scalar($2), OP_PREDEC)); } |
| 1075 | |
| 1076 | ; |
| 1077 | |
| 1078 | /* Constructors for anonymous data */ |
| 1079 | anonymous: '[' expr ']' |
| 1080 | { $$ = newANONLIST($2); } |
| 1081 | | '[' ']' |
| 1082 | { $$ = newANONLIST(NULL);} |
| 1083 | | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */ |
| 1084 | { $$ = newANONHASH($2); } |
| 1085 | | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */ |
| 1086 | { $$ = newANONHASH(NULL); } |
| 1087 | | ANONSUB startanonsub proto subattrlist subbody %prec '(' |
| 1088 | { SvREFCNT_inc_simple_void(PL_compcv); |
| 1089 | $$ = newANONATTRSUB($2, $3, $4, $5); } |
| 1090 | | ANON_SIGSUB startanonsub subattrlist sigsubbody %prec '(' |
| 1091 | { SvREFCNT_inc_simple_void(PL_compcv); |
| 1092 | $$ = newANONATTRSUB($2, NULL, $3, $4); } |
| 1093 | ; |
| 1094 | |
| 1095 | /* Things called with "do" */ |
| 1096 | termdo : DO term %prec UNIOP /* do $filename */ |
| 1097 | { $$ = dofile($2, $1);} |
| 1098 | | DO block %prec '(' /* do { code */ |
| 1099 | { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));} |
| 1100 | ; |
| 1101 | |
| 1102 | term : termbinop |
| 1103 | | termunop |
| 1104 | | anonymous |
| 1105 | | termdo |
| 1106 | | term '?' term ':' term |
| 1107 | { $$ = newCONDOP(0, $1, $3, $5); } |
| 1108 | | REFGEN term /* \$x, \@y, \%z */ |
| 1109 | { $$ = newUNOP(OP_REFGEN, 0, $2); } |
| 1110 | | MY REFGEN term |
| 1111 | { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); } |
| 1112 | | myattrterm %prec UNIOP |
| 1113 | { $$ = $1; } |
| 1114 | | LOCAL term %prec UNIOP |
| 1115 | { $$ = localize($2,0); } |
| 1116 | | '(' expr ')' |
| 1117 | { $$ = sawparens($2); } |
| 1118 | | QWLIST |
| 1119 | { $$ = $1; } |
| 1120 | | '(' ')' |
| 1121 | { $$ = sawparens(newNULLLIST()); } |
| 1122 | | scalar %prec '(' |
| 1123 | { $$ = $1; } |
| 1124 | | star %prec '(' |
| 1125 | { $$ = $1; } |
| 1126 | | hsh %prec '(' |
| 1127 | { $$ = $1; } |
| 1128 | | ary %prec '(' |
| 1129 | { $$ = $1; } |
| 1130 | | arylen %prec '(' /* $#x, $#{ something } */ |
| 1131 | { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));} |
| 1132 | | subscripted |
| 1133 | { $$ = $1; } |
| 1134 | | sliceme '[' expr ']' /* array slice */ |
| 1135 | { $$ = op_prepend_elem(OP_ASLICE, |
| 1136 | newOP(OP_PUSHMARK, 0), |
| 1137 | newLISTOP(OP_ASLICE, 0, |
| 1138 | list($3), |
| 1139 | ref($1, OP_ASLICE))); |
| 1140 | if ($$ && $1) |
| 1141 | $$->op_private |= |
| 1142 | $1->op_private & OPpSLICEWARNING; |
| 1143 | } |
| 1144 | | kvslice '[' expr ']' /* array key/value slice */ |
| 1145 | { $$ = op_prepend_elem(OP_KVASLICE, |
| 1146 | newOP(OP_PUSHMARK, 0), |
| 1147 | newLISTOP(OP_KVASLICE, 0, |
| 1148 | list($3), |
| 1149 | ref(oopsAV($1), OP_KVASLICE))); |
| 1150 | if ($$ && $1) |
| 1151 | $$->op_private |= |
| 1152 | $1->op_private & OPpSLICEWARNING; |
| 1153 | } |
| 1154 | | sliceme '{' expr ';' '}' /* @hash{@keys} */ |
| 1155 | { $$ = op_prepend_elem(OP_HSLICE, |
| 1156 | newOP(OP_PUSHMARK, 0), |
| 1157 | newLISTOP(OP_HSLICE, 0, |
| 1158 | list($3), |
| 1159 | ref(oopsHV($1), OP_HSLICE))); |
| 1160 | if ($$ && $1) |
| 1161 | $$->op_private |= |
| 1162 | $1->op_private & OPpSLICEWARNING; |
| 1163 | } |
| 1164 | | kvslice '{' expr ';' '}' /* %hash{@keys} */ |
| 1165 | { $$ = op_prepend_elem(OP_KVHSLICE, |
| 1166 | newOP(OP_PUSHMARK, 0), |
| 1167 | newLISTOP(OP_KVHSLICE, 0, |
| 1168 | list($3), |
| 1169 | ref($1, OP_KVHSLICE))); |
| 1170 | if ($$ && $1) |
| 1171 | $$->op_private |= |
| 1172 | $1->op_private & OPpSLICEWARNING; |
| 1173 | } |
| 1174 | | THING %prec '(' |
| 1175 | { $$ = $1; } |
| 1176 | | amper /* &foo; */ |
| 1177 | { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); } |
| 1178 | | amper '(' ')' /* &foo() or foo() */ |
| 1179 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); |
| 1180 | } |
| 1181 | | amper '(' expr ')' /* &foo(@args) or foo(@args) */ |
| 1182 | { |
| 1183 | $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 1184 | op_append_elem(OP_LIST, $3, scalar($1))); |
| 1185 | } |
| 1186 | | NOAMP subname optlistexpr /* foo @args (no parens) */ |
| 1187 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 1188 | op_append_elem(OP_LIST, $3, scalar($2))); |
| 1189 | } |
| 1190 | | term ARROW '$' '*' |
| 1191 | { $$ = newSVREF($1); } |
| 1192 | | term ARROW '@' '*' |
| 1193 | { $$ = newAVREF($1); } |
| 1194 | | term ARROW '%' '*' |
| 1195 | { $$ = newHVREF($1); } |
| 1196 | | term ARROW '&' '*' |
| 1197 | { $$ = newUNOP(OP_ENTERSUB, 0, |
| 1198 | scalar(newCVREF($3,$1))); } |
| 1199 | | term ARROW '*' '*' %prec '(' |
| 1200 | { $$ = newGVREF(0,$1); } |
| 1201 | | LOOPEX /* loop exiting command (goto, last, dump, etc) */ |
| 1202 | { $$ = newOP($1, OPf_SPECIAL); |
| 1203 | PL_hints |= HINT_BLOCK_SCOPE; } |
| 1204 | | LOOPEX term |
| 1205 | { $$ = newLOOPEX($1,$2); } |
| 1206 | | NOTOP listexpr /* not $foo */ |
| 1207 | { $$ = newUNOP(OP_NOT, 0, scalar($2)); } |
| 1208 | | UNIOP /* Unary op, $_ implied */ |
| 1209 | { $$ = newOP($1, 0); } |
| 1210 | | UNIOP block /* eval { foo }* */ |
| 1211 | { $$ = newUNOP($1, 0, $2); } |
| 1212 | | UNIOP term /* Unary op */ |
| 1213 | { $$ = newUNOP($1, 0, $2); } |
| 1214 | | REQUIRE /* require, $_ implied */ |
| 1215 | { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); } |
| 1216 | | REQUIRE term /* require Foo */ |
| 1217 | { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); } |
| 1218 | | UNIOPSUB |
| 1219 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } |
| 1220 | | UNIOPSUB term /* Sub treated as unop */ |
| 1221 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 1222 | op_append_elem(OP_LIST, $2, scalar($1))); } |
| 1223 | | FUNC0 /* Nullary operator */ |
| 1224 | { $$ = newOP($1, 0); } |
| 1225 | | FUNC0 '(' ')' |
| 1226 | { $$ = newOP($1, 0);} |
| 1227 | | FUNC0OP /* Same as above, but op created in toke.c */ |
| 1228 | { $$ = $1; } |
| 1229 | | FUNC0OP '(' ')' |
| 1230 | { $$ = $1; } |
| 1231 | | FUNC0SUB /* Sub treated as nullop */ |
| 1232 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } |
| 1233 | | FUNC1 '(' ')' /* not () */ |
| 1234 | { $$ = ($1 == OP_NOT) |
| 1235 | ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0))) |
| 1236 | : newOP($1, OPf_SPECIAL); } |
| 1237 | | FUNC1 '(' expr ')' /* not($foo) */ |
| 1238 | { $$ = newUNOP($1, 0, $3); } |
| 1239 | | PMFUNC /* m//, s///, qr//, tr/// */ |
| 1240 | { |
| 1241 | if ( $1->op_type != OP_TRANS |
| 1242 | && $1->op_type != OP_TRANSR |
| 1243 | && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV)) |
| 1244 | { |
| 1245 | $<ival>$ = start_subparse(FALSE, CVf_ANON); |
| 1246 | SAVEFREESV(PL_compcv); |
| 1247 | } else |
| 1248 | $<ival>$ = 0; |
| 1249 | } |
| 1250 | SUBLEXSTART listexpr optrepl SUBLEXEND |
| 1251 | { $$ = pmruntime($1, $4, $5, 1, $<ival>2); } |
| 1252 | | BAREWORD |
| 1253 | | listop |
| 1254 | | PLUGEXPR |
| 1255 | ; |
| 1256 | |
| 1257 | /* "my" declarations, with optional attributes */ |
| 1258 | myattrterm: MY myterm myattrlist |
| 1259 | { $$ = my_attrs($2,$3); } |
| 1260 | | MY myterm |
| 1261 | { $$ = localize($2,1); } |
| 1262 | | MY REFGEN myterm myattrlist |
| 1263 | { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); } |
| 1264 | ; |
| 1265 | |
| 1266 | /* Things that can be "my"'d */ |
| 1267 | myterm : '(' expr ')' |
| 1268 | { $$ = sawparens($2); } |
| 1269 | | '(' ')' |
| 1270 | { $$ = sawparens(newNULLLIST()); } |
| 1271 | |
| 1272 | | scalar %prec '(' |
| 1273 | { $$ = $1; } |
| 1274 | | hsh %prec '(' |
| 1275 | { $$ = $1; } |
| 1276 | | ary %prec '(' |
| 1277 | { $$ = $1; } |
| 1278 | ; |
| 1279 | |
| 1280 | /* Basic list expressions */ |
| 1281 | optlistexpr: /* NULL */ %prec PREC_LOW |
| 1282 | { $$ = NULL; } |
| 1283 | | listexpr %prec PREC_LOW |
| 1284 | { $$ = $1; } |
| 1285 | ; |
| 1286 | |
| 1287 | optexpr: /* NULL */ |
| 1288 | { $$ = NULL; } |
| 1289 | | expr |
| 1290 | { $$ = $1; } |
| 1291 | ; |
| 1292 | |
| 1293 | optrepl: /* NULL */ |
| 1294 | { $$ = NULL; } |
| 1295 | | '/' expr |
| 1296 | { $$ = $2; } |
| 1297 | ; |
| 1298 | |
| 1299 | /* A little bit of trickery to make "for my $foo (@bar)" actually be |
| 1300 | lexical */ |
| 1301 | my_scalar: scalar |
| 1302 | { parser->in_my = 0; $$ = my($1); } |
| 1303 | ; |
| 1304 | |
| 1305 | my_var : scalar |
| 1306 | | ary |
| 1307 | | hsh |
| 1308 | ; |
| 1309 | |
| 1310 | refgen_topic: my_var |
| 1311 | | amper |
| 1312 | ; |
| 1313 | |
| 1314 | my_refgen: MY REFGEN |
| 1315 | | REFGEN MY |
| 1316 | ; |
| 1317 | |
| 1318 | amper : '&' indirob |
| 1319 | { $$ = newCVREF($1,$2); } |
| 1320 | ; |
| 1321 | |
| 1322 | scalar : '$' indirob |
| 1323 | { $$ = newSVREF($2); } |
| 1324 | ; |
| 1325 | |
| 1326 | ary : '@' indirob |
| 1327 | { $$ = newAVREF($2); |
| 1328 | if ($$) $$->op_private |= $1; |
| 1329 | } |
| 1330 | ; |
| 1331 | |
| 1332 | hsh : '%' indirob |
| 1333 | { $$ = newHVREF($2); |
| 1334 | if ($$) $$->op_private |= $1; |
| 1335 | } |
| 1336 | ; |
| 1337 | |
| 1338 | arylen : DOLSHARP indirob |
| 1339 | { $$ = newAVREF($2); } |
| 1340 | | term ARROW DOLSHARP '*' |
| 1341 | { $$ = newAVREF($1); } |
| 1342 | ; |
| 1343 | |
| 1344 | star : '*' indirob |
| 1345 | { $$ = newGVREF(0,$2); } |
| 1346 | ; |
| 1347 | |
| 1348 | sliceme : ary |
| 1349 | | term ARROW '@' |
| 1350 | { $$ = newAVREF($1); } |
| 1351 | ; |
| 1352 | |
| 1353 | kvslice : hsh |
| 1354 | | term ARROW '%' |
| 1355 | { $$ = newHVREF($1); } |
| 1356 | ; |
| 1357 | |
| 1358 | gelem : star |
| 1359 | | term ARROW '*' |
| 1360 | { $$ = newGVREF(0,$1); } |
| 1361 | ; |
| 1362 | |
| 1363 | /* Indirect objects */ |
| 1364 | indirob : BAREWORD |
| 1365 | { $$ = scalar($1); } |
| 1366 | | scalar %prec PREC_LOW |
| 1367 | { $$ = scalar($1); } |
| 1368 | | block |
| 1369 | { $$ = op_scope($1); } |
| 1370 | |
| 1371 | | PRIVATEREF |
| 1372 | { $$ = $1; } |
| 1373 | ; |