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