This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
move easy lexer state variables into PL_parser
[perl5.git] / perly.c
1 /*    perly.c
2  *
3  *    Copyright (c) 2004, 2005, 2006 Larry Wall and others
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  * 
8  *    Note that this file was originally generated as an output from
9  *    GNU bison version 1.875, but now the code is statically maintained
10  *    and edited; the bits that are dependent on perly.y are now
11  *    #included from the files perly.tab and perly.act.
12  *
13  *    Here is an important copyright statement from the original, generated
14  *    file:
15  *
16  *      As a special exception, when this file is copied by Bison into a
17  *      Bison output file, you may use that output file without
18  *      restriction.  This special exception was added by the Free
19  *      Software Foundation in version 1.24 of Bison.
20  *
21  * Note that this file is also #included in madly.c, to allow compilation
22  * of a second parser, Perl_madparse, that is identical to Perl_yyparse,
23  * but which includes extra code for dumping the parse tree.
24  * This is controlled by the PERL_IN_MADLY_C define.
25  */
26
27 #include "EXTERN.h"
28 #define PERL_IN_PERLY_C
29 #include "perl.h"
30
31 typedef unsigned char yytype_uint8;
32 typedef signed char yytype_int8;
33 typedef unsigned short int yytype_uint16;
34 typedef short int yytype_int16;
35 typedef signed char yysigned_char;
36
37 #ifdef DEBUGGING
38 #  define YYDEBUG 1
39 #else
40 #  define YYDEBUG 0
41 #endif
42
43 /* contains all the parser state tables; auto-generated from perly.y */
44 #include "perly.tab"
45
46 # define YYSIZE_T size_t
47
48 #define YYEOF           0
49 #define YYTERROR        1
50
51 #define YYACCEPT        goto yyacceptlab
52 #define YYABORT         goto yyabortlab
53 #define YYERROR         goto yyerrlab1
54
55 /* Enable debugging if requested.  */
56 #ifdef DEBUGGING
57
58 #  define yydebug (DEBUG_p_TEST)
59
60 #  define YYFPRINTF PerlIO_printf
61
62 #  define YYDPRINTF(Args)                       \
63 do {                                            \
64     if (yydebug)                                \
65         YYFPRINTF Args;                         \
66 } while (0)
67
68 #  define YYDSYMPRINTF(Title, Token, Value)                     \
69 do {                                                            \
70     if (yydebug) {                                              \
71         YYFPRINTF (Perl_debug_log, "%s ", Title);               \
72         yysymprint (aTHX_ Perl_debug_log,  Token, Value);       \
73         YYFPRINTF (Perl_debug_log, "\n");                       \
74     }                                                           \
75 } while (0)
76
77 /*--------------------------------.
78 | Print this symbol on YYOUTPUT.  |
79 `--------------------------------*/
80
81 static void
82 yysymprint(pTHX_ PerlIO * const yyoutput, int yytype, const YYSTYPE * const yyvaluep)
83 {
84     if (yytype < YYNTOKENS) {
85         YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
86 #   ifdef YYPRINT
87         YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
88 #   else
89         YYFPRINTF (yyoutput, "0x%"UVxf, (UV)yyvaluep->ival);
90 #   endif
91     }
92     else
93         YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
94
95     YYFPRINTF (yyoutput, ")");
96 }
97
98
99 /*  yy_stack_print()
100  *  print the top 8 items on the parse stack.
101  */
102
103 static void
104 yy_stack_print (pTHX_ const yy_parser *parser)
105 {
106     const yy_stack_frame *ps, *min;
107
108     min = parser->ps - 8 + 1;
109     if (min <= parser->stack)
110         min = parser->stack + 1;
111
112     PerlIO_printf(Perl_debug_log, "\nindex:");
113     for (ps = min; ps <= parser->ps; ps++)
114         PerlIO_printf(Perl_debug_log, " %8d", ps - parser->stack);
115
116     PerlIO_printf(Perl_debug_log, "\nstate:");
117     for (ps = min; ps <= parser->ps; ps++)
118         PerlIO_printf(Perl_debug_log, " %8d", ps->state);
119
120     PerlIO_printf(Perl_debug_log, "\ntoken:");
121     for (ps = min; ps <= parser->ps; ps++)
122         PerlIO_printf(Perl_debug_log, " %8.8s", ps->name);
123
124     PerlIO_printf(Perl_debug_log, "\nvalue:");
125     for (ps = min; ps <= parser->ps; ps++) {
126         switch (yy_type_tab[yystos[ps->state]]) {
127         case toketype_opval:
128             PerlIO_printf(Perl_debug_log, " %8.8s",
129                   ps->val.opval
130                     ? PL_op_name[ps->val.opval->op_type]
131                     : "(Nullop)"
132             );
133             break;
134 #ifndef PERL_IN_MADLY_C
135         case toketype_p_tkval:
136             PerlIO_printf(Perl_debug_log, " %8.8s",
137                   ps->val.pval ? ps->val.pval : "(NULL)");
138             break;
139
140         case toketype_i_tkval:
141 #endif
142         case toketype_ival:
143             PerlIO_printf(Perl_debug_log, " %8"IVdf, (IV)ps->val.ival);
144             break;
145         default:
146             PerlIO_printf(Perl_debug_log, " %8"UVxf, (UV)ps->val.ival);
147         }
148     }
149     PerlIO_printf(Perl_debug_log, "\n\n");
150 }
151
152 #  define YY_STACK_PRINT(parser)        \
153 do {                                    \
154     if (yydebug && DEBUG_v_TEST)        \
155         yy_stack_print (aTHX_ parser);  \
156 } while (0)
157
158
159 /*------------------------------------------------.
160 | Report that the YYRULE is going to be reduced.  |
161 `------------------------------------------------*/
162
163 static void
164 yy_reduce_print (pTHX_ int yyrule)
165 {
166     int yyi;
167     const unsigned int yylineno = yyrline[yyrule];
168     YYFPRINTF (Perl_debug_log, "Reducing stack by rule %d (line %u), ",
169                           yyrule - 1, yylineno);
170     /* Print the symbols being reduced, and their result.  */
171     for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++)
172         YYFPRINTF (Perl_debug_log, "%s ", yytname [yyrhs[yyi]]);
173     YYFPRINTF (Perl_debug_log, "-> %s\n", yytname [yyr1[yyrule]]);
174 }
175
176 #  define YY_REDUCE_PRINT(Rule)         \
177 do {                                    \
178     if (yydebug)                        \
179         yy_reduce_print (aTHX_ Rule);           \
180 } while (0)
181
182 #else /* !DEBUGGING */
183 #  define YYDPRINTF(Args)
184 #  define YYDSYMPRINTF(Title, Token, Value)
185 #  define YY_STACK_PRINT(parser)
186 #  define YY_REDUCE_PRINT(Rule)
187 #endif /* !DEBUGGING */
188
189 /* called during cleanup (via SAVEDESTRUCTOR_X) to free any items on the
190  * parse stack, thus avoiding leaks if we die  */
191
192 static void
193 S_clear_yystack(pTHX_  const yy_parser *parser)
194 {
195     yy_stack_frame *ps     = parser->ps;
196     int i;
197
198     if (!parser->stack || ps == parser->stack)
199         return;
200
201     YYDPRINTF ((Perl_debug_log, "clearing the parse stack\n"));
202
203     /* Freeing ops on the stack, and the op_latefree / op_latefreed /
204      * op_attached flags:
205      *
206      * When we pop tokens off the stack during error recovery, or when
207      * we pop all the tokens off the stack after a die during a shift or
208      * reduce (i.e. Perl_croak somewhere in yylex() or in one of the
209      * newFOO() functions), then it's possible that some of these tokens are
210      * of type opval, pointing to an OP. All these ops are orphans; each is
211      * its own miniature subtree that has not yet been attached to a
212      * larger tree. In this case, we should clearly free the op (making
213      * sure, for each op we free that we have PL_comppad pointing to the
214      * right place for freeing any SVs attached to the op in threaded
215      * builds.
216      *
217      * However, there is a particular problem if we die in newFOO() called
218      * by a reducing action; e.g.
219      *
220      *    foo : bar baz boz
221      *        { $$ = newFOO($1,$2,$3) }
222      *
223      * where
224      *  OP *newFOO { ....; if (...) croak; .... }
225      *
226      * In this case, when we come to clean bar baz and boz off the stack,
227      * we don't know whether newFOO() has already:
228      *    * freed them
229      *    * left them as is
230      *    * attached them to part of a larger tree
231      *    * attached them to PL_compcv
232      *    * attached them to PL_compcv then freed it (as in BEGIN {die } )
233      *
234      * To get round this problem, we set the flag op_latefree on every op
235      * that gets pushed onto the parser stack. If op_free() sees this
236      * flag, it clears the op and frees any children,, but *doesn't* free
237      * the op itself; instead it sets the op_latefreed flag. This means
238      * that we can safely call op_free() multiple times on each stack op.
239      * So, when clearing the stack, we first, for each op that was being
240      * reduced, call op_free with op_latefree=1. This ensures that all ops
241      * hanging off these op are freed, but the reducing ops themselces are
242      * just undefed. Then we set op_latefreed=0 on *all* ops on the stack
243      * and free them. A little thought should convince you that this
244      * two-part approach to the reducing ops should handle the first three
245      * cases above safely.
246      *
247      * In the case of attaching to PL_compcv (currently just newATTRSUB
248      * does this), then  we set the op_attached flag on the op that has
249      * been so attached, then avoid doing the final op_free during
250      * cleanup, on the assumption that it will happen (or has already
251      * happened) when PL_compcv is freed.
252      *
253      * Note this is fairly fragile mechanism. A more robust approach
254      * would be to use two of these flag bits as 2-bit reference count
255      * field for each op, indicating whether it is pointed to from:
256      *   * a parent op
257      *   * the parser stack
258      *   * a CV
259      * but this would involve reworking all code (core and external) that
260      * manipulate op trees.
261      */
262
263     /* clear any reducing ops (1st pass) */
264
265     for (i=0; i< parser->yylen; i++) {
266         if (yy_type_tab[yystos[ps[-i].state]] == toketype_opval
267             && ps[-i].val.opval) {
268             if ( ! (ps[-i].val.opval->op_attached
269                     && !ps[-i].val.opval->op_latefreed))
270             {
271                 if (ps[-i].comppad != PL_comppad) {
272                     PAD_RESTORE_LOCAL(ps[-i].comppad);
273                 }
274                 op_free(ps[-i].val.opval);
275             }
276         }
277     }
278
279     /* now free whole the stack, including the just-reduced ops */
280
281     while (ps > parser->stack) {
282         if (yy_type_tab[yystos[ps->state]] == toketype_opval
283             && ps->val.opval)
284         {
285             if (ps->comppad != PL_comppad) {
286                 PAD_RESTORE_LOCAL(ps->comppad);
287             }
288             YYDPRINTF ((Perl_debug_log, "(freeing op)\n"));
289             ps->val.opval->op_latefree  = 0;
290             if (!(ps->val.opval->op_attached && !ps->val.opval->op_latefreed))
291                 op_free(ps->val.opval);
292         }
293         ps--;
294     }
295 }
296
297 /* delete a parser object */
298
299 void
300 Perl_parser_free(pTHX_  const yy_parser *parser)
301 {
302     S_clear_yystack(aTHX_ parser);
303     Safefree(parser->stack);
304     PL_parser = parser->old_parser;
305 }
306
307
308 /*----------.
309 | yyparse.  |
310 `----------*/
311
312 int
313 #ifdef PERL_IN_MADLY_C
314 Perl_madparse (pTHX)
315 #else
316 Perl_yyparse (pTHX)
317 #endif
318 {
319     dVAR;
320     register int yystate;
321     register int yyn;
322     int yyresult;
323
324     /* Lookahead token as an internal (translated) token number.  */
325     int yytoken = 0;
326
327     register yy_parser *parser;     /* the parser object */
328     register yy_stack_frame  *ps;   /* current parser stack frame */
329
330 #define YYPOPSTACK   parser->ps = --ps
331 #define YYPUSHSTACK  parser->ps = ++ps
332
333     /* The variable used to return semantic value and location from the
334           action routines: ie $$.  */
335     YYSTYPE yyval;
336
337 #ifndef PERL_IN_MADLY_C
338 #  ifdef PERL_MAD
339     if (PL_madskills)
340         return madparse();
341 #  endif
342 #endif
343
344     YYDPRINTF ((Perl_debug_log, "Starting parse\n"));
345
346     parser = PL_parser;
347     ps = parser->ps;
348
349     ENTER;  /* force parser free before we return */
350     SAVEDESTRUCTOR_X(Perl_parser_free, (void*) parser);
351
352 /*------------------------------------------------------------.
353 | yynewstate -- Push a new state, which is found in yystate.  |
354 `------------------------------------------------------------*/
355   yynewstate:
356
357     yystate = ps->state;
358
359     YYDPRINTF ((Perl_debug_log, "Entering state %d\n", yystate));
360
361     if (yy_type_tab[yystos[yystate]] == toketype_opval && ps->val.opval) {
362         ps->val.opval->op_latefree  = 1;
363         ps->val.opval->op_latefreed = 0;
364     }
365
366     parser->yylen = 0;
367
368     {
369         size_t size = ps - parser->stack + 1;
370
371         /* grow the stack? We always leave 1 spare slot,
372          * in case of a '' -> 'foo' reduction */
373
374         if (size >= parser->stack_size - 1) {
375             /* this will croak on insufficient memory */
376             parser->stack_size *= 2;
377             Renew(parser->stack, parser->stack_size, yy_stack_frame);
378             ps = parser->ps = parser->stack + size -1;
379
380             YYDPRINTF((Perl_debug_log,
381                             "parser stack size increased to %lu frames\n",
382                             (unsigned long int)parser->stack_size));
383         }
384     }
385
386 /* Do appropriate processing given the current state.  */
387 /* Read a lookahead token if we need one and don't already have one.  */
388
389     /* First try to decide what to do without reference to lookahead token.  */
390
391     yyn = yypact[yystate];
392     if (yyn == YYPACT_NINF)
393         goto yydefault;
394
395     /* Not known => get a lookahead token if don't already have one.  */
396
397     /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol.  */
398     if (parser->yychar == YYEMPTY) {
399         YYDPRINTF ((Perl_debug_log, "Reading a token: "));
400 #ifdef PERL_IN_MADLY_C
401         parser->yychar = PL_madskills ? madlex() : yylex();
402 #else
403         parser->yychar = yylex();
404 #endif
405
406 #  ifdef EBCDIC
407         if (parser->yychar >= 0 && parser->yychar < 255) {
408             parser->yychar = NATIVE_TO_ASCII(parser->yychar);
409         }
410 #  endif
411     }
412
413     if (parser->yychar <= YYEOF) {
414         parser->yychar = yytoken = YYEOF;
415         YYDPRINTF ((Perl_debug_log, "Now at end of input.\n"));
416     }
417     else {
418         yytoken = YYTRANSLATE (parser->yychar);
419         YYDSYMPRINTF ("Next token is", yytoken, &parser->yylval);
420     }
421
422     /* If the proper action on seeing token YYTOKEN is to reduce or to
423           detect an error, take that action.  */
424     yyn += yytoken;
425     if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
426         goto yydefault;
427     yyn = yytable[yyn];
428     if (yyn <= 0) {
429         if (yyn == 0 || yyn == YYTABLE_NINF)
430             goto yyerrlab;
431         yyn = -yyn;
432         goto yyreduce;
433     }
434
435     if (yyn == YYFINAL)
436         YYACCEPT;
437
438     /* Shift the lookahead token.  */
439     YYDPRINTF ((Perl_debug_log, "Shifting token %s, ", yytname[yytoken]));
440
441     /* Discard the token being shifted unless it is eof.  */
442     if (parser->yychar != YYEOF)
443         parser->yychar = YYEMPTY;
444
445     YYPUSHSTACK;
446     ps->state   = yyn;
447     ps->val     = parser->yylval;
448     ps->comppad = PL_comppad;
449 #ifdef DEBUGGING
450     ps->name    = (const char *)(yytname[yytoken]);
451 #endif
452
453     /* Count tokens shifted since error; after three, turn off error
454           status.  */
455     if (parser->yyerrstatus)
456         parser->yyerrstatus--;
457
458     goto yynewstate;
459
460
461   /*-----------------------------------------------------------.
462   | yydefault -- do the default action for the current state.  |
463   `-----------------------------------------------------------*/
464   yydefault:
465     yyn = yydefact[yystate];
466     if (yyn == 0)
467         goto yyerrlab;
468     goto yyreduce;
469
470
471   /*-----------------------------.
472   | yyreduce -- Do a reduction.  |
473   `-----------------------------*/
474   yyreduce:
475     /* yyn is the number of a rule to reduce with.  */
476     parser->yylen = yyr2[yyn];
477
478     /* If YYLEN is nonzero, implement the default value of the action:
479       "$$ = $1".
480
481       Otherwise, the following line sets YYVAL to garbage.
482       This behavior is undocumented and Bison
483       users should not rely upon it.  Assigning to YYVAL
484       unconditionally makes the parser a bit smaller, and it avoids a
485       GCC warning that YYVAL may be used uninitialized.  */
486     yyval = ps[1-parser->yylen].val;
487
488     YY_STACK_PRINT(parser);
489     YY_REDUCE_PRINT (yyn);
490
491     switch (yyn) {
492
493
494 #define dep() deprecate("\"do\" to call subroutines")
495
496 #ifdef PERL_IN_MADLY_C
497 #  define IVAL(i) (i)->tk_lval.ival
498 #  define PVAL(p) (p)->tk_lval.pval
499 #  define TOKEN_GETMAD(a,b,c) token_getmad((a),(b),(c))
500 #  define TOKEN_FREE(a) token_free(a)
501 #  define OP_GETMAD(a,b,c) op_getmad((a),(b),(c))
502 #  define IF_MAD(a,b) (a)
503 #  define DO_MAD(a) a
504 #  define MAD
505 #else
506 #  define IVAL(i) (i)
507 #  define PVAL(p) (p)
508 #  define TOKEN_GETMAD(a,b,c)
509 #  define TOKEN_FREE(a)
510 #  define OP_GETMAD(a,b,c)
511 #  define IF_MAD(a,b) (b)
512 #  define DO_MAD(a)
513 #  undef MAD
514 #endif
515
516 /* contains all the rule actions; auto-generated from perly.y */
517 #include "perly.act"
518
519     }
520
521     /* any just-reduced ops with the op_latefreed flag cleared need to be
522      * freed; the rest need the flag resetting */
523     {
524         int i;
525         for (i=0; i< parser->yylen; i++) {
526             if (yy_type_tab[yystos[ps[-i].state]] == toketype_opval
527                 && ps[-i].val.opval)
528             {
529                 ps[-i].val.opval->op_latefree = 0;
530                 if (ps[-i].val.opval->op_latefreed)
531                     op_free(ps[-i].val.opval);
532             }
533         }
534     }
535
536     parser->ps = ps -= (parser->yylen-1);
537
538     /* Now shift the result of the reduction.  Determine what state
539           that goes to, based on the state we popped back to and the rule
540           number reduced by.  */
541
542     ps->val     = yyval;
543     ps->comppad = PL_comppad;
544 #ifdef DEBUGGING
545     ps->name    = (const char *)(yytname [yyr1[yyn]]);
546 #endif
547
548     yyn = yyr1[yyn];
549
550     yystate = yypgoto[yyn - YYNTOKENS] + ps[-1].state;
551     if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == ps[-1].state)
552         yystate = yytable[yystate];
553     else
554         yystate = yydefgoto[yyn - YYNTOKENS];
555     ps->state = yystate;
556
557     goto yynewstate;
558
559
560   /*------------------------------------.
561   | yyerrlab -- here on detecting error |
562   `------------------------------------*/
563   yyerrlab:
564     /* If not already recovering from an error, report this error.  */
565     if (!parser->yyerrstatus) {
566         yyerror ("syntax error");
567     }
568
569
570     if (parser->yyerrstatus == 3) {
571         /* If just tried and failed to reuse lookahead token after an
572               error, discard it.  */
573
574         /* Return failure if at end of input.  */
575         if (parser->yychar == YYEOF) {
576             /* Pop the error token.  */
577             YYPOPSTACK;
578             /* Pop the rest of the stack.  */
579             while (ps > parser->stack) {
580                 YYDSYMPRINTF ("Error: popping", yystos[ps->state], &ps->val);
581                 if (yy_type_tab[yystos[ps->state]] == toketype_opval
582                         && ps->val.opval)
583                 {
584                     YYDPRINTF ((Perl_debug_log, "(freeing op)\n"));
585                     if (ps->comppad != PL_comppad) {
586                         PAD_RESTORE_LOCAL(ps->comppad);
587                     }
588                     ps->val.opval->op_latefree  = 0;
589                     op_free(ps->val.opval);
590                 }
591                 YYPOPSTACK;
592             }
593             YYABORT;
594         }
595
596         YYDSYMPRINTF ("Error: discarding", yytoken, &parser->yylval);
597         parser->yychar = YYEMPTY;
598
599     }
600
601     /* Else will try to reuse lookahead token after shifting the error
602           token.  */
603     goto yyerrlab1;
604
605
606   /*----------------------------------------------------.
607   | yyerrlab1 -- error raised explicitly by an action.  |
608   `----------------------------------------------------*/
609   yyerrlab1:
610     parser->yyerrstatus = 3;    /* Each real token shifted decrements this.  */
611
612     for (;;) {
613         yyn = yypact[yystate];
614         if (yyn != YYPACT_NINF) {
615             yyn += YYTERROR;
616             if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) {
617                 yyn = yytable[yyn];
618                 if (0 < yyn)
619                     break;
620             }
621         }
622
623         /* Pop the current state because it cannot handle the error token.  */
624         if (ps == parser->stack)
625             YYABORT;
626
627         YYDSYMPRINTF ("Error: popping", yystos[ps->state], &ps->val);
628         if (yy_type_tab[yystos[ps->state]] == toketype_opval && ps->val.opval) {
629             YYDPRINTF ((Perl_debug_log, "(freeing op)\n"));
630             if (ps->comppad != PL_comppad) {
631                 PAD_RESTORE_LOCAL(ps->comppad);
632             }
633             ps->val.opval->op_latefree  = 0;
634             op_free(ps->val.opval);
635         }
636         YYPOPSTACK;
637         yystate = ps->state;
638
639         YY_STACK_PRINT(parser);
640     }
641
642     if (yyn == YYFINAL)
643         YYACCEPT;
644
645     YYDPRINTF ((Perl_debug_log, "Shifting error token, "));
646
647     YYPUSHSTACK;
648     ps->state   = yyn;
649     ps->val     = parser->yylval;
650     ps->comppad = PL_comppad;
651 #ifdef DEBUGGING
652     ps->name    ="<err>";
653 #endif
654
655     goto yynewstate;
656
657
658   /*-------------------------------------.
659   | yyacceptlab -- YYACCEPT comes here.  |
660   `-------------------------------------*/
661   yyacceptlab:
662     yyresult = 0;
663     parser->ps = parser->stack; /* disable cleanup */
664     goto yyreturn;
665
666   /*-----------------------------------.
667   | yyabortlab -- YYABORT comes here.  |
668   `-----------------------------------*/
669   yyabortlab:
670     yyresult = 1;
671     goto yyreturn;
672
673   yyreturn:
674     LEAVE;                      /* force parser free before we return */
675     return yyresult;
676 }
677
678 /*
679  * Local variables:
680  * c-indentation-style: bsd
681  * c-basic-offset: 4
682  * indent-tabs-mode: t
683  * End:
684  *
685  * ex: set ts=8 sts=4 sw=4 noet:
686  */