This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix our sub with proto
[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 *
f05e27e5
DM
23 * Note that these derived files are included and compiled twice; once
24 * from perly.c, and once from madly.c. The second time, a number of MAD
25 * macros are defined, which compile in extra code that allows the parse
26 * tree to be accurately dumped. In particular:
27 *
28 * MAD defined if compiling madly.c
29 * DO_MAD(A) expands to A under madly.c, to null otherwise
30 * IF_MAD(a,b) expands to A under madly.c, to B otherwise
31 * TOKEN_GETMAD() expands to token_getmad() under madly.c, to null otherwise
32 * TOKEN_FREE() similarly
33 * OP_GETMAD() similarly
34 * IVAL(i) expands to (i)->tk_lval.ival or (i)
35 * PVAL(p) expands to (p)->tk_lval.pval or (p)
36 *
166f8a29
DM
37 * The main job of of this grammar is to call the various newFOO()
38 * functions in op.c to build a syntax tree of OP structs.
61296642 39 * It relies on the lexer in toke.c to do the tokenizing.
29522234
DM
40 *
41 * Note: due to the way that the cleanup code works WRT to freeing ops on
42 * the parse stack, it is dangerous to assign to the $n variables within
43 * an action.
166f8a29
DM
44 */
45
0de566d7 46/* Make the parser re-entrant. */
8d063cd8 47
0de566d7 48%pure_parser
8d063cd8 49
f05e27e5
DM
50/* FIXME for MAD - is the new mintro on while and until important? */
51
28ac2b49 52%start grammar
9d116dd7 53
8d063cd8 54%union {
d5c6462e
DM
55 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
56 must always be 1st union member) */
79072805
LW
57 char *pval;
58 OP *opval;
59 GV *gvval;
f05e27e5
DM
60#ifdef PERL_IN_MADLY_C
61 TOKEN* p_tkval;
123d08c9 62 TOKEN* i_tkval;
f05e27e5
DM
63#else
64 char *p_tkval;
123d08c9
DM
65 I32 i_tkval;
66#endif
67#ifdef PERL_MAD
68 TOKEN* tkval;
f05e27e5 69#endif
8d063cd8
LW
70}
71
78cdf107 72%token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ
28ac2b49 73
7c70caa5 74%token <i_tkval> '{' '}' '[' ']' '-' '+' '$' '@' '%' '*' '&' ';' '=' '.'
f0fcb552 75
ea25a9b2 76%token <opval> WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST
7eb971ee 77%token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB
88e1f1a2 78%token <opval> PLUGEXPR PLUGSTMT
f05e27e5 79%token <p_tkval> LABEL
123d08c9
DM
80%token <i_tkval> FORMAT SUB ANONSUB PACKAGE USE
81%token <i_tkval> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR
82%token <i_tkval> GIVEN WHEN DEFAULT
be25f609 83%token <i_tkval> LOOPEX DOTDOT YADAYADA
123d08c9
DM
84%token <i_tkval> FUNC0 FUNC1 FUNC UNIOP LSTOP
85%token <i_tkval> RELOP EQOP MULOP ADDOP
86%token <i_tkval> DOLSHARP DO HASHBRACK NOAMP
87%token <i_tkval> LOCAL MY MYSUB REQUIRE
705fe0e5 88%token <i_tkval> COLONATTR FORMLBRACK FORMRBRACK
f05e27e5 89
727a8fe5 90%type <ival> grammar remember mremember
f05e27e5
DM
91%type <ival> startsub startanonsub startformsub
92/* FIXME for MAD - are these two ival? */
b5a64814 93%type <ival> mintro
f05e27e5 94
8e720305 95%type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else
fad39ff1 96%type <opval> expr term subscripted scalar ary hsh arylen star amper sideff
09b1cffe
Z
97%type <opval> listexpr nexpr texpr iexpr mexpr mnexpr miexpr
98%type <opval> optlistexpr optexpr indirob listop method
7c70caa5 99%type <opval> formname subname proto subbody cont my_scalar formblock
f05e27e5 100%type <opval> subattrlist myattrlist myattrterm myterm
891be019 101%type <opval> termbinop termunop anonymous termdo
705fe0e5 102%type <opval> formstmtseq formline formarg
79072805 103
123d08c9 104%nonassoc <i_tkval> PREC_LOW
fad39ff1
SM
105%nonassoc LOOPEX
106
123d08c9
DM
107%left <i_tkval> OROP DOROP
108%left <i_tkval> ANDOP
109%right <i_tkval> NOTOP
36477c24 110%nonassoc LSTOP LSTOPSUB
123d08c9
DM
111%left <i_tkval> ','
112%right <i_tkval> ASSIGNOP
113%right <i_tkval> '?' ':'
be25f609 114%nonassoc DOTDOT YADAYADA
123d08c9
DM
115%left <i_tkval> OROR DORDOR
116%left <i_tkval> ANDAND
117%left <i_tkval> BITOROP
118%left <i_tkval> BITANDOP
a687059c
LW
119%nonassoc EQOP
120%nonassoc RELOP
36477c24 121%nonassoc UNIOP UNIOPSUB
20515881 122%nonassoc REQUIRE
123d08c9 123%left <i_tkval> SHIFTOP
a687059c
LW
124%left ADDOP
125%left MULOP
123d08c9
DM
126%left <i_tkval> MATCHOP
127%right <i_tkval> '!' '~' UMINUS REFGEN
128%right <i_tkval> POWOP
129%nonassoc <i_tkval> PREINC PREDEC POSTINC POSTDEC
130%left <i_tkval> ARROW
131%nonassoc <i_tkval> ')'
132%left <i_tkval> '('
fad39ff1 133%left '[' '{'
8d063cd8 134
123d08c9 135%token <i_tkval> PEG
7f46837f 136
8d063cd8
LW
137%% /* RULES */
138
28ac2b49 139/* Top-level choice of what kind of thing yyparse was called to parse */
727a8fe5
Z
140grammar : GRAMPROG
141 {
142 PL_parser->expect = XSTATE;
143 }
144 remember stmtseq
145 {
146 newPROG(block_end($3,$4));
147 $$ = 0;
148 }
78cdf107
Z
149 | GRAMEXPR
150 {
151 parser->expect = XTERM;
152 }
09b1cffe 153 optexpr
78cdf107
Z
154 {
155 PL_eval_root = $3;
156 $$ = 0;
157 }
e53d8f76
Z
158 | GRAMBLOCK
159 {
160 parser->expect = XBLOCK;
161 }
162 block
163 {
5f211341 164 PL_pad_reset_pending = TRUE;
8359b381
Z
165 PL_eval_root = $3;
166 $$ = 0;
167 yyunlex();
168 parser->yychar = YYEOF;
169 }
170 | GRAMBARESTMT
171 {
172 parser->expect = XSTATE;
173 }
174 barestmt
175 {
176 PL_pad_reset_pending = TRUE;
e53d8f76
Z
177 PL_eval_root = $3;
178 $$ = 0;
179 yyunlex();
180 parser->yychar = YYEOF;
181 }
9eb5c532 182 | GRAMFULLSTMT
28ac2b49 183 {
9eb5c532
Z
184 parser->expect = XSTATE;
185 }
186 fullstmt
187 {
5f211341 188 PL_pad_reset_pending = TRUE;
9eb5c532 189 PL_eval_root = $3;
28ac2b49
Z
190 $$ = 0;
191 yyunlex();
192 parser->yychar = YYEOF;
193 }
07ffcb73
Z
194 | GRAMSTMTSEQ
195 {
196 parser->expect = XSTATE;
197 }
5f211341 198 stmtseq
07ffcb73
Z
199 {
200 PL_eval_root = $3;
201 $$ = 0;
202 }
203 ;
204
891be019 205/* An ordinary block */
5f211341 206block : '{' remember stmtseq '}'
53a7735b
DM
207 { if (PL_parser->copline > (line_t)IVAL($1))
208 PL_parser->copline = (line_t)IVAL($1);
f05e27e5
DM
209 $$ = block_end($2, $3);
210 TOKEN_GETMAD($1,$$,'{');
211 TOKEN_GETMAD($4,$$,'}');
212 }
a0d0e21e
LW
213 ;
214
7c70caa5 215/* format body */
705fe0e5 216formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.'
7c70caa5
FC
217 { if (PL_parser->copline > (line_t)IVAL($1))
218 PL_parser->copline = (line_t)IVAL($1);
705fe0e5 219 $$ = block_end($2, $5);
7c70caa5 220 TOKEN_GETMAD($1,$$,'{');
705fe0e5 221 TOKEN_GETMAD($7,$$,'}');
7c70caa5
FC
222 }
223 ;
224
55497cff
PP
225remember: /* NULL */ /* start a full lexical scope */
226 { $$ = block_start(TRUE); }
227 ;
228
5f211341 229mblock : '{' mremember stmtseq '}'
53a7735b
DM
230 { if (PL_parser->copline > (line_t)IVAL($1))
231 PL_parser->copline = (line_t)IVAL($1);
f05e27e5
DM
232 $$ = block_end($2, $3);
233 TOKEN_GETMAD($1,$$,'{');
234 TOKEN_GETMAD($4,$$,'}');
235 }
55497cff
PP
236 ;
237
238mremember: /* NULL */ /* start a partial lexical scope */
239 { $$ = block_start(FALSE); }
8d063cd8
LW
240 ;
241
eae48c89 242/* A sequence of statements in the program */
5f211341 243stmtseq : /* NULL */
bcabcc50 244 { $$ = (OP*)NULL; }
5f211341 245 | stmtseq fullstmt
2fcb4757 246 { $$ = op_append_list(OP_LINESEQ, $1, $2);
3280af22 247 PL_pad_reset_pending = TRUE;
503de470
DM
248 if ($1 && $2)
249 PL_hints |= HINT_BLOCK_SCOPE;
250 }
8d063cd8
LW
251 ;
252
705fe0e5
FC
253/* A sequence of format lines */
254formstmtseq: /* NULL */
255 { $$ = (OP*)NULL; }
256 | formstmtseq formline
257 { $$ = op_append_list(OP_LINESEQ, $1, $2);
258 PL_pad_reset_pending = TRUE;
259 if ($1 && $2)
260 PL_hints |= HINT_BLOCK_SCOPE;
261 }
262 ;
263
8e720305
Z
264/* A statement in the program, including optional labels */
265fullstmt: barestmt
eae48c89 266 {
8e720305
Z
267 if($1) {
268 $$ = newSTATEOP(0, NULL, $1);
eae48c89 269 } else {
8e720305 270 $$ = IF_MAD(newOP(OP_NULL, 0), NULL);
eae48c89
Z
271 }
272 }
8e720305
Z
273 | labfullstmt
274 { $$ = $1; }
275 ;
276
277labfullstmt: LABEL barestmt
278 {
5db1eb8d
BF
279 $$ = newSTATEOP(SvUTF8(((SVOP*)$1)->op_sv),
280 savepv(SvPVX(((SVOP*)$1)->op_sv)), $2);
8e720305
Z
281 TOKEN_GETMAD($1,
282 $2 ? cLISTOPx($$)->op_first : $$, 'L');
283 }
284 | LABEL labfullstmt
285 {
5db1eb8d
BF
286 $$ = newSTATEOP(SvUTF8(((SVOP*)$1)->op_sv),
287 savepv(SvPVX(((SVOP*)$1)->op_sv)), $2);
8e720305
Z
288 TOKEN_GETMAD($1, cLISTOPx($$)->op_first, 'L');
289 }
eae48c89
Z
290 ;
291
292/* A bare statement, lacking label and other aspects of state op */
293barestmt: PLUGSTMT
0d863452 294 { $$ = $1; }
eae48c89
Z
295 | PEG
296 {
297 $$ = newOP(OP_NULL,0);
298 TOKEN_GETMAD($1,$$,'p');
299 }
7c70caa5 300 | FORMAT startformsub formname formblock
f05e27e5 301 {
eae48c89 302 CV *fmtcv = PL_compcv;
eae48c89
Z
303#ifdef MAD
304 $$ = newFORM($2, $3, $4);
305 prepend_madprops($1->tk_mad, $$, 'F');
306 $1->tk_mad = 0;
307 token_free($1);
308#else
309 newFORM($2, $3, $4);
310 $$ = (OP*)NULL;
311#endif
4a273b91 312 if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) {
eae48c89 313 SvREFCNT_inc_simple_void(fmtcv);
cc76b5cc 314 pad_add_anon(fmtcv, OP_NULL);
f05e27e5 315 }
eae48c89 316 }
764212cf
FC
317 | SUB WORD startsub
318 { const char *const name = SvPV_nolen_const(((SVOP*)$2)->op_sv);
319 if (strEQ(name, "BEGIN") || strEQ(name, "END")
320 || strEQ(name, "INIT") || strEQ(name, "CHECK")
321 || strEQ(name, "UNITCHECK"))
322 CvSPECIAL_on(PL_compcv);
323 PL_parser->in_my = 0;
324 PL_parser->in_my_stash = NULL;
325 }
326 proto subattrlist subbody
eae48c89
Z
327 {
328 SvREFCNT_inc_simple_void(PL_compcv);
329#ifdef MAD
330 {
331 OP* o = newSVOP(OP_ANONCODE, 0,
764212cf 332 (SV*)newATTRSUB($3, $2, $5, $6, $7));
eae48c89
Z
333 $$ = newOP(OP_NULL,0);
334 op_getmad(o,$$,'&');
764212cf
FC
335 op_getmad($2,$$,'n');
336 op_getmad($5,$$,'s');
337 op_getmad($6,$$,'a');
eae48c89 338 token_getmad($1,$$,'d');
764212cf
FC
339 append_madprops($7->op_madprop, $$, 0);
340 $7->op_madprop = 0;
f05e27e5 341 }
eae48c89 342#else
764212cf 343 newATTRSUB($3, $2, $5, $6, $7);
eae48c89
Z
344 $$ = (OP*)NULL;
345#endif
764212cf 346 intro_my();
f05e27e5 347 }
eae48c89 348 | MYSUB startsub subname proto subattrlist subbody
f05e27e5 349 {
eae48c89
Z
350 /* Unimplemented "my sub foo { }" */
351 SvREFCNT_inc_simple_void(PL_compcv);
352#ifdef MAD
353 $$ = newMYSUB($2, $3, $4, $5, $6);
354 token_getmad($1,$$,'d');
355#else
356 newMYSUB($2, $3, $4, $5, $6);
357 $$ = (OP*)NULL;
358#endif
359 }
360 | PACKAGE WORD WORD ';'
5f211341 361 {
eae48c89
Z
362#ifdef MAD
363 $$ = package($3);
364 token_getmad($1,$$,'o');
365 if ($2)
366 package_version($2);
367 token_getmad($4,$$,';');
368#else
369 package($3);
370 if ($2)
371 package_version($2);
372 $$ = (OP*)NULL;
373#endif
374 }
375 | USE startsub
376 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
09b1cffe 377 WORD WORD optlistexpr ';'
eae48c89
Z
378 {
379 SvREFCNT_inc_simple_void(PL_compcv);
380#ifdef MAD
381 $$ = utilize(IVAL($1), $2, $4, $5, $6);
382 token_getmad($1,$$,'o');
383 token_getmad($7,$$,';');
384 if (PL_parser->rsfp_filters &&
385 AvFILLp(PL_parser->rsfp_filters) >= 0)
386 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
387#else
388 utilize(IVAL($1), $2, $4, $5, $6);
389 $$ = (OP*)NULL;
390#endif
391 }
417a992d 392 | IF '(' remember mexpr ')' mblock else
eae48c89 393 {
3ad73efd
Z
394 $$ = block_end($3,
395 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
396 TOKEN_GETMAD($1,$$,'I');
397 TOKEN_GETMAD($2,$$,'(');
398 TOKEN_GETMAD($5,$$,')');
399 PL_parser->copline = (line_t)IVAL($1);
400 }
417a992d 401 | UNLESS '(' remember miexpr ')' mblock else
eae48c89 402 {
3ad73efd
Z
403 $$ = block_end($3,
404 newCONDOP(0, $4, op_scope($6), $7));
eae48c89
Z
405 TOKEN_GETMAD($1,$$,'I');
406 TOKEN_GETMAD($2,$$,'(');
407 TOKEN_GETMAD($5,$$,')');
408 PL_parser->copline = (line_t)IVAL($1);
409 }
b5a64814 410 | GIVEN '(' remember mexpr ')' mblock
eae48c89 411 {
b5a64814 412 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
eae48c89 413 $$ = block_end($3,
b5a64814
FC
414 newGIVENOP($4, op_scope($6),
415 offset == NOT_IN_PAD
416 || PAD_COMPNAME_FLAGS_isOUR(offset)
417 ? 0
418 : offset));
eae48c89
Z
419 PL_parser->copline = (line_t)IVAL($1);
420 }
417a992d 421 | WHEN '(' remember mexpr ')' mblock
3ad73efd 422 { $$ = block_end($3, newWHENOP($4, op_scope($6))); }
eae48c89 423 | DEFAULT block
3ad73efd 424 { $$ = newWHENOP(0, op_scope($2)); }
417a992d 425 | WHILE '(' remember texpr ')' mintro mblock cont
eae48c89
Z
426 {
427 $$ = block_end($3,
428 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 429 $4, $7, $8, $6));
eae48c89
Z
430 TOKEN_GETMAD($1,$$,'W');
431 TOKEN_GETMAD($2,$$,'(');
432 TOKEN_GETMAD($5,$$,')');
433 PL_parser->copline = (line_t)IVAL($1);
434 }
417a992d 435 | UNTIL '(' remember iexpr ')' mintro mblock cont
eae48c89
Z
436 {
437 $$ = block_end($3,
94bf0465
Z
438 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
439 $4, $7, $8, $6));
eae48c89
Z
440 TOKEN_GETMAD($1,$$,'W');
441 TOKEN_GETMAD($2,$$,'(');
442 TOKEN_GETMAD($5,$$,')');
443 PL_parser->copline = (line_t)IVAL($1);
444 }
417a992d 445 | FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')'
eae48c89
Z
446 mblock
447 {
448 OP *initop = IF_MAD($4 ? $4 : newOP(OP_NULL, 0), $4);
449 OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 450 scalar($6), $11, $9, $8);
eae48c89
Z
451 if (initop) {
452 forop = op_prepend_elem(OP_LINESEQ, initop,
453 op_append_elem(OP_LINESEQ,
454 newOP(OP_UNSTACK, OPf_SPECIAL),
455 forop));
5f211341 456 }
eae48c89
Z
457 DO_MAD({ forop = newUNOP(OP_NULL, 0, forop); })
458 $$ = block_end($3, forop);
459 TOKEN_GETMAD($1,$$,'3');
460 TOKEN_GETMAD($2,$$,'(');
461 TOKEN_GETMAD($5,$$,'1');
462 TOKEN_GETMAD($7,$$,'2');
463 TOKEN_GETMAD($10,$$,')');
464 PL_parser->copline = (line_t)IVAL($1);
465 }
417a992d 466 | FOR MY remember my_scalar '(' mexpr ')' mblock cont
eae48c89 467 {
94bf0465 468 $$ = block_end($3, newFOROP(0, $4, $6, $8, $9));
eae48c89
Z
469 TOKEN_GETMAD($1,$$,'W');
470 TOKEN_GETMAD($2,$$,'d');
471 TOKEN_GETMAD($5,$$,'(');
472 TOKEN_GETMAD($7,$$,')');
473 PL_parser->copline = (line_t)IVAL($1);
474 }
417a992d 475 | FOR scalar '(' remember mexpr ')' mblock cont
eae48c89 476 {
94bf0465 477 $$ = block_end($4, newFOROP(0,
3ad73efd 478 op_lvalue($2, OP_ENTERLOOP), $5, $7, $8));
eae48c89
Z
479 TOKEN_GETMAD($1,$$,'W');
480 TOKEN_GETMAD($3,$$,'(');
481 TOKEN_GETMAD($6,$$,')');
482 PL_parser->copline = (line_t)IVAL($1);
483 }
417a992d 484 | FOR '(' remember mexpr ')' mblock cont
eae48c89
Z
485 {
486 $$ = block_end($3,
94bf0465 487 newFOROP(0, (OP*)NULL, $4, $6, $7));
eae48c89
Z
488 TOKEN_GETMAD($1,$$,'W');
489 TOKEN_GETMAD($2,$$,'(');
490 TOKEN_GETMAD($5,$$,')');
491 PL_parser->copline = (line_t)IVAL($1);
492 }
493 | block cont
494 {
495 /* a block is a loop that happens once */
496 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
94bf0465 497 (OP*)NULL, $1, $2, 0);
eae48c89
Z
498 }
499 | PACKAGE WORD WORD '{' remember
500 {
eae48c89 501 package($3);
eae48c89 502 if ($2) {
eae48c89 503 package_version($2);
eae48c89
Z
504 }
505 }
506 stmtseq '}'
507 {
508 /* a block is a loop that happens once */
94bf0465 509 $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
eae48c89 510 (OP*)NULL, block_end($5, $7), (OP*)NULL, 0);
eae48c89
Z
511 TOKEN_GETMAD($4,$$,'{');
512 TOKEN_GETMAD($8,$$,'}');
513 if (PL_parser->copline > (line_t)IVAL($4))
514 PL_parser->copline = (line_t)IVAL($4);
515 }
516 | sideff ';'
517 {
518 PL_parser->expect = XSTATE;
519 $$ = $1;
520 TOKEN_GETMAD($2,$$,';');
521 }
522 | ';'
523 {
524 PL_parser->expect = XSTATE;
525 $$ = IF_MAD(newOP(OP_NULL, 0), (OP*)NULL);
526 TOKEN_GETMAD($1,$$,';');
527 PL_parser->copline = NOLINE;
5f211341 528 }
8d063cd8
LW
529 ;
530
705fe0e5
FC
531/* Format line */
532formline: THING formarg
533 { OP *list;
534 if ($2) {
535 OP *term = $2;
536 DO_MAD(term = newUNOP(OP_NULL, 0, term));
537 list = op_append_elem(OP_LIST, $1, term);
538 }
539 else {
540#ifdef MAD
541 OP *op = newNULLLIST();
542 list = op_append_elem(OP_LIST, $1, op);
543#else
544 list = $1;
545#endif
546 }
547 if (PL_parser->copline == NOLINE)
548 PL_parser->copline = CopLINE(PL_curcop)-1;
549 else PL_parser->copline--;
550 $$ = newSTATEOP(0, NULL,
551 convert(OP_FORMLINE, 0, list));
552 }
553 ;
554
555formarg : /* NULL */
556 { $$ = NULL; }
557 | FORMLBRACK stmtseq FORMRBRACK
558 { $$ = op_unscope($2); }
559 ;
560
891be019 561/* An expression which may have a side-effect */
a687059c 562sideff : error
bcabcc50 563 { $$ = (OP*)NULL; }
a687059c 564 | expr
79072805 565 { $$ = $1; }
a687059c 566 | expr IF expr
f05e27e5
DM
567 { $$ = newLOGOP(OP_AND, 0, $3, $1);
568 TOKEN_GETMAD($2,$$,'i');
569 }
a687059c 570 | expr UNLESS expr
f05e27e5
DM
571 { $$ = newLOGOP(OP_OR, 0, $3, $1);
572 TOKEN_GETMAD($2,$$,'i');
573 }
a687059c 574 | expr WHILE expr
f05e27e5
DM
575 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
576 TOKEN_GETMAD($2,$$,'w');
577 }
55497cff 578 | expr UNTIL iexpr
f05e27e5
DM
579 { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);
580 TOKEN_GETMAD($2,$$,'w');
581 }
ecca16b0 582 | expr FOR expr
94bf0465 583 { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL);
eae48c89 584 TOKEN_GETMAD($2,$$,'w');
94bf0465 585 PL_parser->copline = (line_t)IVAL($2);
f05e27e5 586 }
f20dcd76 587 | expr WHEN expr
3ad73efd 588 { $$ = newWHENOP($3, op_scope($1)); }
79072805
LW
589 ;
590
891be019 591/* else and elsif blocks */
79072805 592else : /* NULL */
bcabcc50 593 { $$ = (OP*)NULL; }
55497cff 594 | ELSE mblock
3ad73efd
Z
595 {
596 ($2)->op_flags |= OPf_PARENS;
597 $$ = op_scope($2);
f05e27e5
DM
598 TOKEN_GETMAD($1,$$,'o');
599 }
417a992d 600 | ELSIF '(' mexpr ')' mblock else
53a7735b 601 { PL_parser->copline = (line_t)IVAL($1);
3ad73efd
Z
602 $$ = newCONDOP(0,
603 newSTATEOP(OPf_SPECIAL,NULL,$3),
604 op_scope($5), $6);
605 PL_hints |= HINT_BLOCK_SCOPE;
f05e27e5
DM
606 TOKEN_GETMAD($1,$$,'I');
607 TOKEN_GETMAD($2,$$,'(');
608 TOKEN_GETMAD($4,$$,')');
609 }
79072805
LW
610 ;
611
891be019 612/* Continue blocks */
79072805 613cont : /* NULL */
bcabcc50 614 { $$ = (OP*)NULL; }
79072805 615 | CONTINUE block
3ad73efd
Z
616 {
617 $$ = op_scope($2);
f05e27e5
DM
618 TOKEN_GETMAD($1,$$,'o');
619 }
79072805
LW
620 ;
621
a034e688
DM
622/* determine whether there are any new my declarations */
623mintro : /* NULL */
624 { $$ = (PL_min_intro_pending &&
625 PL_max_intro_pending >= PL_min_intro_pending);
626 intro_my(); }
627
891be019 628/* Normal expression */
8d063cd8 629nexpr : /* NULL */
bcabcc50 630 { $$ = (OP*)NULL; }
8d063cd8
LW
631 | sideff
632 ;
633
891be019 634/* Boolean expression */
8d063cd8 635texpr : /* NULL means true */
f05e27e5
DM
636 { YYSTYPE tmplval;
637 (void)scan_num("1", &tmplval);
638 $$ = tmplval.opval; }
8d063cd8
LW
639 | expr
640 ;
641
891be019 642/* Inverted boolean expression */
55497cff
PP
643iexpr : expr
644 { $$ = invert(scalar($1)); }
645 ;
646
891be019 647/* Expression with its own lexical scope */
55497cff 648mexpr : expr
bbce6d69
PP
649 { $$ = $1; intro_my(); }
650 ;
651
652mnexpr : nexpr
653 { $$ = $1; intro_my(); }
55497cff
PP
654 ;
655
55497cff 656miexpr : iexpr
bbce6d69 657 { $$ = $1; intro_my(); }
55497cff
PP
658 ;
659
44a8e56a 660formname: WORD { $$ = $1; }
bcabcc50 661 | /* NULL */ { $$ = (OP*)NULL; }
8d063cd8
LW
662 ;
663
fa83b5b6 664startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6
DM
665 { $$ = start_subparse(FALSE, 0);
666 SAVEFREESV(PL_compcv); }
f05e27e5 667
28757baa
PP
668 ;
669
670startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6
DM
671 { $$ = start_subparse(FALSE, CVf_ANON);
672 SAVEFREESV(PL_compcv); }
28757baa
PP
673 ;
674
44a8e56a 675startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6
DM
676 { $$ = start_subparse(TRUE, 0);
677 SAVEFREESV(PL_compcv); }
44a8e56a
PP
678 ;
679
891be019 680/* Name of a subroutine - must be a bareword, could be special */
2596d9fe 681subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
e858de61 682 if (strEQ(name, "BEGIN") || strEQ(name, "END")
3c10abe3
AG
683 || strEQ(name, "INIT") || strEQ(name, "CHECK")
684 || strEQ(name, "UNITCHECK"))
1aff0e91 685 CvSPECIAL_on(PL_compcv);
28757baa 686 $$ = $1; }
a0d0e21e
LW
687 ;
688
891be019 689/* Subroutine prototype */
4633a7c4 690proto : /* NULL */
bcabcc50 691 { $$ = (OP*)NULL; }
4633a7c4
LW
692 | THING
693 ;
28757baa 694
891be019 695/* Optional list of subroutine attributes */
09bef843 696subattrlist: /* NULL */
bcabcc50 697 { $$ = (OP*)NULL; }
09bef843 698 | COLONATTR THING
f05e27e5
DM
699 { $$ = $2;
700 TOKEN_GETMAD($1,$$,':');
701 }
09bef843 702 | COLONATTR
f05e27e5
DM
703 { $$ = IF_MAD(
704 newOP(OP_NULL, 0),
bcabcc50 705 (OP*)NULL
f05e27e5
DM
706 );
707 TOKEN_GETMAD($1,$$,':');
708 }
09bef843
SB
709 ;
710
891be019 711/* List of attributes for a "my" variable declaration */
09bef843 712myattrlist: COLONATTR THING
f05e27e5
DM
713 { $$ = $2;
714 TOKEN_GETMAD($1,$$,':');
715 }
09bef843 716 | COLONATTR
f05e27e5
DM
717 { $$ = IF_MAD(
718 newOP(OP_NULL, 0),
bcabcc50 719 (OP*)NULL
f05e27e5
DM
720 );
721 TOKEN_GETMAD($1,$$,':');
722 }
09bef843
SB
723 ;
724
891be019 725/* Subroutine body - either null or a block */
28757baa 726subbody : block { $$ = $1; }
f05e27e5
DM
727 | ';' { $$ = IF_MAD(
728 newOP(OP_NULL,0),
bcabcc50 729 (OP*)NULL
f05e27e5 730 );
53a7735b 731 PL_parser->expect = XSTATE;
f05e27e5
DM
732 TOKEN_GETMAD($1,$$,';');
733 }
8d063cd8
LW
734 ;
735
891be019 736/* Ordinary expressions; logical combinations */
a0d0e21e 737expr : expr ANDOP expr
f05e27e5
DM
738 { $$ = newLOGOP(OP_AND, 0, $1, $3);
739 TOKEN_GETMAD($2,$$,'o');
740 }
a0d0e21e 741 | expr OROP expr
f05e27e5
DM
742 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
743 TOKEN_GETMAD($2,$$,'o');
744 }
c963b151 745 | expr DOROP expr
f05e27e5
DM
746 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
747 TOKEN_GETMAD($2,$$,'o');
748 }
09b1cffe 749 | listexpr %prec PREC_LOW
a0d0e21e
LW
750 ;
751
891be019 752/* Expressions are a list of terms joined by commas */
09b1cffe 753listexpr: listexpr ','
f05e27e5
DM
754 {
755#ifdef MAD
756 OP* op = newNULLLIST();
757 token_getmad($2,op,',');
2fcb4757 758 $$ = op_append_elem(OP_LIST, $1, op);
f05e27e5
DM
759#else
760 $$ = $1;
761#endif
762 }
09b1cffe 763 | listexpr ',' term
f05e27e5 764 {
29522234 765 OP* term = $3;
f05e27e5 766 DO_MAD(
29522234
DM
767 term = newUNOP(OP_NULL, 0, term);
768 token_getmad($2,term,',');
f05e27e5 769 )
2fcb4757 770 $$ = op_append_elem(OP_LIST, $1, term);
f05e27e5 771 }
fad39ff1 772 | term %prec PREC_LOW
8d063cd8
LW
773 ;
774
891be019 775/* List operators */
09b1cffe 776listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */
f05e27e5 777 { $$ = convert(IVAL($1), OPf_STACKED,
2fcb4757 778 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
f05e27e5
DM
779 TOKEN_GETMAD($1,$$,'o');
780 }
891be019 781 | FUNC '(' indirob expr ')' /* print ($fh @args */
f05e27e5 782 { $$ = convert(IVAL($1), OPf_STACKED,
2fcb4757 783 op_prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
f05e27e5
DM
784 TOKEN_GETMAD($1,$$,'o');
785 TOKEN_GETMAD($2,$$,'(');
786 TOKEN_GETMAD($5,$$,')');
787 }
417a992d 788 | term ARROW method '(' optexpr ')' /* $foo->bar(list) */
4633a7c4 789 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
790 op_append_elem(OP_LIST,
791 op_prepend_elem(OP_LIST, scalar($1), $5),
f05e27e5
DM
792 newUNOP(OP_METHOD, 0, $3)));
793 TOKEN_GETMAD($2,$$,'A');
794 TOKEN_GETMAD($4,$$,'(');
795 TOKEN_GETMAD($6,$$,')');
796 }
891be019 797 | term ARROW method /* $foo->bar */
b1524f17 798 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757 799 op_append_elem(OP_LIST, scalar($1),
f05e27e5
DM
800 newUNOP(OP_METHOD, 0, $3)));
801 TOKEN_GETMAD($2,$$,'A');
802 }
09b1cffe 803 | METHOD indirob optlistexpr /* new Class @args */
4633a7c4 804 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
805 op_append_elem(OP_LIST,
806 op_prepend_elem(OP_LIST, $2, $3),
f05e27e5
DM
807 newUNOP(OP_METHOD, 0, $1)));
808 }
09b1cffe 809 | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */
4633a7c4 810 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
811 op_append_elem(OP_LIST,
812 op_prepend_elem(OP_LIST, $2, $4),
f05e27e5
DM
813 newUNOP(OP_METHOD, 0, $1)));
814 TOKEN_GETMAD($3,$$,'(');
815 TOKEN_GETMAD($5,$$,')');
816 }
09b1cffe 817 | LSTOP optlistexpr /* print @args */
f05e27e5
DM
818 { $$ = convert(IVAL($1), 0, $2);
819 TOKEN_GETMAD($1,$$,'o');
820 }
09b1cffe 821 | FUNC '(' optexpr ')' /* print (@args) */
f05e27e5
DM
822 { $$ = convert(IVAL($1), 0, $3);
823 TOKEN_GETMAD($1,$$,'o');
824 TOKEN_GETMAD($2,$$,'(');
825 TOKEN_GETMAD($4,$$,')');
826 }
718a7425 827 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
5a5094bd 828 { SvREFCNT_inc_simple_void(PL_compcv);
bcabcc50 829 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
09b1cffe 830 optlistexpr %prec LSTOP /* ... @bar */
4633a7c4 831 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757
Z
832 op_append_elem(OP_LIST,
833 op_prepend_elem(OP_LIST, $<opval>4, $5), $1));
f05e27e5 834 }
a687059c
LW
835 ;
836
891be019 837/* Names of methods. May use $object->$methodname */
a0d0e21e
LW
838method : METHOD
839 | scalar
840 ;
841
891be019
SC
842/* Some kind of subscripted expression */
843subscripted: star '{' expr ';' '}' /* *main::{something} */
844 /* In this and all the hash accessors, ';' is
845 * provided by the tokeniser */
264e1af3 846 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
53a7735b 847 PL_parser->expect = XOPERATOR;
f05e27e5
DM
848 TOKEN_GETMAD($2,$$,'{');
849 TOKEN_GETMAD($4,$$,';');
850 TOKEN_GETMAD($5,$$,'}');
851 }
891be019 852 | scalar '[' expr ']' /* $array[$element] */
f05e27e5
DM
853 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
854 TOKEN_GETMAD($2,$$,'[');
855 TOKEN_GETMAD($4,$$,']');
856 }
891be019 857 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1
SM
858 { $$ = newBINOP(OP_AELEM, 0,
859 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
860 scalar($4));
861 TOKEN_GETMAD($2,$$,'a');
862 TOKEN_GETMAD($3,$$,'[');
863 TOKEN_GETMAD($5,$$,']');
864 }
891be019 865 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1
SM
866 { $$ = newBINOP(OP_AELEM, 0,
867 ref(newAVREF($1),OP_RV2AV),
f05e27e5
DM
868 scalar($3));
869 TOKEN_GETMAD($2,$$,'[');
870 TOKEN_GETMAD($4,$$,']');
871 }
5a63ceef 872 | scalar '{' expr ';' '}' /* $foo{bar();} */
fad39ff1 873 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
53a7735b 874 PL_parser->expect = XOPERATOR;
f05e27e5
DM
875 TOKEN_GETMAD($2,$$,'{');
876 TOKEN_GETMAD($4,$$,';');
877 TOKEN_GETMAD($5,$$,'}');
878 }
891be019 879 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1
SM
880 { $$ = newBINOP(OP_HELEM, 0,
881 ref(newHVREF($1),OP_RV2HV),
882 jmaybe($4));
53a7735b 883 PL_parser->expect = XOPERATOR;
f05e27e5
DM
884 TOKEN_GETMAD($2,$$,'a');
885 TOKEN_GETMAD($3,$$,'{');
886 TOKEN_GETMAD($5,$$,';');
887 TOKEN_GETMAD($6,$$,'}');
888 }
891be019 889 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1
SM
890 { $$ = newBINOP(OP_HELEM, 0,
891 ref(newHVREF($1),OP_RV2HV),
892 jmaybe($3));
53a7735b 893 PL_parser->expect = XOPERATOR;
f05e27e5
DM
894 TOKEN_GETMAD($2,$$,'{');
895 TOKEN_GETMAD($4,$$,';');
896 TOKEN_GETMAD($5,$$,'}');
897 }
891be019 898 | term ARROW '(' ')' /* $subref->() */
fad39ff1 899 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
900 newCVREF(0, scalar($1)));
901 TOKEN_GETMAD($2,$$,'a');
902 TOKEN_GETMAD($3,$$,'(');
903 TOKEN_GETMAD($4,$$,')');
904 }
891be019 905 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 906 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 907 op_append_elem(OP_LIST, $4,
f05e27e5
DM
908 newCVREF(0, scalar($1))));
909 TOKEN_GETMAD($2,$$,'a');
910 TOKEN_GETMAD($3,$$,'(');
911 TOKEN_GETMAD($5,$$,')');
912 }
fad39ff1 913
417a992d 914 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1 915 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 916 op_append_elem(OP_LIST, $3,
f05e27e5
DM
917 newCVREF(0, scalar($1))));
918 TOKEN_GETMAD($2,$$,'(');
919 TOKEN_GETMAD($4,$$,')');
920 }
417a992d 921 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 922 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5
DM
923 newCVREF(0, scalar($1)));
924 TOKEN_GETMAD($2,$$,'(');
925 TOKEN_GETMAD($3,$$,')');
926 }
9a9798c2 927 | '(' expr ')' '[' expr ']' /* list slice */
f05e27e5
DM
928 { $$ = newSLICEOP(0, $5, $2);
929 TOKEN_GETMAD($1,$$,'(');
930 TOKEN_GETMAD($3,$$,')');
931 TOKEN_GETMAD($4,$$,'[');
932 TOKEN_GETMAD($6,$$,']');
933 }
ea25a9b2
Z
934 | QWLIST '[' expr ']' /* list literal slice */
935 { $$ = newSLICEOP(0, $3, $1);
936 TOKEN_GETMAD($2,$$,'[');
937 TOKEN_GETMAD($4,$$,']');
938 }
9a9798c2 939 | '(' ')' '[' expr ']' /* empty list slice! */
bcabcc50 940 { $$ = newSLICEOP(0, $4, (OP*)NULL);
f05e27e5
DM
941 TOKEN_GETMAD($1,$$,'(');
942 TOKEN_GETMAD($2,$$,')');
943 TOKEN_GETMAD($3,$$,'[');
944 TOKEN_GETMAD($5,$$,']');
945 }
891be019 946 ;
fad39ff1 947
891be019 948/* Binary operators between terms */
f05e27e5
DM
949termbinop: term ASSIGNOP term /* $x = $y */
950 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
951 TOKEN_GETMAD($2,$$,'o');
952 }
891be019 953 | term POWOP term /* $x ** $y */
f05e27e5
DM
954 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
955 TOKEN_GETMAD($2,$$,'o');
956 }
891be019 957 | term MULOP term /* $x * $y, $x x $y */
f05e27e5 958 { if (IVAL($2) != OP_REPEAT)
79072805 959 scalar($1);
f05e27e5
DM
960 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
961 TOKEN_GETMAD($2,$$,'o');
962 }
891be019 963 | term ADDOP term /* $x + $y */
f05e27e5
DM
964 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
965 TOKEN_GETMAD($2,$$,'o');
966 }
891be019 967 | term SHIFTOP term /* $x >> $y, $x << $y */
f05e27e5
DM
968 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
969 TOKEN_GETMAD($2,$$,'o');
970 }
891be019 971 | term RELOP term /* $x > $y, etc. */
f05e27e5
DM
972 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
973 TOKEN_GETMAD($2,$$,'o');
974 }
891be019 975 | term EQOP term /* $x == $y, $x eq $y */
f05e27e5
DM
976 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
977 TOKEN_GETMAD($2,$$,'o');
978 }
891be019 979 | term BITANDOP term /* $x & $y */
f05e27e5
DM
980 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
981 TOKEN_GETMAD($2,$$,'o');
982 }
891be019 983 | term BITOROP term /* $x | $y */
f05e27e5
DM
984 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
985 TOKEN_GETMAD($2,$$,'o');
986 }
891be019 987 | term DOTDOT term /* $x..$y, $x...$y */
f05e27e5
DM
988 {
989 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
035e2bcc 990 DO_MAD({
f05e27e5
DM
991 UNOP *op;
992 op = (UNOP*)$$;
993 op = (UNOP*)op->op_first; /* get to flop */
994 op = (UNOP*)op->op_first; /* get to flip */
995 op = (UNOP*)op->op_first; /* get to range */
996 token_getmad($2,(OP*)op,'o');
035e2bcc 997 })
f05e27e5 998 }
891be019 999 | term ANDAND term /* $x && $y */
f05e27e5
DM
1000 { $$ = newLOGOP(OP_AND, 0, $1, $3);
1001 TOKEN_GETMAD($2,$$,'o');
1002 }
891be019 1003 | term OROR term /* $x || $y */
f05e27e5
DM
1004 { $$ = newLOGOP(OP_OR, 0, $1, $3);
1005 TOKEN_GETMAD($2,$$,'o');
1006 }
c963b151 1007 | term DORDOR term /* $x // $y */
f05e27e5
DM
1008 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
1009 TOKEN_GETMAD($2,$$,'o');
1010 }
891be019 1011 | term MATCHOP term /* $x =~ /$y/ */
f05e27e5
DM
1012 { $$ = bind_match(IVAL($2), $1, $3);
1013 TOKEN_GETMAD($2,
1014 ($$->op_type == OP_NOT
1015 ? ((UNOP*)$$)->op_first : $$),
1016 '~');
1017 }
891be019 1018 ;
8d063cd8 1019
891be019
SC
1020/* Unary operators and terms */
1021termunop : '-' term %prec UMINUS /* -$x */
f05e27e5
DM
1022 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
1023 TOKEN_GETMAD($1,$$,'o');
1024 }
891be019 1025 | '+' term %prec UMINUS /* +$x */
f05e27e5
DM
1026 { $$ = IF_MAD(
1027 newUNOP(OP_NULL, 0, $2),
1028 $2
1029 );
1030 TOKEN_GETMAD($1,$$,'+');
1031 }
891be019 1032 | '!' term /* !$x */
f05e27e5
DM
1033 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1034 TOKEN_GETMAD($1,$$,'o');
1035 }
891be019 1036 | '~' term /* ~$x */
f05e27e5
DM
1037 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
1038 TOKEN_GETMAD($1,$$,'o');
1039 }
891be019 1040 | term POSTINC /* $x++ */
79072805 1041 { $$ = newUNOP(OP_POSTINC, 0,
3ad73efd 1042 op_lvalue(scalar($1), OP_POSTINC));
f05e27e5
DM
1043 TOKEN_GETMAD($2,$$,'o');
1044 }
891be019 1045 | term POSTDEC /* $x-- */
79072805 1046 { $$ = newUNOP(OP_POSTDEC, 0,
3ad73efd 1047 op_lvalue(scalar($1), OP_POSTDEC));
f05e27e5
DM
1048 TOKEN_GETMAD($2,$$,'o');
1049 }
891be019 1050 | PREINC term /* ++$x */
79072805 1051 { $$ = newUNOP(OP_PREINC, 0,
3ad73efd 1052 op_lvalue(scalar($2), OP_PREINC));
f05e27e5
DM
1053 TOKEN_GETMAD($1,$$,'o');
1054 }
891be019 1055 | PREDEC term /* --$x */
79072805 1056 { $$ = newUNOP(OP_PREDEC, 0,
3ad73efd 1057 op_lvalue(scalar($2), OP_PREDEC));
f05e27e5
DM
1058 TOKEN_GETMAD($1,$$,'o');
1059 }
891be019
SC
1060
1061 ;
1062
1063/* Constructors for anonymous data */
1064anonymous: '[' expr ']'
f05e27e5
DM
1065 { $$ = newANONLIST($2);
1066 TOKEN_GETMAD($1,$$,'[');
1067 TOKEN_GETMAD($3,$$,']');
1068 }
891be019 1069 | '[' ']'
bcabcc50 1070 { $$ = newANONLIST((OP*)NULL);
f05e27e5
DM
1071 TOKEN_GETMAD($1,$$,'[');
1072 TOKEN_GETMAD($2,$$,']');
1073 }
891be019 1074 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
f05e27e5
DM
1075 { $$ = newANONHASH($2);
1076 TOKEN_GETMAD($1,$$,'{');
1077 TOKEN_GETMAD($3,$$,';');
1078 TOKEN_GETMAD($4,$$,'}');
1079 }
891be019 1080 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
bcabcc50 1081 { $$ = newANONHASH((OP*)NULL);
f05e27e5
DM
1082 TOKEN_GETMAD($1,$$,'{');
1083 TOKEN_GETMAD($2,$$,';');
1084 TOKEN_GETMAD($3,$$,'}');
1085 }
718a7425 1086 | ANONSUB startanonsub proto subattrlist block %prec '('
5a5094bd 1087 { SvREFCNT_inc_simple_void(PL_compcv);
718a7425 1088 $$ = newANONATTRSUB($2, $3, $4, $5);
f05e27e5 1089 TOKEN_GETMAD($1,$$,'o');
718a7425
DM
1090 OP_GETMAD($3,$$,'s');
1091 OP_GETMAD($4,$$,'a');
f05e27e5 1092 }
891be019
SC
1093
1094 ;
1095
1096/* Things called with "do" */
1097termdo : DO term %prec UNIOP /* do $filename */
123d08c9 1098 { $$ = dofile($2, IVAL($1));
f05e27e5
DM
1099 TOKEN_GETMAD($1,$$,'o');
1100 }
891be019 1101 | DO block %prec '(' /* do { code */
3ad73efd 1102 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));
f05e27e5
DM
1103 TOKEN_GETMAD($1,$$,'D');
1104 }
417a992d 1105 | DO WORD '(' ')' /* do somesub() */
891be019
SC
1106 { $$ = newUNOP(OP_ENTERSUB,
1107 OPf_SPECIAL|OPf_STACKED,
2fcb4757 1108 op_prepend_elem(OP_LIST,
891be019
SC
1109 scalar(newCVREF(
1110 (OPpENTERSUB_AMPER<<8),
1111 scalar($2)
bcabcc50 1112 )),(OP*)NULL)); dep();
f05e27e5
DM
1113 TOKEN_GETMAD($1,$$,'o');
1114 TOKEN_GETMAD($3,$$,'(');
1115 TOKEN_GETMAD($4,$$,')');
1116 }
417a992d 1117 | DO WORD '(' expr ')' /* do somesub(@args) */
891be019
SC
1118 { $$ = newUNOP(OP_ENTERSUB,
1119 OPf_SPECIAL|OPf_STACKED,
2fcb4757 1120 op_append_elem(OP_LIST,
891be019
SC
1121 $4,
1122 scalar(newCVREF(
1123 (OPpENTERSUB_AMPER<<8),
1124 scalar($2)
f05e27e5
DM
1125 )))); dep();
1126 TOKEN_GETMAD($1,$$,'o');
1127 TOKEN_GETMAD($3,$$,'(');
1128 TOKEN_GETMAD($5,$$,')');
1129 }
417a992d 1130 | DO scalar '(' ')' /* do $subref () */
891be019 1131 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
2fcb4757 1132 op_prepend_elem(OP_LIST,
bcabcc50 1133 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
f05e27e5
DM
1134 TOKEN_GETMAD($1,$$,'o');
1135 TOKEN_GETMAD($3,$$,'(');
1136 TOKEN_GETMAD($4,$$,')');
1137 }
417a992d 1138 | DO scalar '(' expr ')' /* do $subref (@args) */
891be019 1139 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
2fcb4757 1140 op_prepend_elem(OP_LIST,
891be019 1141 $4,
f05e27e5
DM
1142 scalar(newCVREF(0,scalar($2))))); dep();
1143 TOKEN_GETMAD($1,$$,'o');
1144 TOKEN_GETMAD($3,$$,'(');
1145 TOKEN_GETMAD($5,$$,')');
1146 }
891be019
SC
1147
1148 ;
1149
1150term : termbinop
1151 | termunop
1152 | anonymous
1153 | termdo
e9bdcc27 1154 | term '?' term ':' term
f05e27e5
DM
1155 { $$ = newCONDOP(0, $1, $3, $5);
1156 TOKEN_GETMAD($2,$$,'?');
1157 TOKEN_GETMAD($4,$$,':');
1158 }
891be019 1159 | REFGEN term /* \$x, \@y, \%z */
3ad73efd 1160 { $$ = newUNOP(OP_REFGEN, 0, op_lvalue($2,OP_REFGEN));
f05e27e5
DM
1161 TOKEN_GETMAD($1,$$,'o');
1162 }
09bef843
SB
1163 | myattrterm %prec UNIOP
1164 { $$ = $1; }
1165 | LOCAL term %prec UNIOP
f05e27e5 1166 { $$ = localize($2,IVAL($1));
dde83331 1167 TOKEN_GETMAD($1,$$,'k');
f05e27e5 1168 }
a0d0e21e 1169 | '(' expr ')'
f05e27e5
DM
1170 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1171 TOKEN_GETMAD($1,$$,'(');
1172 TOKEN_GETMAD($3,$$,')');
1173 }
ea25a9b2
Z
1174 | QWLIST
1175 { $$ = IF_MAD(newUNOP(OP_NULL,0,$1), $1); }
8d063cd8 1176 | '(' ')'
f05e27e5
DM
1177 { $$ = sawparens(newNULLLIST());
1178 TOKEN_GETMAD($1,$$,'(');
1179 TOKEN_GETMAD($2,$$,')');
1180 }
79072805 1181 | scalar %prec '('
8d063cd8 1182 { $$ = $1; }
79072805 1183 | star %prec '('
8d063cd8 1184 { $$ = $1; }
79072805 1185 | hsh %prec '('
8d063cd8 1186 { $$ = $1; }
79072805 1187 | ary %prec '('
8d063cd8 1188 { $$ = $1; }
891be019 1189 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1190 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1
SM
1191 | subscripted
1192 { $$ = $1; }
891be019 1193 | ary '[' expr ']' /* array slice */
2fcb4757 1194 { $$ = op_prepend_elem(OP_ASLICE,
79072805 1195 newOP(OP_PUSHMARK, 0),
79072805
LW
1196 newLISTOP(OP_ASLICE, 0,
1197 list($3),
f05e27e5
DM
1198 ref($1, OP_ASLICE)));
1199 TOKEN_GETMAD($2,$$,'[');
1200 TOKEN_GETMAD($4,$$,']');
1201 }
891be019 1202 | ary '{' expr ';' '}' /* @hash{@keys} */
2fcb4757 1203 { $$ = op_prepend_elem(OP_HSLICE,
79072805 1204 newOP(OP_PUSHMARK, 0),
79072805
LW
1205 newLISTOP(OP_HSLICE, 0,
1206 list($3),
a0d0e21e 1207 ref(oopsHV($1), OP_HSLICE)));
53a7735b 1208 PL_parser->expect = XOPERATOR;
f05e27e5
DM
1209 TOKEN_GETMAD($2,$$,'{');
1210 TOKEN_GETMAD($4,$$,';');
1211 TOKEN_GETMAD($5,$$,'}');
1212 }
79072805
LW
1213 | THING %prec '('
1214 { $$ = $1; }
891be019 1215 | amper /* &foo; */
c07a80fd 1216 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
417a992d 1217 | amper '(' ')' /* &foo() */
f05e27e5
DM
1218 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1219 TOKEN_GETMAD($2,$$,'(');
1220 TOKEN_GETMAD($3,$$,')');
1221 }
417a992d 1222 | amper '(' expr ')' /* &foo(@args) */
f05e27e5
DM
1223 {
1224 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1225 op_append_elem(OP_LIST, $3, scalar($1)));
035e2bcc 1226 DO_MAD({
f05e27e5
DM
1227 OP* op = $$;
1228 if (op->op_type == OP_CONST) { /* defeat const fold */
1229 op = (OP*)op->op_madprop->mad_val;
1230 }
1231 token_getmad($2,op,'(');
1232 token_getmad($4,op,')');
035e2bcc 1233 })
f05e27e5 1234 }
09b1cffe 1235 | NOAMP WORD optlistexpr /* foo(@args) */
a0d0e21e 1236 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1237 op_append_elem(OP_LIST, $3, scalar($2)));
f05e27e5
DM
1238 TOKEN_GETMAD($1,$$,'o');
1239 }
891be019 1240 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
f05e27e5
DM
1241 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1242 PL_hints |= HINT_BLOCK_SCOPE;
1243 TOKEN_GETMAD($1,$$,'o');
1244 }
a0d0e21e 1245 | LOOPEX term
f05e27e5
DM
1246 { $$ = newLOOPEX(IVAL($1),$2);
1247 TOKEN_GETMAD($1,$$,'o');
1248 }
09b1cffe 1249 | NOTOP listexpr /* not $foo */
f05e27e5
DM
1250 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1251 TOKEN_GETMAD($1,$$,'o');
1252 }
891be019 1253 | UNIOP /* Unary op, $_ implied */
f05e27e5
DM
1254 { $$ = newOP(IVAL($1), 0);
1255 TOKEN_GETMAD($1,$$,'o');
1256 }
1257 | UNIOP block /* eval { foo }* */
1258 { $$ = newUNOP(IVAL($1), 0, $2);
1259 TOKEN_GETMAD($1,$$,'o');
1260 }
891be019 1261 | UNIOP term /* Unary op */
f05e27e5
DM
1262 { $$ = newUNOP(IVAL($1), 0, $2);
1263 TOKEN_GETMAD($1,$$,'o');
1264 }
d2fdf8fd
RGS
1265 | REQUIRE /* require, $_ implied */
1266 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1267 TOKEN_GETMAD($1,$$,'o');
1268 }
1269 | REQUIRE term /* require Foo */
1270 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1271 TOKEN_GETMAD($1,$$,'o');
1272 }
3cd0a11a
RGS
1273 | UNIOPSUB
1274 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1275 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 1276 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
2fcb4757 1277 op_append_elem(OP_LIST, $2, scalar($1))); }
891be019 1278 | FUNC0 /* Nullary operator */
f05e27e5
DM
1279 { $$ = newOP(IVAL($1), 0);
1280 TOKEN_GETMAD($1,$$,'o');
1281 }
ae986130 1282 | FUNC0 '(' ')'
f05e27e5
DM
1283 { $$ = newOP(IVAL($1), 0);
1284 TOKEN_GETMAD($1,$$,'o');
1285 TOKEN_GETMAD($2,$$,'(');
1286 TOKEN_GETMAD($3,$$,')');
1287 }
7eb971ee
FC
1288 | FUNC0OP /* Same as above, but op created in toke.c */
1289 { $$ = $1; }
1290 | FUNC0OP '(' ')'
1291 { $$ = $1;
1292 TOKEN_GETMAD($2,$$,'(');
1293 TOKEN_GETMAD($3,$$,')');
1294 }
891be019 1295 | FUNC0SUB /* Sub treated as nullop */
28757baa 1296 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 1297 scalar($1)); }
891be019 1298 | FUNC1 '(' ')' /* not () */
f05e27e5
DM
1299 { $$ = (IVAL($1) == OP_NOT)
1300 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1301 : newOP(IVAL($1), OPf_SPECIAL);
1302
1303 TOKEN_GETMAD($1,$$,'o');
1304 TOKEN_GETMAD($2,$$,'(');
1305 TOKEN_GETMAD($3,$$,')');
1306 }
891be019 1307 | FUNC1 '(' expr ')' /* not($foo) */
f05e27e5
DM
1308 { $$ = newUNOP(IVAL($1), 0, $3);
1309 TOKEN_GETMAD($1,$$,'o');
1310 TOKEN_GETMAD($2,$$,'(');
1311 TOKEN_GETMAD($4,$$,')');
1312 }
d63c20f2
DM
1313 | PMFUNC /* m//, s///, qr//, tr/// */
1314 {
1315 if ( $1->op_type != OP_TRANS
1316 && $1->op_type != OP_TRANSR
1317 && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV))
1318 {
1319 $<ival>$ = start_subparse(FALSE, CVf_ANON);
1320 SAVEFREESV(PL_compcv);
1321 } else
1322 $<ival>$ = 0;
1323 }
1324 '(' listexpr ')'
1325 { $$ = pmruntime($1, $4, 1, $<ival>2);
1326 TOKEN_GETMAD($3,$$,'(');
1327 TOKEN_GETMAD($5,$$,')');
f05e27e5 1328 }
79072805 1329 | WORD
378cc40b 1330 | listop
be25f609 1331 | YADAYADA
1332 {
1333 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1334 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
950f2ea2 1335 TOKEN_GETMAD($1,$$,'X');
be25f609 1336 }
88e1f1a2 1337 | PLUGEXPR
8d063cd8
LW
1338 ;
1339
891be019 1340/* "my" declarations, with optional attributes */
09bef843 1341myattrterm: MY myterm myattrlist
f05e27e5
DM
1342 { $$ = my_attrs($2,$3);
1343 DO_MAD(
1344 token_getmad($1,$$,'d');
1345 append_madprops($3->op_madprop, $$, 'a');
1346 $3->op_madprop = 0;
1347 )
1348 }
09bef843 1349 | MY myterm
f05e27e5
DM
1350 { $$ = localize($2,IVAL($1));
1351 TOKEN_GETMAD($1,$$,'d');
1352 }
09bef843
SB
1353 ;
1354
891be019 1355/* Things that can be "my"'d */
09bef843 1356myterm : '(' expr ')'
f05e27e5
DM
1357 { $$ = sawparens($2);
1358 TOKEN_GETMAD($1,$$,'(');
1359 TOKEN_GETMAD($3,$$,')');
1360 }
09bef843 1361 | '(' ')'
f05e27e5
DM
1362 { $$ = sawparens(newNULLLIST());
1363 TOKEN_GETMAD($1,$$,'(');
1364 TOKEN_GETMAD($2,$$,')');
1365 }
09bef843
SB
1366 | scalar %prec '('
1367 { $$ = $1; }
1368 | hsh %prec '('
1369 { $$ = $1; }
1370 | ary %prec '('
1371 { $$ = $1; }
1372 ;
1373
891be019 1374/* Basic list expressions */
09b1cffe 1375optlistexpr: /* NULL */ %prec PREC_LOW
bcabcc50 1376 { $$ = (OP*)NULL; }
09b1cffe 1377 | listexpr %prec PREC_LOW
a0d0e21e
LW
1378 { $$ = $1; }
1379 ;
1380
09b1cffe 1381optexpr: /* NULL */
bcabcc50 1382 { $$ = (OP*)NULL; }
79072805
LW
1383 | expr
1384 { $$ = $1; }
1385 ;
1386
891be019
SC
1387/* A little bit of trickery to make "for my $foo (@bar)" actually be
1388 lexical */
55497cff 1389my_scalar: scalar
12bd6ede 1390 { PL_parser->in_my = 0; $$ = my($1); }
55497cff
PP
1391 ;
1392
79072805 1393amper : '&' indirob
f05e27e5
DM
1394 { $$ = newCVREF(IVAL($1),$2);
1395 TOKEN_GETMAD($1,$$,'&');
1396 }
a687059c
LW
1397 ;
1398
79072805 1399scalar : '$' indirob
f05e27e5
DM
1400 { $$ = newSVREF($2);
1401 TOKEN_GETMAD($1,$$,'$');
1402 }
a687059c
LW
1403 ;
1404
79072805 1405ary : '@' indirob
f05e27e5
DM
1406 { $$ = newAVREF($2);
1407 TOKEN_GETMAD($1,$$,'@');
1408 }
79072805
LW
1409 ;
1410
1411hsh : '%' indirob
f05e27e5
DM
1412 { $$ = newHVREF($2);
1413 TOKEN_GETMAD($1,$$,'%');
1414 }
79072805
LW
1415 ;
1416
1417arylen : DOLSHARP indirob
f05e27e5
DM
1418 { $$ = newAVREF($2);
1419 TOKEN_GETMAD($1,$$,'l');
1420 }
79072805
LW
1421 ;
1422
1423star : '*' indirob
f05e27e5
DM
1424 { $$ = newGVREF(0,$2);
1425 TOKEN_GETMAD($1,$$,'*');
1426 }
79072805
LW
1427 ;
1428
891be019 1429/* Indirect objects */
79072805
LW
1430indirob : WORD
1431 { $$ = scalar($1); }
fad39ff1 1432 | scalar %prec PREC_LOW
f05e27e5 1433 { $$ = scalar($1); }
79072805 1434 | block
3ad73efd 1435 { $$ = op_scope($1); }
79072805 1436
93a17b20
LW
1437 | PRIVATEREF
1438 { $$ = $1; }
8d063cd8 1439 ;