This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp_sys.c: some Coverity findings: NULL guards for io pointers
[perl5.git] / perly.y
CommitLineData
a0d0e21e 1/* perly.y
a687059c 2 *
0de566d7 3 * Copyright (c) 1991-2002, 2003, 2004 Larry Wall
a687059c 4 *
9ef589d8
LW
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8d063cd8 7 *
a0d0e21e
LW
8 */
9
10/*
11 * 'I see,' laughed Strider. 'I look foul and feel fair. Is that it?
14a38fd5 12 * All that is gold does not glitter, not all those who wander are lost.'
8d063cd8
LW
13 */
14
166f8a29
DM
15/* This file holds the grammar for the Perl language. If edited, you need
16 * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
17 * and perly.act which are derived from this.
18 *
19 * The main job of of this grammar is to call the various newFOO()
20 * functions in op.c to build a syntax tree of OP structs.
61296642 21 * It relies on the lexer in toke.c to do the tokenizing.
166f8a29
DM
22 */
23
0de566d7 24/* Make the parser re-entrant. */
8d063cd8 25
0de566d7 26%pure_parser
8d063cd8 27
0de566d7 28%start prog
9d116dd7 29
8d063cd8 30%union {
79072805
LW
31 I32 ival;
32 char *pval;
7f46837f
NC
33#ifdef PERL_MAD
34 TOKEN* tkval;
35#endif
79072805
LW
36 OP *opval;
37 GV *gvval;
8d063cd8
LW
38}
39
fad39ff1 40%token <ival> '{'
f0fcb552 41
a0d0e21e 42%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF
4633a7c4 43%token <opval> FUNC0SUB UNIOPSUB LSTOPSUB
79072805 44%token <pval> LABEL
a0d0e21e 45%token <ival> FORMAT SUB ANONSUB PACKAGE USE
79072805 46%token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
0d863452 47%token <ival> GIVEN WHEN DEFAULT
79072805 48%token <ival> LOOPEX DOTDOT
36477c24 49%token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
79072805 50%token <ival> RELOP EQOP MULOP ADDOP
55497cff 51%token <ival> DOLSHARP DO HASHBRACK NOAMP
a72a1c8b 52%token <ival> LOCAL MY MYSUB REQUIRE
09bef843 53%token COLONATTR
79072805 54
a034e688 55%type <ival> prog decl format startsub startanonsub startformsub mintro
0d863452 56%type <ival> progstart remember mremember '&' savescope mydefsv
bbce6d69 57%type <opval> block mblock lineseq line loop cond else
fad39ff1 58%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
a034e688 59%type <opval> argexpr nexpr texpr iexpr mexpr mnexpr miexpr
44a8e56a 60%type <opval> listexpr listexprcom indirob listop method
61%type <opval> formname subname proto subbody cont my_scalar
09bef843 62%type <opval> subattrlist myattrlist mysubrout myattrterm myterm
891be019 63%type <opval> termbinop termunop anonymous termdo
0d863452 64%type <opval> switch case
79072805 65%type <pval> label
79072805 66
fad39ff1
SM
67%nonassoc PREC_LOW
68%nonassoc LOOPEX
69
c963b151 70%left <ival> OROP DOROP
463ee0b2 71%left ANDOP
c07a80fd 72%right NOTOP
36477c24 73%nonassoc LSTOP LSTOPSUB
8d063cd8 74%left ','
a0d0e21e 75%right <ival> ASSIGNOP
8d063cd8
LW
76%right '?' ':'
77%nonassoc DOTDOT
c963b151 78%left OROR DORDOR
8d063cd8 79%left ANDAND
79072805
LW
80%left <ival> BITOROP
81%left <ival> BITANDOP
a687059c
LW
82%nonassoc EQOP
83%nonassoc RELOP
36477c24 84%nonassoc UNIOP UNIOPSUB
20515881 85%nonassoc REQUIRE
79072805 86%left <ival> SHIFTOP
a687059c
LW
87%left ADDOP
88%left MULOP
8990e307 89%left <ival> MATCHOP
79072805
LW
90%right '!' '~' UMINUS REFGEN
91%right <ival> POWOP
92%nonassoc PREINC PREDEC POSTINC POSTDEC
8990e307 93%left ARROW
fad39ff1 94%nonassoc <ival> ')'
8d063cd8 95%left '('
fad39ff1 96%left '[' '{'
8d063cd8 97
7f46837f
NC
98%token PEG
99
8d063cd8
LW
100%% /* RULES */
101
891be019 102/* The whole program */
ecb2f335 103prog : progstart
ae986130 104 /*CONTINUED*/ lineseq
ecb2f335 105 { $$ = $1; newPROG(block_end($1,$2)); }
8d063cd8
LW
106 ;
107
891be019 108/* An ordinary block */
a687059c 109block : '{' remember lineseq '}'
3280af22 110 { if (PL_copline > (line_t)$1)
eb160463 111 PL_copline = (line_t)$1;
36477c24 112 $$ = block_end($2, $3); }
a0d0e21e
LW
113 ;
114
55497cff 115remember: /* NULL */ /* start a full lexical scope */
116 { $$ = block_start(TRUE); }
117 ;
118
0d863452
RH
119mydefsv: /* NULL */ /* lexicalize $_ */
120 { $$ = (I32) allocmy("$_"); }
121 ;
122
ecb2f335
NIS
123progstart:
124 {
ecb2f335
NIS
125 PL_expect = XSTATE; $$ = block_start(TRUE);
126 }
127 ;
128
129
bbce6d69 130mblock : '{' mremember lineseq '}'
3280af22 131 { if (PL_copline > (line_t)$1)
eb160463 132 PL_copline = (line_t)$1;
36477c24 133 $$ = block_end($2, $3); }
55497cff 134 ;
135
136mremember: /* NULL */ /* start a partial lexical scope */
137 { $$ = block_start(FALSE); }
8d063cd8
LW
138 ;
139
500bedb6
DM
140savescope: /* NULL */ /* remember stack pos in case of error */
141 { $$ = PL_savestack_ix; }
142
891be019 143/* A collection of "lines" in the program */
8d063cd8 144lineseq : /* NULL */
79072805
LW
145 { $$ = Nullop; }
146 | lineseq decl
147 { $$ = $1; }
500bedb6
DM
148 | lineseq savescope line
149 { LEAVE_SCOPE($2);
150 $$ = append_list(OP_LINESEQ,
151 (LISTOP*)$1, (LISTOP*)$3);
3280af22 152 PL_pad_reset_pending = TRUE;
500bedb6 153 if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; }
8d063cd8
LW
154 ;
155
891be019 156/* A "line" in the program */
79072805
LW
157line : label cond
158 { $$ = newSTATEOP(0, $1, $2); }
8d063cd8 159 | loop /* loops add their own labels */
0d863452
RH
160 | switch /* ... and so do switches */
161 { $$ = $1; }
162 | label case
163 { $$ = newSTATEOP(0, $1, $2); }
8d063cd8
LW
164 | label ';'
165 { if ($1 != Nullch) {
79072805 166 $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0));
450a55e4
LW
167 }
168 else {
79072805 169 $$ = Nullop;
3280af22 170 PL_copline = NOLINE;
32c2e4fb 171 }
3280af22 172 PL_expect = XSTATE; }
8d063cd8 173 | label sideff ';'
79072805 174 { $$ = newSTATEOP(0, $1, $2);
3280af22 175 PL_expect = XSTATE; }
8d063cd8
LW
176 ;
177
891be019 178/* An expression which may have a side-effect */
a687059c 179sideff : error
79072805 180 { $$ = Nullop; }
a687059c 181 | expr
79072805 182 { $$ = $1; }
a687059c 183 | expr IF expr
79072805 184 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
a687059c 185 | expr UNLESS expr
79072805 186 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
a687059c 187 | expr WHILE expr
8990e307 188 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
55497cff 189 | expr UNTIL iexpr
190 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);}
ecca16b0 191 | expr FOR expr
eb160463 192 { $$ = newFOROP(0, Nullch, (line_t)$2,
ecca16b0 193 Nullop, $3, $1, Nullop); }
79072805
LW
194 ;
195
891be019 196/* else and elsif blocks */
79072805
LW
197else : /* NULL */
198 { $$ = Nullop; }
55497cff 199 | ELSE mblock
38a230cb 200 { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); }
55497cff 201 | ELSIF '(' mexpr ')' mblock else
eb160463 202 { PL_copline = (line_t)$1;
2c15bef3 203 $$ = newCONDOP(0, $3, scope($5), $6);
3280af22 204 PL_hints |= HINT_BLOCK_SCOPE; }
79072805
LW
205 ;
206
891be019 207/* Real conditional expressions */
55497cff 208cond : IF '(' remember mexpr ')' mblock else
eb160463 209 { PL_copline = (line_t)$1;
36477c24 210 $$ = block_end($3,
bbce6d69 211 newCONDOP(0, $4, scope($6), $7)); }
55497cff 212 | UNLESS '(' remember miexpr ')' mblock else
eb160463 213 { PL_copline = (line_t)$1;
36477c24 214 $$ = block_end($3,
bbce6d69 215 newCONDOP(0, $4, scope($6), $7)); }
79072805
LW
216 ;
217
0d863452
RH
218/* Cases for a switch statement */
219case : WHEN '(' remember mexpr ')' mblock
220 { $$ = block_end($3,
221 newWHENOP($4, scope($6))); }
222 | DEFAULT block
223 { $$ = newWHENOP(0, scope($2)); }
224 ;
225
891be019 226/* Continue blocks */
79072805
LW
227cont : /* NULL */
228 { $$ = Nullop; }
229 | CONTINUE block
230 { $$ = scope($2); }
231 ;
232
891be019 233/* Loops: while, until, for, and a bare block */
a034e688 234loop : label WHILE '(' remember texpr ')' mintro mblock cont
eb160463 235 { PL_copline = (line_t)$2;
36477c24 236 $$ = block_end($4,
55497cff 237 newSTATEOP(0, $1,
bbce6d69 238 newWHILEOP(0, 1, (LOOP*)Nullop,
a034e688
DM
239 $2, $5, $8, $9, $7))); }
240 | label UNTIL '(' remember iexpr ')' mintro mblock cont
eb160463 241 { PL_copline = (line_t)$2;
36477c24 242 $$ = block_end($4,
55497cff 243 newSTATEOP(0, $1,
bbce6d69 244 newWHILEOP(0, 1, (LOOP*)Nullop,
a034e688 245 $2, $5, $8, $9, $7))); }
bbce6d69 246 | label FOR MY remember my_scalar '(' mexpr ')' mblock cont
36477c24 247 { $$ = block_end($4,
eb160463 248 newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); }
bbce6d69 249 | label FOR scalar '(' remember mexpr ')' mblock cont
36477c24 250 { $$ = block_end($5,
eb160463 251 newFOROP(0, $1, (line_t)$2, mod($3, OP_ENTERLOOP),
bbce6d69 252 $6, $8, $9)); }
253 | label FOR '(' remember mexpr ')' mblock cont
36477c24 254 { $$ = block_end($4,
eb160463 255 newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); }
a034e688
DM
256 | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
257 mblock
8d063cd8 258 /* basically fake up an initialize-while lineseq */
36c66720 259 { OP *forop;
eb160463 260 PL_copline = (line_t)$2;
36c66720
RH
261 forop = newSTATEOP(0, $1,
262 newWHILEOP(0, 1, (LOOP*)Nullop,
263 $2, scalar($7),
a034e688 264 $12, $10, $9));
36c66720
RH
265 if ($5) {
266 forop = append_elem(OP_LINESEQ,
267 newSTATEOP(0, ($1?savepv($1):Nullch),
268 $5),
269 forop);
270 }
271
272 $$ = block_end($4, forop); }
79072805 273 | label block cont /* a block is a loop that happens once */
fb73857a 274 { $$ = newSTATEOP(0, $1,
275 newWHILEOP(0, 1, (LOOP*)Nullop,
a034e688 276 NOLINE, Nullop, $2, $3, 0)); }
8d063cd8
LW
277 ;
278
0d863452
RH
279/* Switch blocks */
280switch : label GIVEN '(' remember mydefsv mexpr ')' mblock
281 { PL_copline = (line_t) $2;
282 $$ = block_end($4,
283 newSTATEOP(0, $1,
284 newGIVENOP($6, scope($8),
285 (PADOFFSET) $5) )); }
286 ;
287
a034e688
DM
288/* determine whether there are any new my declarations */
289mintro : /* NULL */
290 { $$ = (PL_min_intro_pending &&
291 PL_max_intro_pending >= PL_min_intro_pending);
292 intro_my(); }
293
891be019 294/* Normal expression */
8d063cd8 295nexpr : /* NULL */
79072805 296 { $$ = Nullop; }
8d063cd8
LW
297 | sideff
298 ;
299
891be019 300/* Boolean expression */
8d063cd8 301texpr : /* NULL means true */
b73d6f50 302 { (void)scan_num("1", &yylval); $$ = yylval.opval; }
8d063cd8
LW
303 | expr
304 ;
305
891be019 306/* Inverted boolean expression */
55497cff 307iexpr : expr
308 { $$ = invert(scalar($1)); }
309 ;
310
891be019 311/* Expression with its own lexical scope */
55497cff 312mexpr : expr
bbce6d69 313 { $$ = $1; intro_my(); }
314 ;
315
316mnexpr : nexpr
317 { $$ = $1; intro_my(); }
55497cff 318 ;
319
55497cff 320miexpr : iexpr
bbce6d69 321 { $$ = $1; intro_my(); }
55497cff 322 ;
323
891be019 324/* Optional "MAIN:"-style loop labels */
8d063cd8
LW
325label : /* empty */
326 { $$ = Nullch; }
32c2e4fb 327 | LABEL
8d063cd8
LW
328 ;
329
891be019 330/* Some kind of declaration - does not take part in the parse tree */
8d063cd8
LW
331decl : format
332 { $$ = 0; }
333 | subrout
334 { $$ = 0; }
09bef843
SB
335 | mysubrout
336 { $$ = 0; }
a687059c
LW
337 | package
338 { $$ = 0; }
a0d0e21e 339 | use
85e6fe83 340 { $$ = 0; }
8d063cd8
LW
341 ;
342
44a8e56a 343format : FORMAT startformsub formname block
a0d0e21e 344 { newFORM($2, $3, $4); }
44a8e56a 345 ;
346
347formname: WORD { $$ = $1; }
348 | /* NULL */ { $$ = Nullop; }
8d063cd8
LW
349 ;
350
891be019 351/* Unimplemented "my sub foo { }" */
09bef843
SB
352mysubrout: MYSUB startsub subname proto subattrlist subbody
353 { newMYSUB($2, $3, $4, $5, $6); }
354 ;
355
891be019 356/* Subroutine definition */
09bef843
SB
357subrout : SUB startsub subname proto subattrlist subbody
358 { newATTRSUB($2, $3, $4, $5, $6); }
28757baa 359 ;
360
fa83b5b6 361startsub: /* NULL */ /* start a regular subroutine scope */
774d564b 362 { $$ = start_subparse(FALSE, 0); }
28757baa 363 ;
364
365startanonsub: /* NULL */ /* start an anonymous subroutine scope */
774d564b 366 { $$ = start_subparse(FALSE, CVf_ANON); }
28757baa 367 ;
368
44a8e56a 369startformsub: /* NULL */ /* start a format subroutine scope */
774d564b 370 { $$ = start_subparse(TRUE, 0); }
44a8e56a 371 ;
372
891be019 373/* Name of a subroutine - must be a bareword, could be special */
2596d9fe 374subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
e858de61 375 if (strEQ(name, "BEGIN") || strEQ(name, "END")
7d30b5c4 376 || strEQ(name, "INIT") || strEQ(name, "CHECK"))
1aff0e91 377 CvSPECIAL_on(PL_compcv);
28757baa 378 $$ = $1; }
a0d0e21e
LW
379 ;
380
891be019 381/* Subroutine prototype */
4633a7c4
LW
382proto : /* NULL */
383 { $$ = Nullop; }
384 | THING
385 ;
28757baa 386
891be019 387/* Optional list of subroutine attributes */
09bef843
SB
388subattrlist: /* NULL */
389 { $$ = Nullop; }
390 | COLONATTR THING
391 { $$ = $2; }
392 | COLONATTR
393 { $$ = Nullop; }
394 ;
395
891be019 396/* List of attributes for a "my" variable declaration */
09bef843
SB
397myattrlist: COLONATTR THING
398 { $$ = $2; }
399 | COLONATTR
400 { $$ = Nullop; }
401 ;
402
891be019 403/* Subroutine body - either null or a block */
28757baa 404subbody : block { $$ = $1; }
3280af22 405 | ';' { $$ = Nullop; PL_expect = XSTATE; }
8d063cd8
LW
406 ;
407
a687059c 408package : PACKAGE WORD ';'
79072805 409 { package($2); }
a687059c
LW
410 ;
411
28757baa 412use : USE startsub
1aff0e91 413 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
28757baa 414 WORD WORD listexpr ';'
415 { utilize($1, $2, $4, $5, $6); }
85e6fe83
LW
416 ;
417
891be019 418/* Ordinary expressions; logical combinations */
a0d0e21e
LW
419expr : expr ANDOP expr
420 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
421 | expr OROP expr
422 { $$ = newLOGOP($2, 0, $1, $3); }
c963b151
BD
423 | expr DOROP expr
424 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
fad39ff1 425 | argexpr %prec PREC_LOW
a0d0e21e
LW
426 ;
427
891be019 428/* Expressions are a list of terms joined by commas */
a0d0e21e
LW
429argexpr : argexpr ','
430 { $$ = $1; }
431 | argexpr ',' term
79072805 432 { $$ = append_elem(OP_LIST, $1, $3); }
fad39ff1 433 | term %prec PREC_LOW
8d063cd8
LW
434 ;
435
891be019 436/* List operators */
ad4673e5 437listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
79072805 438 { $$ = convert($1, OPf_STACKED,
a0d0e21e 439 prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); }
891be019 440 | FUNC '(' indirob expr ')' /* print ($fh @args */
79072805 441 { $$ = convert($1, OPf_STACKED,
a0d0e21e 442 prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); }
891be019 443 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
4633a7c4 444 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 445 append_elem(OP_LIST,
55497cff 446 prepend_elem(OP_LIST, scalar($1), $5),
a0d0e21e 447 newUNOP(OP_METHOD, 0, $3))); }
891be019 448 | term ARROW method /* $foo->bar */
b1524f17
SM
449 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
450 append_elem(OP_LIST, scalar($1),
451 newUNOP(OP_METHOD, 0, $3))); }
891be019 452 | METHOD indirob listexpr /* new Class @args */
4633a7c4 453 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 454 append_elem(OP_LIST,
4633a7c4 455 prepend_elem(OP_LIST, $2, $3),
a0d0e21e 456 newUNOP(OP_METHOD, 0, $1))); }
891be019 457 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
4633a7c4 458 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 459 append_elem(OP_LIST,
4633a7c4 460 prepend_elem(OP_LIST, $2, $4),
a0d0e21e 461 newUNOP(OP_METHOD, 0, $1))); }
891be019 462 | LSTOP listexpr /* print @args */
79072805 463 { $$ = convert($1, 0, $2); }
891be019 464 | FUNC '(' listexprcom ')' /* print (@args) */
79072805 465 { $$ = convert($1, 0, $3); }
ad4673e5 466 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
09bef843 467 { $3 = newANONATTRSUB($2, 0, Nullop, $3); }
891be019 468 listexpr %prec LSTOP /* ... @bar */
4633a7c4 469 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
28757baa 470 append_elem(OP_LIST,
471 prepend_elem(OP_LIST, $3, $5), $1)); }
a687059c
LW
472 ;
473
891be019 474/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
475method : METHOD
476 | scalar
477 ;
478
891be019
SC
479/* Some kind of subscripted expression */
480subscripted: star '{' expr ';' '}' /* *main::{something} */
481 /* In this and all the hash accessors, ';' is
482 * provided by the tokeniser */
264e1af3
RGS
483 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
484 PL_expect = XOPERATOR; }
891be019 485 | scalar '[' expr ']' /* $array[$element] */
fad39ff1 486 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); }
891be019 487 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
488 { $$ = newBINOP(OP_AELEM, 0,
489 ref(newAVREF($1),OP_RV2AV),
490 scalar($4));}
891be019 491 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
492 { $$ = newBINOP(OP_AELEM, 0,
493 ref(newAVREF($1),OP_RV2AV),
494 scalar($3));}
891be019 495 | scalar '{' expr ';' '}' /* $foo->{bar();} */
fad39ff1
SM
496 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
497 PL_expect = XOPERATOR; }
891be019 498 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
499 { $$ = newBINOP(OP_HELEM, 0,
500 ref(newHVREF($1),OP_RV2HV),
501 jmaybe($4));
502 PL_expect = XOPERATOR; }
891be019 503 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
504 { $$ = newBINOP(OP_HELEM, 0,
505 ref(newHVREF($1),OP_RV2HV),
506 jmaybe($3));
507 PL_expect = XOPERATOR; }
891be019 508 | term ARROW '(' ')' /* $subref->() */
fad39ff1
SM
509 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
510 newCVREF(0, scalar($1))); }
891be019 511 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1
SM
512 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
513 append_elem(OP_LIST, $4,
514 newCVREF(0, scalar($1)))); }
515
891be019 516 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1
SM
517 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
518 append_elem(OP_LIST, $3,
519 newCVREF(0, scalar($1)))); }
891be019 520 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1
SM
521 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
522 newCVREF(0, scalar($1))); }
9a9798c2
YST
523 | '(' expr ')' '[' expr ']' /* list slice */
524 { $$ = newSLICEOP(0, $5, $2); }
525 | '(' ')' '[' expr ']' /* empty list slice! */
526 { $$ = newSLICEOP(0, $4, Nullop); }
891be019 527 ;
fad39ff1 528
891be019
SC
529/* Binary operators between terms */
530termbinop : term ASSIGNOP term /* $x = $y */
a0d0e21e 531 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
891be019 532 | term POWOP term /* $x ** $y */
79072805 533 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 534 | term MULOP term /* $x * $y, $x x $y */
79072805
LW
535 { if ($2 != OP_REPEAT)
536 scalar($1);
537 $$ = newBINOP($2, 0, $1, scalar($3)); }
891be019 538 | term ADDOP term /* $x + $y */
79072805 539 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 540 | term SHIFTOP term /* $x >> $y, $x << $y */
79072805 541 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 542 | term RELOP term /* $x > $y, etc. */
79072805 543 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 544 | term EQOP term /* $x == $y, $x eq $y */
79072805 545 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 546 | term BITANDOP term /* $x & $y */
79072805 547 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 548 | term BITOROP term /* $x | $y */
79072805 549 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
891be019 550 | term DOTDOT term /* $x..$y, $x...$y */
79072805 551 { $$ = newRANGE($2, scalar($1), scalar($3));}
891be019 552 | term ANDAND term /* $x && $y */
463ee0b2 553 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
891be019 554 | term OROR term /* $x || $y */
463ee0b2 555 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
c963b151
BD
556 | term DORDOR term /* $x // $y */
557 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
891be019 558 | term MATCHOP term /* $x =~ /$y/ */
79072805 559 { $$ = bind_match($2, $1, $3); }
891be019 560 ;
8d063cd8 561
891be019
SC
562/* Unary operators and terms */
563termunop : '-' term %prec UMINUS /* -$x */
79072805 564 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
891be019 565 | '+' term %prec UMINUS /* +$x */
a687059c 566 { $$ = $2; }
891be019 567 | '!' term /* !$x */
79072805 568 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 569 | '~' term /* ~$x */
79072805 570 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));}
891be019 571 | term POSTINC /* $x++ */
79072805 572 { $$ = newUNOP(OP_POSTINC, 0,
463ee0b2 573 mod(scalar($1), OP_POSTINC)); }
891be019 574 | term POSTDEC /* $x-- */
79072805 575 { $$ = newUNOP(OP_POSTDEC, 0,
463ee0b2 576 mod(scalar($1), OP_POSTDEC)); }
891be019 577 | PREINC term /* ++$x */
79072805 578 { $$ = newUNOP(OP_PREINC, 0,
463ee0b2 579 mod(scalar($2), OP_PREINC)); }
891be019 580 | PREDEC term /* --$x */
79072805 581 { $$ = newUNOP(OP_PREDEC, 0,
463ee0b2 582 mod(scalar($2), OP_PREDEC)); }
891be019
SC
583
584 ;
585
586/* Constructors for anonymous data */
587anonymous: '[' expr ']'
588 { $$ = newANONLIST($2); }
589 | '[' ']'
590 { $$ = newANONLIST(Nullop); }
591 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
ecb2f335 592 { $$ = newANONHASH($2); }
891be019
SC
593 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
594 { $$ = newANONHASH(Nullop); }
595 | ANONSUB startanonsub proto subattrlist block %prec '('
596 { $$ = newANONATTRSUB($2, $3, $4, $5); }
597
598 ;
599
600/* Things called with "do" */
601termdo : DO term %prec UNIOP /* do $filename */
850e8516 602 { $$ = dofile($2, $1); }
891be019
SC
603 | DO block %prec '(' /* do { code */
604 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); }
605 | DO WORD '(' ')' /* do somesub() */
606 { $$ = newUNOP(OP_ENTERSUB,
607 OPf_SPECIAL|OPf_STACKED,
608 prepend_elem(OP_LIST,
609 scalar(newCVREF(
610 (OPpENTERSUB_AMPER<<8),
611 scalar($2)
612 )),Nullop)); dep();}
613 | DO WORD '(' expr ')' /* do somesub(@args) */
614 { $$ = newUNOP(OP_ENTERSUB,
615 OPf_SPECIAL|OPf_STACKED,
616 append_elem(OP_LIST,
617 $4,
618 scalar(newCVREF(
619 (OPpENTERSUB_AMPER<<8),
620 scalar($2)
621 )))); dep();}
622 | DO scalar '(' ')' /* do $subref () */
623 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
624 prepend_elem(OP_LIST,
625 scalar(newCVREF(0,scalar($2))), Nullop)); dep();}
626 | DO scalar '(' expr ')' /* do $subref (@args) */
627 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
628 prepend_elem(OP_LIST,
629 $4,
630 scalar(newCVREF(0,scalar($2))))); dep();}
631
632 ;
633
634term : termbinop
635 | termunop
636 | anonymous
637 | termdo
e9bdcc27 638 | term '?' term ':' term
891be019
SC
639 { $$ = newCONDOP(0, $1, $3, $5); }
640 | REFGEN term /* \$x, \@y, \%z */
641 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN)); }
09bef843
SB
642 | myattrterm %prec UNIOP
643 { $$ = $1; }
644 | LOCAL term %prec UNIOP
93a17b20 645 { $$ = localize($2,$1); }
a0d0e21e 646 | '(' expr ')'
79072805 647 { $$ = sawparens($2); }
8d063cd8 648 | '(' ')'
8990e307 649 { $$ = sawparens(newNULLLIST()); }
79072805 650 | scalar %prec '('
8d063cd8 651 { $$ = $1; }
79072805 652 | star %prec '('
8d063cd8 653 { $$ = $1; }
79072805 654 | hsh %prec '('
8d063cd8 655 { $$ = $1; }
79072805 656 | ary %prec '('
8d063cd8 657 { $$ = $1; }
891be019 658 | arylen %prec '(' /* $#x, $#{ something } */
79072805 659 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
660 | subscripted
661 { $$ = $1; }
891be019 662 | ary '[' expr ']' /* array slice */
79072805
LW
663 { $$ = prepend_elem(OP_ASLICE,
664 newOP(OP_PUSHMARK, 0),
79072805
LW
665 newLISTOP(OP_ASLICE, 0,
666 list($3),
a0d0e21e 667 ref($1, OP_ASLICE))); }
891be019 668 | ary '{' expr ';' '}' /* @hash{@keys} */
79072805
LW
669 { $$ = prepend_elem(OP_HSLICE,
670 newOP(OP_PUSHMARK, 0),
79072805
LW
671 newLISTOP(OP_HSLICE, 0,
672 list($3),
a0d0e21e 673 ref(oopsHV($1), OP_HSLICE)));
3280af22 674 PL_expect = XOPERATOR; }
79072805
LW
675 | THING %prec '('
676 { $$ = $1; }
891be019 677 | amper /* &foo; */
c07a80fd 678 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
ecb2f335 679 | amper '(' ')' /* &foo() */
a0d0e21e 680 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 681 | amper '(' expr ')' /* &foo(@args) */
a0d0e21e 682 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 683 append_elem(OP_LIST, $3, scalar($1))); }
891be019 684 | NOAMP WORD listexpr /* foo(@args) */
a0d0e21e 685 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
a5f75d66 686 append_elem(OP_LIST, $3, scalar($2))); }
891be019 687 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
85e6fe83 688 { $$ = newOP($1, OPf_SPECIAL);
3280af22 689 PL_hints |= HINT_BLOCK_SCOPE; }
a0d0e21e 690 | LOOPEX term
8990e307 691 { $$ = newLOOPEX($1,$2); }
891be019 692 | NOTOP argexpr /* not $foo */
c07a80fd 693 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
891be019 694 | UNIOP /* Unary op, $_ implied */
79072805 695 { $$ = newOP($1, 0); }
30173ab5 696 | UNIOP block /* eval { foo } */
79072805 697 { $$ = newUNOP($1, 0, $2); }
891be019 698 | UNIOP term /* Unary op */
79072805 699 { $$ = newUNOP($1, 0, $2); }
a72a1c8b
RGS
700 | REQUIRE /* require, $_ implied */
701 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
702 | REQUIRE term /* require Foo */
703 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
891be019 704 | UNIOPSUB term /* Sub treated as unop */
4633a7c4
LW
705 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
706 append_elem(OP_LIST, $2, scalar($1))); }
891be019 707 | FUNC0 /* Nullary operator */
79072805 708 { $$ = newOP($1, 0); }
ae986130 709 | FUNC0 '(' ')'
79072805 710 { $$ = newOP($1, 0); }
891be019 711 | FUNC0SUB /* Sub treated as nullop */
28757baa 712 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 713 scalar($1)); }
891be019 714 | FUNC1 '(' ')' /* not () */
a758b0b5
MHM
715 { $$ = $1 == OP_NOT ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
716 : newOP($1, OPf_SPECIAL); }
891be019 717 | FUNC1 '(' expr ')' /* not($foo) */
79072805 718 { $$ = newUNOP($1, 0, $3); }
131b3ad0
DM
719 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
720 { $$ = pmruntime($1, $3, 1); }
79072805 721 | WORD
378cc40b 722 | listop
8d063cd8
LW
723 ;
724
891be019 725/* "my" declarations, with optional attributes */
09bef843
SB
726myattrterm: MY myterm myattrlist
727 { $$ = my_attrs($2,$3); }
728 | MY myterm
729 { $$ = localize($2,$1); }
730 ;
731
891be019 732/* Things that can be "my"'d */
09bef843
SB
733myterm : '(' expr ')'
734 { $$ = sawparens($2); }
735 | '(' ')'
736 { $$ = sawparens(newNULLLIST()); }
737 | scalar %prec '('
738 { $$ = $1; }
739 | hsh %prec '('
740 { $$ = $1; }
741 | ary %prec '('
742 { $$ = $1; }
743 ;
744
891be019 745/* Basic list expressions */
fad39ff1 746listexpr: /* NULL */ %prec PREC_LOW
8990e307 747 { $$ = Nullop; }
fad39ff1 748 | argexpr %prec PREC_LOW
a0d0e21e
LW
749 { $$ = $1; }
750 ;
751
752listexprcom: /* NULL */
753 { $$ = Nullop; }
79072805
LW
754 | expr
755 { $$ = $1; }
a0d0e21e
LW
756 | expr ','
757 { $$ = $1; }
79072805
LW
758 ;
759
891be019
SC
760/* A little bit of trickery to make "for my $foo (@bar)" actually be
761 lexical */
55497cff 762my_scalar: scalar
3280af22 763 { PL_in_my = 0; $$ = my($1); }
55497cff 764 ;
765
79072805 766amper : '&' indirob
c07a80fd 767 { $$ = newCVREF($1,$2); }
a687059c
LW
768 ;
769
79072805
LW
770scalar : '$' indirob
771 { $$ = newSVREF($2); }
a687059c
LW
772 ;
773
79072805
LW
774ary : '@' indirob
775 { $$ = newAVREF($2); }
776 ;
777
778hsh : '%' indirob
779 { $$ = newHVREF($2); }
780 ;
781
782arylen : DOLSHARP indirob
783 { $$ = newAVREF($2); }
784 ;
785
786star : '*' indirob
a0d0e21e 787 { $$ = newGVREF(0,$2); }
79072805
LW
788 ;
789
891be019 790/* Indirect objects */
79072805
LW
791indirob : WORD
792 { $$ = scalar($1); }
fad39ff1 793 | scalar %prec PREC_LOW
463ee0b2 794 { $$ = scalar($1); }
79072805 795 | block
a0d0e21e 796 { $$ = scope($1); }
79072805 797
93a17b20
LW
798 | PRIVATEREF
799 { $$ = $1; }
8d063cd8 800 ;