This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Initial attempt at feature 'try'
[perl5.git] / perly.y
1 /*    perly.y
2  *
3  *    Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall
4  *    Copyright (c) 2007, 2008, 2009, 2010, 2011 by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 /*
12  * 'I see,' laughed Strider.  'I look foul and feel fair.  Is that it?
13  *  All that is gold does not glitter, not all those who wander are lost.'
14  *
15  *     [p.171 of _The Lord of the Rings_, I/x: "Strider"]
16  */
17
18 /*
19  * This file holds the grammar for the Perl language. If edited, you need
20  * to run regen_perly.pl, which re-creates the files perly.h, perly.tab
21  * and perly.act which are derived from this.
22  *
23  * The main job of this grammar is to call the various newFOO()
24  * functions in op.c to build a syntax tree of OP structs.
25  * It relies on the lexer in toke.c to do the tokenizing.
26  *
27  * Note: due to the way that the cleanup code works WRT to freeing ops on
28  * the parse stack, it is dangerous to assign to the $n variables within
29  * an action.
30  */
31
32 /*  Make the parser re-entrant. */
33
34 %define api.pure
35
36 %start grammar
37
38 %union {
39     I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
40                                 must always be 1st union member) */
41     char *pval;
42     OP *opval;
43     GV *gvval;
44 }
45
46 %token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ GRAMSUBSIGNATURE
47
48 %token <ival> PERLY_AMPERSAND
49 %token <ival> PERLY_BRACE_OPEN
50 %token <ival> PERLY_BRACE_CLOSE
51 %token <ival> PERLY_BRACKET_OPEN
52 %token <ival> PERLY_BRACKET_CLOSE
53 %token <ival> PERLY_COMMA
54 %token <ival> PERLY_DOLLAR
55 %token <ival> PERLY_DOT
56 %token <ival> PERLY_EQUAL_SIGN
57 %token <ival> PERLY_MINUS
58 %token <ival> PERLY_PERCENT_SIGN
59 %token <ival> PERLY_PLUS
60 %token <ival> PERLY_SEMICOLON
61 %token <ival> PERLY_SLASH
62 %token <ival> PERLY_SNAIL
63 %token <ival> PERLY_STAR
64
65 %token <opval> BAREWORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
66 %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
67 %token <opval> PLUGEXPR PLUGSTMT
68 %token <opval> LABEL
69 %token <ival> FORMAT SUB SIGSUB ANONSUB ANON_SIGSUB PACKAGE USE
70 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
71 %token <ival> GIVEN WHEN DEFAULT
72 %token <ival> TRY CATCH
73 %token <ival> LOOPEX DOTDOT YADAYADA
74 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
75 %token <ival> MULOP ADDOP
76 %token <ival> DOLSHARP DO HASHBRACK NOAMP
77 %token <ival> LOCAL MY REQUIRE
78 %token <ival> COLONATTR FORMLBRACK FORMRBRACK
79 %token <ival> SUBLEXSTART SUBLEXEND
80
81 %type <ival> grammar remember mremember
82 %type <ival>  startsub startanonsub startformsub
83
84 %type <ival> mintro
85
86 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
87 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
88 %type <opval> condition
89 %type <opval> sliceme kvslice gelem
90 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr
91 %type <opval> optlistexpr optexpr optrepl indirob listop method
92 %type <opval> formname subname proto cont my_scalar my_var
93 %type <opval> refgen_topic formblock
94 %type <opval> subattrlist myattrlist myattrterm myterm
95 %type <opval> termbinop termunop anonymous termdo
96 %type <opval> termrelop relopchain termeqop eqopchain
97 %type <ival>  sigslurpsigil
98 %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem
99 %type <opval> sigelem siglist siglistornull subsigguts subsignature optsubsignature
100 %type <opval> subbody optsubbody sigsubbody optsigsubbody
101 %type <opval> formstmtseq formline formarg
102
103 %nonassoc <ival> PREC_LOW
104 %nonassoc LOOPEX
105
106 %left <ival> OROP
107 %left <ival> ANDOP
108 %right <ival> NOTOP
109 %nonassoc LSTOP LSTOPSUB
110 %left PERLY_COMMA
111 %right <ival> ASSIGNOP
112 %right <ival> PERLY_QUESTION_MARK PERLY_COLON
113 %nonassoc DOTDOT
114 %left <ival> OROR DORDOR
115 %left <ival> ANDAND
116 %left <ival> BITOROP
117 %left <ival> BITANDOP
118 %left <ival> CHEQOP NCEQOP
119 %left <ival> CHRELOP NCRELOP
120 %nonassoc UNIOP UNIOPSUB
121 %nonassoc REQUIRE
122 %left <ival> SHIFTOP
123 %left ADDOP
124 %left MULOP
125 %left <ival> MATCHOP
126 %right <ival> PERLY_EXCLAMATION_MARK PERLY_TILDE UMINUS REFGEN
127 %right <ival> POWOP
128 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
129 %left <ival> ARROW
130 %nonassoc <ival> PERLY_PAREN_CLOSE
131 %left <ival> PERLY_PAREN_OPEN
132 %left PERLY_BRACKET_OPEN PERLY_BRACE_OPEN
133
134 %% /* RULES */
135
136 /* Top-level choice of what kind of thing yyparse was called to parse */
137 grammar :       GRAMPROG
138                         {
139                           parser->expect = XSTATE;
140                           $<ival>$ = 0;
141                         }
142                 remember stmtseq
143                         {
144                           newPROG(block_end($remember,$stmtseq));
145                           PL_compiling.cop_seq = 0;
146                           $$ = 0;
147                         }
148         |       GRAMEXPR
149                         {
150                           parser->expect = XTERM;
151                           $<ival>$ = 0;
152                         }
153                 optexpr
154                         {
155                           PL_eval_root = $optexpr;
156                           $$ = 0;
157                         }
158         |       GRAMBLOCK
159                         {
160                           parser->expect = XBLOCK;
161                           $<ival>$ = 0;
162                         }
163                 block
164                         {
165                           PL_pad_reset_pending = TRUE;
166                           PL_eval_root = $block;
167                           $$ = 0;
168                           yyunlex();
169                           parser->yychar = yytoken = YYEOF;
170                         }
171         |       GRAMBARESTMT
172                         {
173                           parser->expect = XSTATE;
174                           $<ival>$ = 0;
175                         }
176                 barestmt
177                         {
178                           PL_pad_reset_pending = TRUE;
179                           PL_eval_root = $barestmt;
180                           $$ = 0;
181                           yyunlex();
182                           parser->yychar = yytoken = YYEOF;
183                         }
184         |       GRAMFULLSTMT
185                         {
186                           parser->expect = XSTATE;
187                           $<ival>$ = 0;
188                         }
189                 fullstmt
190                         {
191                           PL_pad_reset_pending = TRUE;
192                           PL_eval_root = $fullstmt;
193                           $$ = 0;
194                           yyunlex();
195                           parser->yychar = yytoken = YYEOF;
196                         }
197         |       GRAMSTMTSEQ
198                         {
199                           parser->expect = XSTATE;
200                           $<ival>$ = 0;
201                         }
202                 stmtseq
203                         {
204                           PL_eval_root = $stmtseq;
205                           $$ = 0;
206                         }
207         |       GRAMSUBSIGNATURE
208                         {
209                           parser->expect = XSTATE;
210                           $<ival>$ = 0;
211                         }
212                 subsigguts
213                         {
214                           PL_eval_root = $subsigguts;
215                           $$ = 0;
216                         }
217         ;
218
219 /* An ordinary block */
220 block   :       PERLY_BRACE_OPEN remember stmtseq PERLY_BRACE_CLOSE
221                         { if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
222                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
223                           $$ = block_end($remember, $stmtseq);
224                         }
225         ;
226
227 /* format body */
228 formblock:      PERLY_EQUAL_SIGN remember PERLY_SEMICOLON FORMRBRACK formstmtseq PERLY_SEMICOLON PERLY_DOT
229                         { if (parser->copline > (line_t)$PERLY_EQUAL_SIGN)
230                               parser->copline = (line_t)$PERLY_EQUAL_SIGN;
231                           $$ = block_end($remember, $formstmtseq);
232                         }
233         ;
234
235 remember:       %empty  /* start a full lexical scope */
236                         { $$ = block_start(TRUE);
237                           parser->parsed_sub = 0; }
238         ;
239
240 mblock  :       PERLY_BRACE_OPEN mremember stmtseq PERLY_BRACE_CLOSE
241                         { if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
242                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
243                           $$ = block_end($mremember, $stmtseq);
244                         }
245         ;
246
247 mremember:      %empty  /* start a partial lexical scope */
248                         { $$ = block_start(FALSE);
249                           parser->parsed_sub = 0; }
250         ;
251
252 /* A sequence of statements in the program */
253 stmtseq :       %empty
254                         { $$ = NULL; }
255         |       stmtseq[list] fullstmt
256                         {   $$ = op_append_list(OP_LINESEQ, $list, $fullstmt);
257                             PL_pad_reset_pending = TRUE;
258                             if ($list && $fullstmt)
259                                 PL_hints |= HINT_BLOCK_SCOPE;
260                         }
261         ;
262
263 /* A sequence of format lines */
264 formstmtseq:    %empty
265                         { $$ = NULL; }
266         |       formstmtseq[list] formline
267                         {   $$ = op_append_list(OP_LINESEQ, $list, $formline);
268                             PL_pad_reset_pending = TRUE;
269                             if ($list && $formline)
270                                 PL_hints |= HINT_BLOCK_SCOPE;
271                         }
272         ;
273
274 /* A statement in the program, including optional labels */
275 fullstmt:       barestmt
276                         {
277                           $$ = $barestmt ? newSTATEOP(0, NULL, $barestmt) : NULL;
278                         }
279         |       labfullstmt
280                         { $$ = $labfullstmt; }
281         ;
282
283 labfullstmt:    LABEL barestmt
284                         {
285                           SV *label = cSVOPx_sv($LABEL);
286                           $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
287                                             savepv(SvPVX_const(label)), $barestmt);
288                           op_free($LABEL);
289                         }
290         |       LABEL labfullstmt[list]
291                         {
292                           SV *label = cSVOPx_sv($LABEL);
293                           $$ = newSTATEOP(SvFLAGS(label) & SVf_UTF8,
294                                             savepv(SvPVX_const(label)), $list);
295                           op_free($LABEL);
296                         }
297         ;
298
299 /* A bare statement, lacking label and other aspects of state op */
300 barestmt:       PLUGSTMT
301                         { $$ = $PLUGSTMT; }
302         |       FORMAT startformsub formname formblock
303                         {
304                           CV *fmtcv = PL_compcv;
305                           newFORM($startformsub, $formname, $formblock);
306                           $$ = NULL;
307                           if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
308                               pad_add_weakref(fmtcv);
309                           }
310                           parser->parsed_sub = 1;
311                         }
312         |       SUB subname startsub
313                     /* sub declaration or definition not within scope
314                        of 'use feature "signatures"'*/
315                         {
316                           init_named_cv(PL_compcv, $subname);
317                           parser->in_my = 0;
318                           parser->in_my_stash = NULL;
319                         }
320                     proto subattrlist optsubbody
321                         {
322                           SvREFCNT_inc_simple_void(PL_compcv);
323                           $subname->op_type == OP_CONST
324                               ? newATTRSUB($startsub, $subname, $proto, $subattrlist, $optsubbody)
325                               : newMYSUB($startsub, $subname, $proto, $subattrlist, $optsubbody)
326                           ;
327                           $$ = NULL;
328                           intro_my();
329                           parser->parsed_sub = 1;
330                         }
331         |       SIGSUB subname startsub
332                     /* sub declaration or definition under 'use feature
333                      * "signatures"'. (Note that a signature isn't
334                      * allowed in a declaration)
335                      */
336                         {
337                           init_named_cv(PL_compcv, $subname);
338                           parser->in_my = 0;
339                           parser->in_my_stash = NULL;
340                         }
341                     subattrlist optsigsubbody
342                         {
343                           SvREFCNT_inc_simple_void(PL_compcv);
344                           $subname->op_type == OP_CONST
345                               ? newATTRSUB($startsub, $subname, NULL, $subattrlist, $optsigsubbody)
346                               : newMYSUB(  $startsub, $subname, NULL, $subattrlist, $optsigsubbody)
347                           ;
348                           $$ = NULL;
349                           intro_my();
350                           parser->parsed_sub = 1;
351                         }
352         |       PACKAGE BAREWORD[version] BAREWORD[package] PERLY_SEMICOLON
353                         {
354                           package($package);
355                           if ($version)
356                               package_version($version);
357                           $$ = NULL;
358                         }
359         |       USE startsub
360                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
361                 BAREWORD[version] BAREWORD[module] optlistexpr PERLY_SEMICOLON
362                         {
363                           SvREFCNT_inc_simple_void(PL_compcv);
364                           utilize($USE, $startsub, $version, $module, $optlistexpr);
365                           parser->parsed_sub = 1;
366                           $$ = NULL;
367                         }
368         |       IF PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock else
369                         {
370                           $$ = block_end($remember,
371                               newCONDOP(0, $mexpr, op_scope($mblock), $else));
372                           parser->copline = (line_t)$IF;
373                         }
374         |       UNLESS PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock else
375                         {
376                           $$ = block_end($remember,
377                               newCONDOP(0, $mexpr, $else, op_scope($mblock)));
378                           parser->copline = (line_t)$UNLESS;
379                         }
380         |       GIVEN PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock
381                         {
382                           $$ = block_end($remember, newGIVENOP($mexpr, op_scope($mblock), 0));
383                           parser->copline = (line_t)$GIVEN;
384                         }
385         |       WHEN PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock
386                         { $$ = block_end($remember, newWHENOP($mexpr, op_scope($mblock))); }
387         |       DEFAULT block
388                         { $$ = newWHENOP(0, op_scope($block)); }
389         |       WHILE PERLY_PAREN_OPEN remember texpr PERLY_PAREN_CLOSE mintro mblock cont
390                         {
391                           $$ = block_end($remember,
392                                   newWHILEOP(0, 1, NULL,
393                                       $texpr, $mblock, $cont, $mintro));
394                           parser->copline = (line_t)$WHILE;
395                         }
396         |       UNTIL PERLY_PAREN_OPEN remember iexpr PERLY_PAREN_CLOSE mintro mblock cont
397                         {
398                           $$ = block_end($remember,
399                                   newWHILEOP(0, 1, NULL,
400                                       $iexpr, $mblock, $cont, $mintro));
401                           parser->copline = (line_t)$UNTIL;
402                         }
403         |       FOR PERLY_PAREN_OPEN remember mnexpr[init_mnexpr] PERLY_SEMICOLON
404                         { parser->expect = XTERM; }
405                 texpr PERLY_SEMICOLON
406                         { parser->expect = XTERM; }
407                 mintro mnexpr[iterate_mnexpr] PERLY_PAREN_CLOSE
408                 mblock
409                         {
410                           OP *initop = $init_mnexpr;
411                           OP *forop = newWHILEOP(0, 1, NULL,
412                                       scalar($texpr), $mblock, $iterate_mnexpr, $mintro);
413                           if (initop) {
414                               forop = op_prepend_elem(OP_LINESEQ, initop,
415                                   op_append_elem(OP_LINESEQ,
416                                       newOP(OP_UNSTACK, OPf_SPECIAL),
417                                       forop));
418                           }
419                           PL_hints |= HINT_BLOCK_SCOPE;
420                           $$ = block_end($remember, forop);
421                           parser->copline = (line_t)$FOR;
422                         }
423         |       FOR MY remember my_scalar PERLY_PAREN_OPEN mexpr PERLY_PAREN_CLOSE mblock cont
424                         {
425                           $$ = block_end($remember, newFOROP(0, $my_scalar, $mexpr, $mblock, $cont));
426                           parser->copline = (line_t)$FOR;
427                         }
428         |       FOR scalar PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock cont
429                         {
430                           $$ = block_end($remember, newFOROP(0,
431                                       op_lvalue($scalar, OP_ENTERLOOP), $mexpr, $mblock, $cont));
432                           parser->copline = (line_t)$FOR;
433                         }
434         |       FOR my_refgen remember my_var
435                         { parser->in_my = 0; $<opval>$ = my($my_var); }[variable]
436                 PERLY_PAREN_OPEN mexpr PERLY_PAREN_CLOSE mblock cont
437                         {
438                           $$ = block_end(
439                                 $remember,
440                                 newFOROP(0,
441                                          op_lvalue(
442                                             newUNOP(OP_REFGEN, 0,
443                                                     $<opval>variable),
444                                             OP_ENTERLOOP),
445                                          $mexpr, $mblock, $cont)
446                           );
447                           parser->copline = (line_t)$FOR;
448                         }
449         |       FOR REFGEN refgen_topic PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock cont
450                         {
451                           $$ = block_end($remember, newFOROP(
452                                 0, op_lvalue(newUNOP(OP_REFGEN, 0,
453                                                      $refgen_topic),
454                                              OP_ENTERLOOP), $mexpr, $mblock, $cont));
455                           parser->copline = (line_t)$FOR;
456                         }
457         |       FOR PERLY_PAREN_OPEN remember mexpr PERLY_PAREN_CLOSE mblock cont
458                         {
459                           $$ = block_end($remember,
460                                   newFOROP(0, NULL, $mexpr, $mblock, $cont));
461                           parser->copline = (line_t)$FOR;
462                         }
463         |       TRY mblock[try] CATCH PERLY_PAREN_OPEN 
464                         { parser->in_my = 1; }
465                 remember scalar 
466                         { parser->in_my = 0; intro_my(); }
467                 PERLY_PAREN_CLOSE mblock[catch]
468                         {
469                           OP *tryblock, *catchblock;
470                           
471                           tryblock = newUNOP(OP_ENTERTRY, OPf_SPECIAL, $try);
472
473                           catchblock = newLOGOP(OP_CATCH, 0,
474                             newOP(OP_NULL, 0), /* LOGOP always needs an op_first */
475                             block_end($remember, op_scope($catch)));
476
477                           /* catchblock itself is an OP_NULL; the real OP_CATCH is
478                            * its op_first */
479                           assert(cUNOPx(catchblock)->op_first->op_type == OP_CATCH);
480                           cUNOPx(catchblock)->op_first->op_targ = $scalar->op_targ;
481                           op_free($scalar);
482
483                           $$ = op_append_list(OP_LEAVE,
484                             newOP(OP_ENTER, 0),
485                             op_append_list(OP_LINESEQ,
486                               tryblock, catchblock));
487                         }
488         |       block cont
489                         {
490                           /* a block is a loop that happens once */
491                           $$ = newWHILEOP(0, 1, NULL,
492                                   NULL, $block, $cont, 0);
493                         }
494         |       PACKAGE BAREWORD[version] BAREWORD[package] PERLY_BRACE_OPEN remember
495                         {
496                           package($package);
497                           if ($version) {
498                               package_version($version);
499                           }
500                         }
501                 stmtseq PERLY_BRACE_CLOSE
502                         {
503                           /* a block is a loop that happens once */
504                           $$ = newWHILEOP(0, 1, NULL,
505                                   NULL, block_end($remember, $stmtseq), NULL, 0);
506                           if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
507                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
508                         }
509         |       sideff PERLY_SEMICOLON
510                         {
511                           $$ = $sideff;
512                         }
513         |       YADAYADA PERLY_SEMICOLON
514                         {
515                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
516                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
517                         }
518         |       PERLY_SEMICOLON
519                         {
520                           $$ = NULL;
521                           parser->copline = NOLINE;
522                         }
523         ;
524
525 /* Format line */
526 formline:       THING formarg
527                         { OP *list;
528                           if ($formarg) {
529                               OP *term = $formarg;
530                               list = op_append_elem(OP_LIST, $THING, term);
531                           }
532                           else {
533                               list = $THING;
534                           }
535                           if (parser->copline == NOLINE)
536                                parser->copline = CopLINE(PL_curcop)-1;
537                           else parser->copline--;
538                           $$ = newSTATEOP(0, NULL,
539                                           op_convert_list(OP_FORMLINE, 0, list));
540                         }
541         ;
542
543 formarg :       %empty
544                         { $$ = NULL; }
545         |       FORMLBRACK stmtseq FORMRBRACK
546                         { $$ = op_unscope($stmtseq); }
547         ;
548
549 condition: expr
550 ;
551
552 /* An expression which may have a side-effect */
553 sideff  :       error
554                         { $$ = NULL; }
555         |       expr[body]
556                         { $$ = $body; }
557         |       expr[body] IF condition
558                         { $$ = newLOGOP(OP_AND, 0, $condition, $body); }
559         |       expr[body] UNLESS condition
560                         { $$ = newLOGOP(OP_OR, 0, $condition, $body); }
561         |       expr[body] WHILE condition
562                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($condition), $body); }
563         |       expr[body] UNTIL iexpr
564                         { $$ = newLOOPOP(OPf_PARENS, 1, $iexpr, $body); }
565         |       expr[body] FOR condition
566                         { $$ = newFOROP(0, NULL, $condition, $body, NULL);
567                           parser->copline = (line_t)$FOR; }
568         |       expr[body] WHEN condition
569                         { $$ = newWHENOP($condition, op_scope($body)); }
570         ;
571
572 /* else and elsif blocks */
573 else    :       %empty
574                         { $$ = NULL; }
575         |       ELSE mblock
576                         {
577                           ($mblock)->op_flags |= OPf_PARENS;
578                           $$ = op_scope($mblock);
579                         }
580         |       ELSIF PERLY_PAREN_OPEN mexpr PERLY_PAREN_CLOSE mblock else[else.recurse]
581                         { parser->copline = (line_t)$ELSIF;
582                             $$ = newCONDOP(0,
583                                 newSTATEOP(OPf_SPECIAL,NULL,$mexpr),
584                                 op_scope($mblock), $[else.recurse]);
585                           PL_hints |= HINT_BLOCK_SCOPE;
586                         }
587         ;
588
589 /* Continue blocks */
590 cont    :       %empty
591                         { $$ = NULL; }
592         |       CONTINUE block
593                         { $$ = op_scope($block); }
594         ;
595
596 /* determine whether there are any new my declarations */
597 mintro  :       %empty
598                         { $$ = (PL_min_intro_pending &&
599                             PL_max_intro_pending >=  PL_min_intro_pending);
600                           intro_my(); }
601
602 /* Normal expression */
603 nexpr   :       %empty
604                         { $$ = NULL; }
605         |       sideff
606         ;
607
608 /* Boolean expression */
609 texpr   :       %empty /* NULL means true */
610                         { YYSTYPE tmplval;
611                           (void)scan_num("1", &tmplval);
612                           $$ = tmplval.opval; }
613         |       expr
614         ;
615
616 /* Inverted boolean expression */
617 iexpr   :       expr
618                         { $$ = invert(scalar($expr)); }
619         ;
620
621 /* Expression with its own lexical scope */
622 mexpr   :       expr
623                         { $$ = $expr; intro_my(); }
624         ;
625
626 mnexpr  :       nexpr
627                         { $$ = $nexpr; intro_my(); }
628         ;
629
630 formname:       BAREWORD        { $$ = $BAREWORD; }
631         |       %empty  { $$ = NULL; }
632         ;
633
634 startsub:       %empty  /* start a regular subroutine scope */
635                         { $$ = start_subparse(FALSE, 0);
636                             SAVEFREESV(PL_compcv); }
637
638         ;
639
640 startanonsub:   %empty  /* start an anonymous subroutine scope */
641                         { $$ = start_subparse(FALSE, CVf_ANON);
642                             SAVEFREESV(PL_compcv); }
643         ;
644
645 startformsub:   %empty  /* start a format subroutine scope */
646                         { $$ = start_subparse(TRUE, 0);
647                             SAVEFREESV(PL_compcv); }
648         ;
649
650 /* Name of a subroutine - must be a bareword, could be special */
651 subname :       BAREWORD
652         |       PRIVATEREF
653         ;
654
655 /* Subroutine prototype */
656 proto   :       %empty
657                         { $$ = NULL; }
658         |       THING
659         ;
660
661 /* Optional list of subroutine attributes */
662 subattrlist:    %empty
663                         { $$ = NULL; }
664         |       COLONATTR THING
665                         { $$ = $THING; }
666         |       COLONATTR
667                         { $$ = NULL; }
668         ;
669
670 /* List of attributes for a "my" variable declaration */
671 myattrlist:     COLONATTR THING
672                         { $$ = $THING; }
673         |       COLONATTR
674                         { $$ = NULL; }
675         ;
676
677
678
679 /* --------------------------------------
680  * subroutine signature parsing
681  */
682
683 /* the '' or 'foo' part of a '$' or '@foo' etc signature variable  */
684 sigvarname:     %empty
685                         { parser->in_my = 0; $$ = NULL; }
686         |       PRIVATEREF
687                         { parser->in_my = 0; $$ = $PRIVATEREF; }
688         ;
689
690 sigslurpsigil:
691                 PERLY_SNAIL
692                         { $$ = '@'; }
693         |       PERLY_PERCENT_SIGN
694                         { $$ = '%'; }
695
696 /* @, %, @foo, %foo */
697 sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */ 
698                         {
699                             I32 sigil   = $sigslurpsigil;
700                             OP *var     = $sigvarname;
701                             OP *defexpr = $sigdefault;
702
703                             if (parser->sig_slurpy)
704                                 yyerror("Multiple slurpy parameters not allowed");
705                             parser->sig_slurpy = (char)sigil;
706
707                             if (defexpr)
708                                 yyerror("A slurpy parameter may not have "
709                                         "a default value");
710
711                             $$ = var ? newSTATEOP(0, NULL, var) : NULL;
712                         }
713         ;
714
715 /* default part of sub signature scalar element: i.e. '= default_expr' */
716 sigdefault:     %empty
717                         { $$ = NULL; }
718         |       ASSIGNOP
719                         { $$ = newOP(OP_NULL, 0); }
720         |       ASSIGNOP term
721                         { $$ = $term; }
722
723
724 /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */
725 sigscalarelem:
726                 PERLY_DOLLAR sigvarname sigdefault
727                         {
728                             OP *var     = $sigvarname;
729                             OP *defexpr = $sigdefault;
730
731                             if (parser->sig_slurpy)
732                                 yyerror("Slurpy parameter not last");
733
734                             parser->sig_elems++;
735
736                             if (defexpr) {
737                                 parser->sig_optelems++;
738
739                                 if (   defexpr->op_type == OP_NULL
740                                     && !(defexpr->op_flags & OPf_KIDS))
741                                 {
742                                     /* handle '$=' special case */
743                                     if (var)
744                                         yyerror("Optional parameter "
745                                                     "lacks default expression");
746                                     op_free(defexpr);
747                                 }
748                                 else { 
749                                     /* a normal '=default' expression */ 
750                                     OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM,
751                                                         defexpr,
752                                                         LINKLIST(defexpr));
753                                     /* re-purpose op_targ to hold @_ index */
754                                     defop->op_targ =
755                                         (PADOFFSET)(parser->sig_elems - 1);
756
757                                     if (var) {
758                                         var->op_flags |= OPf_STACKED;
759                                         (void)op_sibling_splice(var,
760                                                         NULL, 0, defop);
761                                         scalar(defop);
762                                     }
763                                     else
764                                         var = newUNOP(OP_NULL, 0, defop);
765
766                                     LINKLIST(var);
767                                     /* NB: normally the first child of a
768                                      * logop is executed before the logop,
769                                      * and it pushes a boolean result
770                                      * ready for the logop. For ARGDEFELEM,
771                                      * the op itself does the boolean
772                                      * calculation, so set the first op to
773                                      * it instead.
774                                      */
775                                     var->op_next = defop;
776                                     defexpr->op_next = var;
777                                 }
778                             }
779                             else {
780                                 if (parser->sig_optelems)
781                                     yyerror("Mandatory parameter "
782                                             "follows optional parameter");
783                             }
784
785                             $$ = var ? newSTATEOP(0, NULL, var) : NULL;
786                         }
787         ;
788
789
790 /* subroutine signature element: e.g. '$x = $default' or '%h' */
791 sigelem:        sigscalarelem
792                         { parser->in_my = KEY_sigvar; $$ = $sigscalarelem; }
793         |       sigslurpelem
794                         { parser->in_my = KEY_sigvar; $$ = $sigslurpelem; }
795         ;
796
797 /* list of subroutine signature elements */
798 siglist:
799                 siglist[list] PERLY_COMMA
800                         { $$ = $list; }
801         |       siglist[list] PERLY_COMMA sigelem[element]
802                         {
803                           $$ = op_append_list(OP_LINESEQ, $list, $element);
804                         }
805         |       sigelem[element]  %prec PREC_LOW
806                         { $$ = $element; }
807         ;
808
809 /* () or (....) */
810 siglistornull:          %empty
811                         { $$ = NULL; }
812         |       siglist
813                         { $$ = $siglist; }
814
815 /* optional subroutine signature */
816 optsubsignature:        %empty
817                         { $$ = NULL; }
818         |       subsignature
819                         { $$ = $subsignature; }
820
821 /* Subroutine signature */
822 subsignature:   PERLY_PAREN_OPEN subsigguts PERLY_PAREN_CLOSE
823                         { $$ = $subsigguts; }
824
825 subsigguts:
826                         {
827                             ENTER;
828                             SAVEIV(parser->sig_elems);
829                             SAVEIV(parser->sig_optelems);
830                             SAVEI8(parser->sig_slurpy);
831                             parser->sig_elems    = 0;
832                             parser->sig_optelems = 0;
833                             parser->sig_slurpy   = 0;
834                             parser->in_my        = KEY_sigvar;
835                         }
836                 siglistornull
837                         {
838                             OP            *sigops = $siglistornull;
839                             struct op_argcheck_aux *aux;
840                             OP            *check;
841
842                             if (!FEATURE_SIGNATURES_IS_ENABLED)
843                                 Perl_croak(aTHX_ "Experimental "
844                                     "subroutine signatures not enabled");
845
846                             /* We shouldn't get here otherwise */
847                             Perl_ck_warner_d(aTHX_
848                                 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
849                                 "The signatures feature is experimental");
850
851                             aux = (struct op_argcheck_aux*)
852                                     PerlMemShared_malloc(
853                                         sizeof(struct op_argcheck_aux));
854                             aux->params     = parser->sig_elems;
855                             aux->opt_params = parser->sig_optelems;
856                             aux->slurpy     = parser->sig_slurpy;
857                             check = newUNOP_AUX(OP_ARGCHECK, 0, NULL,
858                                             (UNOP_AUX_item *)aux);
859                             sigops = op_prepend_elem(OP_LINESEQ, check, sigops);
860                             sigops = op_prepend_elem(OP_LINESEQ,
861                                                 newSTATEOP(0, NULL, NULL),
862                                                 sigops);
863                             /* a nextstate at the end handles context
864                              * correctly for an empty sub body */
865                             sigops = op_append_elem(OP_LINESEQ,
866                                                 sigops,
867                                                 newSTATEOP(0, NULL, NULL));
868                             /* wrap the list of arg ops in a NULL aux op.
869                               This serves two purposes. First, it makes
870                               the arg list a separate subtree from the
871                               body of the sub, and secondly the null op
872                               may in future be upgraded to an OP_SIGNATURE
873                               when implemented. For now leave it as
874                               ex-argcheck */
875                             $$ = newUNOP_AUX(OP_ARGCHECK, 0, sigops, NULL);
876                             op_null($$);
877
878                             parser->in_my = 0;
879                             /* tell the toker that attrributes can follow
880                              * this sig, but only so that the toker
881                              * can skip through any (illegal) trailing
882                              * attribute text then give a useful error
883                              * message about "attributes before sig",
884                              * rather than falling over ina mess at
885                              * unrecognised syntax.
886                              */
887                             parser->expect = XATTRBLOCK;
888                             parser->sig_seen = TRUE;
889                             LEAVE;
890                         }
891         ;
892
893 /* Optional subroutine body (for named subroutine declaration) */
894 optsubbody:     subbody { $$ = $subbody; }
895         |       PERLY_SEMICOLON { $$ = NULL; }
896         ;
897
898
899 /* Subroutine body (without signature) */
900 subbody:        remember  PERLY_BRACE_OPEN stmtseq PERLY_BRACE_CLOSE
901                         {
902                           if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
903                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
904                           $$ = block_end($remember, $stmtseq);
905                         }
906         ;
907
908
909 /* optional [ Subroutine body with optional signature ] (for named
910  * subroutine declaration) */
911 optsigsubbody:  sigsubbody { $$ = $sigsubbody; }
912         |       PERLY_SEMICOLON    { $$ = NULL; }
913
914 /* Subroutine body with optional signature */
915 sigsubbody:     remember optsubsignature PERLY_BRACE_OPEN stmtseq PERLY_BRACE_CLOSE
916                         {
917                           if (parser->copline > (line_t)$PERLY_BRACE_OPEN)
918                               parser->copline = (line_t)$PERLY_BRACE_OPEN;
919                           $$ = block_end($remember,
920                                 op_append_list(OP_LINESEQ, $optsubsignature, $stmtseq));
921                         }
922         ;
923
924
925 /* Ordinary expressions; logical combinations */
926 expr    :       expr[lhs] ANDOP expr[rhs]
927                         { $$ = newLOGOP(OP_AND, 0, $lhs, $rhs); }
928         |       expr[lhs] OROP[operator] expr[rhs]
929                         { $$ = newLOGOP($operator, 0, $lhs, $rhs); }
930         |       listexpr %prec PREC_LOW
931         ;
932
933 /* Expressions are a list of terms joined by commas */
934 listexpr:       listexpr[list] PERLY_COMMA
935                         { $$ = $list; }
936         |       listexpr[list] PERLY_COMMA term
937                         {
938                           OP* term = $term;
939                           $$ = op_append_elem(OP_LIST, $list, term);
940                         }
941         |       term %prec PREC_LOW
942         ;
943
944 /* List operators */
945 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
946                         { $$ = op_convert_list($LSTOP, OPf_STACKED,
947                                 op_prepend_elem(OP_LIST, newGVREF($LSTOP,$indirob), $listexpr) );
948                         }
949         |       FUNC PERLY_PAREN_OPEN indirob expr PERLY_PAREN_CLOSE      /* print ($fh @args */
950                         { $$ = op_convert_list($FUNC, OPf_STACKED,
951                                 op_prepend_elem(OP_LIST, newGVREF($FUNC,$indirob), $expr) );
952                         }
953         |       term ARROW method PERLY_PAREN_OPEN optexpr PERLY_PAREN_CLOSE /* $foo->bar(list) */
954                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
955                                 op_append_elem(OP_LIST,
956                                     op_prepend_elem(OP_LIST, scalar($term), $optexpr),
957                                     newMETHOP(OP_METHOD, 0, $method)));
958                         }
959         |       term ARROW method                     /* $foo->bar */
960                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
961                                 op_append_elem(OP_LIST, scalar($term),
962                                     newMETHOP(OP_METHOD, 0, $method)));
963                         }
964         |       METHOD indirob optlistexpr           /* new Class @args */
965                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
966                                 op_append_elem(OP_LIST,
967                                     op_prepend_elem(OP_LIST, $indirob, $optlistexpr),
968                                     newMETHOP(OP_METHOD, 0, $METHOD)));
969                         }
970         |       FUNCMETH indirob PERLY_PAREN_OPEN optexpr PERLY_PAREN_CLOSE    /* method $object (@args) */
971                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
972                                 op_append_elem(OP_LIST,
973                                     op_prepend_elem(OP_LIST, $indirob, $optexpr),
974                                     newMETHOP(OP_METHOD, 0, $FUNCMETH)));
975                         }
976         |       LSTOP optlistexpr                    /* print @args */
977                         { $$ = op_convert_list($LSTOP, 0, $optlistexpr); }
978         |       FUNC PERLY_PAREN_OPEN optexpr PERLY_PAREN_CLOSE                 /* print (@args) */
979                         { $$ = op_convert_list($FUNC, 0, $optexpr); }
980         |       FUNC SUBLEXSTART optexpr SUBLEXEND          /* uc($arg) from "\U..." */
981                         { $$ = op_convert_list($FUNC, 0, $optexpr); }
982         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
983                         { SvREFCNT_inc_simple_void(PL_compcv);
984                           $<opval>$ = newANONATTRSUB($startanonsub, 0, NULL, $block); }[anonattrsub]
985                     optlistexpr         %prec LSTOP  /* ... @bar */
986                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
987                                  op_append_elem(OP_LIST,
988                                    op_prepend_elem(OP_LIST, $<opval>anonattrsub, $optlistexpr), $LSTOPSUB));
989                         }
990         ;
991
992 /* Names of methods. May use $object->$methodname */
993 method  :       METHOD
994         |       scalar
995         ;
996
997 /* Some kind of subscripted expression */
998 subscripted:    gelem PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE        /* *main::{something} */
999                         /* In this and all the hash accessors, PERLY_SEMICOLON is
1000                          * provided by the tokeniser */
1001                         { $$ = newBINOP(OP_GELEM, 0, $gelem, scalar($expr)); }
1002         |       scalar[array] PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE          /* $array[$element] */
1003                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($array), scalar($expr));
1004                         }
1005         |       term[array_reference] ARROW PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE      /* somearef->[$element] */
1006                         { $$ = newBINOP(OP_AELEM, 0,
1007                                         ref(newAVREF($array_reference),OP_RV2AV),
1008                                         scalar($expr));
1009                         }
1010         |       subscripted[array_reference] PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE    /* $foo->[$bar]->[$baz] */
1011                         { $$ = newBINOP(OP_AELEM, 0,
1012                                         ref(newAVREF($array_reference),OP_RV2AV),
1013                                         scalar($expr));
1014                         }
1015         |       scalar[hash] PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE    /* $foo{bar();} */
1016                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($hash), jmaybe($expr));
1017                         }
1018         |       term[hash_reference] ARROW PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE /* somehref->{bar();} */
1019                         { $$ = newBINOP(OP_HELEM, 0,
1020                                         ref(newHVREF($hash_reference),OP_RV2HV),
1021                                         jmaybe($expr)); }
1022         |       subscripted[hash_reference] PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE /* $foo->[bar]->{baz;} */
1023                         { $$ = newBINOP(OP_HELEM, 0,
1024                                         ref(newHVREF($hash_reference),OP_RV2HV),
1025                                         jmaybe($expr)); }
1026         |       term[code_reference] ARROW PERLY_PAREN_OPEN PERLY_PAREN_CLOSE          /* $subref->() */
1027                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1028                                    newCVREF(0, scalar($code_reference)));
1029                           if (parser->expect == XBLOCK)
1030                               parser->expect = XOPERATOR;
1031                         }
1032         |       term[code_reference] ARROW PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE     /* $subref->(@args) */
1033                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1034                                    op_append_elem(OP_LIST, $expr,
1035                                        newCVREF(0, scalar($code_reference))));
1036                           if (parser->expect == XBLOCK)
1037                               parser->expect = XOPERATOR;
1038                         }
1039
1040         |       subscripted[code_reference] PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE   /* $foo->{bar}->(@args) */
1041                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1042                                    op_append_elem(OP_LIST, $expr,
1043                                                newCVREF(0, scalar($code_reference))));
1044                           if (parser->expect == XBLOCK)
1045                               parser->expect = XOPERATOR;
1046                         }
1047         |       subscripted[code_reference] PERLY_PAREN_OPEN PERLY_PAREN_CLOSE        /* $foo->{bar}->() */
1048                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1049                                    newCVREF(0, scalar($code_reference)));
1050                           if (parser->expect == XBLOCK)
1051                               parser->expect = XOPERATOR;
1052                         }
1053         |       PERLY_PAREN_OPEN expr[list] PERLY_PAREN_CLOSE PERLY_BRACKET_OPEN expr[slice] PERLY_BRACKET_CLOSE            /* list slice */
1054                         { $$ = newSLICEOP(0, $slice, $list); }
1055         |       QWLIST PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE            /* list literal slice */
1056                         { $$ = newSLICEOP(0, $expr, $QWLIST); }
1057         |       PERLY_PAREN_OPEN PERLY_PAREN_CLOSE PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE                 /* empty list slice! */
1058                         { $$ = newSLICEOP(0, $expr, NULL); }
1059     ;
1060
1061 /* Binary operators between terms */
1062 termbinop:      term[lhs] ASSIGNOP term[rhs]                     /* $x = $y, $x += $y */
1063                         { $$ = newASSIGNOP(OPf_STACKED, $lhs, $ASSIGNOP, $rhs); }
1064         |       term[lhs] POWOP term[rhs]                        /* $x ** $y */
1065                         { $$ = newBINOP($POWOP, 0, scalar($lhs), scalar($rhs)); }
1066         |       term[lhs] MULOP term[rhs]                        /* $x * $y, $x x $y */
1067                         {   if ($MULOP != OP_REPEAT)
1068                                 scalar($lhs);
1069                             $$ = newBINOP($MULOP, 0, $lhs, scalar($rhs));
1070                         }
1071         |       term[lhs] ADDOP term[rhs]                        /* $x + $y */
1072                         { $$ = newBINOP($ADDOP, 0, scalar($lhs), scalar($rhs)); }
1073         |       term[lhs] SHIFTOP term[rhs]                      /* $x >> $y, $x << $y */
1074                         { $$ = newBINOP($SHIFTOP, 0, scalar($lhs), scalar($rhs)); }
1075         |       termrelop %prec PREC_LOW               /* $x > $y, etc. */
1076                         { $$ = $termrelop; }
1077         |       termeqop %prec PREC_LOW                /* $x == $y, $x cmp $y */
1078                         { $$ = $termeqop; }
1079         |       term[lhs] BITANDOP term[rhs]                     /* $x & $y */
1080                         { $$ = newBINOP($BITANDOP, 0, scalar($lhs), scalar($rhs)); }
1081         |       term[lhs] BITOROP term[rhs]                      /* $x | $y */
1082                         { $$ = newBINOP($BITOROP, 0, scalar($lhs), scalar($rhs)); }
1083         |       term[lhs] DOTDOT term[rhs]                       /* $x..$y, $x...$y */
1084                         { $$ = newRANGE($DOTDOT, scalar($lhs), scalar($rhs)); }
1085         |       term[lhs] ANDAND term[rhs]                       /* $x && $y */
1086                         { $$ = newLOGOP(OP_AND, 0, $lhs, $rhs); }
1087         |       term[lhs] OROR term[rhs]                         /* $x || $y */
1088                         { $$ = newLOGOP(OP_OR, 0, $lhs, $rhs); }
1089         |       term[lhs] DORDOR term[rhs]                       /* $x // $y */
1090                         { $$ = newLOGOP(OP_DOR, 0, $lhs, $rhs); }
1091         |       term[lhs] MATCHOP term[rhs]                      /* $x =~ /$y/ */
1092                         { $$ = bind_match($MATCHOP, $lhs, $rhs); }
1093     ;
1094
1095 termrelop:      relopchain %prec PREC_LOW
1096                         { $$ = cmpchain_finish($relopchain); }
1097         |       term[lhs] NCRELOP term[rhs]
1098                         { $$ = newBINOP($NCRELOP, 0, scalar($lhs), scalar($rhs)); }
1099         |       termrelop NCRELOP
1100                         { yyerror("syntax error"); YYERROR; }
1101         |       termrelop CHRELOP
1102                         { yyerror("syntax error"); YYERROR; }
1103         ;
1104
1105 relopchain:     term[lhs] CHRELOP term[rhs]
1106                         { $$ = cmpchain_start($CHRELOP, $lhs, $rhs); }
1107         |       relopchain[lhs] CHRELOP term[rhs]
1108                         { $$ = cmpchain_extend($CHRELOP, $lhs, $rhs); }
1109         ;
1110
1111 termeqop:       eqopchain %prec PREC_LOW
1112                         { $$ = cmpchain_finish($eqopchain); }
1113         |       term[lhs] NCEQOP term[rhs]
1114                         { $$ = newBINOP($NCEQOP, 0, scalar($lhs), scalar($rhs)); }
1115         |       termeqop NCEQOP
1116                         { yyerror("syntax error"); YYERROR; }
1117         |       termeqop CHEQOP
1118                         { yyerror("syntax error"); YYERROR; }
1119         ;
1120
1121 eqopchain:      term[lhs] CHEQOP term[rhs]
1122                         { $$ = cmpchain_start($CHEQOP, $lhs, $rhs); }
1123         |       eqopchain[lhs] CHEQOP term[rhs]
1124                         { $$ = cmpchain_extend($CHEQOP, $lhs, $rhs); }
1125         ;
1126
1127 /* Unary operators and terms */
1128 termunop : PERLY_MINUS term %prec UMINUS                       /* -$x */
1129                         { $$ = newUNOP(OP_NEGATE, 0, scalar($term)); }
1130         |       PERLY_PLUS term %prec UMINUS                  /* +$x */
1131                         { $$ = $term; }
1132
1133         |       PERLY_EXCLAMATION_MARK term                               /* !$x */
1134                         { $$ = newUNOP(OP_NOT, 0, scalar($term)); }
1135         |       PERLY_TILDE term                               /* ~$x */
1136                         { $$ = newUNOP($PERLY_TILDE, 0, scalar($term)); }
1137         |       term POSTINC                           /* $x++ */
1138                         { $$ = newUNOP(OP_POSTINC, 0,
1139                                         op_lvalue(scalar($term), OP_POSTINC)); }
1140         |       term POSTDEC                           /* $x-- */
1141                         { $$ = newUNOP(OP_POSTDEC, 0,
1142                                         op_lvalue(scalar($term), OP_POSTDEC));}
1143         |       term POSTJOIN    /* implicit join after interpolated ->@ */
1144                         { $$ = op_convert_list(OP_JOIN, 0,
1145                                        op_append_elem(
1146                                         OP_LIST,
1147                                         newSVREF(scalar(
1148                                             newSVOP(OP_CONST,0,
1149                                                     newSVpvs("\""))
1150                                         )),
1151                                         $term
1152                                        ));
1153                         }
1154         |       PREINC term                            /* ++$x */
1155                         { $$ = newUNOP(OP_PREINC, 0,
1156                                         op_lvalue(scalar($term), OP_PREINC)); }
1157         |       PREDEC term                            /* --$x */
1158                         { $$ = newUNOP(OP_PREDEC, 0,
1159                                         op_lvalue(scalar($term), OP_PREDEC)); }
1160
1161     ;
1162
1163 /* Constructors for anonymous data */
1164 anonymous:      PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE
1165                         { $$ = newANONLIST($expr); }
1166         |       PERLY_BRACKET_OPEN PERLY_BRACKET_CLOSE
1167                         { $$ = newANONLIST(NULL);}
1168         |       HASHBRACK expr PERLY_SEMICOLON PERLY_BRACE_CLOSE        %prec PERLY_PAREN_OPEN /* { foo => "Bar" } */
1169                         { $$ = newANONHASH($expr); }
1170         |       HASHBRACK PERLY_SEMICOLON PERLY_BRACE_CLOSE     %prec PERLY_PAREN_OPEN /* { } (PERLY_SEMICOLON by tokener) */
1171                         { $$ = newANONHASH(NULL); }
1172         |       ANONSUB     startanonsub proto subattrlist subbody    %prec PERLY_PAREN_OPEN
1173                         { SvREFCNT_inc_simple_void(PL_compcv);
1174                           $$ = newANONATTRSUB($startanonsub, $proto, $subattrlist, $subbody); }
1175         |       ANON_SIGSUB startanonsub subattrlist sigsubbody %prec PERLY_PAREN_OPEN
1176                         { SvREFCNT_inc_simple_void(PL_compcv);
1177                           $$ = newANONATTRSUB($startanonsub, NULL, $subattrlist, $sigsubbody); }
1178     ;
1179
1180 /* Things called with "do" */
1181 termdo  :       DO term %prec UNIOP                     /* do $filename */
1182                         { $$ = dofile($term, $DO);}
1183         |       DO block        %prec PERLY_PAREN_OPEN               /* do { code */
1184                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($block));}
1185         ;
1186
1187 term[product]   :       termbinop
1188         |       termunop
1189         |       anonymous
1190         |       termdo
1191         |       term[condition] PERLY_QUESTION_MARK term[then] PERLY_COLON term[else]
1192                         { $$ = newCONDOP(0, $condition, $then, $else); }
1193         |       REFGEN term[operand]                          /* \$x, \@y, \%z */
1194                         { $$ = newUNOP(OP_REFGEN, 0, $operand); }
1195         |       MY REFGEN term[operand]
1196                         { $$ = newUNOP(OP_REFGEN, 0, localize($operand,1)); }
1197         |       myattrterm      %prec UNIOP
1198                         { $$ = $myattrterm; }
1199         |       LOCAL term[operand]     %prec UNIOP
1200                         { $$ = localize($operand,0); }
1201         |       PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE
1202                         { $$ = sawparens($expr); }
1203         |       QWLIST
1204                         { $$ = $QWLIST; }
1205         |       PERLY_PAREN_OPEN PERLY_PAREN_CLOSE
1206                         { $$ = sawparens(newNULLLIST()); }
1207         |       scalar  %prec PERLY_PAREN_OPEN
1208                         { $$ = $scalar; }
1209         |       star    %prec PERLY_PAREN_OPEN
1210                         { $$ = $star; }
1211         |       hsh     %prec PERLY_PAREN_OPEN
1212                         { $$ = $hsh; }
1213         |       ary     %prec PERLY_PAREN_OPEN
1214                         { $$ = $ary; }
1215         |       arylen  %prec PERLY_PAREN_OPEN                    /* $#x, $#{ something } */
1216                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($arylen, OP_AV2ARYLEN));}
1217         |       subscripted
1218                         { $$ = $subscripted; }
1219         |       sliceme PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE                     /* array slice */
1220                         { $$ = op_prepend_elem(OP_ASLICE,
1221                                 newOP(OP_PUSHMARK, 0),
1222                                     newLISTOP(OP_ASLICE, 0,
1223                                         list($expr),
1224                                         ref($sliceme, OP_ASLICE)));
1225                           if ($$ && $sliceme)
1226                               $$->op_private |=
1227                                   $sliceme->op_private & OPpSLICEWARNING;
1228                         }
1229         |       kvslice PERLY_BRACKET_OPEN expr PERLY_BRACKET_CLOSE                 /* array key/value slice */
1230                         { $$ = op_prepend_elem(OP_KVASLICE,
1231                                 newOP(OP_PUSHMARK, 0),
1232                                     newLISTOP(OP_KVASLICE, 0,
1233                                         list($expr),
1234                                         ref(oopsAV($kvslice), OP_KVASLICE)));
1235                           if ($$ && $kvslice)
1236                               $$->op_private |=
1237                                   $kvslice->op_private & OPpSLICEWARNING;
1238                         }
1239         |       sliceme PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE                 /* @hash{@keys} */
1240                         { $$ = op_prepend_elem(OP_HSLICE,
1241                                 newOP(OP_PUSHMARK, 0),
1242                                     newLISTOP(OP_HSLICE, 0,
1243                                         list($expr),
1244                                         ref(oopsHV($sliceme), OP_HSLICE)));
1245                           if ($$ && $sliceme)
1246                               $$->op_private |=
1247                                   $sliceme->op_private & OPpSLICEWARNING;
1248                         }
1249         |       kvslice PERLY_BRACE_OPEN expr PERLY_SEMICOLON PERLY_BRACE_CLOSE                 /* %hash{@keys} */
1250                         { $$ = op_prepend_elem(OP_KVHSLICE,
1251                                 newOP(OP_PUSHMARK, 0),
1252                                     newLISTOP(OP_KVHSLICE, 0,
1253                                         list($expr),
1254                                         ref($kvslice, OP_KVHSLICE)));
1255                           if ($$ && $kvslice)
1256                               $$->op_private |=
1257                                   $kvslice->op_private & OPpSLICEWARNING;
1258                         }
1259         |       THING   %prec PERLY_PAREN_OPEN
1260                         { $$ = $THING; }
1261         |       amper                                /* &foo; */
1262                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($amper)); }
1263         |       amper PERLY_PAREN_OPEN PERLY_PAREN_CLOSE                 /* &foo() or foo() */
1264                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($amper));
1265                         }
1266         |       amper PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE          /* &foo(@args) or foo(@args) */
1267                         {
1268                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1269                                 op_append_elem(OP_LIST, $expr, scalar($amper)));
1270                         }
1271         |       NOAMP subname optlistexpr       /* foo @args (no parens) */
1272                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1273                             op_append_elem(OP_LIST, $optlistexpr, scalar($subname)));
1274                         }
1275         |       term[operand] ARROW PERLY_DOLLAR PERLY_STAR
1276                         { $$ = newSVREF($operand); }
1277         |       term[operand] ARROW PERLY_SNAIL PERLY_STAR
1278                         { $$ = newAVREF($operand); }
1279         |       term[operand] ARROW PERLY_PERCENT_SIGN PERLY_STAR
1280                         { $$ = newHVREF($operand); }
1281         |       term[operand] ARROW PERLY_AMPERSAND PERLY_STAR
1282                         { $$ = newUNOP(OP_ENTERSUB, 0,
1283                                        scalar(newCVREF($PERLY_AMPERSAND,$operand))); }
1284         |       term[operand] ARROW PERLY_STAR PERLY_STAR       %prec PERLY_PAREN_OPEN
1285                         { $$ = newGVREF(0,$operand); }
1286         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
1287                         { $$ = newOP($LOOPEX, OPf_SPECIAL);
1288                             PL_hints |= HINT_BLOCK_SCOPE; }
1289         |       LOOPEX term[operand]
1290                         { $$ = newLOOPEX($LOOPEX,$operand); }
1291         |       NOTOP listexpr                       /* not $foo */
1292                         { $$ = newUNOP(OP_NOT, 0, scalar($listexpr)); }
1293         |       UNIOP                                /* Unary op, $_ implied */
1294                         { $$ = newOP($UNIOP, 0); }
1295         |       UNIOP block                          /* eval { foo }* */
1296                         { $$ = newUNOP($UNIOP, 0, $block); }
1297         |       UNIOP term[operand]                           /* Unary op */
1298                         { $$ = newUNOP($UNIOP, 0, $operand); }
1299         |       REQUIRE                              /* require, $_ implied */
1300                         { $$ = newOP(OP_REQUIRE, $REQUIRE ? OPf_SPECIAL : 0); }
1301         |       REQUIRE term[operand]                         /* require Foo */
1302                         { $$ = newUNOP(OP_REQUIRE, $REQUIRE ? OPf_SPECIAL : 0, $operand); }
1303         |       UNIOPSUB
1304                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($UNIOPSUB)); }
1305         |       UNIOPSUB term[operand]                        /* Sub treated as unop */
1306                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1307                             op_append_elem(OP_LIST, $operand, scalar($UNIOPSUB))); }
1308         |       FUNC0                                /* Nullary operator */
1309                         { $$ = newOP($FUNC0, 0); }
1310         |       FUNC0 PERLY_PAREN_OPEN PERLY_PAREN_CLOSE
1311                         { $$ = newOP($FUNC0, 0);}
1312         |       FUNC0OP       /* Same as above, but op created in toke.c */
1313                         { $$ = $FUNC0OP; }
1314         |       FUNC0OP PERLY_PAREN_OPEN PERLY_PAREN_CLOSE
1315                         { $$ = $FUNC0OP; }
1316         |       FUNC0SUB                             /* Sub treated as nullop */
1317                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($FUNC0SUB)); }
1318         |       FUNC1 PERLY_PAREN_OPEN PERLY_PAREN_CLOSE                        /* not () */
1319                         { $$ = ($FUNC1 == OP_NOT)
1320                           ? newUNOP($FUNC1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
1321                           : newOP($FUNC1, OPf_SPECIAL); }
1322         |       FUNC1 PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE                   /* not($foo) */
1323                         { $$ = newUNOP($FUNC1, 0, $expr); }
1324         |       PMFUNC /* m//, s///, qr//, tr/// */
1325                         {
1326                             if (   $PMFUNC->op_type != OP_TRANS
1327                                 && $PMFUNC->op_type != OP_TRANSR
1328                                 && (((PMOP*)$PMFUNC)->op_pmflags & PMf_HAS_CV))
1329                             {
1330                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1331                                 SAVEFREESV(PL_compcv);
1332                             } else
1333                                 $<ival>$ = 0;
1334                         }
1335                     SUBLEXSTART listexpr optrepl SUBLEXEND
1336                         { $$ = pmruntime($PMFUNC, $listexpr, $optrepl, 1, $<ival>2); }
1337         |       BAREWORD
1338         |       listop
1339         |       PLUGEXPR
1340         ;
1341
1342 /* "my" declarations, with optional attributes */
1343 myattrterm:     MY myterm myattrlist
1344                         { $$ = my_attrs($myterm,$myattrlist); }
1345         |       MY myterm
1346                         { $$ = localize($myterm,1); }
1347         |       MY REFGEN myterm myattrlist
1348                         { $$ = newUNOP(OP_REFGEN, 0, my_attrs($myterm,$myattrlist)); }
1349         ;
1350
1351 /* Things that can be "my"'d */
1352 myterm  :       PERLY_PAREN_OPEN expr PERLY_PAREN_CLOSE
1353                         { $$ = sawparens($expr); }
1354         |       PERLY_PAREN_OPEN PERLY_PAREN_CLOSE
1355                         { $$ = sawparens(newNULLLIST()); }
1356
1357         |       scalar  %prec PERLY_PAREN_OPEN
1358                         { $$ = $scalar; }
1359         |       hsh     %prec PERLY_PAREN_OPEN
1360                         { $$ = $hsh; }
1361         |       ary     %prec PERLY_PAREN_OPEN
1362                         { $$ = $ary; }
1363         ;
1364
1365 /* Basic list expressions */
1366 optlistexpr:    %empty %prec PREC_LOW
1367                         { $$ = NULL; }
1368         |       listexpr    %prec PREC_LOW
1369                         { $$ = $listexpr; }
1370         ;
1371
1372 optexpr:        %empty
1373                         { $$ = NULL; }
1374         |       expr
1375                         { $$ = $expr; }
1376         ;
1377
1378 optrepl:        %empty
1379                         { $$ = NULL; }
1380         |       PERLY_SLASH expr
1381                         { $$ = $expr; }
1382         ;
1383
1384 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1385    lexical */
1386 my_scalar:      scalar
1387                         { parser->in_my = 0; $$ = my($scalar); }
1388         ;
1389
1390 my_var  :       scalar
1391         |       ary
1392         |       hsh
1393         ;
1394
1395 refgen_topic:   my_var
1396         |       amper
1397         ;
1398
1399 my_refgen:      MY REFGEN
1400         |       REFGEN MY
1401         ;
1402
1403 amper   :       PERLY_AMPERSAND indirob
1404                         { $$ = newCVREF($PERLY_AMPERSAND,$indirob); }
1405         ;
1406
1407 scalar  :       PERLY_DOLLAR indirob
1408                         { $$ = newSVREF($indirob); }
1409         ;
1410
1411 ary     :       PERLY_SNAIL indirob
1412                         { $$ = newAVREF($indirob);
1413                           if ($$) $$->op_private |= $PERLY_SNAIL;
1414                         }
1415         ;
1416
1417 hsh     :       PERLY_PERCENT_SIGN indirob
1418                         { $$ = newHVREF($indirob);
1419                           if ($$) $$->op_private |= $PERLY_PERCENT_SIGN;
1420                         }
1421         ;
1422
1423 arylen  :       DOLSHARP indirob
1424                         { $$ = newAVREF($indirob); }
1425         |       term ARROW DOLSHARP PERLY_STAR
1426                         { $$ = newAVREF($term); }
1427         ;
1428
1429 star    :       PERLY_STAR indirob
1430                         { $$ = newGVREF(0,$indirob); }
1431         ;
1432
1433 sliceme :       ary
1434         |       term ARROW PERLY_SNAIL
1435                         { $$ = newAVREF($term); }
1436         ;
1437
1438 kvslice :       hsh
1439         |       term ARROW PERLY_PERCENT_SIGN
1440                         { $$ = newHVREF($term); }
1441         ;
1442
1443 gelem   :       star
1444         |       term ARROW PERLY_STAR
1445                         { $$ = newGVREF(0,$term); }
1446         ;
1447
1448 /* Indirect objects */
1449 indirob :       BAREWORD
1450                         { $$ = scalar($BAREWORD); }
1451         |       scalar %prec PREC_LOW
1452                         { $$ = scalar($scalar); }
1453         |       block
1454                         { $$ = op_scope($block); }
1455
1456         |       PRIVATEREF
1457                         { $$ = $PRIVATEREF; }
1458         ;