This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[MERGE] eliminate OP_PUSHRE and optimise split()
[perl5.git] / perly.c
1 /*    perly.c
2  *
3  *    Copyright (c) 2004, 2005, 2006, 2007, 2008,
4  *    2009, 2010, 2011 by Larry Wall and others
5  *
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.
8  * 
9  *    Note that this file was originally generated as an output from
10  *    GNU bison version 1.875, but now the code is statically maintained
11  *    and edited; the bits that are dependent on perly.y are now
12  *    #included from the files perly.tab and perly.act.
13  *
14  *    Here is an important copyright statement from the original, generated
15  *    file:
16  *
17  *      As a special exception, when this file is copied by Bison into a
18  *      Bison output file, you may use that output file without
19  *      restriction.  This special exception was added by the Free
20  *      Software Foundation in version 1.24 of Bison.
21  *
22  */
23
24 #include "EXTERN.h"
25 #define PERL_IN_PERLY_C
26 #include "perl.h"
27 #include "feature.h"
28 #include "keywords.h"
29
30 typedef unsigned char yytype_uint8;
31 typedef signed char yytype_int8;
32 typedef unsigned short int yytype_uint16;
33 typedef short int yytype_int16;
34 typedef signed char yysigned_char;
35
36 /* YYINITDEPTH -- initial size of the parser's stacks.  */
37 #define YYINITDEPTH 200
38
39 #ifdef YYDEBUG
40 #  undef YYDEBUG
41 #endif
42 #ifdef DEBUGGING
43 #  define YYDEBUG 1
44 #else
45 #  define YYDEBUG 0
46 #endif
47
48 #ifndef YY_NULL
49 # define YY_NULL 0
50 #endif
51
52 #ifndef YY_NULLPTR
53 # define YY_NULLPTR NULL
54 #endif
55
56 /* contains all the parser state tables; auto-generated from perly.y */
57 #include "perly.tab"
58
59 # define YYSIZE_T size_t
60
61 #define YYEOF           0
62 #define YYTERROR        1
63
64 #define YYACCEPT        goto yyacceptlab
65 #define YYABORT         goto yyabortlab
66 #define YYERROR         goto yyerrlab1
67
68 /* Enable debugging if requested.  */
69 #ifdef DEBUGGING
70
71 #  define yydebug (DEBUG_p_TEST)
72
73 #  define YYFPRINTF PerlIO_printf
74
75 #  define YYDPRINTF(Args)                       \
76 do {                                            \
77     if (yydebug)                                \
78         YYFPRINTF Args;                         \
79 } while (0)
80
81 #  define YYDSYMPRINTF(Title, Token, Value)                     \
82 do {                                                            \
83     if (yydebug) {                                              \
84         YYFPRINTF (Perl_debug_log, "%s ", Title);               \
85         yysymprint (aTHX_ Perl_debug_log,  Token, Value);       \
86         YYFPRINTF (Perl_debug_log, "\n");                       \
87     }                                                           \
88 } while (0)
89
90 /*--------------------------------.
91 | Print this symbol on YYOUTPUT.  |
92 `--------------------------------*/
93
94 static void
95 yysymprint(pTHX_ PerlIO * const yyoutput, int yytype, const YYSTYPE * const yyvaluep)
96 {
97     PERL_UNUSED_CONTEXT;
98     if (yytype < YYNTOKENS) {
99         YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
100 #   ifdef YYPRINT
101         YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
102 #   else
103         YYFPRINTF (yyoutput, "0x%"UVxf, (UV)yyvaluep->ival);
104 #   endif
105     }
106     else
107         YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
108
109     YYFPRINTF (yyoutput, ")");
110 }
111
112
113 /*  yy_stack_print()
114  *  print the top 8 items on the parse stack.
115  */
116
117 static void
118 yy_stack_print (pTHX_ const yy_parser *parser)
119 {
120     const yy_stack_frame *ps, *min;
121
122     min = parser->ps - 8 + 1;
123     if (min <= parser->stack)
124         min = parser->stack + 1;
125
126     PerlIO_printf(Perl_debug_log, "\nindex:");
127     for (ps = min; ps <= parser->ps; ps++)
128         PerlIO_printf(Perl_debug_log, " %8d", (int)(ps - parser->stack));
129
130     PerlIO_printf(Perl_debug_log, "\nstate:");
131     for (ps = min; ps <= parser->ps; ps++)
132         PerlIO_printf(Perl_debug_log, " %8d", ps->state);
133
134     PerlIO_printf(Perl_debug_log, "\ntoken:");
135     for (ps = min; ps <= parser->ps; ps++)
136         PerlIO_printf(Perl_debug_log, " %8.8s", ps->name);
137
138     PerlIO_printf(Perl_debug_log, "\nvalue:");
139     for (ps = min; ps <= parser->ps; ps++) {
140         switch (yy_type_tab[yystos[ps->state]]) {
141         case toketype_opval:
142             PerlIO_printf(Perl_debug_log, " %8.8s",
143                   ps->val.opval
144                     ? PL_op_name[ps->val.opval->op_type]
145                     : "(Nullop)"
146             );
147             break;
148         case toketype_ival:
149             PerlIO_printf(Perl_debug_log, " %8"IVdf, (IV)ps->val.ival);
150             break;
151         default:
152             PerlIO_printf(Perl_debug_log, " %8"UVxf, (UV)ps->val.ival);
153         }
154     }
155     PerlIO_printf(Perl_debug_log, "\n\n");
156 }
157
158 #  define YY_STACK_PRINT(parser)        \
159 do {                                    \
160     if (yydebug && DEBUG_v_TEST)        \
161         yy_stack_print (aTHX_ parser);  \
162 } while (0)
163
164
165 /*------------------------------------------------.
166 | Report that the YYRULE is going to be reduced.  |
167 `------------------------------------------------*/
168
169 static void
170 yy_reduce_print (pTHX_ int yyrule)
171 {
172     int yyi;
173     const unsigned int yylineno = yyrline[yyrule];
174     YYFPRINTF (Perl_debug_log, "Reducing stack by rule %d (line %u), ",
175                           yyrule - 1, yylineno);
176     /* Print the symbols being reduced, and their result.  */
177 #if PERL_BISON_VERSION >= 30000 /* 3.0+ */
178     for (yyi = 0; yyi < yyr2[yyrule]; yyi++)
179         YYFPRINTF (Perl_debug_log, "%s ",
180             yytname [yystos[(PL_parser->ps)[yyi + 1 - yyr2[yyrule]].state]]);
181 #else
182     for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++)
183         YYFPRINTF (Perl_debug_log, "%s ", yytname [yyrhs[yyi]]);
184 #endif
185     YYFPRINTF (Perl_debug_log, "-> %s\n", yytname [yyr1[yyrule]]);
186 }
187
188 #  define YY_REDUCE_PRINT(Rule)         \
189 do {                                    \
190     if (yydebug)                        \
191         yy_reduce_print (aTHX_ Rule);           \
192 } while (0)
193
194 #else /* !DEBUGGING */
195 #  define YYDPRINTF(Args)
196 #  define YYDSYMPRINTF(Title, Token, Value)
197 #  define YY_STACK_PRINT(parser)
198 #  define YY_REDUCE_PRINT(Rule)
199 #endif /* !DEBUGGING */
200
201 /* called during cleanup (via SAVEDESTRUCTOR_X) to free any items on the
202  * parse stack, thus avoiding leaks if we die  */
203
204 static void
205 S_clear_yystack(pTHX_  const yy_parser *parser)
206 {
207     yy_stack_frame *ps     = parser->ps;
208     int i = 0;
209
210     if (!parser->stack)
211         return;
212
213     YYDPRINTF ((Perl_debug_log, "clearing the parse stack\n"));
214
215     for (i=0; i< parser->yylen; i++) {
216         SvREFCNT_dec(ps[-i].compcv);
217     }
218     ps -= parser->yylen;
219
220     /* now free whole the stack, including the just-reduced ops */
221
222     while (ps > parser->stack) {
223         LEAVE_SCOPE(ps->savestack_ix);
224         if (yy_type_tab[yystos[ps->state]] == toketype_opval
225             && ps->val.opval)
226         {
227             if (ps->compcv && (ps->compcv != PL_compcv)) {
228                 PL_compcv = ps->compcv;
229                 PAD_SET_CUR_NOSAVE(CvPADLIST(PL_compcv), 1);
230                 PL_comppad_name = PadlistNAMES(CvPADLIST(PL_compcv));
231             }
232             YYDPRINTF ((Perl_debug_log, "(freeing op)\n"));
233             op_free(ps->val.opval);
234         }
235         SvREFCNT_dec(ps->compcv);
236         ps--;
237     }
238
239     Safefree(parser->stack);
240 }
241
242
243 /*----------.
244 | yyparse.  |
245 `----------*/
246
247 int
248 Perl_yyparse (pTHX_ int gramtype)
249 {
250     dVAR;
251     int yystate;
252     int yyn;
253     int yyresult;
254
255     /* Lookahead token as an internal (translated) token number.  */
256     int yytoken = 0;
257
258     yy_parser *parser;      /* the parser object */
259     yy_stack_frame  *ps;   /* current parser stack frame */
260
261 #define YYPOPSTACK   parser->ps = --ps
262 #define YYPUSHSTACK  parser->ps = ++ps
263
264     /* The variable used to return semantic value and location from the
265           action routines: ie $$.  */
266     YYSTYPE yyval;
267
268     YYDPRINTF ((Perl_debug_log, "Starting parse\n"));
269
270     parser = PL_parser;
271
272     ENTER;  /* force parser state cleanup/restoration before we return */
273     SAVEPPTR(parser->yylval.pval);
274     SAVEINT(parser->yychar);
275     SAVEINT(parser->yyerrstatus);
276     SAVEINT(parser->stack_size);
277     SAVEINT(parser->yylen);
278     SAVEVPTR(parser->stack);
279     SAVEVPTR(parser->ps);
280
281     /* initialise state for this parse */
282     parser->yychar = gramtype;
283     parser->yyerrstatus = 0;
284     parser->stack_size = YYINITDEPTH;
285     parser->yylen = 0;
286     Newx(parser->stack, YYINITDEPTH, yy_stack_frame);
287     ps = parser->ps = parser->stack;
288     ps->state = 0;
289     SAVEDESTRUCTOR_X(S_clear_yystack, parser);
290
291 /*------------------------------------------------------------.
292 | yynewstate -- Push a new state, which is found in yystate.  |
293 `------------------------------------------------------------*/
294   yynewstate:
295
296     yystate = ps->state;
297
298     YYDPRINTF ((Perl_debug_log, "Entering state %d\n", yystate));
299
300     parser->yylen = 0;
301
302     {
303         size_t size = ps - parser->stack + 1;
304
305         /* grow the stack? We always leave 1 spare slot,
306          * in case of a '' -> 'foo' reduction */
307
308         if (size >= (size_t)parser->stack_size - 1) {
309             /* this will croak on insufficient memory */
310             parser->stack_size *= 2;
311             Renew(parser->stack, parser->stack_size, yy_stack_frame);
312             ps = parser->ps = parser->stack + size -1;
313
314             YYDPRINTF((Perl_debug_log,
315                             "parser stack size increased to %lu frames\n",
316                             (unsigned long int)parser->stack_size));
317         }
318     }
319
320 /* Do appropriate processing given the current state.  */
321 /* Read a lookahead token if we need one and don't already have one.  */
322
323     /* First try to decide what to do without reference to lookahead token.  */
324
325     yyn = yypact[yystate];
326     if (yyn == YYPACT_NINF)
327         goto yydefault;
328
329     /* Not known => get a lookahead token if don't already have one.  */
330
331     /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol.  */
332     if (parser->yychar == YYEMPTY) {
333         YYDPRINTF ((Perl_debug_log, "Reading a token:\n"));
334         parser->yychar = yylex();
335     }
336
337     if (parser->yychar <= YYEOF) {
338         parser->yychar = yytoken = YYEOF;
339         YYDPRINTF ((Perl_debug_log, "Now at end of input.\n"));
340     }
341     else {
342         /* perly.tab is shipped based on an ASCII system, so need to index it
343          * with characters translated to ASCII.  Although it's not designed for
344          * this purpose, we can use NATIVE_TO_UNI here.  It returns its
345          * argument on ASCII platforms, and on EBCDIC translates native to
346          * ascii in the 0-255 range, leaving everything else unchanged.  This
347          * jibes with yylex() returning some bare characters in that range, but
348          * all tokens it returns are either 0, or above 255.  There could be a
349          * problem if NULs weren't 0, or were ever returned as raw chars by
350          * yylex() */
351         yytoken = YYTRANSLATE (NATIVE_TO_UNI(parser->yychar));
352         YYDSYMPRINTF ("Next token is", yytoken, &parser->yylval);
353     }
354
355     /* If the proper action on seeing token YYTOKEN is to reduce or to
356           detect an error, take that action.  */
357     yyn += yytoken;
358     if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
359         goto yydefault;
360     yyn = yytable[yyn];
361     if (yyn <= 0) {
362         if (yyn == 0 || yyn == YYTABLE_NINF)
363             goto yyerrlab;
364         yyn = -yyn;
365         goto yyreduce;
366     }
367
368     if (yyn == YYFINAL)
369         YYACCEPT;
370
371     /* Shift the lookahead token.  */
372     YYDPRINTF ((Perl_debug_log, "Shifting token %s, ", yytname[yytoken]));
373
374     /* Discard the token being shifted unless it is eof.  */
375     if (parser->yychar != YYEOF)
376         parser->yychar = YYEMPTY;
377
378     YYPUSHSTACK;
379     ps->state   = yyn;
380     ps->val     = parser->yylval;
381     ps->compcv  = (CV*)SvREFCNT_inc(PL_compcv);
382     ps->savestack_ix = PL_savestack_ix;
383 #ifdef DEBUGGING
384     ps->name    = (const char *)(yytname[yytoken]);
385 #endif
386
387     /* Count tokens shifted since error; after three, turn off error
388           status.  */
389     if (parser->yyerrstatus)
390         parser->yyerrstatus--;
391
392     goto yynewstate;
393
394
395   /*-----------------------------------------------------------.
396   | yydefault -- do the default action for the current state.  |
397   `-----------------------------------------------------------*/
398   yydefault:
399     yyn = yydefact[yystate];
400     if (yyn == 0)
401         goto yyerrlab;
402     goto yyreduce;
403
404
405   /*-----------------------------.
406   | yyreduce -- Do a reduction.  |
407   `-----------------------------*/
408   yyreduce:
409     /* yyn is the number of a rule to reduce with.  */
410     parser->yylen = yyr2[yyn];
411
412     /* If YYLEN is nonzero, implement the default value of the action:
413       "$$ = $1".
414
415       Otherwise, the following line sets YYVAL to garbage.
416       This behavior is undocumented and Bison
417       users should not rely upon it.  Assigning to YYVAL
418       unconditionally makes the parser a bit smaller, and it avoids a
419       GCC warning that YYVAL may be used uninitialized.  */
420     yyval = ps[1-parser->yylen].val;
421
422     YY_STACK_PRINT(parser);
423     YY_REDUCE_PRINT (yyn);
424
425     switch (yyn) {
426
427 /* contains all the rule actions; auto-generated from perly.y */
428 #include "perly.act"
429
430     }
431
432     {
433         int i;
434         for (i=0; i< parser->yylen; i++) {
435             SvREFCNT_dec(ps[-i].compcv);
436         }
437     }
438
439     parser->ps = ps -= (parser->yylen-1);
440
441     /* Now shift the result of the reduction.  Determine what state
442           that goes to, based on the state we popped back to and the rule
443           number reduced by.  */
444
445     ps->val     = yyval;
446     ps->compcv  = (CV*)SvREFCNT_inc(PL_compcv);
447     ps->savestack_ix = PL_savestack_ix;
448 #ifdef DEBUGGING
449     ps->name    = (const char *)(yytname [yyr1[yyn]]);
450 #endif
451
452     yyn = yyr1[yyn];
453
454     yystate = yypgoto[yyn - YYNTOKENS] + ps[-1].state;
455     if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == ps[-1].state)
456         yystate = yytable[yystate];
457     else
458         yystate = yydefgoto[yyn - YYNTOKENS];
459     ps->state = yystate;
460
461     goto yynewstate;
462
463
464   /*------------------------------------.
465   | yyerrlab -- here on detecting error |
466   `------------------------------------*/
467   yyerrlab:
468     /* If not already recovering from an error, report this error.  */
469     if (!parser->yyerrstatus) {
470         yyerror ("syntax error");
471     }
472
473
474     if (parser->yyerrstatus == 3) {
475         /* If just tried and failed to reuse lookahead token after an
476               error, discard it.  */
477
478         /* Return failure if at end of input.  */
479         if (parser->yychar == YYEOF) {
480             /* Pop the error token.  */
481             SvREFCNT_dec(ps->compcv);
482             YYPOPSTACK;
483             /* Pop the rest of the stack.  */
484             while (ps > parser->stack) {
485                 YYDSYMPRINTF ("Error: popping", yystos[ps->state], &ps->val);
486                 LEAVE_SCOPE(ps->savestack_ix);
487                 if (yy_type_tab[yystos[ps->state]] == toketype_opval
488                         && ps->val.opval)
489                 {
490                     YYDPRINTF ((Perl_debug_log, "(freeing op)\n"));
491                     if (ps->compcv != PL_compcv) {
492                         PL_compcv = ps->compcv;
493                         PAD_SET_CUR_NOSAVE(CvPADLIST(PL_compcv), 1);
494                     }
495                     op_free(ps->val.opval);
496                 }
497                 SvREFCNT_dec(ps->compcv);
498                 YYPOPSTACK;
499             }
500             YYABORT;
501         }
502
503         YYDSYMPRINTF ("Error: discarding", yytoken, &parser->yylval);
504         parser->yychar = YYEMPTY;
505
506     }
507
508     /* Else will try to reuse lookahead token after shifting the error
509           token.  */
510     goto yyerrlab1;
511
512
513   /*----------------------------------------------------.
514   | yyerrlab1 -- error raised explicitly by an action.  |
515   `----------------------------------------------------*/
516   yyerrlab1:
517     parser->yyerrstatus = 3;    /* Each real token shifted decrements this.  */
518
519     for (;;) {
520         yyn = yypact[yystate];
521         if (yyn != YYPACT_NINF) {
522             yyn += YYTERROR;
523             if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) {
524                 yyn = yytable[yyn];
525                 if (0 < yyn)
526                     break;
527             }
528         }
529
530         /* Pop the current state because it cannot handle the error token.  */
531         if (ps == parser->stack)
532             YYABORT;
533
534         YYDSYMPRINTF ("Error: popping", yystos[ps->state], &ps->val);
535         LEAVE_SCOPE(ps->savestack_ix);
536         if (yy_type_tab[yystos[ps->state]] == toketype_opval && ps->val.opval) {
537             YYDPRINTF ((Perl_debug_log, "(freeing op)\n"));
538             if (ps->compcv != PL_compcv) {
539                 PL_compcv = ps->compcv;
540                 PAD_SET_CUR_NOSAVE(CvPADLIST(PL_compcv), 1);
541             }
542             op_free(ps->val.opval);
543         }
544         SvREFCNT_dec(ps->compcv);
545         YYPOPSTACK;
546         yystate = ps->state;
547
548         YY_STACK_PRINT(parser);
549     }
550
551     if (yyn == YYFINAL)
552         YYACCEPT;
553
554     YYDPRINTF ((Perl_debug_log, "Shifting error token, "));
555
556     YYPUSHSTACK;
557     ps->state   = yyn;
558     ps->val     = parser->yylval;
559     ps->compcv  = (CV*)SvREFCNT_inc(PL_compcv);
560     ps->savestack_ix = PL_savestack_ix;
561 #ifdef DEBUGGING
562     ps->name    ="<err>";
563 #endif
564
565     goto yynewstate;
566
567
568   /*-------------------------------------.
569   | yyacceptlab -- YYACCEPT comes here.  |
570   `-------------------------------------*/
571   yyacceptlab:
572     yyresult = 0;
573     for (ps=parser->ps; ps > parser->stack; ps--) {
574         SvREFCNT_dec(ps->compcv);
575     }
576     parser->ps = parser->stack; /* disable cleanup */
577     goto yyreturn;
578
579   /*-----------------------------------.
580   | yyabortlab -- YYABORT comes here.  |
581   `-----------------------------------*/
582   yyabortlab:
583     yyresult = 1;
584     goto yyreturn;
585
586   yyreturn:
587     LEAVE;      /* force parser stack cleanup before we return */
588     return yyresult;
589 }
590
591 /*
592  * ex: set ts=8 sts=4 sw=4 et:
593  */