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