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