This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl5216delta correction - IO::Socket has connected(), not isconnected()
[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 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 %pure_parser
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
47
48 %token <ival> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';' '=' '.'
49
50 %token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
51 %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
52 %token <opval> PLUGEXPR PLUGSTMT
53 %token <pval> LABEL
54 %token <ival> FORMAT SUB ANONSUB PACKAGE USE
55 %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
56 %token <ival> GIVEN WHEN DEFAULT
57 %token <ival> LOOPEX DOTDOT YADAYADA
58 %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP
59 %token <ival> RELOP EQOP MULOP ADDOP
60 %token <ival> DOLSHARP DO HASHBRACK NOAMP
61 %token <ival> LOCAL MY REQUIRE
62 %token <ival> COLONATTR FORMLBRACK FORMRBRACK
63
64 %type <ival> grammar remember mremember
65 %type <ival>  startsub startanonsub startformsub
66
67 %type <ival> mintro
68
69 %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
70 %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
71 %type <opval> sliceme kvslice gelem
72 %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
73 %type <opval> optlistexpr optexpr optrepl indirob listop method
74 %type <opval> formname subname proto optsubbody cont my_scalar my_var
75 %type <opval> refgen_topic formblock
76 %type <opval> subattrlist myattrlist myattrterm myterm
77 %type <opval> realsubbody subsignature termbinop termunop anonymous termdo
78 %type <opval> formstmtseq formline formarg
79
80 %nonassoc <ival> PREC_LOW
81 %nonassoc LOOPEX
82
83 %left <ival> OROP DOROP
84 %left <ival> ANDOP
85 %right <ival> NOTOP
86 %nonassoc LSTOP LSTOPSUB
87 %left <ival> ','
88 %right <ival> ASSIGNOP
89 %right <ival> '?' ':'
90 %nonassoc DOTDOT YADAYADA
91 %left <ival> OROR DORDOR
92 %left <ival> ANDAND
93 %left <ival> BITOROP
94 %left <ival> BITANDOP
95 %nonassoc EQOP
96 %nonassoc RELOP
97 %nonassoc UNIOP UNIOPSUB
98 %nonassoc REQUIRE
99 %left <ival> SHIFTOP
100 %left ADDOP
101 %left MULOP
102 %left <ival> MATCHOP
103 %right <ival> '!' '~' UMINUS REFGEN
104 %right <ival> POWOP
105 %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN
106 %left <ival> ARROW
107 %nonassoc <ival> ')'
108 %left <ival> '('
109 %left '[' '{'
110
111 %% /* RULES */
112
113 /* Top-level choice of what kind of thing yyparse was called to parse */
114 grammar :       GRAMPROG
115                         {
116                           parser->expect = XSTATE;
117                         }
118                 remember stmtseq
119                         {
120                           newPROG(block_end($3,$4));
121                           PL_compiling.cop_seq = 0;
122                           $$ = 0;
123                         }
124         |       GRAMEXPR
125                         {
126                           parser->expect = XTERM;
127                         }
128                 optexpr
129                         {
130                           PL_eval_root = $3;
131                           $$ = 0;
132                         }
133         |       GRAMBLOCK
134                         {
135                           parser->expect = XBLOCK;
136                         }
137                 block
138                         {
139                           PL_pad_reset_pending = TRUE;
140                           PL_eval_root = $3;
141                           $$ = 0;
142                           yyunlex();
143                           parser->yychar = YYEOF;
144                         }
145         |       GRAMBARESTMT
146                         {
147                           parser->expect = XSTATE;
148                         }
149                 barestmt
150                         {
151                           PL_pad_reset_pending = TRUE;
152                           PL_eval_root = $3;
153                           $$ = 0;
154                           yyunlex();
155                           parser->yychar = YYEOF;
156                         }
157         |       GRAMFULLSTMT
158                         {
159                           parser->expect = XSTATE;
160                         }
161                 fullstmt
162                         {
163                           PL_pad_reset_pending = TRUE;
164                           PL_eval_root = $3;
165                           $$ = 0;
166                           yyunlex();
167                           parser->yychar = YYEOF;
168                         }
169         |       GRAMSTMTSEQ
170                         {
171                           parser->expect = XSTATE;
172                         }
173                 stmtseq
174                         {
175                           PL_eval_root = $3;
176                           $$ = 0;
177                         }
178         ;
179
180 /* An ordinary block */
181 block   :       '{' remember stmtseq '}'
182                         { if (parser->copline > (line_t)$1)
183                               parser->copline = (line_t)$1;
184                           $$ = block_end($2, $3);
185                         }
186         ;
187
188 /* format body */
189 formblock:      '=' remember ';' FORMRBRACK formstmtseq ';' '.'
190                         { if (parser->copline > (line_t)$1)
191                               parser->copline = (line_t)$1;
192                           $$ = block_end($2, $5);
193                         }
194         ;
195
196 remember:       /* NULL */      /* start a full lexical scope */
197                         { $$ = block_start(TRUE);
198                           parser->parsed_sub = 0; }
199         ;
200
201 mblock  :       '{' mremember stmtseq '}'
202                         { if (parser->copline > (line_t)$1)
203                               parser->copline = (line_t)$1;
204                           $$ = block_end($2, $3);
205                         }
206         ;
207
208 mremember:      /* NULL */      /* start a partial lexical scope */
209                         { $$ = block_start(FALSE);
210                           parser->parsed_sub = 0; }
211         ;
212
213 /* A sequence of statements in the program */
214 stmtseq :       /* NULL */
215                         { $$ = (OP*)NULL; }
216         |       stmtseq fullstmt
217                         {   $$ = op_append_list(OP_LINESEQ, $1, $2);
218                             PL_pad_reset_pending = TRUE;
219                             if ($1 && $2)
220                                 PL_hints |= HINT_BLOCK_SCOPE;
221                         }
222         ;
223
224 /* A sequence of format lines */
225 formstmtseq:    /* NULL */
226                         { $$ = (OP*)NULL; }
227         |       formstmtseq formline
228                         {   $$ = op_append_list(OP_LINESEQ, $1, $2);
229                             PL_pad_reset_pending = TRUE;
230                             if ($1 && $2)
231                                 PL_hints |= HINT_BLOCK_SCOPE;
232                         }
233         ;
234
235 /* A statement in the program, including optional labels */
236 fullstmt:       barestmt
237                         {
238                           $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL;
239                         }
240         |       labfullstmt
241                         { $$ = $1; }
242         ;
243
244 labfullstmt:    LABEL barestmt
245                         {
246                           $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
247                         }
248         |       LABEL labfullstmt
249                         {
250                           $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2);
251                         }
252         ;
253
254 /* A bare statement, lacking label and other aspects of state op */
255 barestmt:       PLUGSTMT
256                         { $$ = $1; }
257         |       FORMAT startformsub formname formblock
258                         {
259                           CV *fmtcv = PL_compcv;
260                           newFORM($2, $3, $4);
261                           $$ = (OP*)NULL;
262                           if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
263                               pad_add_weakref(fmtcv);
264                           }
265                           parser->parsed_sub = 1;
266                         }
267         |       SUB subname startsub
268                         {
269                           if ($2->op_type == OP_CONST) {
270                             const char *const name =
271                                 SvPV_nolen_const(((SVOP*)$2)->op_sv);
272                             if (strEQ(name, "BEGIN") || strEQ(name, "END")
273                               || strEQ(name, "INIT") || strEQ(name, "CHECK")
274                               || strEQ(name, "UNITCHECK"))
275                               CvSPECIAL_on(PL_compcv);
276                           }
277                           else
278                           /* State subs inside anonymous subs need to be
279                              clonable themselves. */
280                           if (CvANON(CvOUTSIDE(PL_compcv))
281                            || CvCLONE(CvOUTSIDE(PL_compcv))
282                            || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST(
283                                                 CvOUTSIDE(PL_compcv)
284                                              ))[$2->op_targ]))
285                               CvCLONE_on(PL_compcv);
286                           parser->in_my = 0;
287                           parser->in_my_stash = NULL;
288                         }
289                 proto subattrlist optsubbody
290                         {
291                           SvREFCNT_inc_simple_void(PL_compcv);
292                           $2->op_type == OP_CONST
293                               ? newATTRSUB($3, $2, $5, $6, $7)
294                               : newMYSUB($3, $2, $5, $6, $7)
295                           ;
296                           $$ = (OP*)NULL;
297                           intro_my();
298                           parser->parsed_sub = 1;
299                         }
300         |       PACKAGE WORD WORD ';'
301                         {
302                           package($3);
303                           if ($2)
304                               package_version($2);
305                           $$ = (OP*)NULL;
306                         }
307         |       USE startsub
308                         { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
309                 WORD WORD optlistexpr ';'
310                         {
311                           SvREFCNT_inc_simple_void(PL_compcv);
312                           utilize($1, $2, $4, $5, $6);
313                           parser->parsed_sub = 1;
314                           $$ = (OP*)NULL;
315                         }
316         |       IF '(' remember mexpr ')' mblock else
317                         {
318                           $$ = block_end($3,
319                               newCONDOP(0, $4, op_scope($6), $7));
320                           parser->copline = (line_t)$1;
321                         }
322         |       UNLESS '(' remember miexpr ')' mblock else
323                         {
324                           $$ = block_end($3,
325                               newCONDOP(0, $4, op_scope($6), $7));
326                           parser->copline = (line_t)$1;
327                         }
328         |       GIVEN '(' remember mexpr ')' mblock
329                         {
330                           const PADOFFSET offset = pad_findmy_pvs("$_", 0);
331                           $$ = block_end($3,
332                                   newGIVENOP($4, op_scope($6),
333                                     offset == NOT_IN_PAD
334                                     || PAD_COMPNAME_FLAGS_isOUR(offset)
335                                       ? 0
336                                       : offset));
337                           parser->copline = (line_t)$1;
338                         }
339         |       WHEN '(' remember mexpr ')' mblock
340                         { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
341         |       DEFAULT block
342                         { $$ = newWHENOP(0, op_scope($2)); }
343         |       WHILE '(' remember texpr ')' mintro mblock cont
344                         {
345                           $$ = block_end($3,
346                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
347                                       $4, $7, $8, $6));
348                           parser->copline = (line_t)$1;
349                         }
350         |       UNTIL '(' remember iexpr ')' mintro mblock cont
351                         {
352                           $$ = block_end($3,
353                                   newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
354                                       $4, $7, $8, $6));
355                           parser->copline = (line_t)$1;
356                         }
357         |       FOR '(' remember mnexpr ';'
358                         { parser->expect = XTERM; }
359                 texpr ';'
360                         { parser->expect = XTERM; }
361                 mintro mnexpr ')'
362                 mblock
363                         {
364                           OP *initop = $4;
365                           OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
366                                       scalar($7), $13, $11, $10);
367                           if (initop) {
368                               forop = op_prepend_elem(OP_LINESEQ, initop,
369                                   op_append_elem(OP_LINESEQ,
370                                       newOP(OP_UNSTACK, OPf_SPECIAL),
371                                       forop));
372                           }
373                           PL_hints |= HINT_BLOCK_SCOPE;
374                           $$ = block_end($3, forop);
375                           parser->copline = (line_t)$1;
376                         }
377         |       FOR MY remember my_scalar '(' mexpr ')' mblock cont
378                         {
379                           $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
380                           parser->copline = (line_t)$1;
381                         }
382         |       FOR scalar '(' remember mexpr ')' mblock cont
383                         {
384                           $$ = block_end($4, newFOROP(0,
385                                       op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
386                           parser->copline = (line_t)$1;
387                         }
388         |       FOR REFGEN MY remember my_var
389                         { parser->in_my = 0; $<opval>$ = my($5); }
390                 '(' mexpr ')' mblock cont
391                         {
392                           $$ = block_end(
393                                 $4,
394                                 newFOROP(0,
395                                          op_lvalue(
396                                             newUNOP(OP_REFGEN, 0,
397                                                     $<opval>6),
398                                             OP_ENTERLOOP),
399                                          $8, $10, $11)
400                           );
401                           parser->copline = (line_t)$1;
402                         }
403         |       FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont
404                         {
405                           $$ = block_end($5, newFOROP(
406                                 0, op_lvalue(newUNOP(OP_REFGEN, 0,
407                                                      $3),
408                                              OP_ENTERLOOP), $6, $8, $9));
409                           parser->copline = (line_t)$1;
410                         }
411         |       FOR '(' remember mexpr ')' mblock cont
412                         {
413                           $$ = block_end($3,
414                                   newFOROP(0, (OP*)NULL, $4, $6, $7));
415                           parser->copline = (line_t)$1;
416                         }
417         |       block cont
418                         {
419                           /* a block is a loop that happens once */
420                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
421                                   (OP*)NULL, $1, $2, 0);
422                         }
423         |       PACKAGE WORD WORD '{' remember
424                         {
425                           package($3);
426                           if ($2) {
427                               package_version($2);
428                           }
429                         }
430                 stmtseq '}'
431                         {
432                           /* a block is a loop that happens once */
433                           $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
434                                   (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
435                           if (parser->copline > (line_t)$4)
436                               parser->copline = (line_t)$4;
437                         }
438         |       sideff ';'
439                         {
440                           $$ = $1;
441                         }
442         |       ';'
443                         {
444                           $$ = (OP*)NULL;
445                           parser->copline = NOLINE;
446                         }
447         ;
448
449 /* Format line */
450 formline:       THING formarg
451                         { OP *list;
452                           if ($2) {
453                               OP *term = $2;
454                               list = op_append_elem(OP_LIST, $1, term);
455                           }
456                           else {
457                               list = $1;
458                           }
459                           if (parser->copline == NOLINE)
460                                parser->copline = CopLINE(PL_curcop)-1;
461                           else parser->copline--;
462                           $$ = newSTATEOP(0, NULL,
463                                           op_convert_list(OP_FORMLINE, 0, list));
464                         }
465         ;
466
467 formarg :       /* NULL */
468                         { $$ = NULL; }
469         |       FORMLBRACK stmtseq FORMRBRACK
470                         { $$ = op_unscope($2); }
471         ;
472
473 /* An expression which may have a side-effect */
474 sideff  :       error
475                         { $$ = (OP*)NULL; }
476         |       expr
477                         { $$ = $1; }
478         |       expr IF expr
479                         { $$ = newLOGOP(OP_AND, 0, $3, $1); }
480         |       expr UNLESS expr
481                         { $$ = newLOGOP(OP_OR, 0, $3, $1); }
482         |       expr WHILE expr
483                         { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); }
484         |       expr UNTIL iexpr
485                         { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); }
486         |       expr FOR expr
487                         { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
488                           parser->copline = (line_t)$2; }
489         |       expr WHEN expr
490                         { $$ = newWHENOP($3, op_scope($1)); }
491         ;
492
493 /* else and elsif blocks */
494 else    :       /* NULL */
495                         { $$ = (OP*)NULL; }
496         |       ELSE mblock
497                         {
498                           ($2)->op_flags |= OPf_PARENS;
499                           $$ = op_scope($2);
500                         }
501         |       ELSIF '(' mexpr ')' mblock else
502                         { parser->copline = (line_t)$1;
503                             $$ = newCONDOP(0,
504                                 newSTATEOP(OPf_SPECIAL,NULL,$3),
505                                 op_scope($5), $6);
506                           PL_hints |= HINT_BLOCK_SCOPE;
507                         }
508         ;
509
510 /* Continue blocks */
511 cont    :       /* NULL */
512                         { $$ = (OP*)NULL; }
513         |       CONTINUE block
514                         { $$ = op_scope($2); }
515         ;
516
517 /* determine whether there are any new my declarations */
518 mintro  :       /* NULL */
519                         { $$ = (PL_min_intro_pending &&
520                             PL_max_intro_pending >=  PL_min_intro_pending);
521                           intro_my(); }
522
523 /* Normal expression */
524 nexpr   :       /* NULL */
525                         { $$ = (OP*)NULL; }
526         |       sideff
527         ;
528
529 /* Boolean expression */
530 texpr   :       /* NULL means true */
531                         { YYSTYPE tmplval;
532                           (void)scan_num("1", &tmplval);
533                           $$ = tmplval.opval; }
534         |       expr
535         ;
536
537 /* Inverted boolean expression */
538 iexpr   :       expr
539                         { $$ = invert(scalar($1)); }
540         ;
541
542 /* Expression with its own lexical scope */
543 mexpr   :       expr
544                         { $$ = $1; intro_my(); }
545         ;
546
547 mnexpr  :       nexpr
548                         { $$ = $1; intro_my(); }
549         ;
550
551 miexpr  :       iexpr
552                         { $$ = $1; intro_my(); }
553         ;
554
555 formname:       WORD            { $$ = $1; }
556         |       /* NULL */      { $$ = (OP*)NULL; }
557         ;
558
559 startsub:       /* NULL */      /* start a regular subroutine scope */
560                         { $$ = start_subparse(FALSE, 0);
561                             SAVEFREESV(PL_compcv); }
562
563         ;
564
565 startanonsub:   /* NULL */      /* start an anonymous subroutine scope */
566                         { $$ = start_subparse(FALSE, CVf_ANON);
567                             SAVEFREESV(PL_compcv); }
568         ;
569
570 startformsub:   /* NULL */      /* start a format subroutine scope */
571                         { $$ = start_subparse(TRUE, 0);
572                             SAVEFREESV(PL_compcv); }
573         ;
574
575 /* Name of a subroutine - must be a bareword, could be special */
576 subname :       WORD
577         |       PRIVATEREF
578         ;
579
580 /* Subroutine prototype */
581 proto   :       /* NULL */
582                         { $$ = (OP*)NULL; }
583         |       THING
584         ;
585
586 /* Optional list of subroutine attributes */
587 subattrlist:    /* NULL */
588                         { $$ = (OP*)NULL; }
589         |       COLONATTR THING
590                         { $$ = $2; }
591         |       COLONATTR
592                         { $$ = (OP*)NULL; }
593         ;
594
595 /* List of attributes for a "my" variable declaration */
596 myattrlist:     COLONATTR THING
597                         { $$ = $2; }
598         |       COLONATTR
599                         { $$ = (OP*)NULL; }
600         ;
601
602 /* Optional subroutine signature */
603 subsignature:   /* NULL */ { $$ = (OP*)NULL; }
604         |       '('
605                         {
606                           if (!FEATURE_SIGNATURES_IS_ENABLED)
607                             Perl_croak(aTHX_ "Experimental "
608                                 "subroutine signatures not enabled");
609                           Perl_ck_warner_d(aTHX_
610                                 packWARN(WARN_EXPERIMENTAL__SIGNATURES),
611                                 "The signatures feature is experimental");
612                           $<opval>$ = parse_subsignature();
613                         }
614                 ')'
615                         {
616                           $$ = op_append_list(OP_LINESEQ, $<opval>2,
617                                 newSTATEOP(0, NULL, sawparens(newNULLLIST())));
618                           parser->expect = XBLOCK;
619                         }
620         ;
621
622 /* Subroutine body - block with optional signature */
623 realsubbody:    remember subsignature '{' stmtseq '}'
624                         {
625                           if (parser->copline > (line_t)$3)
626                               parser->copline = (line_t)$3;
627                           $$ = block_end($1,
628                                 op_append_list(OP_LINESEQ, $2, $4));
629                         }
630         ;
631
632 /* Optional subroutine body, for named subroutine declaration */
633 optsubbody:     realsubbody { $$ = $1; }
634         |       ';'     { $$ = (OP*)NULL; }
635         ;
636
637 /* Ordinary expressions; logical combinations */
638 expr    :       expr ANDOP expr
639                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
640         |       expr OROP expr
641                         { $$ = newLOGOP($2, 0, $1, $3); }
642         |       expr DOROP expr
643                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
644         |       listexpr %prec PREC_LOW
645         ;
646
647 /* Expressions are a list of terms joined by commas */
648 listexpr:       listexpr ','
649                         { $$ = $1; }
650         |       listexpr ',' term
651                         { 
652                           OP* term = $3;
653                           $$ = op_append_elem(OP_LIST, $1, term);
654                         }
655         |       term %prec PREC_LOW
656         ;
657
658 /* List operators */
659 listop  :       LSTOP indirob listexpr /* map {...} @args or print $fh @args */
660                         { $$ = op_convert_list($1, OPf_STACKED,
661                                 op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) );
662                         }
663         |       FUNC '(' indirob expr ')'      /* print ($fh @args */
664                         { $$ = op_convert_list($1, OPf_STACKED,
665                                 op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) );
666                         }
667         |       term ARROW method '(' optexpr ')' /* $foo->bar(list) */
668                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
669                                 op_append_elem(OP_LIST,
670                                     op_prepend_elem(OP_LIST, scalar($1), $5),
671                                     newMETHOP(OP_METHOD, 0, $3)));
672                         }
673         |       term ARROW method                     /* $foo->bar */
674                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
675                                 op_append_elem(OP_LIST, scalar($1),
676                                     newMETHOP(OP_METHOD, 0, $3)));
677                         }
678         |       METHOD indirob optlistexpr           /* new Class @args */
679                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
680                                 op_append_elem(OP_LIST,
681                                     op_prepend_elem(OP_LIST, $2, $3),
682                                     newMETHOP(OP_METHOD, 0, $1)));
683                         }
684         |       FUNCMETH indirob '(' optexpr ')'    /* method $object (@args) */
685                         { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED,
686                                 op_append_elem(OP_LIST,
687                                     op_prepend_elem(OP_LIST, $2, $4),
688                                     newMETHOP(OP_METHOD, 0, $1)));
689                         }
690         |       LSTOP optlistexpr                    /* print @args */
691                         { $$ = op_convert_list($1, 0, $2); }
692         |       FUNC '(' optexpr ')'                 /* print (@args) */
693                         { $$ = op_convert_list($1, 0, $3); }
694         |       LSTOPSUB startanonsub block /* sub f(&@);   f { foo } ... */
695                         { SvREFCNT_inc_simple_void(PL_compcv);
696                           $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
697                     optlistexpr         %prec LSTOP  /* ... @bar */
698                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
699                                  op_append_elem(OP_LIST,
700                                    op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
701                         }
702         ;
703
704 /* Names of methods. May use $object->$methodname */
705 method  :       METHOD
706         |       scalar
707         ;
708
709 /* Some kind of subscripted expression */
710 subscripted:    gelem '{' expr ';' '}'        /* *main::{something} */
711                         /* In this and all the hash accessors, ';' is
712                          * provided by the tokeniser */
713                         { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); }
714         |       scalar '[' expr ']'          /* $array[$element] */
715                         { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
716                         }
717         |       term ARROW '[' expr ']'      /* somearef->[$element] */
718                         { $$ = newBINOP(OP_AELEM, 0,
719                                         ref(newAVREF($1),OP_RV2AV),
720                                         scalar($4));
721                         }
722         |       subscripted '[' expr ']'    /* $foo->[$bar]->[$baz] */
723                         { $$ = newBINOP(OP_AELEM, 0,
724                                         ref(newAVREF($1),OP_RV2AV),
725                                         scalar($3));
726                         }
727         |       scalar '{' expr ';' '}'    /* $foo{bar();} */
728                         { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
729                         }
730         |       term ARROW '{' expr ';' '}' /* somehref->{bar();} */
731                         { $$ = newBINOP(OP_HELEM, 0,
732                                         ref(newHVREF($1),OP_RV2HV),
733                                         jmaybe($4)); }
734         |       subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
735                         { $$ = newBINOP(OP_HELEM, 0,
736                                         ref(newHVREF($1),OP_RV2HV),
737                                         jmaybe($3)); }
738         |       term ARROW '(' ')'          /* $subref->() */
739                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
740                                    newCVREF(0, scalar($1))); }
741         |       term ARROW '(' expr ')'     /* $subref->(@args) */
742                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
743                                    op_append_elem(OP_LIST, $4,
744                                        newCVREF(0, scalar($1)))); }
745
746         |       subscripted '(' expr ')'   /* $foo->{bar}->(@args) */
747                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
748                                    op_append_elem(OP_LIST, $3,
749                                                newCVREF(0, scalar($1)))); }
750         |       subscripted '(' ')'        /* $foo->{bar}->() */
751                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
752                                    newCVREF(0, scalar($1))); }
753         |       '(' expr ')' '[' expr ']'            /* list slice */
754                         { $$ = newSLICEOP(0, $5, $2); }
755         |       QWLIST '[' expr ']'            /* list literal slice */
756                         { $$ = newSLICEOP(0, $3, $1); }
757         |       '(' ')' '[' expr ']'                 /* empty list slice! */
758                         { $$ = newSLICEOP(0, $4, (OP*)NULL); }
759     ;
760
761 /* Binary operators between terms */
762 termbinop:      term ASSIGNOP term                     /* $x = $y */
763                         { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); }
764         |       term POWOP term                        /* $x ** $y */
765                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
766         |       term MULOP term                        /* $x * $y, $x x $y */
767                         {   if ($2 != OP_REPEAT)
768                                 scalar($1);
769                             $$ = newBINOP($2, 0, $1, scalar($3));
770                         }
771         |       term ADDOP term                        /* $x + $y */
772                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
773         |       term SHIFTOP term                      /* $x >> $y, $x << $y */
774                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
775         |       term RELOP term                        /* $x > $y, etc. */
776                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
777         |       term EQOP term                         /* $x == $y, $x eq $y */
778                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
779         |       term BITANDOP term                     /* $x & $y */
780                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
781         |       term BITOROP term                      /* $x | $y */
782                         { $$ = newBINOP($2, 0, scalar($1), scalar($3)); }
783         |       term DOTDOT term                       /* $x..$y, $x...$y */
784                         { $$ = newRANGE($2, scalar($1), scalar($3)); }
785         |       term ANDAND term                       /* $x && $y */
786                         { $$ = newLOGOP(OP_AND, 0, $1, $3); }
787         |       term OROR term                         /* $x || $y */
788                         { $$ = newLOGOP(OP_OR, 0, $1, $3); }
789         |       term DORDOR term                       /* $x // $y */
790                         { $$ = newLOGOP(OP_DOR, 0, $1, $3); }
791         |       term MATCHOP term                      /* $x =~ /$y/ */
792                         { $$ = bind_match($2, $1, $3); }
793     ;
794
795 /* Unary operators and terms */
796 termunop : '-' term %prec UMINUS                       /* -$x */
797                         { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); }
798         |       '+' term %prec UMINUS                  /* +$x */
799                         { $$ = $2; }
800
801         |       '!' term                               /* !$x */
802                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
803         |       '~' term                               /* ~$x */
804                         { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2)); }
805         |       term POSTINC                           /* $x++ */
806                         { $$ = newUNOP(OP_POSTINC, 0,
807                                         op_lvalue(scalar($1), OP_POSTINC)); }
808         |       term POSTDEC                           /* $x-- */
809                         { $$ = newUNOP(OP_POSTDEC, 0,
810                                         op_lvalue(scalar($1), OP_POSTDEC));}
811         |       term POSTJOIN    /* implicit join after interpolated ->@ */
812                         { $$ = op_convert_list(OP_JOIN, 0,
813                                        op_append_elem(
814                                         OP_LIST,
815                                         newSVREF(scalar(
816                                             newSVOP(OP_CONST,0,
817                                                     newSVpvs("\""))
818                                         )),
819                                         $1
820                                        ));
821                         }
822         |       PREINC term                            /* ++$x */
823                         { $$ = newUNOP(OP_PREINC, 0,
824                                         op_lvalue(scalar($2), OP_PREINC)); }
825         |       PREDEC term                            /* --$x */
826                         { $$ = newUNOP(OP_PREDEC, 0,
827                                         op_lvalue(scalar($2), OP_PREDEC)); }
828
829     ;
830
831 /* Constructors for anonymous data */
832 anonymous:      '[' expr ']'
833                         { $$ = newANONLIST($2); }
834         |       '[' ']'
835                         { $$ = newANONLIST((OP*)NULL);}
836         |       HASHBRACK expr ';' '}'  %prec '(' /* { foo => "Bar" } */
837                         { $$ = newANONHASH($2); }
838         |       HASHBRACK ';' '}'       %prec '(' /* { } (';' by tokener) */
839                         { $$ = newANONHASH((OP*)NULL); }
840         |       ANONSUB startanonsub proto subattrlist realsubbody      %prec '('
841                         { SvREFCNT_inc_simple_void(PL_compcv);
842                           $$ = newANONATTRSUB($2, $3, $4, $5); }
843
844     ;
845
846 /* Things called with "do" */
847 termdo  :       DO term %prec UNIOP                     /* do $filename */
848                         { $$ = dofile($2, $1);}
849         |       DO block        %prec '('               /* do { code */
850                         { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));}
851         ;
852
853 term    :       termbinop
854         |       termunop
855         |       anonymous
856         |       termdo
857         |       term '?' term ':' term
858                         { $$ = newCONDOP(0, $1, $3, $5); }
859         |       REFGEN term                          /* \$x, \@y, \%z */
860                         { $$ = newUNOP(OP_REFGEN, 0, $2); }
861         |       myattrterm      %prec UNIOP
862                         { $$ = $1; }
863         |       LOCAL term      %prec UNIOP
864                         { $$ = localize($2,$1); }
865         |       '(' expr ')'
866                         { $$ = sawparens($2); }
867         |       QWLIST
868                         { $$ = $1; }
869         |       '(' ')'
870                         { $$ = sawparens(newNULLLIST()); }
871         |       scalar  %prec '('
872                         { $$ = $1; }
873         |       star    %prec '('
874                         { $$ = $1; }
875         |       hsh     %prec '('
876                         { $$ = $1; }
877         |       ary     %prec '('
878                         { $$ = $1; }
879         |       arylen  %prec '('                    /* $#x, $#{ something } */
880                         { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
881         |       subscripted
882                         { $$ = $1; }
883         |       sliceme '[' expr ']'                     /* array slice */
884                         { $$ = op_prepend_elem(OP_ASLICE,
885                                 newOP(OP_PUSHMARK, 0),
886                                     newLISTOP(OP_ASLICE, 0,
887                                         list($3),
888                                         ref($1, OP_ASLICE)));
889                           if ($$ && $1)
890                               $$->op_private |=
891                                   $1->op_private & OPpSLICEWARNING;
892                         }
893         |       kvslice '[' expr ']'                 /* array key/value slice */
894                         { $$ = op_prepend_elem(OP_KVASLICE,
895                                 newOP(OP_PUSHMARK, 0),
896                                     newLISTOP(OP_KVASLICE, 0,
897                                         list($3),
898                                         ref(oopsAV($1), OP_KVASLICE)));
899                           if ($$ && $1)
900                               $$->op_private |=
901                                   $1->op_private & OPpSLICEWARNING;
902                         }
903         |       sliceme '{' expr ';' '}'                 /* @hash{@keys} */
904                         { $$ = op_prepend_elem(OP_HSLICE,
905                                 newOP(OP_PUSHMARK, 0),
906                                     newLISTOP(OP_HSLICE, 0,
907                                         list($3),
908                                         ref(oopsHV($1), OP_HSLICE)));
909                           if ($$ && $1)
910                               $$->op_private |=
911                                   $1->op_private & OPpSLICEWARNING;
912                         }
913         |       kvslice '{' expr ';' '}'                 /* %hash{@keys} */
914                         { $$ = op_prepend_elem(OP_KVHSLICE,
915                                 newOP(OP_PUSHMARK, 0),
916                                     newLISTOP(OP_KVHSLICE, 0,
917                                         list($3),
918                                         ref($1, OP_KVHSLICE)));
919                           if ($$ && $1)
920                               $$->op_private |=
921                                   $1->op_private & OPpSLICEWARNING;
922                         }
923         |       THING   %prec '('
924                         { $$ = $1; }
925         |       amper                                /* &foo; */
926                         { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
927         |       amper '(' ')'                 /* &foo() or foo() */
928                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
929                         }
930         |       amper '(' expr ')'          /* &foo(@args) or foo(@args) */
931                         {
932                           $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
933                                 op_append_elem(OP_LIST, $3, scalar($1)));
934                         }
935         |       NOAMP subname optlistexpr       /* foo @args (no parens) */
936                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
937                             op_append_elem(OP_LIST, $3, scalar($2)));
938                         }
939         |       term ARROW '$' '*'
940                         { $$ = newSVREF($1); }
941         |       term ARROW '@' '*'
942                         { $$ = newAVREF($1); }
943         |       term ARROW '%' '*'
944                         { $$ = newHVREF($1); }
945         |       term ARROW '&' '*'
946                         { $$ = newUNOP(OP_ENTERSUB, 0,
947                                        scalar(newCVREF($3,$1))); }
948         |       term ARROW '*' '*'      %prec '('
949                         { $$ = newGVREF(0,$1); }
950         |       LOOPEX  /* loop exiting command (goto, last, dump, etc) */
951                         { $$ = newOP($1, OPf_SPECIAL);
952                             PL_hints |= HINT_BLOCK_SCOPE; }
953         |       LOOPEX term
954                         { $$ = newLOOPEX($1,$2); }
955         |       NOTOP listexpr                       /* not $foo */
956                         { $$ = newUNOP(OP_NOT, 0, scalar($2)); }
957         |       UNIOP                                /* Unary op, $_ implied */
958                         { $$ = newOP($1, 0); }
959         |       UNIOP block                          /* eval { foo }* */
960                         { $$ = newUNOP($1, 0, $2); }
961         |       UNIOP term                           /* Unary op */
962                         { $$ = newUNOP($1, 0, $2); }
963         |       REQUIRE                              /* require, $_ implied */
964                         { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); }
965         |       REQUIRE term                         /* require Foo */
966                         { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); }
967         |       UNIOPSUB
968                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
969         |       UNIOPSUB term                        /* Sub treated as unop */
970                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
971                             op_append_elem(OP_LIST, $2, scalar($1))); }
972         |       FUNC0                                /* Nullary operator */
973                         { $$ = newOP($1, 0); }
974         |       FUNC0 '(' ')'
975                         { $$ = newOP($1, 0);}
976         |       FUNC0OP       /* Same as above, but op created in toke.c */
977                         { $$ = $1; }
978         |       FUNC0OP '(' ')'
979                         { $$ = $1; }
980         |       FUNC0SUB                             /* Sub treated as nullop */
981                         { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
982         |       FUNC1 '(' ')'                        /* not () */
983                         { $$ = ($1 == OP_NOT)
984                           ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0)))
985                           : newOP($1, OPf_SPECIAL); }
986         |       FUNC1 '(' expr ')'                   /* not($foo) */
987                         { $$ = newUNOP($1, 0, $3); }
988         |       PMFUNC /* m//, s///, qr//, tr/// */
989                         {
990                             if (   $1->op_type != OP_TRANS
991                                 && $1->op_type != OP_TRANSR
992                                 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
993                             {
994                                 $<ival>$ = start_subparse(FALSE, CVf_ANON);
995                                 SAVEFREESV(PL_compcv);
996                             } else
997                                 $<ival>$ = 0;
998                         }
999                     '(' listexpr optrepl ')'
1000                         { $$ = pmruntime($1, $4, $5, 1, $<ival>2); }
1001         |       WORD
1002         |       listop
1003         |       YADAYADA
1004                         {
1005                           $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1006                                 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
1007                         }
1008         |       PLUGEXPR
1009         ;
1010
1011 /* "my" declarations, with optional attributes */
1012 myattrterm:     MY myterm myattrlist
1013                         { $$ = my_attrs($2,$3); }
1014         |       MY myterm
1015                         { $$ = localize($2,$1); }
1016         ;
1017
1018 /* Things that can be "my"'d */
1019 myterm  :       '(' expr ')'
1020                         { $$ = sawparens($2); }
1021         |       '(' ')'
1022                         { $$ = sawparens(newNULLLIST()); }
1023
1024         |       scalar  %prec '('
1025                         { $$ = $1; }
1026         |       hsh     %prec '('
1027                         { $$ = $1; }
1028         |       ary     %prec '('
1029                         { $$ = $1; }
1030         ;
1031
1032 /* Basic list expressions */
1033 optlistexpr:    /* NULL */ %prec PREC_LOW
1034                         { $$ = (OP*)NULL; }
1035         |       listexpr    %prec PREC_LOW
1036                         { $$ = $1; }
1037         ;
1038
1039 optexpr:        /* NULL */
1040                         { $$ = (OP*)NULL; }
1041         |       expr
1042                         { $$ = $1; }
1043         ;
1044
1045 optrepl:        /* NULL */
1046                         { $$ = (OP*)NULL; }
1047         |       '/' expr
1048                         { $$ = $2; }
1049         ;
1050
1051 /* A little bit of trickery to make "for my $foo (@bar)" actually be
1052    lexical */
1053 my_scalar:      scalar
1054                         { parser->in_my = 0; $$ = my($1); }
1055         ;
1056
1057 my_var  :       scalar
1058         |       ary
1059         |       hsh
1060         ;
1061
1062 refgen_topic:   my_var
1063         |       amper
1064         ;
1065
1066 amper   :       '&' indirob
1067                         { $$ = newCVREF($1,$2); }
1068         ;
1069
1070 scalar  :       '$' indirob
1071                         { $$ = newSVREF($2); }
1072         ;
1073
1074 ary     :       '@' indirob
1075                         { $$ = newAVREF($2);
1076                           if ($$) $$->op_private |= $1;
1077                         }
1078         ;
1079
1080 hsh     :       '%' indirob
1081                         { $$ = newHVREF($2);
1082                           if ($$) $$->op_private |= $1;
1083                         }
1084         ;
1085
1086 arylen  :       DOLSHARP indirob
1087                         { $$ = newAVREF($2); }
1088         |       term ARROW DOLSHARP '*'
1089                         { $$ = newAVREF($1); }
1090         ;
1091
1092 star    :       '*' indirob
1093                         { $$ = newGVREF(0,$2); }
1094         ;
1095
1096 sliceme :       ary
1097         |       term ARROW '@'
1098                         { $$ = newAVREF($1); }
1099         ;
1100
1101 kvslice :       hsh
1102         |       term ARROW '%'
1103                         { $$ = newHVREF($1); }
1104         ;
1105
1106 gelem   :       star
1107         |       term ARROW '*'
1108                         { $$ = newGVREF(0,$1); }
1109         ;
1110
1111 /* Indirect objects */
1112 indirob :       WORD
1113                         { $$ = scalar($1); }
1114         |       scalar %prec PREC_LOW
1115                         { $$ = scalar($1); }
1116         |       block
1117                         { $$ = op_scope($1); }
1118
1119         |       PRIVATEREF
1120                         { $$ = $1; }
1121         ;