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