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