perl5160delta: Another XS bug
[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  * Note that these derived files are included and compiled twice; once
24  * from perly.c, and once from madly.c. The second time, a number of MAD
25  * macros are defined, which compile in extra code that allows the parse
26  * tree to be accurately dumped. In particular:
27  *
28  * MAD            defined if compiling madly.c
29  * DO_MAD(A)      expands to A  under madly.c, to null otherwise
30  * IF_MAD(a,b)    expands to A under madly.c, to B otherwise
31  * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
32  * TOKEN_FREE()   similarly
33  * OP_GETMAD()    similarly
34  * IVAL(i)        expands to (i)->tk_lval.ival or (i)
35  * PVAL(p)        expands to (p)->tk_lval.pval or (p)
36  *
37  * The main job of of this grammar is to call the various newFOO()
38  * functions in op.c to build a syntax tree of OP structs.
39  * It relies on the lexer in toke.c to do the tokenizing.
40  *
41  * Note: due to the way that the cleanup code works WRT to freeing ops on
42  * the parse stack, it is dangerous to assign to the $n variables within
43  * an action.
44  */
45
46 /*  Make the parser re-entrant. */
47
48 %pure_parser
49
50 /* FIXME for MAD - is the new mintro on while and until important?  */
51
52 %start grammar
53
54 %union {
55     I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
56                                 must always be 1st union member) */
57     char *pval;
58     OP *opval;
59     GV *gvval;
60 #ifdef PERL_IN_MADLY_C
61     TOKEN* p_tkval;
62     TOKEN* i_tkval;
63 #else
64     char *p_tkval;
65     I32 i_tkval;
66 #endif
67 #ifdef PERL_MAD
68     TOKEN* tkval;
69 #endif
70 }
71
72 %token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ
73
74 %token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';'
75
76 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
77 %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
78 %token <opval> PLUGEXPR PLUGSTMT
79 %token <p_tkval> LABEL
80 %token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
81 %token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
82 %token <i_tkval> GIVEN WHEN DEFAULT
83 %token <i_tkval> LOOPEX DOTDOT YADAYADA
84 %token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
85 %token <i_tkval> RELOP EQOP MULOP ADDOP
86 %token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
87 %token <i_tkval> LOCAL MY MYSUB REQUIRE
88 %token <i_tkval> COLONATTR
89
90 %type <i_tkval> lpar_or_qw
91
92 %type <ival> grammar remember mremember
93 %type <ival>  startsub startanonsub startformsub
94 /* FIXME for MAD - are these two ival? */
95 %type <ival> mydefsv mintro
96
97 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
98 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
99 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
100 %type <opval> optlistexpr optexpr indirob listop method
101 %type <opval> formname subname proto subbody cont my_scalar
102 %type <opval> subattrlist myattrlist myattrterm myterm
103 %type <opval> termbinop termunop anonymous termdo
104
105 %nonassoc <i_tkval> PREC_LOW
106 %nonassoc LOOPEX
107
108 %left <i_tkval> OROP DOROP
109 %left <i_tkval> ANDOP
110 %right <i_tkval> NOTOP
111 %nonassoc LSTOP LSTOPSUB
112 %left <i_tkval> ','
113 %right <i_tkval> ASSIGNOP
114 %right <i_tkval> '?' ':'
115 %nonassoc DOTDOT YADAYADA
116 %left <i_tkval> OROR DORDOR
117 %left <i_tkval> ANDAND
118 %left <i_tkval> BITOROP
119 %left <i_tkval> BITANDOP
120 %nonassoc EQOP
121 %nonassoc RELOP
122 %nonassoc UNIOP UNIOPSUB
123 %nonassoc REQUIRE
124 %left <i_tkval> SHIFTOP
125 %left ADDOP
126 %left MULOP
127 %left <i_tkval> MATCHOP
128 %right <i_tkval> '!' '~' UMINUS REFGEN
129 %right <i_tkval> POWOP
130 %nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
131 %left <i_tkval> ARROW
132 %nonassoc <i_tkval> ')'
133 %left <i_tkval> '('
134 %left '[' '{'
135
136 %token <i_tkval> PEG
137
138 %% /* RULES */
139
140 /* Top-level choice of what kind of thing yyparse was called to parse */
141 grammar :       GRAMPROG
142                         {
143                           PL_parser->expect = XSTATE;
144                         }
145                 remember stmtseq
146                         {
147                           newPROG(block_end($3,$4));
148                           $$ = 0;
149                         }
150         |       GRAMEXPR
151                         {
152                           parser->expect = XTERM;
153                         }
154                 optexpr
155                         {
156                           PL_eval_root = $3;
157                           $$ = 0;
158                         }
159         |       GRAMBLOCK
160                         {
161                           parser->expect = XBLOCK;
162                         }
163                 block
164                         {
165                           PL_pad_reset_pending = TRUE;
166                           PL_eval_root = $3;
167                           $$ = 0;
168                           yyunlex();
169                           parser->yychar = YYEOF;
170                         }
171         |       GRAMBARESTMT
172                         {
173                           parser->expect = XSTATE;
174                         }
175                 barestmt
176                         {
177                           PL_pad_reset_pending = TRUE;
178                           PL_eval_root = $3;
179                           $$ = 0;
180                           yyunlex();
181                           parser->yychar = YYEOF;
182                         }
183         |       GRAMFULLSTMT
184                         {
185                           parser->expect = XSTATE;
186                         }
187                 fullstmt
188                         {
189                           PL_pad_reset_pending = TRUE;
190                           PL_eval_root = $3;
191                           $$ = 0;
192                           yyunlex();
193                           parser->yychar = YYEOF;
194                         }
195         |       GRAMSTMTSEQ
196                         {
197                           parser->expect = XSTATE;
198                         }
199                 stmtseq
200                         {
201                           PL_eval_root = $3;
202                           $$ = 0;
203                         }
204         ;
205
206 /* An ordinary block */
207 block   :       '{' remember stmtseq '}'
208                         { if (PL_parser->copline > (line_t)IVAL($1))
209                               PL_parser->copline = (line_t)IVAL($1);
210                           $$ = block_end($2, $3);
211                           TOKEN_GETMAD($1,$$,'{');
212                           TOKEN_GETMAD($4,$$,'}');
213                         }
214         ;
215
216 remember:       /* NULL */      /* start a full lexical scope */
217                         { $$ = block_start(TRUE); }
218         ;
219
220 mydefsv:        /* NULL */      /* lexicalize $_ */
221                         { $$ = (I32) Perl_allocmy(aTHX_ STR_WITH_LEN("$_"), 0); }
222         ;
223
224 mblock  :       '{' mremember stmtseq '}'
225                         { if (PL_parser->copline > (line_t)IVAL($1))
226                               PL_parser->copline = (line_t)IVAL($1);
227                           $$ = block_end($2, $3);
228                           TOKEN_GETMAD($1,$$,'{');
229                           TOKEN_GETMAD($4,$$,'}');
230                         }
231         ;
232
233 mremember:      /* NULL */      /* start a partial lexical scope */
234                         { $$ = block_start(FALSE); }
235         ;
236
237 /* A sequence of statements in the program */
238 stmtseq :       /* NULL */
239                         { $$ = (OP*)NULL; }
240         |       stmtseq fullstmt
241                         {   $$ = op_append_list(OP_LINESEQ, $1, $2);
242                             PL_pad_reset_pending = TRUE;
243                             if ($1 && $2)
244                                 PL_hints |= HINT_BLOCK_SCOPE;
245                         }
246         ;
247
248 /* A statement in the program, including optional labels */
249 fullstmt:       barestmt
250                         {
251                           if($1) {
252                               $$ = newSTATEOP(0, NULL, $1);
253                           } else {
254                               $$ = IF_MAD(newOP(OP_NULL, 0), NULL);
255                           }
256                         }
257         |       labfullstmt
258                         { $$ = $1; }
259         ;
260
261 labfullstmt:    LABEL barestmt
262                         {
263                           $$ = newSTATEOP(SvUTF8(((SVOP*)$1)->op_sv),
264                                         savepv(SvPVX(((SVOP*)$1)->op_sv)), $2);
265                           TOKEN_GETMAD($1,
266                               $2 ? cLISTOPx($$)->op_first : $$, 'L');
267                         }
268         |       LABEL labfullstmt
269                         {
270                           $$ = newSTATEOP(SvUTF8(((SVOP*)$1)->op_sv),
271                                         savepv(SvPVX(((SVOP*)$1)->op_sv)), $2);
272                           TOKEN_GETMAD($1, cLISTOPx($$)->op_first, 'L');
273                         }
274         ;
275
276 /* A bare statement, lacking label and other aspects of state op */
277 barestmt:       PLUGSTMT
278                         { $$ = $1; }
279         |       PEG
280                         {
281                           $$ = newOP(OP_NULL,0);
282                           TOKEN_GETMAD($1,$$,'p');
283                         }
284         |       FORMAT startformsub formname block
285                         {
286                           CV *fmtcv = PL_compcv;
287                           SvREFCNT_inc_simple_void(PL_compcv);
288 #ifdef MAD
289                           $$ = newFORM($2, $3, $4);
290                           prepend_madprops($1->tk_mad, $$, 'F');
291                           $1->tk_mad = 0;
292                           token_free($1);
293 #else
294                           newFORM($2, $3, $4);
295                           $$ = (OP*)NULL;
296 #endif
297                           if (CvOUTSIDE(fmtcv) && !CvUNIQUE(CvOUTSIDE(fmtcv))) {
298                               SvREFCNT_inc_simple_void(fmtcv);
299                               pad_add_anon(fmtcv, OP_NULL);
300                           }
301                         }
302         |       SUB startsub subname proto subattrlist subbody
303                         {
304                           SvREFCNT_inc_simple_void(PL_compcv);
305 #ifdef MAD
306                           {
307                               OP* o = newSVOP(OP_ANONCODE, 0,
308                                 (SV*)newATTRSUB($2, $3, $4, $5, $6));
309                               $$ = newOP(OP_NULL,0);
310                               op_getmad(o,$$,'&');
311                               op_getmad($3,$$,'n');
312                               op_getmad($4,$$,'s');
313                               op_getmad($5,$$,'a');
314                               token_getmad($1,$$,'d');
315                               append_madprops($6->op_madprop, $$, 0);
316                               $6->op_madprop = 0;
317                           }
318 #else
319                           newATTRSUB($2, $3, $4, $5, $6);
320                           $$ = (OP*)NULL;
321 #endif
322                         }
323         |       MYSUB startsub subname proto subattrlist subbody
324                         {
325                           /* Unimplemented "my sub foo { }" */
326                           SvREFCNT_inc_simple_void(PL_compcv);
327 #ifdef MAD
328                           $$ = newMYSUB($2, $3, $4, $5, $6);
329                           token_getmad($1,$$,'d');
330 #else
331                           newMYSUB($2, $3, $4, $5, $6);
332                           $$ = (OP*)NULL;
333 #endif
334                         }
335         |       PACKAGE WORD WORD ';'
336                         {
337 #ifdef MAD
338                           $$ = package($3);
339                           token_getmad($1,$$,'o');
340                           if ($2)
341                               package_version($2);
342                           token_getmad($4,$$,';');
343 #else
344                           package($3);
345                           if ($2)
346                               package_version($2);
347                           $$ = (OP*)NULL;
348 #endif
349                         }
350         |       USE startsub
351                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
352                 WORD WORD optlistexpr ';'
353                         {
354                           SvREFCNT_inc_simple_void(PL_compcv);
355 #ifdef MAD
356                           $$ = utilize(IVAL($1), $2, $4, $5, $6);
357                           token_getmad($1,$$,'o');
358                           token_getmad($7,$$,';');
359                           if (PL_parser->rsfp_filters &&
360                                       AvFILLp(PL_parser->rsfp_filters) >= 0)
361                               append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
362 #else
363                           utilize(IVAL($1), $2, $4, $5, $6);
364                           $$ = (OP*)NULL;
365 #endif
366                         }
367         |       IF lpar_or_qw remember mexpr ')' mblock else
368                         {
369                           $$ = block_end($3,
370                               newCONDOP(0, $4, op_scope($6), $7));
371                           TOKEN_GETMAD($1,$$,'I');
372                           TOKEN_GETMAD($2,$$,'(');
373                           TOKEN_GETMAD($5,$$,')');
374                           PL_parser->copline = (line_t)IVAL($1);
375                         }
376         |       UNLESS lpar_or_qw remember miexpr ')' mblock else
377                         {
378                           $$ = block_end($3,
379                               newCONDOP(0, $4, op_scope($6), $7));
380                           TOKEN_GETMAD($1,$$,'I');
381                           TOKEN_GETMAD($2,$$,'(');
382                           TOKEN_GETMAD($5,$$,')');
383                           PL_parser->copline = (line_t)IVAL($1);
384                         }
385         |       GIVEN lpar_or_qw remember mydefsv mexpr ')' mblock
386                         {
387                           $$ = block_end($3,
388                                   newGIVENOP($5, op_scope($7), (PADOFFSET)$4));
389                           PL_parser->copline = (line_t)IVAL($1);
390                         }
391         |       WHEN lpar_or_qw remember mexpr ')' mblock
392                         { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
393         |       DEFAULT block
394                         { $$ = newWHENOP(0, op_scope($2)); }
395         |       WHILE lpar_or_qw remember texpr ')' mintro mblock cont
396                         {
397                           $$ = block_end($3,
398                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
399                                       $4, $7, $8, $6));
400                           TOKEN_GETMAD($1,$$,'W');
401                           TOKEN_GETMAD($2,$$,'(');
402                           TOKEN_GETMAD($5,$$,')');
403                           PL_parser->copline = (line_t)IVAL($1);
404                         }
405         |       UNTIL lpar_or_qw remember iexpr ')' mintro mblock cont
406                         {
407                           $$ = block_end($3,
408                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
409                                       $4, $7, $8, $6));
410                           TOKEN_GETMAD($1,$$,'W');
411                           TOKEN_GETMAD($2,$$,'(');
412                           TOKEN_GETMAD($5,$$,')');
413                           PL_parser->copline = (line_t)IVAL($1);
414                         }
415         |       FOR lpar_or_qw remember mnexpr ';' texpr ';' mintro mnexpr ')'
416                 mblock
417                         {
418                           OP *initop = IF_MAD($4 ? $4 : newOP(OP_NULL, 0), $4);
419                           OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
420                                       scalar($6), $11, $9, $8);
421                           if (initop) {
422                               forop = op_prepend_elem(OP_LINESEQ, initop,
423                                   op_append_elem(OP_LINESEQ,
424                                       newOP(OP_UNSTACK, OPf_SPECIAL),
425                                       forop));
426                           }
427                           DO_MAD({ forop = newUNOP(OP_NULL, 0, forop); })
428                           $$ = block_end($3, forop);
429                           TOKEN_GETMAD($1,$$,'3');
430                           TOKEN_GETMAD($2,$$,'(');
431                           TOKEN_GETMAD($5,$$,'1');
432                           TOKEN_GETMAD($7,$$,'2');
433                           TOKEN_GETMAD($10,$$,')');
434                           PL_parser->copline = (line_t)IVAL($1);
435                         }
436         |       FOR MY remember my_scalar lpar_or_qw mexpr ')' mblock cont
437                         {
438                           $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
439                           TOKEN_GETMAD($1,$$,'W');
440                           TOKEN_GETMAD($2,$$,'d');
441                           TOKEN_GETMAD($5,$$,'(');
442                           TOKEN_GETMAD($7,$$,')');
443                           PL_parser->copline = (line_t)IVAL($1);
444                         }
445         |       FOR scalar lpar_or_qw remember mexpr ')' mblock cont
446                         {
447                           $$ = block_end($4, newFOROP(0,
448                                       op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
449                           TOKEN_GETMAD($1,$$,'W');
450                           TOKEN_GETMAD($3,$$,'(');
451                           TOKEN_GETMAD($6,$$,')');
452                           PL_parser->copline = (line_t)IVAL($1);
453                         }
454         |       FOR lpar_or_qw remember mexpr ')' mblock cont
455                         {
456                           $$ = block_end($3,
457                                   newFOROP(0, (OP*)NULL, $4, $6, $7));
458                           TOKEN_GETMAD($1,$$,'W');
459                           TOKEN_GETMAD($2,$$,'(');
460                           TOKEN_GETMAD($5,$$,')');
461                           PL_parser->copline = (line_t)IVAL($1);
462                         }
463         |       block cont
464                         {
465                           /* a block is a loop that happens once */
466                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
467                                   (OP*)NULL, $1, $2, 0);
468                         }
469         |       PACKAGE WORD WORD '{' remember
470                         {
471                           int save_3_latefree = $3->op_latefree;
472                           $3->op_latefree = 1;
473                           package($3);
474                           $3->op_latefree = save_3_latefree;
475                           if ($2) {
476                               int save_2_latefree = $2->op_latefree;
477                               $2->op_latefree = 1;
478                               package_version($2);
479                               $2->op_latefree = save_2_latefree;
480                           }
481                         }
482                 stmtseq '}'
483                         {
484                           /* a block is a loop that happens once */
485                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
486                                   (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
487                           op_free($3);
488                           if ($2)
489                               op_free($2);
490                           TOKEN_GETMAD($4,$$,'{');
491                           TOKEN_GETMAD($8,$$,'}');
492                           if (PL_parser->copline > (line_t)IVAL($4))
493                               PL_parser->copline = (line_t)IVAL($4);
494                         }
495         |       sideff ';'
496                         {
497                           PL_parser->expect = XSTATE;
498                           $$ = $1;
499                           TOKEN_GETMAD($2,$$,';');
500                         }
501         |       ';'
502                         {
503                           PL_parser->expect = XSTATE;
504                           $$ = IF_MAD(newOP(OP_NULL, 0), (OP*)NULL);
505                           TOKEN_GETMAD($1,$$,';');
506                           PL_parser->copline = NOLINE;
507                         }
508         ;
509
510 /* An expression which may have a side-effect */
511 sideff  :       error
512                         { $$ = (OP*)NULL; }
513         |       expr
514                         { $$ = $1; }
515         |       expr IF expr
516                         { $$ = newLOGOP(OP_AND, 0, $3, $1);
517                           TOKEN_GETMAD($2,$$,'i');
518                         }
519         |       expr UNLESS expr
520                         { $$ = newLOGOP(OP_OR, 0, $3, $1);
521                           TOKEN_GETMAD($2,$$,'i');
522                         }
523         |       expr WHILE expr
524                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
525                           TOKEN_GETMAD($2,$$,'w');
526                         }
527         |       expr UNTIL iexpr
528                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
529                           TOKEN_GETMAD($2,$$,'w');
530                         }
531         |       expr FOR expr
532                         { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
533                           TOKEN_GETMAD($2,$$,'w');
534                           PL_parser->copline = (line_t)IVAL($2);
535                         }
536         |       expr WHEN expr
537                         { $$ = newWHENOP($3, op_scope($1)); }
538         ;
539
540 /* else and elsif blocks */
541 else    :       /* NULL */
542                         { $$ = (OP*)NULL; }
543         |       ELSE mblock
544                         {
545                           ($2)->op_flags |= OPf_PARENS;
546                           $$ = op_scope($2);
547                           TOKEN_GETMAD($1,$$,'o');
548                         }
549         |       ELSIF lpar_or_qw mexpr ')' mblock else
550                         { PL_parser->copline = (line_t)IVAL($1);
551                             $$ = newCONDOP(0,
552                                 newSTATEOP(OPf_SPECIAL,NULL,$3),
553                                 op_scope($5), $6);
554                           PL_hints |= HINT_BLOCK_SCOPE;
555                           TOKEN_GETMAD($1,$$,'I');
556                           TOKEN_GETMAD($2,$$,'(');
557                           TOKEN_GETMAD($4,$$,')');
558                         }
559         ;
560
561 /* Continue blocks */
562 cont    :       /* NULL */
563                         { $$ = (OP*)NULL; }
564         |       CONTINUE block
565                         {
566                           $$ = op_scope($2);
567                           TOKEN_GETMAD($1,$$,'o');
568                         }
569         ;
570
571 /* determine whether there are any new my declarations */
572 mintro  :       /* NULL */
573                         { $$ = (PL_min_intro_pending &&
574                             PL_max_intro_pending >=  PL_min_intro_pending);
575                           intro_my(); }
576
577 /* Normal expression */
578 nexpr   :       /* NULL */
579                         { $$ = (OP*)NULL; }
580         |       sideff
581         ;
582
583 /* Boolean expression */
584 texpr   :       /* NULL means true */
585                         { YYSTYPE tmplval;
586                           (void)scan_num("1", &tmplval);
587                           $$ = tmplval.opval; }
588         |       expr
589         ;
590
591 /* Inverted boolean expression */
592 iexpr   :       expr
593                         { $$ = invert(scalar($1)); }
594         ;
595
596 /* Expression with its own lexical scope */
597 mexpr   :       expr
598                         { $$ = $1; intro_my(); }
599         ;
600
601 mnexpr  :       nexpr
602                         { $$ = $1; intro_my(); }
603         ;
604
605 miexpr  :       iexpr
606                         { $$ = $1; intro_my(); }
607         ;
608
609 formname:       WORD            { $$ = $1; }
610         |       /* NULL */      { $$ = (OP*)NULL; }
611         ;
612
613 startsub:       /* NULL */      /* start a regular subroutine scope */
614                         { $$ = start_subparse(FALSE, 0);
615                             SAVEFREESV(PL_compcv); }
616
617         ;
618
619 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
620                         { $$ = start_subparse(FALSE, CVf_ANON);
621                             SAVEFREESV(PL_compcv); }
622         ;
623
624 startformsub:   /* NULL */      /* start a format subroutine scope */
625                         { $$ = start_subparse(TRUE, 0);
626                             SAVEFREESV(PL_compcv); }
627         ;
628
629 /* Name of a subroutine - must be a bareword, could be special */
630 subname :       WORD    { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
631                           if (strEQ(name, "BEGIN") || strEQ(name, "END")
632                               || strEQ(name, "INIT") || strEQ(name, "CHECK")
633                               || strEQ(name, "UNITCHECK"))
634                               CvSPECIAL_on(PL_compcv);
635                           $$ = $1; }
636         ;
637
638 /* Subroutine prototype */
639 proto   :       /* NULL */
640                         { $$ = (OP*)NULL; }
641         |       THING
642         ;
643
644 /* Optional list of subroutine attributes */
645 subattrlist:    /* NULL */
646                         { $$ = (OP*)NULL; }
647         |       COLONATTR THING
648                         { $$ = $2;
649                           TOKEN_GETMAD($1,$$,':');
650                         }
651         |       COLONATTR
652                         { $$ = IF_MAD(
653                                     newOP(OP_NULL, 0),
654                                     (OP*)NULL
655                                 );
656                           TOKEN_GETMAD($1,$$,':');
657                         }
658         ;
659
660 /* List of attributes for a "my" variable declaration */
661 myattrlist:     COLONATTR THING
662                         { $$ = $2;
663                           TOKEN_GETMAD($1,$$,':');
664                         }
665         |       COLONATTR
666                         { $$ = IF_MAD(
667                                     newOP(OP_NULL, 0),
668                                     (OP*)NULL
669                                 );
670                           TOKEN_GETMAD($1,$$,':');
671                         }
672         ;
673
674 /* Subroutine body - either null or a block */
675 subbody :       block   { $$ = $1; }
676         |       ';'     { $$ = IF_MAD(
677                                     newOP(OP_NULL,0),
678                                     (OP*)NULL
679                                 );
680                           PL_parser->expect = XSTATE;
681                           TOKEN_GETMAD($1,$$,';');
682                         }
683         ;
684
685 /* Ordinary expressions; logical combinations */
686 expr    :       expr ANDOP expr
687                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
688                           TOKEN_GETMAD($2,$$,'o');
689                         }
690         |       expr OROP expr
691                         { $$ = newLOGOP(IVAL($2), 0, $1, $3);
692                           TOKEN_GETMAD($2,$$,'o');
693                         }
694         |       expr DOROP expr
695                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
696                           TOKEN_GETMAD($2,$$,'o');
697                         }
698         |       listexpr %prec PREC_LOW
699         ;
700
701 /* Expressions are a list of terms joined by commas */
702 listexpr:       listexpr ','
703                         {
704 #ifdef MAD
705                           OP* op = newNULLLIST();
706                           token_getmad($2,op,',');
707                           $$ = op_append_elem(OP_LIST, $1, op);
708 #else
709                           $$ = $1;
710 #endif
711                         }
712         |       listexpr ',' term
713                         { 
714                           OP* term = $3;
715                           DO_MAD(
716                               term = newUNOP(OP_NULL, 0, term);
717                               token_getmad($2,term,',');
718                           )
719                           $$ = op_append_elem(OP_LIST, $1, term);
720                         }
721         |       term %prec PREC_LOW
722         ;
723
724 /* List operators */
725 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
726                         { $$ = convert(IVAL($1), OPf_STACKED,
727                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
728                           TOKEN_GETMAD($1,$$,'o');
729                         }
730         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
731                         { $$ = convert(IVAL($1), OPf_STACKED,
732                                 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
733                           TOKEN_GETMAD($1,$$,'o');
734                           TOKEN_GETMAD($2,$$,'(');
735                           TOKEN_GETMAD($5,$$,')');
736                         }
737         |       term ARROW method lpar_or_qw optexpr ')' /* $foo->bar(list) */
738                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
739                                 op_append_elem(OP_LIST,
740                                     op_prepend_elem(OP_LIST, scalar($1), $5),
741                                     newUNOP(OP_METHOD, 0, $3)));
742                           TOKEN_GETMAD($2,$$,'A');
743                           TOKEN_GETMAD($4,$$,'(');
744                           TOKEN_GETMAD($6,$$,')');
745                         }
746         |       term ARROW method                     /* $foo->bar */
747                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
748                                 op_append_elem(OP_LIST, scalar($1),
749                                     newUNOP(OP_METHOD, 0, $3)));
750                           TOKEN_GETMAD($2,$$,'A');
751                         }
752         |       METHOD indirob optlistexpr           /* new Class @args */
753                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
754                                 op_append_elem(OP_LIST,
755                                     op_prepend_elem(OP_LIST, $2, $3),
756                                     newUNOP(OP_METHOD, 0, $1)));
757                         }
758         |       FUNCMETH indirob '(' optexpr ')'    /* method $object (@args) */
759                         { $$ = convert(OP_ENTERSUB, OPf_STACKED,
760                                 op_append_elem(OP_LIST,
761                                     op_prepend_elem(OP_LIST, $2, $4),
762                                     newUNOP(OP_METHOD, 0, $1)));
763                           TOKEN_GETMAD($3,$$,'(');
764                           TOKEN_GETMAD($5,$$,')');
765                         }
766         |       LSTOP optlistexpr                    /* print @args */
767                         { $$ = convert(IVAL($1), 0, $2);
768                           TOKEN_GETMAD($1,$$,'o');
769                         }
770         |       FUNC '(' optexpr ')'                 /* print (@args) */
771                         { $$ = convert(IVAL($1), 0, $3);
772                           TOKEN_GETMAD($1,$$,'o');
773                           TOKEN_GETMAD($2,$$,'(');
774                           TOKEN_GETMAD($4,$$,')');
775                         }
776         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
777                         { SvREFCNT_inc_simple_void(PL_compcv);
778                           $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
779                     optlistexpr         %prec LSTOP  /* ... @bar */
780                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
781                                  op_append_elem(OP_LIST,
782                                    op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
783                         }
784         ;
785
786 /* Names of methods. May use $object->$methodname */
787 method  :       METHOD
788         |       scalar
789         ;
790
791 /* Some kind of subscripted expression */
792 subscripted:    star '{' expr ';' '}'        /* *main::{something} */
793                         /* In this and all the hash accessors, ';' is
794                          * provided by the tokeniser */
795                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
796                             PL_parser->expect = XOPERATOR;
797                           TOKEN_GETMAD($2,$$,'{');
798                           TOKEN_GETMAD($4,$$,';');
799                           TOKEN_GETMAD($5,$$,'}');
800                         }
801         |       scalar '[' expr ']'          /* $array[$element] */
802                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
803                           TOKEN_GETMAD($2,$$,'[');
804                           TOKEN_GETMAD($4,$$,']');
805                         }
806         |       term ARROW '[' expr ']'      /* somearef->[$element] */
807                         { $$ = newBINOP(OP_AELEM, 0,
808                                         ref(newAVREF($1),OP_RV2AV),
809                                         scalar($4));
810                           TOKEN_GETMAD($2,$$,'a');
811                           TOKEN_GETMAD($3,$$,'[');
812                           TOKEN_GETMAD($5,$$,']');
813                         }
814         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
815                         { $$ = newBINOP(OP_AELEM, 0,
816                                         ref(newAVREF($1),OP_RV2AV),
817                                         scalar($3));
818                           TOKEN_GETMAD($2,$$,'[');
819                           TOKEN_GETMAD($4,$$,']');
820                         }
821         |       scalar '{' expr ';' '}'    /* $foo{bar();} */
822                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
823                             PL_parser->expect = XOPERATOR;
824                           TOKEN_GETMAD($2,$$,'{');
825                           TOKEN_GETMAD($4,$$,';');
826                           TOKEN_GETMAD($5,$$,'}');
827                         }
828         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
829                         { $$ = newBINOP(OP_HELEM, 0,
830                                         ref(newHVREF($1),OP_RV2HV),
831                                         jmaybe($4));
832                             PL_parser->expect = XOPERATOR;
833                           TOKEN_GETMAD($2,$$,'a');
834                           TOKEN_GETMAD($3,$$,'{');
835                           TOKEN_GETMAD($5,$$,';');
836                           TOKEN_GETMAD($6,$$,'}');
837                         }
838         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
839                         { $$ = newBINOP(OP_HELEM, 0,
840                                         ref(newHVREF($1),OP_RV2HV),
841                                         jmaybe($3));
842                             PL_parser->expect = XOPERATOR;
843                           TOKEN_GETMAD($2,$$,'{');
844                           TOKEN_GETMAD($4,$$,';');
845                           TOKEN_GETMAD($5,$$,'}');
846                         }
847         |       term ARROW '(' ')'          /* $subref->() */
848                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
849                                    newCVREF(0, scalar($1)));
850                           TOKEN_GETMAD($2,$$,'a');
851                           TOKEN_GETMAD($3,$$,'(');
852                           TOKEN_GETMAD($4,$$,')');
853                         }
854         |       term ARROW '(' expr ')'     /* $subref->(@args) */
855                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
856                                    op_append_elem(OP_LIST, $4,
857                                        newCVREF(0, scalar($1))));
858                           TOKEN_GETMAD($2,$$,'a');
859                           TOKEN_GETMAD($3,$$,'(');
860                           TOKEN_GETMAD($5,$$,')');
861                         }
862
863         |       subscripted lpar_or_qw expr ')'   /* $foo->{bar}->(@args) */
864                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
865                                    op_append_elem(OP_LIST, $3,
866                                                newCVREF(0, scalar($1))));
867                           TOKEN_GETMAD($2,$$,'(');
868                           TOKEN_GETMAD($4,$$,')');
869                         }
870         |       subscripted lpar_or_qw ')'        /* $foo->{bar}->() */
871                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
872                                    newCVREF(0, scalar($1)));
873                           TOKEN_GETMAD($2,$$,'(');
874                           TOKEN_GETMAD($3,$$,')');
875                         }
876         |       '(' expr ')' '[' expr ']'            /* list slice */
877                         { $$ = newSLICEOP(0, $5, $2);
878                           TOKEN_GETMAD($1,$$,'(');
879                           TOKEN_GETMAD($3,$$,')');
880                           TOKEN_GETMAD($4,$$,'[');
881                           TOKEN_GETMAD($6,$$,']');
882                         }
883         |       QWLIST '[' expr ']'            /* list literal slice */
884                         { $$ = newSLICEOP(0, $3, $1);
885                           TOKEN_GETMAD($2,$$,'[');
886                           TOKEN_GETMAD($4,$$,']');
887                         }
888         |       '(' ')' '[' expr ']'                 /* empty list slice! */
889                         { $$ = newSLICEOP(0, $4, (OP*)NULL);
890                           TOKEN_GETMAD($1,$$,'(');
891                           TOKEN_GETMAD($2,$$,')');
892                           TOKEN_GETMAD($3,$$,'[');
893                           TOKEN_GETMAD($5,$$,']');
894                         }
895     ;
896
897 /* Binary operators between terms */
898 termbinop:      term ASSIGNOP term                     /* $x = $y */
899                         { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
900                           TOKEN_GETMAD($2,$$,'o');
901                         }
902         |       term POWOP term                        /* $x ** $y */
903                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
904                           TOKEN_GETMAD($2,$$,'o');
905                         }
906         |       term MULOP term                        /* $x * $y, $x x $y */
907                         {   if (IVAL($2) != OP_REPEAT)
908                                 scalar($1);
909                             $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
910                           TOKEN_GETMAD($2,$$,'o');
911                         }
912         |       term ADDOP term                        /* $x + $y */
913                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
914                           TOKEN_GETMAD($2,$$,'o');
915                         }
916         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
917                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
918                           TOKEN_GETMAD($2,$$,'o');
919                         }
920         |       term RELOP term                        /* $x > $y, etc. */
921                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
922                           TOKEN_GETMAD($2,$$,'o');
923                         }
924         |       term EQOP term                         /* $x == $y, $x eq $y */
925                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
926                           TOKEN_GETMAD($2,$$,'o');
927                         }
928         |       term BITANDOP term                     /* $x & $y */
929                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
930                           TOKEN_GETMAD($2,$$,'o');
931                         }
932         |       term BITOROP term                      /* $x | $y */
933                         { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
934                           TOKEN_GETMAD($2,$$,'o');
935                         }
936         |       term DOTDOT term                       /* $x..$y, $x...$y */
937                         {
938                           $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
939                           DO_MAD({
940                               UNOP *op;
941                               op = (UNOP*)$$;
942                               op = (UNOP*)op->op_first; /* get to flop */
943                               op = (UNOP*)op->op_first; /* get to flip */
944                               op = (UNOP*)op->op_first; /* get to range */
945                               token_getmad($2,(OP*)op,'o');
946                             })
947                         }
948         |       term ANDAND term                       /* $x && $y */
949                         { $$ = newLOGOP(OP_AND, 0, $1, $3);
950                           TOKEN_GETMAD($2,$$,'o');
951                         }
952         |       term OROR term                         /* $x || $y */
953                         { $$ = newLOGOP(OP_OR, 0, $1, $3);
954                           TOKEN_GETMAD($2,$$,'o');
955                         }
956         |       term DORDOR term                       /* $x // $y */
957                         { $$ = newLOGOP(OP_DOR, 0, $1, $3);
958                           TOKEN_GETMAD($2,$$,'o');
959                         }
960         |       term MATCHOP term                      /* $x =~ /$y/ */
961                         { $$ = bind_match(IVAL($2), $1, $3);
962                           TOKEN_GETMAD($2,
963                                 ($$->op_type == OP_NOT
964                                     ? ((UNOP*)$$)->op_first : $$),
965                                 '~');
966                         }
967     ;
968
969 /* Unary operators and terms */
970 termunop : '-' term %prec UMINUS                       /* -$x */
971                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
972                           TOKEN_GETMAD($1,$$,'o');
973                         }
974         |       '+' term %prec UMINUS                  /* +$x */
975                         { $$ = IF_MAD(
976                                     newUNOP(OP_NULL, 0, $2),
977                                     $2
978                                 );
979                           TOKEN_GETMAD($1,$$,'+');
980                         }
981         |       '!' term                               /* !$x */
982                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
983                           TOKEN_GETMAD($1,$$,'o');
984                         }
985         |       '~' term                               /* ~$x */
986                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
987                           TOKEN_GETMAD($1,$$,'o');
988                         }
989         |       term POSTINC                           /* $x++ */
990                         { $$ = newUNOP(OP_POSTINC, 0,
991                                         op_lvalue(scalar($1), OP_POSTINC));
992                           TOKEN_GETMAD($2,$$,'o');
993                         }
994         |       term POSTDEC                           /* $x-- */
995                         { $$ = newUNOP(OP_POSTDEC, 0,
996                                         op_lvalue(scalar($1), OP_POSTDEC));
997                           TOKEN_GETMAD($2,$$,'o');
998                         }
999         |       PREINC term                            /* ++$x */
1000                         { $$ = newUNOP(OP_PREINC, 0,
1001                                         op_lvalue(scalar($2), OP_PREINC));
1002                           TOKEN_GETMAD($1,$$,'o');
1003                         }
1004         |       PREDEC term                            /* --$x */
1005                         { $$ = newUNOP(OP_PREDEC, 0,
1006                                         op_lvalue(scalar($2), OP_PREDEC));
1007                           TOKEN_GETMAD($1,$$,'o');
1008                         }
1009
1010     ;
1011
1012 /* Constructors for anonymous data */
1013 anonymous:      '[' expr ']'
1014                         { $$ = newANONLIST($2);
1015                           TOKEN_GETMAD($1,$$,'[');
1016                           TOKEN_GETMAD($3,$$,']');
1017                         }
1018         |       '[' ']'
1019                         { $$ = newANONLIST((OP*)NULL);
1020                           TOKEN_GETMAD($1,$$,'[');
1021                           TOKEN_GETMAD($2,$$,']');
1022                         }
1023         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
1024                         { $$ = newANONHASH($2);
1025                           TOKEN_GETMAD($1,$$,'{');
1026                           TOKEN_GETMAD($3,$$,';');
1027                           TOKEN_GETMAD($4,$$,'}');
1028                         }
1029         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
1030                         { $$ = newANONHASH((OP*)NULL);
1031                           TOKEN_GETMAD($1,$$,'{');
1032                           TOKEN_GETMAD($2,$$,';');
1033                           TOKEN_GETMAD($3,$$,'}');
1034                         }
1035         |       ANONSUB startanonsub proto subattrlist block    %prec '('
1036                         { SvREFCNT_inc_simple_void(PL_compcv);
1037                           $$ = newANONATTRSUB($2, $3, $4, $5);
1038                           TOKEN_GETMAD($1,$$,'o');
1039                           OP_GETMAD($3,$$,'s');
1040                           OP_GETMAD($4,$$,'a');
1041                         }
1042
1043     ;
1044
1045 /* Things called with "do" */
1046 termdo  :       DO term %prec UNIOP                     /* do $filename */
1047                         { $$ = dofile($2, IVAL($1));
1048                           TOKEN_GETMAD($1,$$,'o');
1049                         }
1050         |       DO block        %prec '('               /* do { code */
1051                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
1052                           TOKEN_GETMAD($1,$$,'D');
1053                         }
1054         |       DO WORD lpar_or_qw ')'                  /* do somesub() */
1055                         { $$ = newUNOP(OP_ENTERSUB,
1056                             OPf_SPECIAL|OPf_STACKED,
1057                             op_prepend_elem(OP_LIST,
1058                                 scalar(newCVREF(
1059                                     (OPpENTERSUB_AMPER<<8),
1060                                     scalar($2)
1061                                 )),(OP*)NULL)); dep();
1062                           TOKEN_GETMAD($1,$$,'o');
1063                           TOKEN_GETMAD($3,$$,'(');
1064                           TOKEN_GETMAD($4,$$,')');
1065                         }
1066         |       DO WORD lpar_or_qw expr ')'             /* do somesub(@args) */
1067                         { $$ = newUNOP(OP_ENTERSUB,
1068                             OPf_SPECIAL|OPf_STACKED,
1069                             op_append_elem(OP_LIST,
1070                                 $4,
1071                                 scalar(newCVREF(
1072                                     (OPpENTERSUB_AMPER<<8),
1073                                     scalar($2)
1074                                 )))); dep();
1075                           TOKEN_GETMAD($1,$$,'o');
1076                           TOKEN_GETMAD($3,$$,'(');
1077                           TOKEN_GETMAD($5,$$,')');
1078                         }
1079         |       DO scalar lpar_or_qw ')'                /* do $subref () */
1080                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1081                             op_prepend_elem(OP_LIST,
1082                                 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
1083                           TOKEN_GETMAD($1,$$,'o');
1084                           TOKEN_GETMAD($3,$$,'(');
1085                           TOKEN_GETMAD($4,$$,')');
1086                         }
1087         |       DO scalar lpar_or_qw expr ')'           /* do $subref (@args) */
1088                         { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1089                             op_prepend_elem(OP_LIST,
1090                                 $4,
1091                                 scalar(newCVREF(0,scalar($2))))); dep();
1092                           TOKEN_GETMAD($1,$$,'o');
1093                           TOKEN_GETMAD($3,$$,'(');
1094                           TOKEN_GETMAD($5,$$,')');
1095                         }
1096
1097         ;
1098
1099 term    :       termbinop
1100         |       termunop
1101         |       anonymous
1102         |       termdo
1103         |       term '?' term ':' term
1104                         { $$ = newCONDOP(0, $1, $3, $5);
1105                           TOKEN_GETMAD($2,$$,'?');
1106                           TOKEN_GETMAD($4,$$,':');
1107                         }
1108         |       REFGEN term                          /* \$x, \@y, \%z */
1109                         { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
1110                           TOKEN_GETMAD($1,$$,'o');
1111                         }
1112         |       myattrterm      %prec UNIOP
1113                         { $$ = $1; }
1114         |       LOCAL term      %prec UNIOP
1115                         { $$ = localize($2,IVAL($1));
1116                           TOKEN_GETMAD($1,$$,'k');
1117                         }
1118         |       '(' expr ')'
1119                         { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1120                           TOKEN_GETMAD($1,$$,'(');
1121                           TOKEN_GETMAD($3,$$,')');
1122                         }
1123         |       QWLIST
1124                         { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
1125         |       '(' ')'
1126                         { $$ = sawparens(newNULLLIST());
1127                           TOKEN_GETMAD($1,$$,'(');
1128                           TOKEN_GETMAD($2,$$,')');
1129                         }
1130         |       scalar  %prec '('
1131                         { $$ = $1; }
1132         |       star    %prec '('
1133                         { $$ = $1; }
1134         |       hsh     %prec '('
1135                         { $$ = $1; }
1136         |       ary     %prec '('
1137                         { $$ = $1; }
1138         |       arylen  %prec '('                    /* $#x, $#{ something } */
1139                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
1140         |       subscripted
1141                         { $$ = $1; }
1142         |       ary '[' expr ']'                     /* array slice */
1143                         { $$ = op_prepend_elem(OP_ASLICE,
1144                                 newOP(OP_PUSHMARK, 0),
1145                                     newLISTOP(OP_ASLICE, 0,
1146                                         list($3),
1147                                         ref($1, OP_ASLICE)));
1148                           TOKEN_GETMAD($2,$$,'[');
1149                           TOKEN_GETMAD($4,$$,']');
1150                         }
1151         |       ary '{' expr ';' '}'                 /* @hash{@keys} */
1152                         { $$ = op_prepend_elem(OP_HSLICE,
1153                                 newOP(OP_PUSHMARK, 0),
1154                                     newLISTOP(OP_HSLICE, 0,
1155                                         list($3),
1156                                         ref(oopsHV($1), OP_HSLICE)));
1157                             PL_parser->expect = XOPERATOR;
1158                           TOKEN_GETMAD($2,$$,'{');
1159                           TOKEN_GETMAD($4,$$,';');
1160                           TOKEN_GETMAD($5,$$,'}');
1161                         }
1162         |       THING   %prec '('
1163                         { $$ = $1; }
1164         |       amper                                /* &foo; */
1165                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
1166         |       amper lpar_or_qw ')'                 /* &foo() */
1167                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1168                           TOKEN_GETMAD($2,$$,'(');
1169                           TOKEN_GETMAD($3,$$,')');
1170                         }
1171         |       amper lpar_or_qw expr ')'            /* &foo(@args) */
1172                         {
1173                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1174                                 op_append_elem(OP_LIST, $3, scalar($1)));
1175                           DO_MAD({
1176                               OP* op = $$;
1177                               if (op->op_type == OP_CONST) { /* defeat const fold */
1178                                 op = (OP*)op->op_madprop->mad_val;
1179                               }
1180                               token_getmad($2,op,'(');
1181                               token_getmad($4,op,')');
1182                           })
1183                         }
1184         |       NOAMP WORD optlistexpr               /* foo(@args) */
1185                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1186                             op_append_elem(OP_LIST, $3, scalar($2)));
1187                           TOKEN_GETMAD($1,$$,'o');
1188                         }
1189         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
1190                         { $$ = newOP(IVAL($1), OPf_SPECIAL);
1191                             PL_hints |= HINT_BLOCK_SCOPE;
1192                           TOKEN_GETMAD($1,$$,'o');
1193                         }
1194         |       LOOPEX term
1195                         { $$ = newLOOPEX(IVAL($1),$2);
1196                           TOKEN_GETMAD($1,$$,'o');
1197                         }
1198         |       NOTOP listexpr                       /* not $foo */
1199                         { $$ = newUNOP(OP_NOT, 0, scalar($2));
1200                           TOKEN_GETMAD($1,$$,'o');
1201                         }
1202         |       UNIOP                                /* Unary op, $_ implied */
1203                         { $$ = newOP(IVAL($1), 0);
1204                           TOKEN_GETMAD($1,$$,'o');
1205                         }
1206         |       UNIOP block                          /* eval { foo }* */
1207                         { $$ = newUNOP(IVAL($1), 0, $2);
1208                           TOKEN_GETMAD($1,$$,'o');
1209                         }
1210         |       UNIOP term                           /* Unary op */
1211                         { $$ = newUNOP(IVAL($1), 0, $2);
1212                           TOKEN_GETMAD($1,$$,'o');
1213                         }
1214         |       REQUIRE                              /* require, $_ implied */
1215                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1216                           TOKEN_GETMAD($1,$$,'o');
1217                         }
1218         |       REQUIRE term                         /* require Foo */
1219                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1220                           TOKEN_GETMAD($1,$$,'o');
1221                         }
1222         |       UNIOPSUB
1223                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
1224         |       UNIOPSUB term                        /* Sub treated as unop */
1225                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1226                             op_append_elem(OP_LIST, $2, scalar($1))); }
1227         |       FUNC0                                /* Nullary operator */
1228                         { $$ = newOP(IVAL($1), 0);
1229                           TOKEN_GETMAD($1,$$,'o');
1230                         }
1231         |       FUNC0 '(' ')'
1232                         { $$ = newOP(IVAL($1), 0);
1233                           TOKEN_GETMAD($1,$$,'o');
1234                           TOKEN_GETMAD($2,$$,'(');
1235                           TOKEN_GETMAD($3,$$,')');
1236                         }
1237         |       FUNC0OP       /* Same as above, but op created in toke.c */
1238                         { $$ = $1; }
1239         |       FUNC0OP '(' ')'
1240                         { $$ = $1;
1241                           TOKEN_GETMAD($2,$$,'(');
1242                           TOKEN_GETMAD($3,$$,')');
1243                         }
1244         |       FUNC0SUB                             /* Sub treated as nullop */
1245                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1246                                 scalar($1)); }
1247         |       FUNC1 '(' ')'                        /* not () */
1248                         { $$ = (IVAL($1) == OP_NOT)
1249                             ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1250                             : newOP(IVAL($1), OPf_SPECIAL);
1251
1252                           TOKEN_GETMAD($1,$$,'o');
1253                           TOKEN_GETMAD($2,$$,'(');
1254                           TOKEN_GETMAD($3,$$,')');
1255                         }
1256         |       FUNC1 '(' expr ')'                   /* not($foo) */
1257                         { $$ = newUNOP(IVAL($1), 0, $3);
1258                           TOKEN_GETMAD($1,$$,'o');
1259                           TOKEN_GETMAD($2,$$,'(');
1260                           TOKEN_GETMAD($4,$$,')');
1261                         }
1262         |       PMFUNC '(' listexpr ')'         /* m//, s///, tr/// */
1263                         { $$ = pmruntime($1, $3, 1);
1264                           TOKEN_GETMAD($2,$$,'(');
1265                           TOKEN_GETMAD($4,$$,')');
1266                         }
1267         |       WORD
1268         |       listop
1269         |       YADAYADA
1270                         {
1271                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1272                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1273                           TOKEN_GETMAD($1,$$,'X');
1274                         }
1275         |       PLUGEXPR
1276         ;
1277
1278 /* "my" declarations, with optional attributes */
1279 myattrterm:     MY myterm myattrlist
1280                         { $$ = my_attrs($2,$3);
1281                           DO_MAD(
1282                               token_getmad($1,$$,'d');
1283                               append_madprops($3->op_madprop, $$, 'a');
1284                               $3->op_madprop = 0;
1285                           )
1286                         }
1287         |       MY myterm
1288                         { $$ = localize($2,IVAL($1));
1289                           TOKEN_GETMAD($1,$$,'d');
1290                         }
1291         ;
1292
1293 /* Things that can be "my"'d */
1294 myterm  :       '(' expr ')'
1295                         { $$ = sawparens($2);
1296                           TOKEN_GETMAD($1,$$,'(');
1297                           TOKEN_GETMAD($3,$$,')');
1298                         }
1299         |       '(' ')'
1300                         { $$ = sawparens(newNULLLIST());
1301                           TOKEN_GETMAD($1,$$,'(');
1302                           TOKEN_GETMAD($2,$$,')');
1303                         }
1304         |       scalar  %prec '('
1305                         { $$ = $1; }
1306         |       hsh     %prec '('
1307                         { $$ = $1; }
1308         |       ary     %prec '('
1309                         { $$ = $1; }
1310         ;
1311
1312 /* Basic list expressions */
1313 optlistexpr:    /* NULL */ %prec PREC_LOW
1314                         { $$ = (OP*)NULL; }
1315         |       listexpr    %prec PREC_LOW
1316                         { $$ = $1; }
1317         ;
1318
1319 optexpr:        /* NULL */
1320                         { $$ = (OP*)NULL; }
1321         |       expr
1322                         { $$ = $1; }
1323         ;
1324
1325 lpar_or_qw:     '('
1326                         { $$ = $1; }
1327         |       QWLIST
1328                         { munge_qwlist_to_paren_list($1); }
1329                 '('
1330                         { $$ = $3; }
1331         ;
1332
1333 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1334    lexical */
1335 my_scalar:      scalar
1336                         { PL_parser->in_my = 0; $$ = my($1); }
1337         ;
1338
1339 amper   :       '&' indirob
1340                         { $$ = newCVREF(IVAL($1),$2);
1341                           TOKEN_GETMAD($1,$$,'&');
1342                         }
1343         ;
1344
1345 scalar  :       '$' indirob
1346                         { $$ = newSVREF($2);
1347                           TOKEN_GETMAD($1,$$,'$');
1348                         }
1349         ;
1350
1351 ary     :       '@' indirob
1352                         { $$ = newAVREF($2);
1353                           TOKEN_GETMAD($1,$$,'@');
1354                         }
1355         ;
1356
1357 hsh     :       '%' indirob
1358                         { $$ = newHVREF($2);
1359                           TOKEN_GETMAD($1,$$,'%');
1360                         }
1361         ;
1362
1363 arylen  :       DOLSHARP indirob
1364                         { $$ = newAVREF($2);
1365                           TOKEN_GETMAD($1,$$,'l');
1366                         }
1367         ;
1368
1369 star    :       '*' indirob
1370                         { $$ = newGVREF(0,$2);
1371                           TOKEN_GETMAD($1,$$,'*');
1372                         }
1373         ;
1374
1375 /* Indirect objects */
1376 indirob :       WORD
1377                         { $$ = scalar($1); }
1378         |       scalar %prec PREC_LOW
1379                         { $$ = scalar($1); }
1380         |       block
1381                         { $$ = op_scope($1); }
1382
1383         |       PRIVATEREF
1384                         { $$ = $1; }
1385         ;