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