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