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