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