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