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