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