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