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