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