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