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