Use simpler language.
[p5sagit/p5-mst-13.2.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 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 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 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 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 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 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 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 44 */
45
0de566d7 46/* Make the parser re-entrant. */
8d063cd8 47
0de566d7 48%pure_parser
8d063cd8 49
f05e27e5 50/* FIXME for MAD - is the new mintro on while and until important? */
51
0de566d7 52%start prog
9d116dd7 53
8d063cd8 54%union {
d5c6462e 55 I32 ival; /* __DEFAULT__ (marker for regen_perly.pl;
56 must always be 1st union member) */
79072805 57 char *pval;
58 OP *opval;
59 GV *gvval;
f05e27e5 60#ifdef PERL_IN_MADLY_C
61 TOKEN* p_tkval;
123d08c9 62 TOKEN* i_tkval;
f05e27e5 63#else
64 char *p_tkval;
123d08c9 65 I32 i_tkval;
66#endif
67#ifdef PERL_MAD
68 TOKEN* tkval;
f05e27e5 69#endif
8d063cd8 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 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 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 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 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 106%nonassoc LOOPEX
107
123d08c9 108%left <i_tkval> OROP DOROP
109%left <i_tkval> ANDOP
110%right <i_tkval> NOTOP
36477c24 111%nonassoc LSTOP LSTOPSUB
123d08c9 112%left <i_tkval> ','
113%right <i_tkval> ASSIGNOP
114%right <i_tkval> '?' ':'
be25f609 115%nonassoc DOTDOT YADAYADA
123d08c9 116%left <i_tkval> OROR DORDOR
117%left <i_tkval> ANDAND
118%left <i_tkval> BITOROP
119%left <i_tkval> BITANDOP
a687059c 120%nonassoc EQOP
121%nonassoc RELOP
36477c24 122%nonassoc UNIOP UNIOPSUB
20515881 123%nonassoc REQUIRE
123d08c9 124%left <i_tkval> SHIFTOP
a687059c 125%left ADDOP
126%left MULOP
123d08c9 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 138%% /* RULES */
139
891be019 140/* The whole program */
ecb2f335 141prog : progstart
ae986130 142 /*CONTINUED*/ lineseq
ecb2f335 143 { $$ = $1; newPROG(block_end($1,$2)); }
8d063cd8 144 ;
145
891be019 146/* An ordinary block */
a687059c 147block : '{' remember lineseq '}'
53a7735b 148 { if (PL_parser->copline > (line_t)IVAL($1))
149 PL_parser->copline = (line_t)IVAL($1);
f05e27e5 150 $$ = block_end($2, $3);
151 TOKEN_GETMAD($1,$$,'{');
152 TOKEN_GETMAD($4,$$,'}');
153 }
a0d0e21e 154 ;
155
55497cff 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 162 ;
163
ecb2f335 164progstart:
165 {
53a7735b 166 PL_parser->expect = XSTATE; $$ = block_start(TRUE);
ecb2f335 167 }
168 ;
169
170
bbce6d69 171mblock : '{' mremember lineseq '}'
53a7735b 172 { if (PL_parser->copline > (line_t)IVAL($1))
173 PL_parser->copline = (line_t)IVAL($1);
f05e27e5 174 $$ = block_end($2, $3);
175 TOKEN_GETMAD($1,$$,'{');
176 TOKEN_GETMAD($4,$$,'}');
177 }
55497cff 178 ;
179
180mremember: /* NULL */ /* start a partial lexical scope */
181 { $$ = block_start(FALSE); }
8d063cd8 182 ;
183
891be019 184/* A collection of "lines" in the program */
8d063cd8 185lineseq : /* NULL */
bcabcc50 186 { $$ = (OP*)NULL; }
79072805 187 | lineseq decl
f05e27e5 188 {
189 $$ = IF_MAD(
190 append_list(OP_LINESEQ,
191 (LISTOP*)$1, (LISTOP*)$2),
192 $1);
193 }
503de470 194 | lineseq line
195 { $$ = append_list(OP_LINESEQ,
196 (LISTOP*)$1, (LISTOP*)$2);
3280af22 197 PL_pad_reset_pending = TRUE;
503de470 198 if ($1 && $2)
199 PL_hints |= HINT_BLOCK_SCOPE;
200 }
8d063cd8 201 ;
202
891be019 203/* A "line" in the program */
79072805 204line : label cond
f05e27e5 205 { $$ = newSTATEOP(0, PVAL($1), $2);
206 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
8d063cd8 207 | loop /* loops add their own labels */
0d863452 208 | switch /* ... and so do switches */
209 { $$ = $1; }
210 | label case
f05e27e5 211 { $$ = newSTATEOP(0, PVAL($1), $2); }
8d063cd8 212 | label ';'
f05e27e5 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 224 TOKEN_FREE($1);
225 TOKEN_GETMAD($2,$$,';');
226 }
53a7735b 227 PL_parser->expect = XSTATE;
f05e27e5 228 }
8d063cd8 229 | label sideff ';'
f05e27e5 230 {
231 $$ = newSTATEOP(0, PVAL($1), $2);
53a7735b 232 PL_parser->expect = XSTATE;
035e2bcc 233 DO_MAD({
f05e27e5 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 245 | label PLUGSTMT
246 { $$ = newSTATEOP(0, PVAL($1), $2); }
8d063cd8 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 255 { $$ = newLOGOP(OP_AND, 0, $3, $1);
256 TOKEN_GETMAD($2,$$,'i');
257 }
a687059c 258 | expr UNLESS expr
f05e27e5 259 { $$ = newLOGOP(OP_OR, 0, $3, $1);
260 TOKEN_GETMAD($2,$$,'i');
261 }
a687059c 262 | expr WHILE expr
f05e27e5 263 { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1);
264 TOKEN_GETMAD($2,$$,'w');
265 }
55497cff 266 | expr UNTIL iexpr
f05e27e5 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 273 TOKEN_GETMAD($2,((LISTOP*)$$)->op_first->op_sibling,'w');
274 }
f20dcd76 275 | expr WHEN expr
276 { $$ = newWHENOP($3, scope($1)); }
79072805 277 ;
278
891be019 279/* else and elsif blocks */
79072805 280else : /* NULL */
bcabcc50 281 { $$ = (OP*)NULL; }
55497cff 282 | ELSE mblock
f05e27e5 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 289 PL_hints |= HINT_BLOCK_SCOPE;
290 TOKEN_GETMAD($1,$$,'I');
291 TOKEN_GETMAD($2,$$,'(');
292 TOKEN_GETMAD($4,$$,')');
293 }
79072805 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 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 308 newCONDOP(0, $4, scope($6), $7));
309 TOKEN_GETMAD($1,$$,'I');
310 TOKEN_GETMAD($2,$$,'(');
311 TOKEN_GETMAD($5,$$,')');
312 }
79072805 313 ;
314
0d863452 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 327 { $$ = scope($2);
328 TOKEN_GETMAD($1,$$,'o');
329 }
79072805 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 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 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 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 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 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 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 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 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 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 412 if ($5) {
413 forop = append_elem(OP_LINESEQ,
63031daf 414 newSTATEOP(0, CopLABEL_alloc($1), $5),
36c66720 415 forop);
416 }
417
f05e27e5 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 423 newWHILEOP(0, 1, (LOOP*)(OP*)NULL,
424 NOLINE, (OP*)NULL, $2, $3, 0));
f05e27e5 425 TOKEN_GETMAD($1,((LISTOP*)$$)->op_first,'L'); }
8d063cd8 426 ;
427
0d863452 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 433 newGIVENOP($6, scope($8),
434 (PADOFFSET) $5) )); }
435 ;
436
a034e688 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 446 | sideff
447 ;
448
891be019 449/* Boolean expression */
8d063cd8 450texpr : /* NULL means true */
f05e27e5 451 { YYSTYPE tmplval;
452 (void)scan_num("1", &tmplval);
453 $$ = tmplval.opval; }
8d063cd8 454 | expr
455 ;
456
891be019 457/* Inverted boolean expression */
55497cff 458iexpr : expr
459 { $$ = invert(scalar($1)); }
460 ;
461
891be019 462/* Expression with its own lexical scope */
55497cff 463mexpr : expr
bbce6d69 464 { $$ = $1; intro_my(); }
465 ;
466
467mnexpr : nexpr
468 { $$ = $1; intro_my(); }
55497cff 469 ;
470
55497cff 471miexpr : iexpr
bbce6d69 472 { $$ = $1; intro_my(); }
55497cff 473 ;
474
891be019 475/* Optional "MAIN:"-style loop labels */
8d063cd8 476label : /* empty */
f05e27e5 477 {
478#ifdef MAD
479 YYSTYPE tmplval;
1a9a51d4 480 tmplval.pval = NULL;
f05e27e5 481 $$ = newTOKEN(OP_NULL, tmplval, 0);
482#else
1a9a51d4 483 $$ = NULL;
f05e27e5 484#endif
485 }
32c2e4fb 486 | LABEL
8d063cd8 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 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 511 ;
512
718a7425 513format : FORMAT startformsub formname block
421f30ed 514 {
515 CV *fmtcv = PL_compcv;
516 SvREFCNT_inc_simple_void(PL_compcv);
f05e27e5 517#ifdef MAD
718a7425 518 $$ = newFORM($2, $3, $4);
f05e27e5 519 prepend_madprops($1->tk_mad, $$, 'F');
520 $1->tk_mad = 0;
521 token_free($1);
522#else
718a7425 523 newFORM($2, $3, $4);
bcabcc50 524 $$ = (OP*)NULL;
f05e27e5 525#endif
421f30ed 526 if (CvOUTSIDE(fmtcv) && !CvUNIQUE(CvOUTSIDE(fmtcv))) {
527 SvREFCNT_inc_simple_void(fmtcv);
528 pad_add_anon((SV*)fmtcv, OP_NULL);
529 }
f05e27e5 530 }
44a8e56a 531 ;
532
533formname: WORD { $$ = $1; }
bcabcc50 534 | /* NULL */ { $$ = (OP*)NULL; }
8d063cd8 535 ;
536
891be019 537/* Unimplemented "my sub foo { }" */
718a7425 538mysubrout: MYSUB startsub subname proto subattrlist subbody
5a5094bd 539 { SvREFCNT_inc_simple_void(PL_compcv);
f05e27e5 540#ifdef MAD
718a7425 541 $$ = newMYSUB($2, $3, $4, $5, $6);
f05e27e5 542 token_getmad($1,$$,'d');
543#else
718a7425 544 newMYSUB($2, $3, $4, $5, $6);
bcabcc50 545 $$ = (OP*)NULL;
f05e27e5 546#endif
547 }
09bef843 548 ;
549
891be019 550/* Subroutine definition */
718a7425 551subrout : SUB startsub subname proto subattrlist subbody
5a5094bd 552 { SvREFCNT_inc_simple_void(PL_compcv);
f05e27e5 553#ifdef MAD
035e2bcc 554 {
555 OP* o = newSVOP(OP_ANONCODE, 0,
556 (SV*)newATTRSUB($2, $3, $4, $5, $6));
557 $$ = newOP(OP_NULL,0);
558 op_getmad(o,$$,'&');
559 op_getmad($3,$$,'n');
560 op_getmad($4,$$,'s');
561 op_getmad($5,$$,'a');
562 token_getmad($1,$$,'d');
563 append_madprops($6->op_madprop, $$, 0);
564 $6->op_madprop = 0;
565 }
f05e27e5 566#else
718a7425 567 newATTRSUB($2, $3, $4, $5, $6);
bcabcc50 568 $$ = (OP*)NULL;
f05e27e5 569#endif
570 }
28757baa 571 ;
572
fa83b5b6 573startsub: /* NULL */ /* start a regular subroutine scope */
a8ff2fa6 574 { $$ = start_subparse(FALSE, 0);
575 SAVEFREESV(PL_compcv); }
f05e27e5 576
28757baa 577 ;
578
579startanonsub: /* NULL */ /* start an anonymous subroutine scope */
a8ff2fa6 580 { $$ = start_subparse(FALSE, CVf_ANON);
581 SAVEFREESV(PL_compcv); }
28757baa 582 ;
583
44a8e56a 584startformsub: /* NULL */ /* start a format subroutine scope */
a8ff2fa6 585 { $$ = start_subparse(TRUE, 0);
586 SAVEFREESV(PL_compcv); }
44a8e56a 587 ;
588
891be019 589/* Name of a subroutine - must be a bareword, could be special */
2596d9fe 590subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv);
e858de61 591 if (strEQ(name, "BEGIN") || strEQ(name, "END")
3c10abe3 592 || strEQ(name, "INIT") || strEQ(name, "CHECK")
593 || strEQ(name, "UNITCHECK"))
1aff0e91 594 CvSPECIAL_on(PL_compcv);
28757baa 595 $$ = $1; }
a0d0e21e 596 ;
597
891be019 598/* Subroutine prototype */
4633a7c4 599proto : /* NULL */
bcabcc50 600 { $$ = (OP*)NULL; }
4633a7c4 601 | THING
602 ;
28757baa 603
891be019 604/* Optional list of subroutine attributes */
09bef843 605subattrlist: /* NULL */
bcabcc50 606 { $$ = (OP*)NULL; }
09bef843 607 | COLONATTR THING
f05e27e5 608 { $$ = $2;
609 TOKEN_GETMAD($1,$$,':');
610 }
09bef843 611 | COLONATTR
f05e27e5 612 { $$ = IF_MAD(
613 newOP(OP_NULL, 0),
bcabcc50 614 (OP*)NULL
f05e27e5 615 );
616 TOKEN_GETMAD($1,$$,':');
617 }
09bef843 618 ;
619
891be019 620/* List of attributes for a "my" variable declaration */
09bef843 621myattrlist: COLONATTR THING
f05e27e5 622 { $$ = $2;
623 TOKEN_GETMAD($1,$$,':');
624 }
09bef843 625 | COLONATTR
f05e27e5 626 { $$ = IF_MAD(
627 newOP(OP_NULL, 0),
bcabcc50 628 (OP*)NULL
f05e27e5 629 );
630 TOKEN_GETMAD($1,$$,':');
631 }
09bef843 632 ;
633
891be019 634/* Subroutine body - either null or a block */
28757baa 635subbody : block { $$ = $1; }
f05e27e5 636 | ';' { $$ = IF_MAD(
637 newOP(OP_NULL,0),
bcabcc50 638 (OP*)NULL
f05e27e5 639 );
53a7735b 640 PL_parser->expect = XSTATE;
f05e27e5 641 TOKEN_GETMAD($1,$$,';');
642 }
8d063cd8 643 ;
644
6fa4d285 645package : PACKAGE WORD WORD ';'
f05e27e5 646 {
647#ifdef MAD
0f811330 648 $$ = package($3);
649 token_getmad($1,$$,'o');
650 if ($2)
651 package_version($2);
652 token_getmad($4,$$,';');
f05e27e5 653#else
6fa4d285 654 package($3);
0f811330 655 if ($2)
656 package_version($2);
bcabcc50 657 $$ = (OP*)NULL;
0f811330 658#endif
f05e27e5 659 }
a687059c 660 ;
661
718a7425 662use : USE startsub
1aff0e91 663 { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ }
28757baa 664 WORD WORD listexpr ';'
5a5094bd 665 { SvREFCNT_inc_simple_void(PL_compcv);
f05e27e5 666#ifdef MAD
718a7425 667 $$ = utilize(IVAL($1), $2, $4, $5, $6);
f05e27e5 668 token_getmad($1,$$,'o');
718a7425 669 token_getmad($7,$$,';');
5486870f 670 if (PL_parser->rsfp_filters &&
671 AvFILLp(PL_parser->rsfp_filters) >= 0)
a22f28a4 672 append_madprops(newMADPROP('!', MAD_NULL, NULL, 0), $$, 0);
f05e27e5 673#else
718a7425 674 utilize(IVAL($1), $2, $4, $5, $6);
bcabcc50 675 $$ = (OP*)NULL;
f05e27e5 676#endif
677 }
85e6fe83 678 ;
679
891be019 680/* Ordinary expressions; logical combinations */
a0d0e21e 681expr : expr ANDOP expr
f05e27e5 682 { $$ = newLOGOP(OP_AND, 0, $1, $3);
683 TOKEN_GETMAD($2,$$,'o');
684 }
a0d0e21e 685 | expr OROP expr
f05e27e5 686 { $$ = newLOGOP(IVAL($2), 0, $1, $3);
687 TOKEN_GETMAD($2,$$,'o');
688 }
c963b151 689 | expr DOROP expr
f05e27e5 690 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
691 TOKEN_GETMAD($2,$$,'o');
692 }
fad39ff1 693 | argexpr %prec PREC_LOW
a0d0e21e 694 ;
695
891be019 696/* Expressions are a list of terms joined by commas */
a0d0e21e 697argexpr : argexpr ','
f05e27e5 698 {
699#ifdef MAD
700 OP* op = newNULLLIST();
701 token_getmad($2,op,',');
702 $$ = append_elem(OP_LIST, $1, op);
703#else
704 $$ = $1;
705#endif
706 }
a0d0e21e 707 | argexpr ',' term
f05e27e5 708 {
29522234 709 OP* term = $3;
f05e27e5 710 DO_MAD(
29522234 711 term = newUNOP(OP_NULL, 0, term);
712 token_getmad($2,term,',');
f05e27e5 713 )
29522234 714 $$ = append_elem(OP_LIST, $1, term);
f05e27e5 715 }
fad39ff1 716 | term %prec PREC_LOW
8d063cd8 717 ;
718
891be019 719/* List operators */
ad4673e5 720listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */
f05e27e5 721 { $$ = convert(IVAL($1), OPf_STACKED,
722 prepend_elem(OP_LIST, newGVREF(IVAL($1),$2), $3) );
723 TOKEN_GETMAD($1,$$,'o');
724 }
891be019 725 | FUNC '(' indirob expr ')' /* print ($fh @args */
f05e27e5 726 { $$ = convert(IVAL($1), OPf_STACKED,
727 prepend_elem(OP_LIST, newGVREF(IVAL($1),$3), $4) );
728 TOKEN_GETMAD($1,$$,'o');
729 TOKEN_GETMAD($2,$$,'(');
730 TOKEN_GETMAD($5,$$,')');
731 }
891be019 732 | term ARROW method '(' listexprcom ')' /* $foo->bar(list) */
4633a7c4 733 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 734 append_elem(OP_LIST,
55497cff 735 prepend_elem(OP_LIST, scalar($1), $5),
f05e27e5 736 newUNOP(OP_METHOD, 0, $3)));
737 TOKEN_GETMAD($2,$$,'A');
738 TOKEN_GETMAD($4,$$,'(');
739 TOKEN_GETMAD($6,$$,')');
740 }
891be019 741 | term ARROW method /* $foo->bar */
b1524f17 742 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
743 append_elem(OP_LIST, scalar($1),
f05e27e5 744 newUNOP(OP_METHOD, 0, $3)));
745 TOKEN_GETMAD($2,$$,'A');
746 }
891be019 747 | METHOD indirob listexpr /* new Class @args */
4633a7c4 748 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 749 append_elem(OP_LIST,
4633a7c4 750 prepend_elem(OP_LIST, $2, $3),
f05e27e5 751 newUNOP(OP_METHOD, 0, $1)));
752 }
891be019 753 | FUNCMETH indirob '(' listexprcom ')' /* method $object (@args) */
4633a7c4 754 { $$ = convert(OP_ENTERSUB, OPf_STACKED,
a0d0e21e 755 append_elem(OP_LIST,
4633a7c4 756 prepend_elem(OP_LIST, $2, $4),
f05e27e5 757 newUNOP(OP_METHOD, 0, $1)));
758 TOKEN_GETMAD($3,$$,'(');
759 TOKEN_GETMAD($5,$$,')');
760 }
891be019 761 | LSTOP listexpr /* print @args */
f05e27e5 762 { $$ = convert(IVAL($1), 0, $2);
763 TOKEN_GETMAD($1,$$,'o');
764 }
891be019 765 | FUNC '(' listexprcom ')' /* print (@args) */
f05e27e5 766 { $$ = convert(IVAL($1), 0, $3);
767 TOKEN_GETMAD($1,$$,'o');
768 TOKEN_GETMAD($2,$$,'(');
769 TOKEN_GETMAD($4,$$,')');
770 }
718a7425 771 | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */
5a5094bd 772 { SvREFCNT_inc_simple_void(PL_compcv);
bcabcc50 773 $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); }
891be019 774 listexpr %prec LSTOP /* ... @bar */
4633a7c4 775 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
28757baa 776 append_elem(OP_LIST,
29522234 777 prepend_elem(OP_LIST, $<opval>4, $5), $1));
f05e27e5 778 }
a687059c 779 ;
780
891be019 781/* Names of methods. May use $object->$methodname */
a0d0e21e 782method : METHOD
783 | scalar
784 ;
785
891be019 786/* Some kind of subscripted expression */
787subscripted: star '{' expr ';' '}' /* *main::{something} */
788 /* In this and all the hash accessors, ';' is
789 * provided by the tokeniser */
264e1af3 790 { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3));
53a7735b 791 PL_parser->expect = XOPERATOR;
f05e27e5 792 TOKEN_GETMAD($2,$$,'{');
793 TOKEN_GETMAD($4,$$,';');
794 TOKEN_GETMAD($5,$$,'}');
795 }
891be019 796 | scalar '[' expr ']' /* $array[$element] */
f05e27e5 797 { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3));
798 TOKEN_GETMAD($2,$$,'[');
799 TOKEN_GETMAD($4,$$,']');
800 }
891be019 801 | term ARROW '[' expr ']' /* somearef->[$element] */
fad39ff1 802 { $$ = newBINOP(OP_AELEM, 0,
803 ref(newAVREF($1),OP_RV2AV),
f05e27e5 804 scalar($4));
805 TOKEN_GETMAD($2,$$,'a');
806 TOKEN_GETMAD($3,$$,'[');
807 TOKEN_GETMAD($5,$$,']');
808 }
891be019 809 | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */
fad39ff1 810 { $$ = newBINOP(OP_AELEM, 0,
811 ref(newAVREF($1),OP_RV2AV),
f05e27e5 812 scalar($3));
813 TOKEN_GETMAD($2,$$,'[');
814 TOKEN_GETMAD($4,$$,']');
815 }
891be019 816 | scalar '{' expr ';' '}' /* $foo->{bar();} */
fad39ff1 817 { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3));
53a7735b 818 PL_parser->expect = XOPERATOR;
f05e27e5 819 TOKEN_GETMAD($2,$$,'{');
820 TOKEN_GETMAD($4,$$,';');
821 TOKEN_GETMAD($5,$$,'}');
822 }
891be019 823 | term ARROW '{' expr ';' '}' /* somehref->{bar();} */
fad39ff1 824 { $$ = newBINOP(OP_HELEM, 0,
825 ref(newHVREF($1),OP_RV2HV),
826 jmaybe($4));
53a7735b 827 PL_parser->expect = XOPERATOR;
f05e27e5 828 TOKEN_GETMAD($2,$$,'a');
829 TOKEN_GETMAD($3,$$,'{');
830 TOKEN_GETMAD($5,$$,';');
831 TOKEN_GETMAD($6,$$,'}');
832 }
891be019 833 | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */
fad39ff1 834 { $$ = newBINOP(OP_HELEM, 0,
835 ref(newHVREF($1),OP_RV2HV),
836 jmaybe($3));
53a7735b 837 PL_parser->expect = XOPERATOR;
f05e27e5 838 TOKEN_GETMAD($2,$$,'{');
839 TOKEN_GETMAD($4,$$,';');
840 TOKEN_GETMAD($5,$$,'}');
841 }
891be019 842 | term ARROW '(' ')' /* $subref->() */
fad39ff1 843 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5 844 newCVREF(0, scalar($1)));
845 TOKEN_GETMAD($2,$$,'a');
846 TOKEN_GETMAD($3,$$,'(');
847 TOKEN_GETMAD($4,$$,')');
848 }
891be019 849 | term ARROW '(' expr ')' /* $subref->(@args) */
fad39ff1 850 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
851 append_elem(OP_LIST, $4,
f05e27e5 852 newCVREF(0, scalar($1))));
853 TOKEN_GETMAD($2,$$,'a');
854 TOKEN_GETMAD($3,$$,'(');
855 TOKEN_GETMAD($5,$$,')');
856 }
fad39ff1 857
891be019 858 | subscripted '(' expr ')' /* $foo->{bar}->(@args) */
fad39ff1 859 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
860 append_elem(OP_LIST, $3,
f05e27e5 861 newCVREF(0, scalar($1))));
862 TOKEN_GETMAD($2,$$,'(');
863 TOKEN_GETMAD($4,$$,')');
864 }
891be019 865 | subscripted '(' ')' /* $foo->{bar}->() */
fad39ff1 866 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5 867 newCVREF(0, scalar($1)));
868 TOKEN_GETMAD($2,$$,'(');
869 TOKEN_GETMAD($3,$$,')');
870 }
9a9798c2 871 | '(' expr ')' '[' expr ']' /* list slice */
f05e27e5 872 { $$ = newSLICEOP(0, $5, $2);
873 TOKEN_GETMAD($1,$$,'(');
874 TOKEN_GETMAD($3,$$,')');
875 TOKEN_GETMAD($4,$$,'[');
876 TOKEN_GETMAD($6,$$,']');
877 }
9a9798c2 878 | '(' ')' '[' expr ']' /* empty list slice! */
bcabcc50 879 { $$ = newSLICEOP(0, $4, (OP*)NULL);
f05e27e5 880 TOKEN_GETMAD($1,$$,'(');
881 TOKEN_GETMAD($2,$$,')');
882 TOKEN_GETMAD($3,$$,'[');
883 TOKEN_GETMAD($5,$$,']');
884 }
891be019 885 ;
fad39ff1 886
891be019 887/* Binary operators between terms */
f05e27e5 888termbinop: term ASSIGNOP term /* $x = $y */
889 { $$ = newASSIGNOP(OPf_STACKED, $1, IVAL($2), $3);
890 TOKEN_GETMAD($2,$$,'o');
891 }
891be019 892 | term POWOP term /* $x ** $y */
f05e27e5 893 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
894 TOKEN_GETMAD($2,$$,'o');
895 }
891be019 896 | term MULOP term /* $x * $y, $x x $y */
f05e27e5 897 { if (IVAL($2) != OP_REPEAT)
79072805 898 scalar($1);
f05e27e5 899 $$ = newBINOP(IVAL($2), 0, $1, scalar($3));
900 TOKEN_GETMAD($2,$$,'o');
901 }
891be019 902 | term ADDOP term /* $x + $y */
f05e27e5 903 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
904 TOKEN_GETMAD($2,$$,'o');
905 }
891be019 906 | term SHIFTOP term /* $x >> $y, $x << $y */
f05e27e5 907 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
908 TOKEN_GETMAD($2,$$,'o');
909 }
891be019 910 | term RELOP term /* $x > $y, etc. */
f05e27e5 911 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
912 TOKEN_GETMAD($2,$$,'o');
913 }
891be019 914 | term EQOP term /* $x == $y, $x eq $y */
f05e27e5 915 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
916 TOKEN_GETMAD($2,$$,'o');
917 }
891be019 918 | term BITANDOP term /* $x & $y */
f05e27e5 919 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
920 TOKEN_GETMAD($2,$$,'o');
921 }
891be019 922 | term BITOROP term /* $x | $y */
f05e27e5 923 { $$ = newBINOP(IVAL($2), 0, scalar($1), scalar($3));
924 TOKEN_GETMAD($2,$$,'o');
925 }
891be019 926 | term DOTDOT term /* $x..$y, $x...$y */
f05e27e5 927 {
928 $$ = newRANGE(IVAL($2), scalar($1), scalar($3));
035e2bcc 929 DO_MAD({
f05e27e5 930 UNOP *op;
931 op = (UNOP*)$$;
932 op = (UNOP*)op->op_first; /* get to flop */
933 op = (UNOP*)op->op_first; /* get to flip */
934 op = (UNOP*)op->op_first; /* get to range */
935 token_getmad($2,(OP*)op,'o');
035e2bcc 936 })
f05e27e5 937 }
891be019 938 | term ANDAND term /* $x && $y */
f05e27e5 939 { $$ = newLOGOP(OP_AND, 0, $1, $3);
940 TOKEN_GETMAD($2,$$,'o');
941 }
891be019 942 | term OROR term /* $x || $y */
f05e27e5 943 { $$ = newLOGOP(OP_OR, 0, $1, $3);
944 TOKEN_GETMAD($2,$$,'o');
945 }
c963b151 946 | term DORDOR term /* $x // $y */
f05e27e5 947 { $$ = newLOGOP(OP_DOR, 0, $1, $3);
948 TOKEN_GETMAD($2,$$,'o');
949 }
891be019 950 | term MATCHOP term /* $x =~ /$y/ */
f05e27e5 951 { $$ = bind_match(IVAL($2), $1, $3);
952 TOKEN_GETMAD($2,
953 ($$->op_type == OP_NOT
954 ? ((UNOP*)$$)->op_first : $$),
955 '~');
956 }
891be019 957 ;
8d063cd8 958
891be019 959/* Unary operators and terms */
960termunop : '-' term %prec UMINUS /* -$x */
f05e27e5 961 { $$ = newUNOP(OP_NEGATE, 0, scalar($2));
962 TOKEN_GETMAD($1,$$,'o');
963 }
891be019 964 | '+' term %prec UMINUS /* +$x */
f05e27e5 965 { $$ = IF_MAD(
966 newUNOP(OP_NULL, 0, $2),
967 $2
968 );
969 TOKEN_GETMAD($1,$$,'+');
970 }
891be019 971 | '!' term /* !$x */
f05e27e5 972 { $$ = newUNOP(OP_NOT, 0, scalar($2));
973 TOKEN_GETMAD($1,$$,'o');
974 }
891be019 975 | '~' term /* ~$x */
f05e27e5 976 { $$ = newUNOP(OP_COMPLEMENT, 0, scalar($2));
977 TOKEN_GETMAD($1,$$,'o');
978 }
891be019 979 | term POSTINC /* $x++ */
79072805 980 { $$ = newUNOP(OP_POSTINC, 0,
f05e27e5 981 mod(scalar($1), OP_POSTINC));
982 TOKEN_GETMAD($2,$$,'o');
983 }
891be019 984 | term POSTDEC /* $x-- */
79072805 985 { $$ = newUNOP(OP_POSTDEC, 0,
f05e27e5 986 mod(scalar($1), OP_POSTDEC));
987 TOKEN_GETMAD($2,$$,'o');
988 }
891be019 989 | PREINC term /* ++$x */
79072805 990 { $$ = newUNOP(OP_PREINC, 0,
f05e27e5 991 mod(scalar($2), OP_PREINC));
992 TOKEN_GETMAD($1,$$,'o');
993 }
891be019 994 | PREDEC term /* --$x */
79072805 995 { $$ = newUNOP(OP_PREDEC, 0,
f05e27e5 996 mod(scalar($2), OP_PREDEC));
997 TOKEN_GETMAD($1,$$,'o');
998 }
891be019 999
1000 ;
1001
1002/* Constructors for anonymous data */
1003anonymous: '[' expr ']'
f05e27e5 1004 { $$ = newANONLIST($2);
1005 TOKEN_GETMAD($1,$$,'[');
1006 TOKEN_GETMAD($3,$$,']');
1007 }
891be019 1008 | '[' ']'
bcabcc50 1009 { $$ = newANONLIST((OP*)NULL);
f05e27e5 1010 TOKEN_GETMAD($1,$$,'[');
1011 TOKEN_GETMAD($2,$$,']');
1012 }
891be019 1013 | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */
f05e27e5 1014 { $$ = newANONHASH($2);
1015 TOKEN_GETMAD($1,$$,'{');
1016 TOKEN_GETMAD($3,$$,';');
1017 TOKEN_GETMAD($4,$$,'}');
1018 }
891be019 1019 | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */
bcabcc50 1020 { $$ = newANONHASH((OP*)NULL);
f05e27e5 1021 TOKEN_GETMAD($1,$$,'{');
1022 TOKEN_GETMAD($2,$$,';');
1023 TOKEN_GETMAD($3,$$,'}');
1024 }
718a7425 1025 | ANONSUB startanonsub proto subattrlist block %prec '('
5a5094bd 1026 { SvREFCNT_inc_simple_void(PL_compcv);
718a7425 1027 $$ = newANONATTRSUB($2, $3, $4, $5);
f05e27e5 1028 TOKEN_GETMAD($1,$$,'o');
718a7425 1029 OP_GETMAD($3,$$,'s');
1030 OP_GETMAD($4,$$,'a');
f05e27e5 1031 }
891be019 1032
1033 ;
1034
1035/* Things called with "do" */
1036termdo : DO term %prec UNIOP /* do $filename */
123d08c9 1037 { $$ = dofile($2, IVAL($1));
f05e27e5 1038 TOKEN_GETMAD($1,$$,'o');
1039 }
891be019 1040 | DO block %prec '(' /* do { code */
f05e27e5 1041 { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2));
1042 TOKEN_GETMAD($1,$$,'D');
1043 }
891be019 1044 | DO WORD '(' ')' /* do somesub() */
1045 { $$ = newUNOP(OP_ENTERSUB,
1046 OPf_SPECIAL|OPf_STACKED,
1047 prepend_elem(OP_LIST,
1048 scalar(newCVREF(
1049 (OPpENTERSUB_AMPER<<8),
1050 scalar($2)
bcabcc50 1051 )),(OP*)NULL)); dep();
f05e27e5 1052 TOKEN_GETMAD($1,$$,'o');
1053 TOKEN_GETMAD($3,$$,'(');
1054 TOKEN_GETMAD($4,$$,')');
1055 }
891be019 1056 | DO WORD '(' expr ')' /* do somesub(@args) */
1057 { $$ = newUNOP(OP_ENTERSUB,
1058 OPf_SPECIAL|OPf_STACKED,
1059 append_elem(OP_LIST,
1060 $4,
1061 scalar(newCVREF(
1062 (OPpENTERSUB_AMPER<<8),
1063 scalar($2)
f05e27e5 1064 )))); dep();
1065 TOKEN_GETMAD($1,$$,'o');
1066 TOKEN_GETMAD($3,$$,'(');
1067 TOKEN_GETMAD($5,$$,')');
1068 }
891be019 1069 | DO scalar '(' ')' /* do $subref () */
1070 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1071 prepend_elem(OP_LIST,
bcabcc50 1072 scalar(newCVREF(0,scalar($2))), (OP*)NULL)); dep();
f05e27e5 1073 TOKEN_GETMAD($1,$$,'o');
1074 TOKEN_GETMAD($3,$$,'(');
1075 TOKEN_GETMAD($4,$$,')');
1076 }
891be019 1077 | DO scalar '(' expr ')' /* do $subref (@args) */
1078 { $$ = newUNOP(OP_ENTERSUB, OPf_SPECIAL|OPf_STACKED,
1079 prepend_elem(OP_LIST,
1080 $4,
f05e27e5 1081 scalar(newCVREF(0,scalar($2))))); dep();
1082 TOKEN_GETMAD($1,$$,'o');
1083 TOKEN_GETMAD($3,$$,'(');
1084 TOKEN_GETMAD($5,$$,')');
1085 }
891be019 1086
1087 ;
1088
1089term : termbinop
1090 | termunop
1091 | anonymous
1092 | termdo
e9bdcc27 1093 | term '?' term ':' term
f05e27e5 1094 { $$ = newCONDOP(0, $1, $3, $5);
1095 TOKEN_GETMAD($2,$$,'?');
1096 TOKEN_GETMAD($4,$$,':');
1097 }
891be019 1098 | REFGEN term /* \$x, \@y, \%z */
f05e27e5 1099 { $$ = newUNOP(OP_REFGEN, 0, mod($2,OP_REFGEN));
1100 TOKEN_GETMAD($1,$$,'o');
1101 }
09bef843 1102 | myattrterm %prec UNIOP
1103 { $$ = $1; }
1104 | LOCAL term %prec UNIOP
f05e27e5 1105 { $$ = localize($2,IVAL($1));
dde83331 1106 TOKEN_GETMAD($1,$$,'k');
f05e27e5 1107 }
a0d0e21e 1108 | '(' expr ')'
f05e27e5 1109 { $$ = sawparens(IF_MAD(newUNOP(OP_NULL,0,$2), $2));
1110 TOKEN_GETMAD($1,$$,'(');
1111 TOKEN_GETMAD($3,$$,')');
1112 }
8d063cd8 1113 | '(' ')'
f05e27e5 1114 { $$ = sawparens(newNULLLIST());
1115 TOKEN_GETMAD($1,$$,'(');
1116 TOKEN_GETMAD($2,$$,')');
1117 }
79072805 1118 | scalar %prec '('
8d063cd8 1119 { $$ = $1; }
79072805 1120 | star %prec '('
8d063cd8 1121 { $$ = $1; }
79072805 1122 | hsh %prec '('
8d063cd8 1123 { $$ = $1; }
79072805 1124 | ary %prec '('
8d063cd8 1125 { $$ = $1; }
891be019 1126 | arylen %prec '(' /* $#x, $#{ something } */
79072805 1127 { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));}
fad39ff1 1128 | subscripted
1129 { $$ = $1; }
891be019 1130 | ary '[' expr ']' /* array slice */
79072805 1131 { $$ = prepend_elem(OP_ASLICE,
1132 newOP(OP_PUSHMARK, 0),
79072805 1133 newLISTOP(OP_ASLICE, 0,
1134 list($3),
f05e27e5 1135 ref($1, OP_ASLICE)));
1136 TOKEN_GETMAD($2,$$,'[');
1137 TOKEN_GETMAD($4,$$,']');
1138 }
891be019 1139 | ary '{' expr ';' '}' /* @hash{@keys} */
79072805 1140 { $$ = prepend_elem(OP_HSLICE,
1141 newOP(OP_PUSHMARK, 0),
79072805 1142 newLISTOP(OP_HSLICE, 0,
1143 list($3),
a0d0e21e 1144 ref(oopsHV($1), OP_HSLICE)));
53a7735b 1145 PL_parser->expect = XOPERATOR;
f05e27e5 1146 TOKEN_GETMAD($2,$$,'{');
1147 TOKEN_GETMAD($4,$$,';');
1148 TOKEN_GETMAD($5,$$,'}');
1149 }
79072805 1150 | THING %prec '('
1151 { $$ = $1; }
891be019 1152 | amper /* &foo; */
c07a80fd 1153 { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); }
ecb2f335 1154 | amper '(' ')' /* &foo() */
f05e27e5 1155 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1));
1156 TOKEN_GETMAD($2,$$,'(');
1157 TOKEN_GETMAD($3,$$,')');
1158 }
891be019 1159 | amper '(' expr ')' /* &foo(@args) */
f05e27e5 1160 {
1161 $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1162 append_elem(OP_LIST, $3, scalar($1)));
035e2bcc 1163 DO_MAD({
f05e27e5 1164 OP* op = $$;
1165 if (op->op_type == OP_CONST) { /* defeat const fold */
1166 op = (OP*)op->op_madprop->mad_val;
1167 }
1168 token_getmad($2,op,'(');
1169 token_getmad($4,op,')');
035e2bcc 1170 })
f05e27e5 1171 }
891be019 1172 | NOAMP WORD listexpr /* foo(@args) */
a0d0e21e 1173 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
f05e27e5 1174 append_elem(OP_LIST, $3, scalar($2)));
1175 TOKEN_GETMAD($1,$$,'o');
1176 }
891be019 1177 | LOOPEX /* loop exiting command (goto, last, dump, etc) */
f05e27e5 1178 { $$ = newOP(IVAL($1), OPf_SPECIAL);
1179 PL_hints |= HINT_BLOCK_SCOPE;
1180 TOKEN_GETMAD($1,$$,'o');
1181 }
a0d0e21e 1182 | LOOPEX term
f05e27e5 1183 { $$ = newLOOPEX(IVAL($1),$2);
1184 TOKEN_GETMAD($1,$$,'o');
1185 }
891be019 1186 | NOTOP argexpr /* not $foo */
f05e27e5 1187 { $$ = newUNOP(OP_NOT, 0, scalar($2));
1188 TOKEN_GETMAD($1,$$,'o');
1189 }
891be019 1190 | UNIOP /* Unary op, $_ implied */
f05e27e5 1191 { $$ = newOP(IVAL($1), 0);
1192 TOKEN_GETMAD($1,$$,'o');
1193 }
1194 | UNIOP block /* eval { foo }* */
1195 { $$ = newUNOP(IVAL($1), 0, $2);
1196 TOKEN_GETMAD($1,$$,'o');
1197 }
891be019 1198 | UNIOP term /* Unary op */
f05e27e5 1199 { $$ = newUNOP(IVAL($1), 0, $2);
1200 TOKEN_GETMAD($1,$$,'o');
1201 }
d2fdf8fd 1202 | REQUIRE /* require, $_ implied */
1203 { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0);
1204 TOKEN_GETMAD($1,$$,'o');
1205 }
1206 | REQUIRE term /* require Foo */
1207 { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2);
1208 TOKEN_GETMAD($1,$$,'o');
1209 }
3cd0a11a 1210 | UNIOPSUB
1211 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); }
891be019 1212 | UNIOPSUB term /* Sub treated as unop */
4633a7c4 1213 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
1214 append_elem(OP_LIST, $2, scalar($1))); }
891be019 1215 | FUNC0 /* Nullary operator */
f05e27e5 1216 { $$ = newOP(IVAL($1), 0);
1217 TOKEN_GETMAD($1,$$,'o');
1218 }
ae986130 1219 | FUNC0 '(' ')'
f05e27e5 1220 { $$ = newOP(IVAL($1), 0);
1221 TOKEN_GETMAD($1,$$,'o');
1222 TOKEN_GETMAD($2,$$,'(');
1223 TOKEN_GETMAD($3,$$,')');
1224 }
891be019 1225 | FUNC0SUB /* Sub treated as nullop */
28757baa 1226 { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED,
4633a7c4 1227 scalar($1)); }
891be019 1228 | FUNC1 '(' ')' /* not () */
f05e27e5 1229 { $$ = (IVAL($1) == OP_NOT)
1230 ? newUNOP(IVAL($1), 0, newSVOP(OP_CONST, 0, newSViv(0)))
1231 : newOP(IVAL($1), OPf_SPECIAL);
1232
1233 TOKEN_GETMAD($1,$$,'o');
1234 TOKEN_GETMAD($2,$$,'(');
1235 TOKEN_GETMAD($3,$$,')');
1236 }
891be019 1237 | FUNC1 '(' expr ')' /* not($foo) */
f05e27e5 1238 { $$ = newUNOP(IVAL($1), 0, $3);
1239 TOKEN_GETMAD($1,$$,'o');
1240 TOKEN_GETMAD($2,$$,'(');
1241 TOKEN_GETMAD($4,$$,')');
1242 }
1243 | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */
1244 { $$ = pmruntime($1, $3, 1);
1245 TOKEN_GETMAD($2,$$,'(');
1246 TOKEN_GETMAD($4,$$,')');
1247 }
79072805 1248 | WORD
378cc40b 1249 | listop
be25f609 1250 | YADAYADA
1251 {
1252 $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0),
1253 newSVOP(OP_CONST, 0, newSVpvs("Unimplemented")));
950f2ea2 1254 TOKEN_GETMAD($1,$$,'X');
be25f609 1255 }
88e1f1a2 1256 | PLUGEXPR
8d063cd8 1257 ;
1258
891be019 1259/* "my" declarations, with optional attributes */
09bef843 1260myattrterm: MY myterm myattrlist
f05e27e5 1261 { $$ = my_attrs($2,$3);
1262 DO_MAD(
1263 token_getmad($1,$$,'d');
1264 append_madprops($3->op_madprop, $$, 'a');
1265 $3->op_madprop = 0;
1266 )
1267 }
09bef843 1268 | MY myterm
f05e27e5 1269 { $$ = localize($2,IVAL($1));
1270 TOKEN_GETMAD($1,$$,'d');
1271 }
09bef843 1272 ;
1273
891be019 1274/* Things that can be "my"'d */
09bef843 1275myterm : '(' expr ')'
f05e27e5 1276 { $$ = sawparens($2);
1277 TOKEN_GETMAD($1,$$,'(');
1278 TOKEN_GETMAD($3,$$,')');
1279 }
09bef843 1280 | '(' ')'
f05e27e5 1281 { $$ = sawparens(newNULLLIST());
1282 TOKEN_GETMAD($1,$$,'(');
1283 TOKEN_GETMAD($2,$$,')');
1284 }
09bef843 1285 | scalar %prec '('
1286 { $$ = $1; }
1287 | hsh %prec '('
1288 { $$ = $1; }
1289 | ary %prec '('
1290 { $$ = $1; }
1291 ;
1292
891be019 1293/* Basic list expressions */
fad39ff1 1294listexpr: /* NULL */ %prec PREC_LOW
bcabcc50 1295 { $$ = (OP*)NULL; }
fad39ff1 1296 | argexpr %prec PREC_LOW
a0d0e21e 1297 { $$ = $1; }
1298 ;
1299
1300listexprcom: /* NULL */
bcabcc50 1301 { $$ = (OP*)NULL; }
79072805 1302 | expr
1303 { $$ = $1; }
a0d0e21e 1304 | expr ','
f05e27e5 1305 {
1306#ifdef MAD
1307 OP* op = newNULLLIST();
1308 token_getmad($2,op,',');
1309 $$ = append_elem(OP_LIST, $1, op);
1310#else
1311 $$ = $1;
1312#endif
1313
1314 }
79072805 1315 ;
1316
891be019 1317/* A little bit of trickery to make "for my $foo (@bar)" actually be
1318 lexical */
55497cff 1319my_scalar: scalar
12bd6ede 1320 { PL_parser->in_my = 0; $$ = my($1); }
55497cff 1321 ;
1322
79072805 1323amper : '&' indirob
f05e27e5 1324 { $$ = newCVREF(IVAL($1),$2);
1325 TOKEN_GETMAD($1,$$,'&');
1326 }
a687059c 1327 ;
1328
79072805 1329scalar : '$' indirob
f05e27e5 1330 { $$ = newSVREF($2);
1331 TOKEN_GETMAD($1,$$,'$');
1332 }
a687059c 1333 ;
1334
79072805 1335ary : '@' indirob
f05e27e5 1336 { $$ = newAVREF($2);
1337 TOKEN_GETMAD($1,$$,'@');
1338 }
79072805 1339 ;
1340
1341hsh : '%' indirob
f05e27e5 1342 { $$ = newHVREF($2);
1343 TOKEN_GETMAD($1,$$,'%');
1344 }
79072805 1345 ;
1346
1347arylen : DOLSHARP indirob
f05e27e5 1348 { $$ = newAVREF($2);
1349 TOKEN_GETMAD($1,$$,'l');
1350 }
79072805 1351 ;
1352
1353star : '*' indirob
f05e27e5 1354 { $$ = newGVREF(0,$2);
1355 TOKEN_GETMAD($1,$$,'*');
1356 }
79072805 1357 ;
1358
891be019 1359/* Indirect objects */
79072805 1360indirob : WORD
1361 { $$ = scalar($1); }
fad39ff1 1362 | scalar %prec PREC_LOW
f05e27e5 1363 { $$ = scalar($1); }
79072805 1364 | block
a0d0e21e 1365 { $$ = scope($1); }
79072805 1366
93a17b20 1367 | PRIVATEREF
1368 { $$ = $1; }
8d063cd8 1369 ;