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