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