Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* perly.y |
a687059c | 2 | * |
f05e27e5 | 3 | * Copyright (c) 1991-2002, 2003, 2004, 2005, 2006 Larry Wall |
2eee27d7 | 4 | * Copyright (c) 2007, 2008, 2009, 2010, 2011 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 | * | |
23 | * The main job of of this grammar is to call the various newFOO() | |
24 | * functions in op.c to build a syntax tree of OP structs. | |
61296642 | 25 | * It relies on the lexer in toke.c to do the tokenizing. |
29522234 DM |
26 | * |
27 | * Note: due to the way that the cleanup code works WRT to freeing ops on | |
28 | * the parse stack, it is dangerous to assign to the $n variables within | |
29 | * an action. | |
166f8a29 DM |
30 | */ |
31 | ||
0de566d7 | 32 | /* Make the parser re-entrant. */ |
8d063cd8 | 33 | |
f39ff1f3 | 34 | %pure-parser |
8d063cd8 | 35 | |
28ac2b49 | 36 | %start grammar |
9d116dd7 | 37 | |
8d063cd8 | 38 | %union { |
d5c6462e DM |
39 | I32 ival; /* __DEFAULT__ (marker for regen_perly.pl; |
40 | must always be 1st union member) */ | |
79072805 LW |
41 | char *pval; |
42 | OP *opval; | |
43 | GV *gvval; | |
8d063cd8 LW |
44 | } |
45 | ||
78cdf107 | 46 | %token <ival> GRAMPROG GRAMEXPR GRAMBLOCK GRAMBARESTMT GRAMFULLSTMT GRAMSTMTSEQ |
28ac2b49 | 47 | |
f10ebf6c | 48 | %token <ival> '{' '}' '[' ']' '-' '+' '@' '%' '&' '=' '.' |
f0fcb552 | 49 | |
185c2e96 | 50 | %token <opval> BAREWORD METHOD FUNCMETH THING PMFUNC PRIVATEREF QWLIST |
7eb971ee | 51 | %token <opval> FUNC0OP FUNC0SUB UNIOPSUB LSTOPSUB |
88e1f1a2 | 52 | %token <opval> PLUGEXPR PLUGSTMT |
b5bbe64a JH |
53 | %token <pval> LABEL |
54 | %token <ival> FORMAT SUB ANONSUB PACKAGE USE | |
55 | %token <ival> WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR | |
56 | %token <ival> GIVEN WHEN DEFAULT | |
57 | %token <ival> LOOPEX DOTDOT YADAYADA | |
58 | %token <ival> FUNC0 FUNC1 FUNC UNIOP LSTOP | |
59 | %token <ival> RELOP EQOP MULOP ADDOP | |
60 | %token <ival> DOLSHARP DO HASHBRACK NOAMP | |
61 | %token <ival> LOCAL MY REQUIRE | |
62 | %token <ival> COLONATTR FORMLBRACK FORMRBRACK | |
f05e27e5 | 63 | |
727a8fe5 | 64 | %type <ival> grammar remember mremember |
f05e27e5 | 65 | %type <ival> startsub startanonsub startformsub |
b5bbe64a | 66 | |
b5a64814 | 67 | %type <ival> mintro |
f05e27e5 | 68 | |
8e720305 | 69 | %type <opval> stmtseq fullstmt labfullstmt barestmt block mblock else |
fad39ff1 | 70 | %type <opval> expr term subscripted scalar ary hsh arylen star amper sideff |
76eba8ab | 71 | %type <opval> sliceme kvslice gelem |
08b3e84f | 72 | %type <opval> listexpr nexpr texpr iexpr mexpr mnexpr |
9b6b7be8 | 73 | %type <opval> optlistexpr optexpr optrepl indirob listop method |
d39c26a6 FC |
74 | %type <opval> formname subname proto optsubbody cont my_scalar my_var |
75 | %type <opval> refgen_topic formblock | |
f05e27e5 | 76 | %type <opval> subattrlist myattrlist myattrterm myterm |
d3d9da4a DM |
77 | %type <opval> termbinop termunop anonymous termdo |
78 | %type <ival> sigslurpsigil | |
79 | %type <opval> sigvarname sigdefault sigscalarelem sigslurpelem | |
80 | %type <opval> sigelem siglist siglistornull subsignature | |
705fe0e5 | 81 | %type <opval> formstmtseq formline formarg |
79072805 | 82 | |
b5bbe64a | 83 | %nonassoc <ival> PREC_LOW |
fad39ff1 SM |
84 | %nonassoc LOOPEX |
85 | ||
b5bbe64a JH |
86 | %left <ival> OROP DOROP |
87 | %left <ival> ANDOP | |
88 | %right <ival> NOTOP | |
36477c24 | 89 | %nonassoc LSTOP LSTOPSUB |
b5bbe64a JH |
90 | %left <ival> ',' |
91 | %right <ival> ASSIGNOP | |
92 | %right <ival> '?' ':' | |
be25f609 | 93 | %nonassoc DOTDOT YADAYADA |
b5bbe64a JH |
94 | %left <ival> OROR DORDOR |
95 | %left <ival> ANDAND | |
96 | %left <ival> BITOROP | |
97 | %left <ival> BITANDOP | |
a687059c LW |
98 | %nonassoc EQOP |
99 | %nonassoc RELOP | |
36477c24 | 100 | %nonassoc UNIOP UNIOPSUB |
20515881 | 101 | %nonassoc REQUIRE |
b5bbe64a | 102 | %left <ival> SHIFTOP |
a687059c LW |
103 | %left ADDOP |
104 | %left MULOP | |
b5bbe64a JH |
105 | %left <ival> MATCHOP |
106 | %right <ival> '!' '~' UMINUS REFGEN | |
107 | %right <ival> POWOP | |
108 | %nonassoc <ival> PREINC PREDEC POSTINC POSTDEC POSTJOIN | |
109 | %left <ival> ARROW | |
110 | %nonassoc <ival> ')' | |
111 | %left <ival> '(' | |
fad39ff1 | 112 | %left '[' '{' |
8d063cd8 LW |
113 | |
114 | %% /* RULES */ | |
115 | ||
28ac2b49 | 116 | /* Top-level choice of what kind of thing yyparse was called to parse */ |
727a8fe5 Z |
117 | grammar : GRAMPROG |
118 | { | |
624fa8bd | 119 | parser->expect = XSTATE; |
727a8fe5 Z |
120 | } |
121 | remember stmtseq | |
122 | { | |
123 | newPROG(block_end($3,$4)); | |
8635e3c2 | 124 | PL_compiling.cop_seq = 0; |
727a8fe5 Z |
125 | $$ = 0; |
126 | } | |
78cdf107 Z |
127 | | GRAMEXPR |
128 | { | |
129 | parser->expect = XTERM; | |
130 | } | |
09b1cffe | 131 | optexpr |
78cdf107 Z |
132 | { |
133 | PL_eval_root = $3; | |
134 | $$ = 0; | |
135 | } | |
e53d8f76 Z |
136 | | GRAMBLOCK |
137 | { | |
138 | parser->expect = XBLOCK; | |
139 | } | |
140 | block | |
141 | { | |
5f211341 | 142 | PL_pad_reset_pending = TRUE; |
8359b381 Z |
143 | PL_eval_root = $3; |
144 | $$ = 0; | |
145 | yyunlex(); | |
146 | parser->yychar = YYEOF; | |
147 | } | |
148 | | GRAMBARESTMT | |
149 | { | |
150 | parser->expect = XSTATE; | |
151 | } | |
152 | barestmt | |
153 | { | |
154 | PL_pad_reset_pending = TRUE; | |
e53d8f76 Z |
155 | PL_eval_root = $3; |
156 | $$ = 0; | |
157 | yyunlex(); | |
158 | parser->yychar = YYEOF; | |
159 | } | |
9eb5c532 | 160 | | GRAMFULLSTMT |
28ac2b49 | 161 | { |
9eb5c532 Z |
162 | parser->expect = XSTATE; |
163 | } | |
164 | fullstmt | |
165 | { | |
5f211341 | 166 | PL_pad_reset_pending = TRUE; |
9eb5c532 | 167 | PL_eval_root = $3; |
28ac2b49 Z |
168 | $$ = 0; |
169 | yyunlex(); | |
170 | parser->yychar = YYEOF; | |
171 | } | |
07ffcb73 Z |
172 | | GRAMSTMTSEQ |
173 | { | |
174 | parser->expect = XSTATE; | |
175 | } | |
5f211341 | 176 | stmtseq |
07ffcb73 Z |
177 | { |
178 | PL_eval_root = $3; | |
179 | $$ = 0; | |
180 | } | |
181 | ; | |
182 | ||
891be019 | 183 | /* An ordinary block */ |
5f211341 | 184 | block : '{' remember stmtseq '}' |
624fa8bd FC |
185 | { if (parser->copline > (line_t)$1) |
186 | parser->copline = (line_t)$1; | |
f05e27e5 | 187 | $$ = block_end($2, $3); |
f05e27e5 | 188 | } |
a0d0e21e LW |
189 | ; |
190 | ||
7c70caa5 | 191 | /* format body */ |
705fe0e5 | 192 | formblock: '=' remember ';' FORMRBRACK formstmtseq ';' '.' |
624fa8bd FC |
193 | { if (parser->copline > (line_t)$1) |
194 | parser->copline = (line_t)$1; | |
705fe0e5 | 195 | $$ = block_end($2, $5); |
7c70caa5 FC |
196 | } |
197 | ; | |
198 | ||
55497cff | 199 | remember: /* NULL */ /* start a full lexical scope */ |
34b54951 FC |
200 | { $$ = block_start(TRUE); |
201 | parser->parsed_sub = 0; } | |
55497cff PP |
202 | ; |
203 | ||
5f211341 | 204 | mblock : '{' mremember stmtseq '}' |
624fa8bd FC |
205 | { if (parser->copline > (line_t)$1) |
206 | parser->copline = (line_t)$1; | |
f05e27e5 | 207 | $$ = block_end($2, $3); |
f05e27e5 | 208 | } |
55497cff PP |
209 | ; |
210 | ||
211 | mremember: /* NULL */ /* start a partial lexical scope */ | |
34b54951 FC |
212 | { $$ = block_start(FALSE); |
213 | parser->parsed_sub = 0; } | |
8d063cd8 LW |
214 | ; |
215 | ||
eae48c89 | 216 | /* A sequence of statements in the program */ |
5f211341 | 217 | stmtseq : /* NULL */ |
bcabcc50 | 218 | { $$ = (OP*)NULL; } |
5f211341 | 219 | | stmtseq fullstmt |
2fcb4757 | 220 | { $$ = op_append_list(OP_LINESEQ, $1, $2); |
3280af22 | 221 | PL_pad_reset_pending = TRUE; |
503de470 DM |
222 | if ($1 && $2) |
223 | PL_hints |= HINT_BLOCK_SCOPE; | |
224 | } | |
8d063cd8 LW |
225 | ; |
226 | ||
705fe0e5 FC |
227 | /* A sequence of format lines */ |
228 | formstmtseq: /* NULL */ | |
229 | { $$ = (OP*)NULL; } | |
230 | | formstmtseq formline | |
231 | { $$ = op_append_list(OP_LINESEQ, $1, $2); | |
232 | PL_pad_reset_pending = TRUE; | |
233 | if ($1 && $2) | |
234 | PL_hints |= HINT_BLOCK_SCOPE; | |
235 | } | |
236 | ; | |
237 | ||
8e720305 Z |
238 | /* A statement in the program, including optional labels */ |
239 | fullstmt: barestmt | |
eae48c89 | 240 | { |
b5bbe64a | 241 | $$ = $1 ? newSTATEOP(0, NULL, $1) : NULL; |
eae48c89 | 242 | } |
8e720305 Z |
243 | | labfullstmt |
244 | { $$ = $1; } | |
245 | ; | |
246 | ||
247 | labfullstmt: LABEL barestmt | |
248 | { | |
b5bbe64a | 249 | $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2); |
8e720305 Z |
250 | } |
251 | | LABEL labfullstmt | |
252 | { | |
b5bbe64a | 253 | $$ = newSTATEOP(SVf_UTF8 * $1[strlen($1)+1], $1, $2); |
8e720305 | 254 | } |
eae48c89 Z |
255 | ; |
256 | ||
257 | /* A bare statement, lacking label and other aspects of state op */ | |
258 | barestmt: PLUGSTMT | |
0d863452 | 259 | { $$ = $1; } |
7c70caa5 | 260 | | FORMAT startformsub formname formblock |
f05e27e5 | 261 | { |
eae48c89 | 262 | CV *fmtcv = PL_compcv; |
eae48c89 Z |
263 | newFORM($2, $3, $4); |
264 | $$ = (OP*)NULL; | |
4a273b91 | 265 | if (CvOUTSIDE(fmtcv) && !CvEVAL(CvOUTSIDE(fmtcv))) { |
74a9453a | 266 | pad_add_weakref(fmtcv); |
f05e27e5 | 267 | } |
8b9fb2f9 | 268 | parser->parsed_sub = 1; |
eae48c89 | 269 | } |
50278755 FC |
270 | | SUB subname startsub |
271 | { | |
272 | if ($2->op_type == OP_CONST) { | |
273 | const char *const name = | |
274 | SvPV_nolen_const(((SVOP*)$2)->op_sv); | |
275 | if (strEQ(name, "BEGIN") || strEQ(name, "END") | |
764212cf FC |
276 | || strEQ(name, "INIT") || strEQ(name, "CHECK") |
277 | || strEQ(name, "UNITCHECK")) | |
278 | CvSPECIAL_on(PL_compcv); | |
50278755 | 279 | } |
e07561e6 FC |
280 | else |
281 | /* State subs inside anonymous subs need to be | |
282 | clonable themselves. */ | |
6d5c2147 FC |
283 | if (CvANON(CvOUTSIDE(PL_compcv)) |
284 | || CvCLONE(CvOUTSIDE(PL_compcv)) | |
285 | || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST( | |
286 | CvOUTSIDE(PL_compcv) | |
287 | ))[$2->op_targ])) | |
e07561e6 | 288 | CvCLONE_on(PL_compcv); |
624fa8bd FC |
289 | parser->in_my = 0; |
290 | parser->in_my_stash = NULL; | |
764212cf | 291 | } |
30d9c59b | 292 | proto subattrlist optsubbody |
eae48c89 Z |
293 | { |
294 | SvREFCNT_inc_simple_void(PL_compcv); | |
50278755 FC |
295 | $2->op_type == OP_CONST |
296 | ? newATTRSUB($3, $2, $5, $6, $7) | |
297 | : newMYSUB($3, $2, $5, $6, $7) | |
50278755 | 298 | ; |
eae48c89 | 299 | $$ = (OP*)NULL; |
764212cf | 300 | intro_my(); |
34b54951 | 301 | parser->parsed_sub = 1; |
f05e27e5 | 302 | } |
abcf453d PM |
303 | | SUB subname startsub |
304 | { | |
305 | if ($2->op_type == OP_CONST) { | |
306 | const char *const name = | |
307 | SvPV_nolen_const(((SVOP*)$2)->op_sv); | |
308 | if (strEQ(name, "BEGIN") || strEQ(name, "END") | |
309 | || strEQ(name, "INIT") || strEQ(name, "CHECK") | |
310 | || strEQ(name, "UNITCHECK")) | |
311 | CvSPECIAL_on(PL_compcv); | |
312 | } | |
313 | else | |
314 | /* State subs inside anonymous subs need to be | |
315 | clonable themselves. */ | |
316 | if (CvANON(CvOUTSIDE(PL_compcv)) | |
317 | || CvCLONE(CvOUTSIDE(PL_compcv)) | |
318 | || !PadnameIsSTATE(PadlistNAMESARRAY(CvPADLIST( | |
319 | CvOUTSIDE(PL_compcv) | |
320 | ))[$2->op_targ])) | |
321 | CvCLONE_on(PL_compcv); | |
322 | parser->in_my = 0; | |
323 | parser->in_my_stash = NULL; | |
324 | } | |
325 | remember subsignature subattrlist '{' stmtseq '}' | |
326 | { | |
327 | OP *body; | |
328 | if (parser->copline > (line_t)$8) | |
329 | parser->copline = (line_t)$8; | |
330 | body = block_end($5, | |
331 | op_append_list(OP_LINESEQ, $6, $9)); | |
332 | ||
333 | SvREFCNT_inc_simple_void(PL_compcv); | |
334 | $2->op_type == OP_CONST | |
335 | ? newATTRSUB($3, $2, NULL, $7, body) | |
336 | : newMYSUB($3, $2, NULL, $7, body) | |
337 | ; | |
338 | $$ = (OP*)NULL; | |
339 | intro_my(); | |
340 | parser->parsed_sub = 1; | |
341 | } | |
185c2e96 | 342 | | PACKAGE BAREWORD BAREWORD ';' |
5f211341 | 343 | { |
eae48c89 Z |
344 | package($3); |
345 | if ($2) | |
346 | package_version($2); | |
347 | $$ = (OP*)NULL; | |
eae48c89 Z |
348 | } |
349 | | USE startsub | |
350 | { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ } | |
185c2e96 | 351 | BAREWORD BAREWORD optlistexpr ';' |
eae48c89 Z |
352 | { |
353 | SvREFCNT_inc_simple_void(PL_compcv); | |
b5bbe64a | 354 | utilize($1, $2, $4, $5, $6); |
34b54951 | 355 | parser->parsed_sub = 1; |
eae48c89 | 356 | $$ = (OP*)NULL; |
eae48c89 | 357 | } |
417a992d | 358 | | IF '(' remember mexpr ')' mblock else |
eae48c89 | 359 | { |
3ad73efd Z |
360 | $$ = block_end($3, |
361 | newCONDOP(0, $4, op_scope($6), $7)); | |
624fa8bd | 362 | parser->copline = (line_t)$1; |
eae48c89 | 363 | } |
08b3e84f | 364 | | UNLESS '(' remember mexpr ')' mblock else |
eae48c89 | 365 | { |
3ad73efd | 366 | $$ = block_end($3, |
08b3e84f | 367 | newCONDOP(0, $4, $7, op_scope($6))); |
624fa8bd | 368 | parser->copline = (line_t)$1; |
eae48c89 | 369 | } |
b5a64814 | 370 | | GIVEN '(' remember mexpr ')' mblock |
eae48c89 | 371 | { |
5d051ee0 | 372 | $$ = block_end($3, newGIVENOP($4, op_scope($6), 0)); |
624fa8bd | 373 | parser->copline = (line_t)$1; |
eae48c89 | 374 | } |
417a992d | 375 | | WHEN '(' remember mexpr ')' mblock |
3ad73efd | 376 | { $$ = block_end($3, newWHENOP($4, op_scope($6))); } |
eae48c89 | 377 | | DEFAULT block |
3ad73efd | 378 | { $$ = newWHENOP(0, op_scope($2)); } |
417a992d | 379 | | WHILE '(' remember texpr ')' mintro mblock cont |
eae48c89 Z |
380 | { |
381 | $$ = block_end($3, | |
382 | newWHILEOP(0, 1, (LOOP*)(OP*)NULL, | |
94bf0465 | 383 | $4, $7, $8, $6)); |
624fa8bd | 384 | parser->copline = (line_t)$1; |
eae48c89 | 385 | } |
417a992d | 386 | | UNTIL '(' remember iexpr ')' mintro mblock cont |
eae48c89 Z |
387 | { |
388 | $$ = block_end($3, | |
94bf0465 Z |
389 | newWHILEOP(0, 1, (LOOP*)(OP*)NULL, |
390 | $4, $7, $8, $6)); | |
624fa8bd | 391 | parser->copline = (line_t)$1; |
eae48c89 | 392 | } |
2d0e3c96 FC |
393 | | FOR '(' remember mnexpr ';' |
394 | { parser->expect = XTERM; } | |
395 | texpr ';' | |
396 | { parser->expect = XTERM; } | |
397 | mintro mnexpr ')' | |
eae48c89 Z |
398 | mblock |
399 | { | |
b5bbe64a | 400 | OP *initop = $4; |
eae48c89 | 401 | OP *forop = newWHILEOP(0, 1, (LOOP*)(OP*)NULL, |
2d0e3c96 | 402 | scalar($7), $13, $11, $10); |
eae48c89 Z |
403 | if (initop) { |
404 | forop = op_prepend_elem(OP_LINESEQ, initop, | |
405 | op_append_elem(OP_LINESEQ, | |
406 | newOP(OP_UNSTACK, OPf_SPECIAL), | |
407 | forop)); | |
5f211341 | 408 | } |
0f602692 | 409 | PL_hints |= HINT_BLOCK_SCOPE; |
eae48c89 | 410 | $$ = block_end($3, forop); |
624fa8bd | 411 | parser->copline = (line_t)$1; |
eae48c89 | 412 | } |
417a992d | 413 | | FOR MY remember my_scalar '(' mexpr ')' mblock cont |
eae48c89 | 414 | { |
94bf0465 | 415 | $$ = block_end($3, newFOROP(0, $4, $6, $8, $9)); |
624fa8bd | 416 | parser->copline = (line_t)$1; |
eae48c89 | 417 | } |
417a992d | 418 | | FOR scalar '(' remember mexpr ')' mblock cont |
eae48c89 | 419 | { |
94bf0465 | 420 | $$ = block_end($4, newFOROP(0, |
3ad73efd | 421 | op_lvalue($2, OP_ENTERLOOP), $5, $7, $8)); |
624fa8bd | 422 | parser->copline = (line_t)$1; |
eae48c89 | 423 | } |
e118fea3 FC |
424 | | FOR my_refgen remember my_var |
425 | { parser->in_my = 0; $<opval>$ = my($4); } | |
d39c26a6 FC |
426 | '(' mexpr ')' mblock cont |
427 | { | |
428 | $$ = block_end( | |
e118fea3 | 429 | $3, |
d39c26a6 FC |
430 | newFOROP(0, |
431 | op_lvalue( | |
432 | newUNOP(OP_REFGEN, 0, | |
e118fea3 | 433 | $<opval>5), |
d39c26a6 | 434 | OP_ENTERLOOP), |
e118fea3 | 435 | $7, $9, $10) |
d39c26a6 FC |
436 | ); |
437 | parser->copline = (line_t)$1; | |
438 | } | |
439 | | FOR REFGEN refgen_topic '(' remember mexpr ')' mblock cont | |
440 | { | |
441 | $$ = block_end($5, newFOROP( | |
442 | 0, op_lvalue(newUNOP(OP_REFGEN, 0, | |
96801525 | 443 | $3), |
d39c26a6 FC |
444 | OP_ENTERLOOP), $6, $8, $9)); |
445 | parser->copline = (line_t)$1; | |
446 | } | |
417a992d | 447 | | FOR '(' remember mexpr ')' mblock cont |
eae48c89 Z |
448 | { |
449 | $$ = block_end($3, | |
94bf0465 | 450 | newFOROP(0, (OP*)NULL, $4, $6, $7)); |
624fa8bd | 451 | parser->copline = (line_t)$1; |
eae48c89 Z |
452 | } |
453 | | block cont | |
454 | { | |
455 | /* a block is a loop that happens once */ | |
456 | $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL, | |
94bf0465 | 457 | (OP*)NULL, $1, $2, 0); |
eae48c89 | 458 | } |
185c2e96 | 459 | | PACKAGE BAREWORD BAREWORD '{' remember |
eae48c89 | 460 | { |
eae48c89 | 461 | package($3); |
eae48c89 | 462 | if ($2) { |
eae48c89 | 463 | package_version($2); |
eae48c89 Z |
464 | } |
465 | } | |
466 | stmtseq '}' | |
467 | { | |
468 | /* a block is a loop that happens once */ | |
94bf0465 | 469 | $$ = newWHILEOP(0, 1, (LOOP*)(OP*)NULL, |
eae48c89 | 470 | (OP*)NULL, block_end($5, $7), (OP*)NULL, 0); |
624fa8bd FC |
471 | if (parser->copline > (line_t)$4) |
472 | parser->copline = (line_t)$4; | |
eae48c89 Z |
473 | } |
474 | | sideff ';' | |
475 | { | |
eae48c89 | 476 | $$ = $1; |
eae48c89 Z |
477 | } |
478 | | ';' | |
479 | { | |
b5bbe64a | 480 | $$ = (OP*)NULL; |
624fa8bd | 481 | parser->copline = NOLINE; |
5f211341 | 482 | } |
8d063cd8 LW |
483 | ; |
484 | ||
705fe0e5 FC |
485 | /* Format line */ |
486 | formline: THING formarg | |
487 | { OP *list; | |
488 | if ($2) { | |
489 | OP *term = $2; | |
705fe0e5 FC |
490 | list = op_append_elem(OP_LIST, $1, term); |
491 | } | |
492 | else { | |
705fe0e5 | 493 | list = $1; |
705fe0e5 | 494 | } |
624fa8bd FC |
495 | if (parser->copline == NOLINE) |
496 | parser->copline = CopLINE(PL_curcop)-1; | |
497 | else parser->copline--; | |
705fe0e5 | 498 | $$ = newSTATEOP(0, NULL, |
03d05f6e | 499 | op_convert_list(OP_FORMLINE, 0, list)); |
705fe0e5 FC |
500 | } |
501 | ; | |
502 | ||
503 | formarg : /* NULL */ | |
504 | { $$ = NULL; } | |
505 | | FORMLBRACK stmtseq FORMRBRACK | |
506 | { $$ = op_unscope($2); } | |
507 | ; | |
508 | ||
891be019 | 509 | /* An expression which may have a side-effect */ |
a687059c | 510 | sideff : error |
bcabcc50 | 511 | { $$ = (OP*)NULL; } |
a687059c | 512 | | expr |
79072805 | 513 | { $$ = $1; } |
a687059c | 514 | | expr IF expr |
b5bbe64a | 515 | { $$ = newLOGOP(OP_AND, 0, $3, $1); } |
a687059c | 516 | | expr UNLESS expr |
b5bbe64a | 517 | { $$ = newLOGOP(OP_OR, 0, $3, $1); } |
a687059c | 518 | | expr WHILE expr |
b5bbe64a | 519 | { $$ = newLOOPOP(OPf_PARENS, 1, scalar($3), $1); } |
55497cff | 520 | | expr UNTIL iexpr |
b5bbe64a | 521 | { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1); } |
ecca16b0 | 522 | | expr FOR expr |
94bf0465 | 523 | { $$ = newFOROP(0, (OP*)NULL, $3, $1, (OP*)NULL); |
624fa8bd | 524 | parser->copline = (line_t)$2; } |
f20dcd76 | 525 | | expr WHEN expr |
3ad73efd | 526 | { $$ = newWHENOP($3, op_scope($1)); } |
79072805 LW |
527 | ; |
528 | ||
891be019 | 529 | /* else and elsif blocks */ |
79072805 | 530 | else : /* NULL */ |
bcabcc50 | 531 | { $$ = (OP*)NULL; } |
55497cff | 532 | | ELSE mblock |
3ad73efd Z |
533 | { |
534 | ($2)->op_flags |= OPf_PARENS; | |
535 | $$ = op_scope($2); | |
f05e27e5 | 536 | } |
417a992d | 537 | | ELSIF '(' mexpr ')' mblock else |
624fa8bd | 538 | { parser->copline = (line_t)$1; |
3ad73efd Z |
539 | $$ = newCONDOP(0, |
540 | newSTATEOP(OPf_SPECIAL,NULL,$3), | |
541 | op_scope($5), $6); | |
542 | PL_hints |= HINT_BLOCK_SCOPE; | |
f05e27e5 | 543 | } |
79072805 LW |
544 | ; |
545 | ||
891be019 | 546 | /* Continue blocks */ |
79072805 | 547 | cont : /* NULL */ |
bcabcc50 | 548 | { $$ = (OP*)NULL; } |
79072805 | 549 | | CONTINUE block |
b5bbe64a | 550 | { $$ = op_scope($2); } |
79072805 LW |
551 | ; |
552 | ||
a034e688 DM |
553 | /* determine whether there are any new my declarations */ |
554 | mintro : /* NULL */ | |
555 | { $$ = (PL_min_intro_pending && | |
556 | PL_max_intro_pending >= PL_min_intro_pending); | |
557 | intro_my(); } | |
558 | ||
891be019 | 559 | /* Normal expression */ |
8d063cd8 | 560 | nexpr : /* NULL */ |
bcabcc50 | 561 | { $$ = (OP*)NULL; } |
8d063cd8 LW |
562 | | sideff |
563 | ; | |
564 | ||
891be019 | 565 | /* Boolean expression */ |
8d063cd8 | 566 | texpr : /* NULL means true */ |
f05e27e5 DM |
567 | { YYSTYPE tmplval; |
568 | (void)scan_num("1", &tmplval); | |
569 | $$ = tmplval.opval; } | |
8d063cd8 LW |
570 | | expr |
571 | ; | |
572 | ||
891be019 | 573 | /* Inverted boolean expression */ |
55497cff PP |
574 | iexpr : expr |
575 | { $$ = invert(scalar($1)); } | |
576 | ; | |
577 | ||
891be019 | 578 | /* Expression with its own lexical scope */ |
55497cff | 579 | mexpr : expr |
bbce6d69 PP |
580 | { $$ = $1; intro_my(); } |
581 | ; | |
582 | ||
583 | mnexpr : nexpr | |
584 | { $$ = $1; intro_my(); } | |
55497cff PP |
585 | ; |
586 | ||
185c2e96 | 587 | formname: BAREWORD { $$ = $1; } |
bcabcc50 | 588 | | /* NULL */ { $$ = (OP*)NULL; } |
8d063cd8 LW |
589 | ; |
590 | ||
fa83b5b6 | 591 | startsub: /* NULL */ /* start a regular subroutine scope */ |
a8ff2fa6 DM |
592 | { $$ = start_subparse(FALSE, 0); |
593 | SAVEFREESV(PL_compcv); } | |
f05e27e5 | 594 | |
28757baa PP |
595 | ; |
596 | ||
597 | startanonsub: /* NULL */ /* start an anonymous subroutine scope */ | |
a8ff2fa6 DM |
598 | { $$ = start_subparse(FALSE, CVf_ANON); |
599 | SAVEFREESV(PL_compcv); } | |
28757baa PP |
600 | ; |
601 | ||
44a8e56a | 602 | startformsub: /* NULL */ /* start a format subroutine scope */ |
a8ff2fa6 DM |
603 | { $$ = start_subparse(TRUE, 0); |
604 | SAVEFREESV(PL_compcv); } | |
44a8e56a PP |
605 | ; |
606 | ||
891be019 | 607 | /* Name of a subroutine - must be a bareword, could be special */ |
185c2e96 | 608 | subname : BAREWORD |
4210d3f1 | 609 | | PRIVATEREF |
a0d0e21e LW |
610 | ; |
611 | ||
891be019 | 612 | /* Subroutine prototype */ |
4633a7c4 | 613 | proto : /* NULL */ |
bcabcc50 | 614 | { $$ = (OP*)NULL; } |
4633a7c4 LW |
615 | | THING |
616 | ; | |
28757baa | 617 | |
891be019 | 618 | /* Optional list of subroutine attributes */ |
09bef843 | 619 | subattrlist: /* NULL */ |
bcabcc50 | 620 | { $$ = (OP*)NULL; } |
09bef843 | 621 | | COLONATTR THING |
b5bbe64a | 622 | { $$ = $2; } |
09bef843 | 623 | | COLONATTR |
b5bbe64a | 624 | { $$ = (OP*)NULL; } |
09bef843 SB |
625 | ; |
626 | ||
891be019 | 627 | /* List of attributes for a "my" variable declaration */ |
09bef843 | 628 | myattrlist: COLONATTR THING |
b5bbe64a | 629 | { $$ = $2; } |
09bef843 | 630 | | COLONATTR |
b5bbe64a | 631 | { $$ = (OP*)NULL; } |
09bef843 SB |
632 | ; |
633 | ||
abcf453d | 634 | |
d3d9da4a DM |
635 | |
636 | /* -------------------------------------- | |
637 | * subroutine signature parsing | |
638 | */ | |
639 | ||
640 | /* the '' or 'foo' part of a '$' or '@foo' etc signature variable */ | |
641 | sigvarname: /* NULL */ | |
642 | { $$ = (OP*)NULL; } | |
643 | | PRIVATEREF | |
4fa06845 | 644 | { $$ = $1; } |
d3d9da4a DM |
645 | ; |
646 | ||
647 | sigslurpsigil: | |
648 | '@' | |
649 | { $$ = '@'; } | |
650 | | '%' | |
651 | { $$ = '%'; } | |
652 | ||
653 | /* @, %, @foo, %foo */ | |
654 | sigslurpelem: sigslurpsigil sigvarname sigdefault/* def only to catch errors */ | |
655 | { | |
4fa06845 DM |
656 | I32 sigil = $1; |
657 | OP *var = $2; | |
d3d9da4a | 658 | OP *defexpr = $3; |
d3d9da4a DM |
659 | |
660 | if (PL_parser->sig_slurpy) | |
661 | yyerror("Multiple slurpy parameters not allowed"); | |
5b9ec8a2 | 662 | PL_parser->sig_slurpy = (char)sigil; |
d3d9da4a DM |
663 | |
664 | if (defexpr) | |
bb6b75cd | 665 | yyerror("A slurpy parameter may not have " |
d3d9da4a DM |
666 | "a default value"); |
667 | ||
4fa06845 | 668 | $$ = var ? newSTATEOP(0, NULL, var) : (OP*)NULL; |
d3d9da4a DM |
669 | } |
670 | ; | |
671 | ||
672 | /* default part of sub signature scalar element: i.e. '= default_expr' */ | |
673 | sigdefault: /* NULL */ | |
674 | { $$ = (OP*)NULL; } | |
675 | | ASSIGNOP | |
676 | { $$ = newOP(OP_NULL, 0); } | |
677 | | ASSIGNOP term | |
678 | { $$ = $2; } | |
679 | ||
680 | ||
681 | /* subroutine signature scalar element: e.g. '$x', '$=', '$x = $default' */ | |
682 | sigscalarelem: | |
683 | '$' sigvarname sigdefault | |
684 | { | |
685 | OP *var = $2; | |
686 | OP *defexpr = $3; | |
d3d9da4a DM |
687 | |
688 | if (PL_parser->sig_slurpy) | |
689 | yyerror("Slurpy parameter not last"); | |
690 | ||
691 | PL_parser->sig_elems++; | |
692 | ||
d3d9da4a DM |
693 | if (defexpr) { |
694 | PL_parser->sig_optelems++; | |
4fa06845 DM |
695 | |
696 | if ( defexpr->op_type == OP_NULL | |
d3d9da4a DM |
697 | && !(defexpr->op_flags & OPf_KIDS)) |
698 | { | |
4fa06845 DM |
699 | /* handle '$=' special case */ |
700 | if (var) | |
701 | yyerror("Optional parameter " | |
702 | "lacks default expression"); | |
d3d9da4a DM |
703 | op_free(defexpr); |
704 | } | |
4fa06845 DM |
705 | else { |
706 | /* a normal '=default' expression */ | |
707 | OP *defop = (OP*)alloc_LOGOP(OP_ARGDEFELEM, | |
708 | defexpr, | |
709 | LINKLIST(defexpr)); | |
710 | /* re-purpose op_targ to hold @_ index */ | |
6daeaaa3 DM |
711 | defop->op_targ = |
712 | (PADOFFSET)(PL_parser->sig_elems - 1); | |
4fa06845 DM |
713 | |
714 | if (var) { | |
715 | var->op_flags |= OPf_STACKED; | |
716 | (void)op_sibling_splice(var, | |
717 | NULL, 0, defop); | |
718 | scalar(defop); | |
719 | } | |
720 | else | |
721 | var = newUNOP(OP_NULL, 0, defop); | |
722 | ||
723 | LINKLIST(var); | |
724 | /* NB: normally the first child of a | |
725 | * logop is executed before the logop, | |
726 | * and it pushes a boolean result | |
727 | * ready for the logop. For ARGDEFELEM, | |
728 | * the op itself does the boolean | |
729 | * calculation, so set the first op to | |
730 | * it instead. | |
731 | */ | |
732 | var->op_next = defop; | |
733 | defexpr->op_next = var; | |
d3d9da4a DM |
734 | } |
735 | } | |
736 | else { | |
737 | if (PL_parser->sig_optelems) | |
738 | yyerror("Mandatory parameter " | |
739 | "follows optional parameter"); | |
d3d9da4a DM |
740 | } |
741 | ||
4fa06845 | 742 | $$ = var ? newSTATEOP(0, NULL, var) : (OP*)NULL; |
d3d9da4a DM |
743 | } |
744 | ; | |
745 | ||
746 | ||
747 | /* subroutine signature element: e.g. '$x = $default' or '%h' */ | |
748 | sigelem: sigscalarelem | |
749 | { parser->expect = XSIGVAR; $$ = $1; } | |
750 | | sigslurpelem | |
751 | { parser->expect = XSIGVAR; $$ = $1; } | |
752 | ; | |
753 | ||
754 | /* list of subroutine signature elements */ | |
755 | siglist: | |
756 | siglist ',' | |
757 | { $$ = $1; } | |
758 | | siglist ',' sigelem | |
30d9c59b | 759 | { |
d3d9da4a | 760 | $$ = op_append_list(OP_LINESEQ, $1, $3); |
30d9c59b | 761 | } |
d3d9da4a DM |
762 | | sigelem %prec PREC_LOW |
763 | { $$ = $1; } | |
30d9c59b Z |
764 | ; |
765 | ||
d3d9da4a DM |
766 | /* () or (....) */ |
767 | siglistornull: /* NULL */ | |
768 | { $$ = (OP*)NULL; } | |
769 | | siglist | |
770 | { $$ = $1; } | |
771 | ||
772 | /* Subroutine signature */ | |
773 | subsignature: '(' | |
774 | { | |
775 | ENTER; | |
6daeaaa3 DM |
776 | SAVEIV(PL_parser->sig_elems); |
777 | SAVEIV(PL_parser->sig_optelems); | |
d3d9da4a DM |
778 | SAVEI8(PL_parser->sig_slurpy); |
779 | PL_parser->sig_elems = 0; | |
780 | PL_parser->sig_optelems = 0; | |
781 | PL_parser->sig_slurpy = 0; | |
782 | parser->expect = XSIGVAR; | |
783 | } | |
784 | siglistornull | |
785 | ')' | |
786 | { | |
4fa06845 DM |
787 | OP *sigops = $3; |
788 | UNOP_AUX_item *aux; | |
789 | OP *check; | |
d3d9da4a DM |
790 | |
791 | assert(FEATURE_SIGNATURES_IS_ENABLED); | |
792 | ||
793 | /* We shouldn't get here otherwise */ | |
794 | Perl_ck_warner_d(aTHX_ | |
795 | packWARN(WARN_EXPERIMENTAL__SIGNATURES), | |
796 | "The signatures feature is experimental"); | |
797 | ||
4fa06845 DM |
798 | aux = (UNOP_AUX_item*)PerlMemShared_malloc( |
799 | sizeof(UNOP_AUX_item) * 3); | |
6daeaaa3 DM |
800 | aux[0].iv = PL_parser->sig_elems; |
801 | aux[1].iv = PL_parser->sig_optelems; | |
4fa06845 DM |
802 | aux[2].iv = PL_parser->sig_slurpy; |
803 | check = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux); | |
804 | sigops = op_prepend_elem(OP_LINESEQ, check, sigops); | |
805 | sigops = op_prepend_elem(OP_LINESEQ, | |
806 | newSTATEOP(0, NULL, NULL), | |
807 | sigops); | |
808 | /* a nextstate at the end handles context | |
809 | * correctly for an empty sub body */ | |
810 | $$ = op_append_elem(OP_LINESEQ, | |
811 | sigops, | |
812 | newSTATEOP(0, NULL, NULL)); | |
d3d9da4a DM |
813 | |
814 | parser->expect = XATTRBLOCK; | |
815 | LEAVE; | |
816 | } | |
817 | ; | |
818 | ||
819 | ||
820 | ||
30d9c59b | 821 | /* Optional subroutine body, for named subroutine declaration */ |
abcf453d | 822 | optsubbody: block |
2d0e3c96 | 823 | | ';' { $$ = (OP*)NULL; } |
8d063cd8 LW |
824 | ; |
825 | ||
891be019 | 826 | /* Ordinary expressions; logical combinations */ |
a0d0e21e | 827 | expr : expr ANDOP expr |
b5bbe64a | 828 | { $$ = newLOGOP(OP_AND, 0, $1, $3); } |
a0d0e21e | 829 | | expr OROP expr |
b5bbe64a | 830 | { $$ = newLOGOP($2, 0, $1, $3); } |
c963b151 | 831 | | expr DOROP expr |
b5bbe64a | 832 | { $$ = newLOGOP(OP_DOR, 0, $1, $3); } |
09b1cffe | 833 | | listexpr %prec PREC_LOW |
a0d0e21e LW |
834 | ; |
835 | ||
891be019 | 836 | /* Expressions are a list of terms joined by commas */ |
09b1cffe | 837 | listexpr: listexpr ',' |
b5bbe64a | 838 | { $$ = $1; } |
09b1cffe | 839 | | listexpr ',' term |
abcf453d | 840 | { |
29522234 | 841 | OP* term = $3; |
2fcb4757 | 842 | $$ = op_append_elem(OP_LIST, $1, term); |
f05e27e5 | 843 | } |
fad39ff1 | 844 | | term %prec PREC_LOW |
8d063cd8 LW |
845 | ; |
846 | ||
891be019 | 847 | /* List operators */ |
09b1cffe | 848 | listop : LSTOP indirob listexpr /* map {...} @args or print $fh @args */ |
03d05f6e | 849 | { $$ = op_convert_list($1, OPf_STACKED, |
b5bbe64a | 850 | op_prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); |
f05e27e5 | 851 | } |
891be019 | 852 | | FUNC '(' indirob expr ')' /* print ($fh @args */ |
03d05f6e | 853 | { $$ = op_convert_list($1, OPf_STACKED, |
b5bbe64a | 854 | op_prepend_elem(OP_LIST, newGVREF($1,$3), $4) ); |
f05e27e5 | 855 | } |
417a992d | 856 | | term ARROW method '(' optexpr ')' /* $foo->bar(list) */ |
03d05f6e | 857 | { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED, |
2fcb4757 Z |
858 | op_append_elem(OP_LIST, |
859 | op_prepend_elem(OP_LIST, scalar($1), $5), | |
b46e009d | 860 | newMETHOP(OP_METHOD, 0, $3))); |
f05e27e5 | 861 | } |
891be019 | 862 | | term ARROW method /* $foo->bar */ |
03d05f6e | 863 | { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED, |
2fcb4757 | 864 | op_append_elem(OP_LIST, scalar($1), |
b46e009d | 865 | newMETHOP(OP_METHOD, 0, $3))); |
f05e27e5 | 866 | } |
09b1cffe | 867 | | METHOD indirob optlistexpr /* new Class @args */ |
03d05f6e | 868 | { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED, |
2fcb4757 Z |
869 | op_append_elem(OP_LIST, |
870 | op_prepend_elem(OP_LIST, $2, $3), | |
b46e009d | 871 | newMETHOP(OP_METHOD, 0, $1))); |
f05e27e5 | 872 | } |
09b1cffe | 873 | | FUNCMETH indirob '(' optexpr ')' /* method $object (@args) */ |
03d05f6e | 874 | { $$ = op_convert_list(OP_ENTERSUB, OPf_STACKED, |
2fcb4757 Z |
875 | op_append_elem(OP_LIST, |
876 | op_prepend_elem(OP_LIST, $2, $4), | |
b46e009d | 877 | newMETHOP(OP_METHOD, 0, $1))); |
f05e27e5 | 878 | } |
09b1cffe | 879 | | LSTOP optlistexpr /* print @args */ |
03d05f6e | 880 | { $$ = op_convert_list($1, 0, $2); } |
09b1cffe | 881 | | FUNC '(' optexpr ')' /* print (@args) */ |
03d05f6e | 882 | { $$ = op_convert_list($1, 0, $3); } |
718a7425 | 883 | | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */ |
5a5094bd | 884 | { SvREFCNT_inc_simple_void(PL_compcv); |
bcabcc50 | 885 | $<opval>$ = newANONATTRSUB($2, 0, (OP*)NULL, $3); } |
09b1cffe | 886 | optlistexpr %prec LSTOP /* ... @bar */ |
4633a7c4 | 887 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
2fcb4757 Z |
888 | op_append_elem(OP_LIST, |
889 | op_prepend_elem(OP_LIST, $<opval>4, $5), $1)); | |
f05e27e5 | 890 | } |
a687059c LW |
891 | ; |
892 | ||
891be019 | 893 | /* Names of methods. May use $object->$methodname */ |
a0d0e21e LW |
894 | method : METHOD |
895 | | scalar | |
896 | ; | |
897 | ||
891be019 | 898 | /* Some kind of subscripted expression */ |
89f35911 | 899 | subscripted: gelem '{' expr ';' '}' /* *main::{something} */ |
891be019 SC |
900 | /* In this and all the hash accessors, ';' is |
901 | * provided by the tokeniser */ | |
5c86b6df | 902 | { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); } |
891be019 | 903 | | scalar '[' expr ']' /* $array[$element] */ |
f05e27e5 | 904 | { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); |
f05e27e5 | 905 | } |
891be019 | 906 | | term ARROW '[' expr ']' /* somearef->[$element] */ |
fad39ff1 SM |
907 | { $$ = newBINOP(OP_AELEM, 0, |
908 | ref(newAVREF($1),OP_RV2AV), | |
f05e27e5 | 909 | scalar($4)); |
f05e27e5 | 910 | } |
891be019 | 911 | | subscripted '[' expr ']' /* $foo->[$bar]->[$baz] */ |
fad39ff1 SM |
912 | { $$ = newBINOP(OP_AELEM, 0, |
913 | ref(newAVREF($1),OP_RV2AV), | |
f05e27e5 | 914 | scalar($3)); |
f05e27e5 | 915 | } |
5a63ceef | 916 | | scalar '{' expr ';' '}' /* $foo{bar();} */ |
fad39ff1 | 917 | { $$ = newBINOP(OP_HELEM, 0, oopsHV($1), jmaybe($3)); |
f05e27e5 | 918 | } |
891be019 | 919 | | term ARROW '{' expr ';' '}' /* somehref->{bar();} */ |
fad39ff1 SM |
920 | { $$ = newBINOP(OP_HELEM, 0, |
921 | ref(newHVREF($1),OP_RV2HV), | |
5c86b6df | 922 | jmaybe($4)); } |
891be019 | 923 | | subscripted '{' expr ';' '}' /* $foo->[bar]->{baz;} */ |
fad39ff1 SM |
924 | { $$ = newBINOP(OP_HELEM, 0, |
925 | ref(newHVREF($1),OP_RV2HV), | |
5c86b6df | 926 | jmaybe($3)); } |
891be019 | 927 | | term ARROW '(' ')' /* $subref->() */ |
fad39ff1 | 928 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
b5bbe64a | 929 | newCVREF(0, scalar($1))); } |
891be019 | 930 | | term ARROW '(' expr ')' /* $subref->(@args) */ |
fad39ff1 | 931 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
2fcb4757 | 932 | op_append_elem(OP_LIST, $4, |
b5bbe64a | 933 | newCVREF(0, scalar($1)))); } |
fad39ff1 | 934 | |
417a992d | 935 | | subscripted '(' expr ')' /* $foo->{bar}->(@args) */ |
fad39ff1 | 936 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
2fcb4757 | 937 | op_append_elem(OP_LIST, $3, |
b5bbe64a | 938 | newCVREF(0, scalar($1)))); } |
417a992d | 939 | | subscripted '(' ')' /* $foo->{bar}->() */ |
fad39ff1 | 940 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
b5bbe64a | 941 | newCVREF(0, scalar($1))); } |
9a9798c2 | 942 | | '(' expr ')' '[' expr ']' /* list slice */ |
b5bbe64a | 943 | { $$ = newSLICEOP(0, $5, $2); } |
ea25a9b2 | 944 | | QWLIST '[' expr ']' /* list literal slice */ |
b5bbe64a | 945 | { $$ = newSLICEOP(0, $3, $1); } |
9a9798c2 | 946 | | '(' ')' '[' expr ']' /* empty list slice! */ |
b5bbe64a | 947 | { $$ = newSLICEOP(0, $4, (OP*)NULL); } |
891be019 | 948 | ; |
fad39ff1 | 949 | |
891be019 | 950 | /* Binary operators between terms */ |
f05e27e5 | 951 | termbinop: term ASSIGNOP term /* $x = $y */ |
b5bbe64a | 952 | { $$ = newASSIGNOP(OPf_STACKED, $1, $2, $3); } |
891be019 | 953 | | term POWOP term /* $x ** $y */ |
b5bbe64a | 954 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
891be019 | 955 | | term MULOP term /* $x * $y, $x x $y */ |
b5bbe64a | 956 | { if ($2 != OP_REPEAT) |
79072805 | 957 | scalar($1); |
b5bbe64a | 958 | $$ = newBINOP($2, 0, $1, scalar($3)); |
f05e27e5 | 959 | } |
891be019 | 960 | | term ADDOP term /* $x + $y */ |
b5bbe64a | 961 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
891be019 | 962 | | term SHIFTOP term /* $x >> $y, $x << $y */ |
b5bbe64a | 963 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
891be019 | 964 | | term RELOP term /* $x > $y, etc. */ |
b5bbe64a | 965 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
891be019 | 966 | | term EQOP term /* $x == $y, $x eq $y */ |
b5bbe64a | 967 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
891be019 | 968 | | term BITANDOP term /* $x & $y */ |
b5bbe64a | 969 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
891be019 | 970 | | term BITOROP term /* $x | $y */ |
b5bbe64a | 971 | { $$ = newBINOP($2, 0, scalar($1), scalar($3)); } |
891be019 | 972 | | term DOTDOT term /* $x..$y, $x...$y */ |
b5bbe64a | 973 | { $$ = newRANGE($2, scalar($1), scalar($3)); } |
891be019 | 974 | | term ANDAND term /* $x && $y */ |
b5bbe64a | 975 | { $$ = newLOGOP(OP_AND, 0, $1, $3); } |
891be019 | 976 | | term OROR term /* $x || $y */ |
b5bbe64a | 977 | { $$ = newLOGOP(OP_OR, 0, $1, $3); } |
c963b151 | 978 | | term DORDOR term /* $x // $y */ |
b5bbe64a | 979 | { $$ = newLOGOP(OP_DOR, 0, $1, $3); } |
891be019 | 980 | | term MATCHOP term /* $x =~ /$y/ */ |
b5bbe64a | 981 | { $$ = bind_match($2, $1, $3); } |
891be019 | 982 | ; |
8d063cd8 | 983 | |
891be019 SC |
984 | /* Unary operators and terms */ |
985 | termunop : '-' term %prec UMINUS /* -$x */ | |
b5bbe64a | 986 | { $$ = newUNOP(OP_NEGATE, 0, scalar($2)); } |
891be019 | 987 | | '+' term %prec UMINUS /* +$x */ |
b5bbe64a JH |
988 | { $$ = $2; } |
989 | ||
891be019 | 990 | | '!' term /* !$x */ |
b5bbe64a | 991 | { $$ = newUNOP(OP_NOT, 0, scalar($2)); } |
891be019 | 992 | | '~' term /* ~$x */ |
8823cb89 | 993 | { $$ = newUNOP($1, 0, scalar($2)); } |
891be019 | 994 | | term POSTINC /* $x++ */ |
79072805 | 995 | { $$ = newUNOP(OP_POSTINC, 0, |
b5bbe64a | 996 | op_lvalue(scalar($1), OP_POSTINC)); } |
891be019 | 997 | | term POSTDEC /* $x-- */ |
79072805 | 998 | { $$ = newUNOP(OP_POSTDEC, 0, |
b5bbe64a | 999 | op_lvalue(scalar($1), OP_POSTDEC));} |
cc624add | 1000 | | term POSTJOIN /* implicit join after interpolated ->@ */ |
03d05f6e | 1001 | { $$ = op_convert_list(OP_JOIN, 0, |
cc624add FC |
1002 | op_append_elem( |
1003 | OP_LIST, | |
1004 | newSVREF(scalar( | |
1005 | newSVOP(OP_CONST,0, | |
1006 | newSVpvs("\"")) | |
1007 | )), | |
1008 | $1 | |
1009 | )); | |
cc624add | 1010 | } |
891be019 | 1011 | | PREINC term /* ++$x */ |
79072805 | 1012 | { $$ = newUNOP(OP_PREINC, 0, |
b5bbe64a | 1013 | op_lvalue(scalar($2), OP_PREINC)); } |
891be019 | 1014 | | PREDEC term /* --$x */ |
79072805 | 1015 | { $$ = newUNOP(OP_PREDEC, 0, |
b5bbe64a | 1016 | op_lvalue(scalar($2), OP_PREDEC)); } |
891be019 SC |
1017 | |
1018 | ; | |
1019 | ||
1020 | /* Constructors for anonymous data */ | |
1021 | anonymous: '[' expr ']' | |
b5bbe64a | 1022 | { $$ = newANONLIST($2); } |
891be019 | 1023 | | '[' ']' |
b5bbe64a | 1024 | { $$ = newANONLIST((OP*)NULL);} |
891be019 | 1025 | | HASHBRACK expr ';' '}' %prec '(' /* { foo => "Bar" } */ |
b5bbe64a | 1026 | { $$ = newANONHASH($2); } |
891be019 | 1027 | | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */ |
b5bbe64a | 1028 | { $$ = newANONHASH((OP*)NULL); } |
abcf453d | 1029 | | ANONSUB startanonsub proto subattrlist block %prec '(' |
5a5094bd | 1030 | { SvREFCNT_inc_simple_void(PL_compcv); |
b5bbe64a | 1031 | $$ = newANONATTRSUB($2, $3, $4, $5); } |
abcf453d PM |
1032 | | ANONSUB startanonsub remember subsignature subattrlist '{' stmtseq '}' %prec '(' |
1033 | { | |
1034 | OP *body; | |
1035 | if (parser->copline > (line_t)$6) | |
1036 | parser->copline = (line_t)$6; | |
1037 | body = block_end($3, | |
1038 | op_append_list(OP_LINESEQ, $4, $7)); | |
1039 | SvREFCNT_inc_simple_void(PL_compcv); | |
1040 | $$ = newANONATTRSUB($2, NULL, $5, body); | |
1041 | } | |
891be019 SC |
1042 | |
1043 | ; | |
1044 | ||
1045 | /* Things called with "do" */ | |
1046 | termdo : DO term %prec UNIOP /* do $filename */ | |
b5bbe64a | 1047 | { $$ = dofile($2, $1);} |
891be019 | 1048 | | DO block %prec '(' /* do { code */ |
b5bbe64a | 1049 | { $$ = newUNOP(OP_NULL, OPf_SPECIAL, op_scope($2));} |
891be019 SC |
1050 | ; |
1051 | ||
1052 | term : termbinop | |
1053 | | termunop | |
1054 | | anonymous | |
1055 | | termdo | |
e9bdcc27 | 1056 | | term '?' term ':' term |
b5bbe64a | 1057 | { $$ = newCONDOP(0, $1, $3, $5); } |
891be019 | 1058 | | REFGEN term /* \$x, \@y, \%z */ |
070d3cb6 | 1059 | { $$ = newUNOP(OP_REFGEN, 0, $2); } |
e118fea3 FC |
1060 | | MY REFGEN term |
1061 | { $$ = newUNOP(OP_REFGEN, 0, localize($3,1)); } | |
09bef843 SB |
1062 | | myattrterm %prec UNIOP |
1063 | { $$ = $1; } | |
1064 | | LOCAL term %prec UNIOP | |
28383d1a | 1065 | { $$ = localize($2,0); } |
a0d0e21e | 1066 | | '(' expr ')' |
b5bbe64a | 1067 | { $$ = sawparens($2); } |
ea25a9b2 | 1068 | | QWLIST |
b5bbe64a | 1069 | { $$ = $1; } |
8d063cd8 | 1070 | | '(' ')' |
b5bbe64a | 1071 | { $$ = sawparens(newNULLLIST()); } |
79072805 | 1072 | | scalar %prec '(' |
8d063cd8 | 1073 | { $$ = $1; } |
79072805 | 1074 | | star %prec '(' |
8d063cd8 | 1075 | { $$ = $1; } |
79072805 | 1076 | | hsh %prec '(' |
8d063cd8 | 1077 | { $$ = $1; } |
79072805 | 1078 | | ary %prec '(' |
8d063cd8 | 1079 | { $$ = $1; } |
891be019 | 1080 | | arylen %prec '(' /* $#x, $#{ something } */ |
79072805 | 1081 | { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));} |
fad39ff1 SM |
1082 | | subscripted |
1083 | { $$ = $1; } | |
89f35911 | 1084 | | sliceme '[' expr ']' /* array slice */ |
2fcb4757 | 1085 | { $$ = op_prepend_elem(OP_ASLICE, |
79072805 | 1086 | newOP(OP_PUSHMARK, 0), |
79072805 LW |
1087 | newLISTOP(OP_ASLICE, 0, |
1088 | list($3), | |
f05e27e5 | 1089 | ref($1, OP_ASLICE))); |
429a2555 FC |
1090 | if ($$ && $1) |
1091 | $$->op_private |= | |
1092 | $1->op_private & OPpSLICEWARNING; | |
f05e27e5 | 1093 | } |
76eba8ab | 1094 | | kvslice '[' expr ']' /* array key/value slice */ |
6dd3e0f2 RZ |
1095 | { $$ = op_prepend_elem(OP_KVASLICE, |
1096 | newOP(OP_PUSHMARK, 0), | |
1097 | newLISTOP(OP_KVASLICE, 0, | |
1098 | list($3), | |
1099 | ref(oopsAV($1), OP_KVASLICE))); | |
95a31aad FC |
1100 | if ($$ && $1) |
1101 | $$->op_private |= | |
1102 | $1->op_private & OPpSLICEWARNING; | |
6dd3e0f2 | 1103 | } |
89f35911 | 1104 | | sliceme '{' expr ';' '}' /* @hash{@keys} */ |
2fcb4757 | 1105 | { $$ = op_prepend_elem(OP_HSLICE, |
79072805 | 1106 | newOP(OP_PUSHMARK, 0), |
79072805 LW |
1107 | newLISTOP(OP_HSLICE, 0, |
1108 | list($3), | |
a0d0e21e | 1109 | ref(oopsHV($1), OP_HSLICE))); |
429a2555 FC |
1110 | if ($$ && $1) |
1111 | $$->op_private |= | |
1112 | $1->op_private & OPpSLICEWARNING; | |
f05e27e5 | 1113 | } |
76eba8ab | 1114 | | kvslice '{' expr ';' '}' /* %hash{@keys} */ |
5cae3edb RZ |
1115 | { $$ = op_prepend_elem(OP_KVHSLICE, |
1116 | newOP(OP_PUSHMARK, 0), | |
1117 | newLISTOP(OP_KVHSLICE, 0, | |
1118 | list($3), | |
1119 | ref($1, OP_KVHSLICE))); | |
95a31aad FC |
1120 | if ($$ && $1) |
1121 | $$->op_private |= | |
1122 | $1->op_private & OPpSLICEWARNING; | |
5cae3edb | 1123 | } |
79072805 LW |
1124 | | THING %prec '(' |
1125 | { $$ = $1; } | |
891be019 | 1126 | | amper /* &foo; */ |
c07a80fd | 1127 | { $$ = newUNOP(OP_ENTERSUB, 0, scalar($1)); } |
fb602e32 | 1128 | | amper '(' ')' /* &foo() or foo() */ |
f05e27e5 | 1129 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); |
f05e27e5 | 1130 | } |
fb602e32 | 1131 | | amper '(' expr ')' /* &foo(@args) or foo(@args) */ |
f05e27e5 DM |
1132 | { |
1133 | $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, | |
2fcb4757 | 1134 | op_append_elem(OP_LIST, $3, scalar($1))); |
f05e27e5 | 1135 | } |
fb602e32 | 1136 | | NOAMP subname optlistexpr /* foo @args (no parens) */ |
a0d0e21e | 1137 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
2fcb4757 | 1138 | op_append_elem(OP_LIST, $3, scalar($2))); |
f05e27e5 | 1139 | } |
89f35911 | 1140 | | term ARROW '$' '*' |
b5bbe64a | 1141 | { $$ = newSVREF($1); } |
89f35911 | 1142 | | term ARROW '@' '*' |
b5bbe64a | 1143 | { $$ = newAVREF($1); } |
89f35911 | 1144 | | term ARROW '%' '*' |
b5bbe64a | 1145 | { $$ = newHVREF($1); } |
89f35911 FC |
1146 | | term ARROW '&' '*' |
1147 | { $$ = newUNOP(OP_ENTERSUB, 0, | |
b5bbe64a | 1148 | scalar(newCVREF($3,$1))); } |
89f35911 | 1149 | | term ARROW '*' '*' %prec '(' |
b5bbe64a | 1150 | { $$ = newGVREF(0,$1); } |
891be019 | 1151 | | LOOPEX /* loop exiting command (goto, last, dump, etc) */ |
b5bbe64a JH |
1152 | { $$ = newOP($1, OPf_SPECIAL); |
1153 | PL_hints |= HINT_BLOCK_SCOPE; } | |
a0d0e21e | 1154 | | LOOPEX term |
b5bbe64a | 1155 | { $$ = newLOOPEX($1,$2); } |
09b1cffe | 1156 | | NOTOP listexpr /* not $foo */ |
b5bbe64a | 1157 | { $$ = newUNOP(OP_NOT, 0, scalar($2)); } |
891be019 | 1158 | | UNIOP /* Unary op, $_ implied */ |
b5bbe64a | 1159 | { $$ = newOP($1, 0); } |
f05e27e5 | 1160 | | UNIOP block /* eval { foo }* */ |
b5bbe64a | 1161 | { $$ = newUNOP($1, 0, $2); } |
891be019 | 1162 | | UNIOP term /* Unary op */ |
b5bbe64a | 1163 | { $$ = newUNOP($1, 0, $2); } |
d2fdf8fd | 1164 | | REQUIRE /* require, $_ implied */ |
b5bbe64a | 1165 | { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); } |
d2fdf8fd | 1166 | | REQUIRE term /* require Foo */ |
b5bbe64a | 1167 | { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); } |
3cd0a11a RGS |
1168 | | UNIOPSUB |
1169 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } | |
891be019 | 1170 | | UNIOPSUB term /* Sub treated as unop */ |
4633a7c4 | 1171 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, |
2fcb4757 | 1172 | op_append_elem(OP_LIST, $2, scalar($1))); } |
891be019 | 1173 | | FUNC0 /* Nullary operator */ |
b5bbe64a | 1174 | { $$ = newOP($1, 0); } |
ae986130 | 1175 | | FUNC0 '(' ')' |
b5bbe64a | 1176 | { $$ = newOP($1, 0);} |
7eb971ee FC |
1177 | | FUNC0OP /* Same as above, but op created in toke.c */ |
1178 | { $$ = $1; } | |
1179 | | FUNC0OP '(' ')' | |
b5bbe64a | 1180 | { $$ = $1; } |
891be019 | 1181 | | FUNC0SUB /* Sub treated as nullop */ |
b5bbe64a | 1182 | { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } |
891be019 | 1183 | | FUNC1 '(' ')' /* not () */ |
b5bbe64a JH |
1184 | { $$ = ($1 == OP_NOT) |
1185 | ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0))) | |
1186 | : newOP($1, OPf_SPECIAL); } | |
891be019 | 1187 | | FUNC1 '(' expr ')' /* not($foo) */ |
b5bbe64a | 1188 | { $$ = newUNOP($1, 0, $3); } |
d63c20f2 DM |
1189 | | PMFUNC /* m//, s///, qr//, tr/// */ |
1190 | { | |
1191 | if ( $1->op_type != OP_TRANS | |
1192 | && $1->op_type != OP_TRANSR | |
1193 | && (((PMOP*)$1)->op_pmflags & PMf_HAS_CV)) | |
1194 | { | |
1195 | $<ival>$ = start_subparse(FALSE, CVf_ANON); | |
1196 | SAVEFREESV(PL_compcv); | |
1197 | } else | |
1198 | $<ival>$ = 0; | |
1199 | } | |
9b6b7be8 FC |
1200 | '(' listexpr optrepl ')' |
1201 | { $$ = pmruntime($1, $4, $5, 1, $<ival>2); } | |
185c2e96 | 1202 | | BAREWORD |
378cc40b | 1203 | | listop |
be25f609 | 1204 | | YADAYADA |
1205 | { | |
1206 | $$ = newLISTOP(OP_DIE, 0, newOP(OP_PUSHMARK, 0), | |
1207 | newSVOP(OP_CONST, 0, newSVpvs("Unimplemented"))); | |
1208 | } | |
88e1f1a2 | 1209 | | PLUGEXPR |
8d063cd8 LW |
1210 | ; |
1211 | ||
891be019 | 1212 | /* "my" declarations, with optional attributes */ |
09bef843 | 1213 | myattrterm: MY myterm myattrlist |
b5bbe64a | 1214 | { $$ = my_attrs($2,$3); } |
09bef843 | 1215 | | MY myterm |
28383d1a | 1216 | { $$ = localize($2,1); } |
e118fea3 FC |
1217 | | MY REFGEN myterm myattrlist |
1218 | { $$ = newUNOP(OP_REFGEN, 0, my_attrs($3,$4)); } | |
09bef843 SB |
1219 | ; |
1220 | ||
891be019 | 1221 | /* Things that can be "my"'d */ |
09bef843 | 1222 | myterm : '(' expr ')' |
b5bbe64a | 1223 | { $$ = sawparens($2); } |
09bef843 | 1224 | | '(' ')' |
b5bbe64a JH |
1225 | { $$ = sawparens(newNULLLIST()); } |
1226 | ||
09bef843 SB |
1227 | | scalar %prec '(' |
1228 | { $$ = $1; } | |
1229 | | hsh %prec '(' | |
1230 | { $$ = $1; } | |
1231 | | ary %prec '(' | |
1232 | { $$ = $1; } | |
1233 | ; | |
1234 | ||
891be019 | 1235 | /* Basic list expressions */ |
09b1cffe | 1236 | optlistexpr: /* NULL */ %prec PREC_LOW |
bcabcc50 | 1237 | { $$ = (OP*)NULL; } |
09b1cffe | 1238 | | listexpr %prec PREC_LOW |
a0d0e21e LW |
1239 | { $$ = $1; } |
1240 | ; | |
1241 | ||
09b1cffe | 1242 | optexpr: /* NULL */ |
bcabcc50 | 1243 | { $$ = (OP*)NULL; } |
79072805 LW |
1244 | | expr |
1245 | { $$ = $1; } | |
1246 | ; | |
1247 | ||
9b6b7be8 FC |
1248 | optrepl: /* NULL */ |
1249 | { $$ = (OP*)NULL; } | |
1250 | | '/' expr | |
1251 | { $$ = $2; } | |
1252 | ; | |
1253 | ||
891be019 SC |
1254 | /* A little bit of trickery to make "for my $foo (@bar)" actually be |
1255 | lexical */ | |
55497cff | 1256 | my_scalar: scalar |
624fa8bd | 1257 | { parser->in_my = 0; $$ = my($1); } |
55497cff PP |
1258 | ; |
1259 | ||
d39c26a6 FC |
1260 | my_var : scalar |
1261 | | ary | |
1262 | | hsh | |
1263 | ; | |
1264 | ||
1265 | refgen_topic: my_var | |
1266 | | amper | |
1267 | ; | |
1268 | ||
e118fea3 FC |
1269 | my_refgen: MY REFGEN |
1270 | | REFGEN MY | |
1271 | ; | |
1272 | ||
79072805 | 1273 | amper : '&' indirob |
b5bbe64a | 1274 | { $$ = newCVREF($1,$2); } |
a687059c LW |
1275 | ; |
1276 | ||
79072805 | 1277 | scalar : '$' indirob |
b5bbe64a | 1278 | { $$ = newSVREF($2); } |
a687059c LW |
1279 | ; |
1280 | ||
79072805 | 1281 | ary : '@' indirob |
f05e27e5 | 1282 | { $$ = newAVREF($2); |
b5bbe64a | 1283 | if ($$) $$->op_private |= $1; |
f05e27e5 | 1284 | } |
79072805 LW |
1285 | ; |
1286 | ||
1287 | hsh : '%' indirob | |
f05e27e5 | 1288 | { $$ = newHVREF($2); |
b5bbe64a | 1289 | if ($$) $$->op_private |= $1; |
f05e27e5 | 1290 | } |
79072805 LW |
1291 | ; |
1292 | ||
1293 | arylen : DOLSHARP indirob | |
b5bbe64a | 1294 | { $$ = newAVREF($2); } |
ff25e5db | 1295 | | term ARROW DOLSHARP '*' |
b5bbe64a | 1296 | { $$ = newAVREF($1); } |
79072805 LW |
1297 | ; |
1298 | ||
1299 | star : '*' indirob | |
b5bbe64a | 1300 | { $$ = newGVREF(0,$2); } |
79072805 LW |
1301 | ; |
1302 | ||
89f35911 FC |
1303 | sliceme : ary |
1304 | | term ARROW '@' | |
b5bbe64a | 1305 | { $$ = newAVREF($1); } |
89f35911 FC |
1306 | ; |
1307 | ||
76eba8ab FC |
1308 | kvslice : hsh |
1309 | | term ARROW '%' | |
b5bbe64a | 1310 | { $$ = newHVREF($1); } |
76eba8ab FC |
1311 | ; |
1312 | ||
89f35911 FC |
1313 | gelem : star |
1314 | | term ARROW '*' | |
b5bbe64a | 1315 | { $$ = newGVREF(0,$1); } |
89f35911 FC |
1316 | ; |
1317 | ||
891be019 | 1318 | /* Indirect objects */ |
185c2e96 | 1319 | indirob : BAREWORD |
79072805 | 1320 | { $$ = scalar($1); } |
fad39ff1 | 1321 | | scalar %prec PREC_LOW |
f05e27e5 | 1322 | { $$ = scalar($1); } |
79072805 | 1323 | | block |
3ad73efd | 1324 | { $$ = op_scope($1); } |
79072805 | 1325 | |
93a17b20 LW |
1326 | | PRIVATEREF |
1327 | { $$ = $1; } | |
8d063cd8 | 1328 | ; |