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