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