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