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