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