This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Untie STDERR in IPC::Open3.
[perl5.git] / perly.y
... / ...
CommitLineData
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
47
48%token <ival> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';' '=' '.'
49
50%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
51%token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
52%token <opval> PLUGEXPR PLUGSTMT
53%token <pval> LABEL
54%token <ival> FORMAT SUB ANONSUB 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
64%type <ival> grammar remember mremember
65%type <ival> startsub startanonsub startformsub
66
67%type <ival> mintro
68
69%type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
70%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
71%type <opval> sliceme kvslice gelem
72%type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
73%type <opval> optlistexpr optexpr indirob listop method
74%type <opval> formname subname proto optsubbody cont my_scalar formblock
75%type <opval> subattrlist myattrlist myattrterm myterm
76%type <opval> realsubbody subsignature termbinop termunop anonymous termdo
77%type <opval> formstmtseq formline formarg
78
79%nonassoc <ival> PREC_LOW
80%nonassoc LOOPEX
81
82%left <ival> OROP DOROP
83%left <ival> ANDOP
84%right <ival> NOTOP
85%nonassoc LSTOP LSTOPSUB
86%left <ival> ','
87%right <ival> ASSIGNOP
88%right <ival> '?' ':'
89%nonassoc DOTDOT YADAYADA
90%left <ival> OROR DORDOR
91%left <ival> ANDAND
92%left <ival> BITOROP
93%left <ival> BITANDOP
94%nonassoc EQOP
95%nonassoc RELOP
96%nonassoc UNIOP UNIOPSUB
97%nonassoc REQUIRE
98%left <ival> SHIFTOP
99%left ADDOP
100%left MULOP
101%left <ival> MATCHOP
102%right <ival> '!' '~' UMINUS REFGEN
103%right <ival> POWOP
104%nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
105%left <ival> ARROW
106%nonassoc <ival> ')'
107%left <ival> '('
108%left '[' '{'
109
110%% /* RULES */
111
112/* Top-level choice of what kind of thing yyparse was called to parse */
113grammar : GRAMPROG
114 {
115 PL_parser->expect = XSTATE;
116 }
117 remember stmtseq
118 {
119 newPROG(block_end($3,$4));
120 $$ = 0;
121 }
122 | GRAMEXPR
123 {
124 parser->expect = XTERM;
125 }
126 optexpr
127 {
128 PL_eval_root = $3;
129 $$ = 0;
130 }
131 | GRAMBLOCK
132 {
133 parser->expect = XBLOCK;
134 }
135 block
136 {
137 PL_pad_reset_pending = TRUE;
138 PL_eval_root = $3;
139 $$ = 0;
140 yyunlex();
141 parser->yychar = YYEOF;
142 }
143 | GRAMBARESTMT
144 {
145 parser->expect = XSTATE;
146 }
147 barestmt
148 {
149 PL_pad_reset_pending = TRUE;
150 PL_eval_root = $3;
151 $$ = 0;
152 yyunlex();
153 parser->yychar = YYEOF;
154 }
155 | GRAMFULLSTMT
156 {
157 parser->expect = XSTATE;
158 }
159 fullstmt
160 {
161 PL_pad_reset_pending = TRUE;
162 PL_eval_root = $3;
163 $$ = 0;
164 yyunlex();
165 parser->yychar = YYEOF;
166 }
167 | GRAMSTMTSEQ
168 {
169 parser->expect = XSTATE;
170 }
171 stmtseq
172 {
173 PL_eval_root = $3;
174 $$ = 0;
175 }
176 ;
177
178/* An ordinary block */
179block : '{' remember stmtseq '}'
180 { if (PL_parser->copline > (line_t)$1)
181 PL_parser->copline = (line_t)$1;
182 $$ = block_end($2, $3);
183 }
184 ;
185
186/* format body */
187formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
188 { if (PL_parser->copline > (line_t)$1)
189 PL_parser->copline = (line_t)$1;
190 $$ = block_end($2, $5);
191 }
192 ;
193
194remember: /* NULL */ /* start a full lexical scope */
195 { $$ = block_start(TRUE); }
196 ;
197
198mblock : '{' mremember stmtseq '}'
199 { if (PL_parser->copline > (line_t)$1)
200 PL_parser->copline = (line_t)$1;
201 $$ = block_end($2, $3);
202 }
203 ;
204
205mremember: /* NULL */ /* start a partial lexical scope */
206 { $$ = block_start(FALSE); }
207 ;
208
209/* A sequence of statements in the program */
210stmtseq : /* NULL */
211 { $$ = (OP*)NULL; }
212 | stmtseq fullstmt
213 { $$ = op_append_list(OP_LINESEQ, $1, $2);
214 PL_pad_reset_pending = TRUE;
215 if ($1 && $2)
216 PL_hints |= HINT_BLOCK_SCOPE;
217 }
218 ;
219
220/* A sequence of format lines */
221formstmtseq: /* NULL */
222 { $$ = (OP*)NULL; }
223 | formstmtseq formline
224 { $$ = op_append_list(OP_LINESEQ, $1, $2);
225 PL_pad_reset_pending = TRUE;
226 if ($1 && $2)
227 PL_hints |= HINT_BLOCK_SCOPE;
228 }
229 ;
230
231/* A statement in the program, including optional labels */
232fullstmt: barestmt
233 {
234 $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
235 }
236 | labfullstmt
237 { $$ = $1; }
238 ;
239
240labfullstmt: LABEL barestmt
241 {
242 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
243 }
244 | LABEL labfullstmt
245 {
246 $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
247 }
248 ;
249
250/* A bare statement, lacking label and other aspects of state op */
251barestmt: PLUGSTMT
252 { $$ = $1; }
253 | FORMAT startformsub formname formblock
254 {
255 CV *fmtcv = PL_compcv;
256 newFORM($2, $3, $4);
257 $$ = (OP*)NULL;
258 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
259 SvREFCNT_inc_simple_void(fmtcv);
260 pad_add_anon(fmtcv, OP_NULL);
261 }
262 }
263 | SUB subname startsub
264 {
265 if ($2->op_type == OP_CONST) {
266 const char *const name =
267 SvPV_nolen_const(((SVOP*)$2)->op_sv);
268 if (strEQ(name, "BEGIN") || strEQ(name, "END")
269 || strEQ(name, "INIT") || strEQ(name, "CHECK")
270 || strEQ(name, "UNITCHECK"))
271 CvSPECIAL_on(PL_compcv);
272 }
273 else
274 /* State subs inside anonymous subs need to be
275 clonable themselves. */
276 if (CvANON(CvOUTSIDE(PL_compcv))
277 || CvCLONE(CvOUTSIDE(PL_compcv))
278 || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
279 CvOUTSIDE(PL_compcv)
280 ))[$2->op_targ]))
281 CvCLONE_on(PL_compcv);
282 PL_parser->in_my = 0;
283 PL_parser->in_my_stash = NULL;
284 }
285 proto subattrlist optsubbody
286 {
287 SvREFCNT_inc_simple_void(PL_compcv);
288 $2->op_type == OP_CONST
289 ? newATTRSUB($3, $2, $5, $6, $7)
290 : newMYSUB($3, $2, $5, $6, $7)
291 ;
292 $$ = (OP*)NULL;
293 intro_my();
294 }
295 | PACKAGE WORD WORD ';'
296 {
297 package($3);
298 if ($2)
299 package_version($2);
300 $$ = (OP*)NULL;
301 }
302 | USE startsub
303 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
304 WORD WORD optlistexpr ';'
305 {
306 SvREFCNT_inc_simple_void(PL_compcv);
307 utilize($1, $2, $4, $5, $6);
308 $$ = (OP*)NULL;
309 }
310 | IF '(' remember mexpr ')' mblock else
311 {
312 $$ = block_end($3,
313 newCONDOP(0, $4, op_scope($6), $7));
314 PL_parser->copline = (line_t)$1;
315 }
316 | UNLESS '(' remember miexpr ')' mblock else
317 {
318 $$ = block_end($3,
319 newCONDOP(0, $4, op_scope($6), $7));
320 PL_parser->copline = (line_t)$1;
321 }
322 | GIVEN '(' remember mexpr ')' mblock
323 {
324 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
325 $$ = block_end($3,
326 newGIVENOP($4, op_scope($6),
327 offset == NOT_IN_PAD
328 || PAD_COMPNAME_FLAGS_isOUR(offset)
329 ? 0
330 : offset));
331 PL_parser->copline = (line_t)$1;
332 }
333 | WHEN '(' remember mexpr ')' mblock
334 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
335 | DEFAULT block
336 { $$ = newWHENOP(0, op_scope($2)); }
337 | WHILE '(' remember texpr ')' mintro mblock cont
338 {
339 $$ = block_end($3,
340 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
341 $4, $7, $8, $6));
342 PL_parser->copline = (line_t)$1;
343 }
344 | UNTIL '(' remember iexpr ')' mintro mblock cont
345 {
346 $$ = block_end($3,
347 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
348 $4, $7, $8, $6));
349 PL_parser->copline = (line_t)$1;
350 }
351 | FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
352 mblock
353 {
354 OP *initop = $4;
355 OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
356 scalar($6), $11, $9, $8);
357 if (initop) {
358 forop = op_prepend_elem(OP_LINESEQ, initop,
359 op_append_elem(OP_LINESEQ,
360 newOP(OP_UNSTACK, OPf_SPECIAL),
361 forop));
362 }
363 $$ = block_end($3, forop);
364 PL_parser->copline = (line_t)$1;
365 }
366 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
367 {
368 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
369 PL_parser->copline = (line_t)$1;
370 }
371 | FOR scalar '(' remember mexpr ')' mblock cont
372 {
373 $$ = block_end($4, newFOROP(0,
374 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
375 PL_parser->copline = (line_t)$1;
376 }
377 | FOR '(' remember mexpr ')' mblock cont
378 {
379 $$ = block_end($3,
380 newFOROP(0, (OP*)NULL, $4, $6, $7));
381 PL_parser->copline = (line_t)$1;
382 }
383 | block cont
384 {
385 /* a block is a loop that happens once */
386 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
387 (OP*)NULL, $1, $2, 0);
388 }
389 | PACKAGE WORD WORD '{' remember
390 {
391 package($3);
392 if ($2) {
393 package_version($2);
394 }
395 }
396 stmtseq '}'
397 {
398 /* a block is a loop that happens once */
399 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
400 (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
401 if (PL_parser->copline > (line_t)$4)
402 PL_parser->copline = (line_t)$4;
403 }
404 | sideff ';'
405 {
406 PL_parser->expect = XSTATE;
407 $$ = $1;
408 }
409 | ';'
410 {
411 PL_parser->expect = XSTATE;
412 $$ = (OP*)NULL;
413 PL_parser->copline = NOLINE;
414 }
415 ;
416
417/* Format line */
418formline: THING formarg
419 { OP *list;
420 if ($2) {
421 OP *term = $2;
422 list = op_append_elem(OP_LIST, $1, term);
423 }
424 else {
425 list = $1;
426 }
427 if (PL_parser->copline == NOLINE)
428 PL_parser->copline = CopLINE(PL_curcop)-1;
429 else PL_parser->copline--;
430 $$ = newSTATEOP(0, NULL,
431 convert(OP_FORMLINE, 0, list));
432 }
433 ;
434
435formarg : /* NULL */
436 { $$ = NULL; }
437 | FORMLBRACK stmtseq FORMRBRACK
438 { $$ = op_unscope($2); }
439 ;
440
441/* An expression which may have a side-effect */
442sideff : error
443 { $$ = (OP*)NULL; }
444 | expr
445 { $$ = $1; }
446 | expr IF expr
447 { $$ = newLOGOP(OP_AND, 0, $3, $1); }
448 | expr UNLESS expr
449 { $$ = newLOGOP(OP_OR, 0, $3, $1); }
450 | expr WHILE expr
451 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
452 | expr UNTIL iexpr
453 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
454 | expr FOR expr
455 { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
456 PL_parser->copline = (line_t)$2; }
457 | expr WHEN expr
458 { $$ = newWHENOP($3, op_scope($1)); }
459 ;
460
461/* else and elsif blocks */
462else : /* NULL */
463 { $$ = (OP*)NULL; }
464 | ELSE mblock
465 {
466 ($2)->op_flags |= OPf_PARENS;
467 $$ = op_scope($2);
468 }
469 | ELSIF '(' mexpr ')' mblock else
470 { PL_parser->copline = (line_t)$1;
471 $$ = newCONDOP(0,
472 newSTATEOP(OPf_SPECIAL,NULL,$3),
473 op_scope($5), $6);
474 PL_hints |= HINT_BLOCK_SCOPE;
475 }
476 ;
477
478/* Continue blocks */
479cont : /* NULL */
480 { $$ = (OP*)NULL; }
481 | CONTINUE block
482 { $$ = op_scope($2); }
483 ;
484
485/* determine whether there are any new my declarations */
486mintro : /* NULL */
487 { $$ = (PL_min_intro_pending &&
488 PL_max_intro_pending >= PL_min_intro_pending);
489 intro_my(); }
490
491/* Normal expression */
492nexpr : /* NULL */
493 { $$ = (OP*)NULL; }
494 | sideff
495 ;
496
497/* Boolean expression */
498texpr : /* NULL means true */
499 { YYSTYPE tmplval;
500 (void)scan_num("1", &tmplval);
501 $$ = tmplval.opval; }
502 | expr
503 ;
504
505/* Inverted boolean expression */
506iexpr : expr
507 { $$ = invert(scalar($1)); }
508 ;
509
510/* Expression with its own lexical scope */
511mexpr : expr
512 { $$ = $1; intro_my(); }
513 ;
514
515mnexpr : nexpr
516 { $$ = $1; intro_my(); }
517 ;
518
519miexpr : iexpr
520 { $$ = $1; intro_my(); }
521 ;
522
523formname: WORD { $$ = $1; }
524 | /* NULL */ { $$ = (OP*)NULL; }
525 ;
526
527startsub: /* NULL */ /* start a regular subroutine scope */
528 { $$ = start_subparse(FALSE, 0);
529 SAVEFREESV(PL_compcv); }
530
531 ;
532
533startanonsub: /* NULL */ /* start an anonymous subroutine scope */
534 { $$ = start_subparse(FALSE, CVf_ANON);
535 SAVEFREESV(PL_compcv); }
536 ;
537
538startformsub: /* NULL */ /* start a format subroutine scope */
539 { $$ = start_subparse(TRUE, 0);
540 SAVEFREESV(PL_compcv); }
541 ;
542
543/* Name of a subroutine - must be a bareword, could be special */
544subname : WORD
545 | PRIVATEREF
546 ;
547
548/* Subroutine prototype */
549proto : /* NULL */
550 { $$ = (OP*)NULL; }
551 | THING
552 ;
553
554/* Optional list of subroutine attributes */
555subattrlist: /* NULL */
556 { $$ = (OP*)NULL; }
557 | COLONATTR THING
558 { $$ = $2; }
559 | COLONATTR
560 { $$ = (OP*)NULL; }
561 ;
562
563/* List of attributes for a "my" variable declaration */
564myattrlist: COLONATTR THING
565 { $$ = $2; }
566 | COLONATTR
567 { $$ = (OP*)NULL; }
568 ;
569
570/* Optional subroutine signature */
571subsignature: /* NULL */ { $$ = (OP*)NULL; }
572 | '('
573 {
574 if (!FEATURE_SIGNATURES_IS_ENABLED)
575 Perl_croak(aTHX_ "Experimental "
576 "subroutine signatures not enabled");
577 Perl_ck_warner_d(aTHX_
578 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
579 "The signatures feature is experimental");
580 $<opval>$ = parse_subsignature();
581 }
582 ')'
583 {
584 $$ = op_append_list(OP_LINESEQ, $<opval>2,
585 newSTATEOP(0, NULL, sawparens(newNULLLIST())));
586 PL_parser->expect = XBLOCK;
587 }
588 ;
589
590/* Subroutine body - block with optional signature */
591realsubbody: remember subsignature '{' stmtseq '}'
592 {
593 if (PL_parser->copline > (line_t)$3)
594 PL_parser->copline = (line_t)$3;
595 $$ = block_end($1,
596 op_append_list(OP_LINESEQ, $2, $4));
597 }
598 ;
599
600/* Optional subroutine body, for named subroutine declaration */
601optsubbody: realsubbody { $$ = $1; }
602 | ';' { $$ = (OP*)NULL;
603 PL_parser->expect = XSTATE;
604 }
605 ;
606
607/* Ordinary expressions; logical combinations */
608expr : expr ANDOP expr
609 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
610 | expr OROP expr
611 { $$ = newLOGOP($2, 0, $1, $3); }
612 | expr DOROP expr
613 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
614 | listexpr %prec PREC_LOW
615 ;
616
617/* Expressions are a list of terms joined by commas */
618listexpr: listexpr ','
619 { $$ = $1; }
620 | listexpr ',' term
621 {
622 OP* term = $3;
623 $$ = op_append_elem(OP_LIST, $1, term);
624 }
625 | term %prec PREC_LOW
626 ;
627
628/* List operators */
629listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
630 { $$ = convert($1, OPf_STACKED,
631 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
632 }
633 | FUNC '(' indirob expr ')' /* print ($fh @args */
634 { $$ = convert($1, OPf_STACKED,
635 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
636 }
637 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
638 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
639 op_append_elem(OP_LIST,
640 op_prepend_elem(OP_LIST, scalar($1), $5),
641 newUNOP(OP_METHOD, 0, $3)));
642 }
643 | term ARROW method /* $foo->bar */
644 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
645 op_append_elem(OP_LIST, scalar($1),
646 newUNOP(OP_METHOD, 0, $3)));
647 }
648 | METHOD indirob optlistexpr /* new Class @args */
649 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
650 op_append_elem(OP_LIST,
651 op_prepend_elem(OP_LIST, $2, $3),
652 newUNOP(OP_METHOD, 0, $1)));
653 }
654 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
655 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
656 op_append_elem(OP_LIST,
657 op_prepend_elem(OP_LIST, $2, $4),
658 newUNOP(OP_METHOD, 0, $1)));
659 }
660 | LSTOP optlistexpr /* print @args */
661 { $$ = convert($1, 0, $2); }
662 | FUNC '(' optexpr ')' /* print (@args) */
663 { $$ = convert($1, 0, $3); }
664 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
665 { SvREFCNT_inc_simple_void(PL_compcv);
666 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
667 optlistexpr %prec LSTOP /* ... @bar */
668 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
669 op_append_elem(OP_LIST,
670 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
671 }
672 ;
673
674/* Names of methods. May use $object->$methodname */
675method : METHOD
676 | scalar
677 ;
678
679/* Some kind of subscripted expression */
680subscripted: gelem '{' expr ';' '}' /* *main::{something} */
681 /* In this and all the hash accessors, ';' is
682 * provided by the tokeniser */
683 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
684 PL_parser->expect = XOPERATOR;
685 }
686 | scalar '[' expr ']' /* $array[$element] */
687 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
688 }
689 | term ARROW '[' expr ']' /* somearef->[$element] */
690 { $$ = newBINOP(OP_AELEM, 0,
691 ref(newAVREF($1),OP_RV2AV),
692 scalar($4));
693 }
694 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
695 { $$ = newBINOP(OP_AELEM, 0,
696 ref(newAVREF($1),OP_RV2AV),
697 scalar($3));
698 }
699 | scalar '{' expr ';' '}' /* $foo{bar();} */
700 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
701 PL_parser->expect = XOPERATOR;
702 }
703 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
704 { $$ = newBINOP(OP_HELEM, 0,
705 ref(newHVREF($1),OP_RV2HV),
706 jmaybe($4));
707 PL_parser->expect = XOPERATOR;
708 }
709 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
710 { $$ = newBINOP(OP_HELEM, 0,
711 ref(newHVREF($1),OP_RV2HV),
712 jmaybe($3));
713 PL_parser->expect = XOPERATOR;
714 }
715 | term ARROW '(' ')' /* $subref->() */
716 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
717 newCVREF(0, scalar($1))); }
718 | term ARROW '(' expr ')' /* $subref->(@args) */
719 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
720 op_append_elem(OP_LIST, $4,
721 newCVREF(0, scalar($1)))); }
722
723 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
724 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
725 op_append_elem(OP_LIST, $3,
726 newCVREF(0, scalar($1)))); }
727 | subscripted '(' ')' /* $foo->{bar}->() */
728 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
729 newCVREF(0, scalar($1))); }
730 | '(' expr ')' '[' expr ']' /* list slice */
731 { $$ = newSLICEOP(0, $5, $2); }
732 | QWLIST '[' expr ']' /* list literal slice */
733 { $$ = newSLICEOP(0, $3, $1); }
734 | '(' ')' '[' expr ']' /* empty list slice! */
735 { $$ = newSLICEOP(0, $4, (OP*)NULL); }
736 ;
737
738/* Binary operators between terms */
739termbinop: term ASSIGNOP term /* $x = $y */
740 { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
741 | term POWOP term /* $x ** $y */
742 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
743 | term MULOP term /* $x * $y, $x x $y */
744 { if ($2 != OP_REPEAT)
745 scalar($1);
746 $$ = newBINOP($2, 0, $1, scalar($3));
747 }
748 | term ADDOP term /* $x + $y */
749 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
750 | term SHIFTOP term /* $x >> $y, $x << $y */
751 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
752 | term RELOP term /* $x > $y, etc. */
753 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
754 | term EQOP term /* $x == $y, $x eq $y */
755 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
756 | term BITANDOP term /* $x & $y */
757 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
758 | term BITOROP term /* $x | $y */
759 { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
760 | term DOTDOT term /* $x..$y, $x...$y */
761 { $$ = newRANGE($2, scalar($1), scalar($3)); }
762 | term ANDAND term /* $x && $y */
763 { $$ = newLOGOP(OP_AND, 0, $1, $3); }
764 | term OROR term /* $x || $y */
765 { $$ = newLOGOP(OP_OR, 0, $1, $3); }
766 | term DORDOR term /* $x // $y */
767 { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
768 | term MATCHOP term /* $x =~ /$y/ */
769 { $$ = bind_match($2, $1, $3); }
770 ;
771
772/* Unary operators and terms */
773termunop : '-' term %prec UMINUS /* -$x */
774 { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
775 | '+' term %prec UMINUS /* +$x */
776 { $$ = $2; }
777
778 | '!' term /* !$x */
779 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
780 | '~' term /* ~$x */
781 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2)); }
782 | term POSTINC /* $x++ */
783 { $$ = newUNOP(OP_POSTINC, 0,
784 op_lvalue(scalar($1), OP_POSTINC)); }
785 | term POSTDEC /* $x-- */
786 { $$ = newUNOP(OP_POSTDEC, 0,
787 op_lvalue(scalar($1), OP_POSTDEC));}
788 | term POSTJOIN /* implicit join after interpolated ->@ */
789 { $$ = convert(OP_JOIN, 0,
790 op_append_elem(
791 OP_LIST,
792 newSVREF(scalar(
793 newSVOP(OP_CONST,0,
794 newSVpvs("\""))
795 )),
796 $1
797 ));
798 }
799 | PREINC term /* ++$x */
800 { $$ = newUNOP(OP_PREINC, 0,
801 op_lvalue(scalar($2), OP_PREINC)); }
802 | PREDEC term /* --$x */
803 { $$ = newUNOP(OP_PREDEC, 0,
804 op_lvalue(scalar($2), OP_PREDEC)); }
805
806 ;
807
808/* Constructors for anonymous data */
809anonymous: '[' expr ']'
810 { $$ = newANONLIST($2); }
811 | '[' ']'
812 { $$ = newANONLIST((OP*)NULL);}
813 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
814 { $$ = newANONHASH($2); }
815 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
816 { $$ = newANONHASH((OP*)NULL); }
817 | ANONSUB startanonsub proto subattrlist realsubbody %prec '('
818 { SvREFCNT_inc_simple_void(PL_compcv);
819 $$ = newANONATTRSUB($2, $3, $4, $5); }
820
821 ;
822
823/* Things called with "do" */
824termdo : DO term %prec UNIOP /* do $filename */
825 { $$ = dofile($2, $1);}
826 | DO block %prec '(' /* do { code */
827 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
828 ;
829
830term : termbinop
831 | termunop
832 | anonymous
833 | termdo
834 | term '?' term ':' term
835 { $$ = newCONDOP(0, $1, $3, $5); }
836 | REFGEN term /* \$x, \@y, \%z */
837 { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN)); }
838 | myattrterm %prec UNIOP
839 { $$ = $1; }
840 | LOCAL term %prec UNIOP
841 { $$ = localize($2,$1); }
842 | '(' expr ')'
843 { $$ = sawparens($2); }
844 | QWLIST
845 { $$ = $1; }
846 | '(' ')'
847 { $$ = sawparens(newNULLLIST()); }
848 | scalar %prec '('
849 { $$ = $1; }
850 | star %prec '('
851 { $$ = $1; }
852 | hsh %prec '('
853 { $$ = $1; }
854 | ary %prec '('
855 { $$ = $1; }
856 | arylen %prec '(' /* $#x, $#{ something } */
857 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
858 | subscripted
859 { $$ = $1; }
860 | sliceme '[' expr ']' /* array slice */
861 { $$ = op_prepend_elem(OP_ASLICE,
862 newOP(OP_PUSHMARK, 0),
863 newLISTOP(OP_ASLICE, 0,
864 list($3),
865 ref($1, OP_ASLICE)));
866 if ($$ && $1)
867 $$->op_private |=
868 $1->op_private & OPpSLICEWARNING;
869 }
870 | kvslice '[' expr ']' /* array key/value slice */
871 { $$ = op_prepend_elem(OP_KVASLICE,
872 newOP(OP_PUSHMARK, 0),
873 newLISTOP(OP_KVASLICE, 0,
874 list($3),
875 ref(oopsAV($1), OP_KVASLICE)));
876 if ($$ && $1)
877 $$->op_private |=
878 $1->op_private & OPpSLICEWARNING;
879 }
880 | sliceme '{' expr ';' '}' /* @hash{@keys} */
881 { $$ = op_prepend_elem(OP_HSLICE,
882 newOP(OP_PUSHMARK, 0),
883 newLISTOP(OP_HSLICE, 0,
884 list($3),
885 ref(oopsHV($1), OP_HSLICE)));
886 if ($$ && $1)
887 $$->op_private |=
888 $1->op_private & OPpSLICEWARNING;
889 PL_parser->expect = XOPERATOR;
890 }
891 | kvslice '{' expr ';' '}' /* %hash{@keys} */
892 { $$ = op_prepend_elem(OP_KVHSLICE,
893 newOP(OP_PUSHMARK, 0),
894 newLISTOP(OP_KVHSLICE, 0,
895 list($3),
896 ref($1, OP_KVHSLICE)));
897 if ($$ && $1)
898 $$->op_private |=
899 $1->op_private & OPpSLICEWARNING;
900 PL_parser->expect = XOPERATOR;
901 }
902 | THING %prec '('
903 { $$ = $1; }
904 | amper /* &foo; */
905 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
906 | amper '(' ')' /* &foo() or foo() */
907 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
908 }
909 | amper '(' expr ')' /* &foo(@args) or foo(@args) */
910 {
911 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
912 op_append_elem(OP_LIST, $3, scalar($1)));
913 }
914 | NOAMP subname optlistexpr /* foo @args (no parens) */
915 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
916 op_append_elem(OP_LIST, $3, scalar($2)));
917 }
918 | term ARROW '$' '*'
919 { $$ = newSVREF($1); }
920 | term ARROW '@' '*'
921 { $$ = newAVREF($1); }
922 | term ARROW '%' '*'
923 { $$ = newHVREF($1); }
924 | term ARROW '&' '*'
925 { $$ = newUNOP(OP_ENTERSUB, 0,
926 scalar(newCVREF($3,$1))); }
927 | term ARROW '*' '*' %prec '('
928 { $$ = newGVREF(0,$1); }
929 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
930 { $$ = newOP($1, OPf_SPECIAL);
931 PL_hints |= HINT_BLOCK_SCOPE; }
932 | LOOPEX term
933 { $$ = newLOOPEX($1,$2); }
934 | NOTOP listexpr /* not $foo */
935 { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
936 | UNIOP /* Unary op, $_ implied */
937 { $$ = newOP($1, 0); }
938 | UNIOP block /* eval { foo }* */
939 { $$ = newUNOP($1, 0, $2); }
940 | UNIOP term /* Unary op */
941 { $$ = newUNOP($1, 0, $2); }
942 | REQUIRE /* require, $_ implied */
943 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
944 | REQUIRE term /* require Foo */
945 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
946 | UNIOPSUB
947 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
948 | UNIOPSUB term /* Sub treated as unop */
949 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
950 op_append_elem(OP_LIST, $2, scalar($1))); }
951 | FUNC0 /* Nullary operator */
952 { $$ = newOP($1, 0); }
953 | FUNC0 '(' ')'
954 { $$ = newOP($1, 0);}
955 | FUNC0OP /* Same as above, but op created in toke.c */
956 { $$ = $1; }
957 | FUNC0OP '(' ')'
958 { $$ = $1; }
959 | FUNC0SUB /* Sub treated as nullop */
960 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
961 | FUNC1 '(' ')' /* not () */
962 { $$ = ($1 == OP_NOT)
963 ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
964 : newOP($1, OPf_SPECIAL); }
965 | FUNC1 '(' expr ')' /* not($foo) */
966 { $$ = newUNOP($1, 0, $3); }
967 | PMFUNC /* m//, s///, qr//, tr/// */
968 {
969 if ( $1->op_type != OP_TRANS
970 && $1->op_type != OP_TRANSR
971 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
972 {
973 $<ival>$ = start_subparse(FALSE, CVf_ANON);
974 SAVEFREESV(PL_compcv);
975 } else
976 $<ival>$ = 0;
977 }
978 '(' listexpr ')'
979 { $$ = pmruntime($1, $4, 1, $<ival>2); }
980 | WORD
981 | listop
982 | YADAYADA
983 {
984 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
985 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
986 }
987 | PLUGEXPR
988 ;
989
990/* "my" declarations, with optional attributes */
991myattrterm: MY myterm myattrlist
992 { $$ = my_attrs($2,$3); }
993 | MY myterm
994 { $$ = localize($2,$1); }
995 ;
996
997/* Things that can be "my"'d */
998myterm : '(' expr ')'
999 { $$ = sawparens($2); }
1000 | '(' ')'
1001 { $$ = sawparens(newNULLLIST()); }
1002
1003 | scalar %prec '('
1004 { $$ = $1; }
1005 | hsh %prec '('
1006 { $$ = $1; }
1007 | ary %prec '('
1008 { $$ = $1; }
1009 ;
1010
1011/* Basic list expressions */
1012optlistexpr: /* NULL */ %prec PREC_LOW
1013 { $$ = (OP*)NULL; }
1014 | listexpr %prec PREC_LOW
1015 { $$ = $1; }
1016 ;
1017
1018optexpr: /* NULL */
1019 { $$ = (OP*)NULL; }
1020 | expr
1021 { $$ = $1; }
1022 ;
1023
1024/* A little bit of trickery to make "for my $foo (@bar)" actually be
1025 lexical */
1026my_scalar: scalar
1027 { PL_parser->in_my = 0; $$ = my($1); }
1028 ;
1029
1030amper : '&' indirob
1031 { $$ = newCVREF($1,$2); }
1032 ;
1033
1034scalar : '$' indirob
1035 { $$ = newSVREF($2); }
1036 ;
1037
1038ary : '@' indirob
1039 { $$ = newAVREF($2);
1040 if ($$) $$->op_private |= $1;
1041 }
1042 ;
1043
1044hsh : '%' indirob
1045 { $$ = newHVREF($2);
1046 if ($$) $$->op_private |= $1;
1047 }
1048 ;
1049
1050arylen : DOLSHARP indirob
1051 { $$ = newAVREF($2); }
1052 | term ARROW DOLSHARP '*'
1053 { $$ = newAVREF($1); }
1054 ;
1055
1056star : '*' indirob
1057 { $$ = newGVREF(0,$2); }
1058 ;
1059
1060sliceme : ary
1061 | term ARROW '@'
1062 { $$ = newAVREF($1); }
1063 ;
1064
1065kvslice : hsh
1066 | term ARROW '%'
1067 { $$ = newHVREF($1); }
1068 ;
1069
1070gelem : star
1071 | term ARROW '*'
1072 { $$ = newGVREF(0,$1); }
1073 ;
1074
1075/* Indirect objects */
1076indirob : WORD
1077 { $$ = scalar($1); }
1078 | scalar %prec PREC_LOW
1079 { $$ = scalar($1); }
1080 | block
1081 { $$ = op_scope($1); }
1082
1083 | PRIVATEREF
1084 { $$ = $1; }
1085 ;