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