1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 2.00 of the Zend license,     |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.zend.com/license/2_00.txt.                                |
11   | If you did not receive a copy of the Zend license and are unable to  |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@zend.com so we can mail you a copy immediately.              |
14   +----------------------------------------------------------------------+
15   | Authors: Marcus Boerger <helly@php.net>                              |
16   |          Nuno Lopes <nlopess@php.net>                                |
17   |          Scott MacVicar <scottmac@php.net>                           |
18   | Flex version authors:                                                |
19   |          Andi Gutmans <andi@zend.com>                                |
20   |          Zeev Suraski <zeev@zend.com>                                |
21   +----------------------------------------------------------------------+
22*/
23
24/* $Id$ */
25
26#if 0
27# define YYDEBUG(s, c) printf("state: %d char: %c\n", s, c)
28#else
29# define YYDEBUG(s, c)
30#endif
31
32#include "zend_language_scanner_defs.h"
33
34#include <errno.h>
35#include "zend.h"
36#ifdef PHP_WIN32
37# include <Winuser.h>
38#endif
39#include "zend_alloc.h"
40#include <zend_language_parser.h>
41#include "zend_compile.h"
42#include "zend_language_scanner.h"
43#include "zend_highlight.h"
44#include "zend_constants.h"
45#include "zend_variables.h"
46#include "zend_operators.h"
47#include "zend_API.h"
48#include "zend_strtod.h"
49#include "zend_exceptions.h"
50#include "zend_virtual_cwd.h"
51#include "tsrm_config_common.h"
52
53#define YYCTYPE   unsigned char
54#define YYFILL(n) { if ((YYCURSOR + n) >= (YYLIMIT + ZEND_MMAP_AHEAD)) { return 0; } }
55#define YYCURSOR  SCNG(yy_cursor)
56#define YYLIMIT   SCNG(yy_limit)
57#define YYMARKER  SCNG(yy_marker)
58
59#define YYGETCONDITION()  SCNG(yy_state)
60#define YYSETCONDITION(s) SCNG(yy_state) = s
61
62#define STATE(name)  yyc##name
63
64/* emulate flex constructs */
65#define BEGIN(state) YYSETCONDITION(STATE(state))
66#define YYSTATE      YYGETCONDITION()
67#define yytext       ((char*)SCNG(yy_text))
68#define yyleng       SCNG(yy_leng)
69#define yyless(x)    do { YYCURSOR = (unsigned char*)yytext + x; \
70                          yyleng   = (unsigned int)x; } while(0)
71#define yymore()     goto yymore_restart
72
73/* perform sanity check. If this message is triggered you should
74   increase the ZEND_MMAP_AHEAD value in the zend_streams.h file */
75/*!max:re2c */
76#if ZEND_MMAP_AHEAD < YYMAXFILL
77# error ZEND_MMAP_AHEAD should be greater than or equal to YYMAXFILL
78#endif
79
80#ifdef HAVE_STDARG_H
81# include <stdarg.h>
82#endif
83
84#ifdef HAVE_UNISTD_H
85# include <unistd.h>
86#endif
87
88/* Globals Macros */
89#define SCNG    LANG_SCNG
90#ifdef ZTS
91ZEND_API ts_rsrc_id language_scanner_globals_id;
92#else
93ZEND_API zend_php_scanner_globals language_scanner_globals;
94#endif
95
96#define HANDLE_NEWLINES(s, l)                                                   \
97do {                                                                            \
98    char *p = (s), *boundary = p+(l);                                           \
99                                                                                \
100    while (p<boundary) {                                                        \
101        if (*p == '\n' || (*p == '\r' && (*(p+1) != '\n'))) {                   \
102            CG(zend_lineno)++;                                                  \
103        }                                                                       \
104        p++;                                                                    \
105    }                                                                           \
106} while (0)
107
108#define HANDLE_NEWLINE(c) \
109{ \
110    if (c == '\n' || c == '\r') { \
111        CG(zend_lineno)++; \
112    } \
113}
114
115/* To save initial string length after scanning to first variable */
116#define SET_DOUBLE_QUOTES_SCANNED_LENGTH(len) SCNG(scanned_string_len) = (len)
117#define GET_DOUBLE_QUOTES_SCANNED_LENGTH()    SCNG(scanned_string_len)
118
119#define IS_LABEL_START(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z') || (c) == '_' || (c) >= 0x7F)
120
121#define ZEND_IS_OCT(c)  ((c)>='0' && (c)<='7')
122#define ZEND_IS_HEX(c)  (((c)>='0' && (c)<='9') || ((c)>='a' && (c)<='f') || ((c)>='A' && (c)<='F'))
123
124BEGIN_EXTERN_C()
125
126static size_t encoding_filter_script_to_internal(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length)
127{
128    const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding();
129    ZEND_ASSERT(internal_encoding);
130    return zend_multibyte_encoding_converter(to, to_length, from, from_length, internal_encoding, LANG_SCNG(script_encoding));
131}
132
133static size_t encoding_filter_script_to_intermediate(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length)
134{
135    return zend_multibyte_encoding_converter(to, to_length, from, from_length, zend_multibyte_encoding_utf8, LANG_SCNG(script_encoding));
136}
137
138static size_t encoding_filter_intermediate_to_script(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length)
139{
140    return zend_multibyte_encoding_converter(to, to_length, from, from_length,
141LANG_SCNG(script_encoding), zend_multibyte_encoding_utf8);
142}
143
144static size_t encoding_filter_intermediate_to_internal(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length)
145{
146    const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding();
147    ZEND_ASSERT(internal_encoding);
148    return zend_multibyte_encoding_converter(to, to_length, from, from_length,
149internal_encoding, zend_multibyte_encoding_utf8);
150}
151
152
153static void _yy_push_state(int new_state)
154{
155    zend_stack_push(&SCNG(state_stack), (void *) &YYGETCONDITION());
156    YYSETCONDITION(new_state);
157}
158
159#define yy_push_state(state_and_tsrm) _yy_push_state(yyc##state_and_tsrm)
160
161static void yy_pop_state(void)
162{
163    int *stack_state = zend_stack_top(&SCNG(state_stack));
164    YYSETCONDITION(*stack_state);
165    zend_stack_del_top(&SCNG(state_stack));
166}
167
168static void yy_scan_buffer(char *str, unsigned int len)
169{
170    YYCURSOR       = (YYCTYPE*)str;
171    YYLIMIT        = YYCURSOR + len;
172    if (!SCNG(yy_start)) {
173        SCNG(yy_start) = YYCURSOR;
174    }
175}
176
177void startup_scanner(void)
178{
179    CG(parse_error) = 0;
180    CG(doc_comment) = NULL;
181    zend_stack_init(&SCNG(state_stack), sizeof(int));
182    zend_ptr_stack_init(&SCNG(heredoc_label_stack));
183}
184
185static void heredoc_label_dtor(zend_heredoc_label *heredoc_label) {
186    efree(heredoc_label->label);
187}
188
189void shutdown_scanner(void)
190{
191    CG(parse_error) = 0;
192    RESET_DOC_COMMENT();
193    zend_stack_destroy(&SCNG(state_stack));
194    zend_ptr_stack_clean(&SCNG(heredoc_label_stack), (void (*)(void *)) &heredoc_label_dtor, 1);
195    zend_ptr_stack_destroy(&SCNG(heredoc_label_stack));
196}
197
198ZEND_API void zend_save_lexical_state(zend_lex_state *lex_state)
199{
200    lex_state->yy_leng   = SCNG(yy_leng);
201    lex_state->yy_start  = SCNG(yy_start);
202    lex_state->yy_text   = SCNG(yy_text);
203    lex_state->yy_cursor = SCNG(yy_cursor);
204    lex_state->yy_marker = SCNG(yy_marker);
205    lex_state->yy_limit  = SCNG(yy_limit);
206
207    lex_state->state_stack = SCNG(state_stack);
208    zend_stack_init(&SCNG(state_stack), sizeof(int));
209
210    lex_state->heredoc_label_stack = SCNG(heredoc_label_stack);
211    zend_ptr_stack_init(&SCNG(heredoc_label_stack));
212
213    lex_state->in = SCNG(yy_in);
214    lex_state->yy_state = YYSTATE;
215    lex_state->filename = zend_get_compiled_filename();
216    lex_state->lineno = CG(zend_lineno);
217
218    lex_state->script_org = SCNG(script_org);
219    lex_state->script_org_size = SCNG(script_org_size);
220    lex_state->script_filtered = SCNG(script_filtered);
221    lex_state->script_filtered_size = SCNG(script_filtered_size);
222    lex_state->input_filter = SCNG(input_filter);
223    lex_state->output_filter = SCNG(output_filter);
224    lex_state->script_encoding = SCNG(script_encoding);
225
226    lex_state->ast = CG(ast);
227    lex_state->ast_arena = CG(ast_arena);
228}
229
230ZEND_API void zend_restore_lexical_state(zend_lex_state *lex_state)
231{
232    SCNG(yy_leng)   = lex_state->yy_leng;
233    SCNG(yy_start)  = lex_state->yy_start;
234    SCNG(yy_text)   = lex_state->yy_text;
235    SCNG(yy_cursor) = lex_state->yy_cursor;
236    SCNG(yy_marker) = lex_state->yy_marker;
237    SCNG(yy_limit)  = lex_state->yy_limit;
238
239    zend_stack_destroy(&SCNG(state_stack));
240    SCNG(state_stack) = lex_state->state_stack;
241
242    zend_ptr_stack_clean(&SCNG(heredoc_label_stack), (void (*)(void *)) &heredoc_label_dtor, 1);
243    zend_ptr_stack_destroy(&SCNG(heredoc_label_stack));
244    SCNG(heredoc_label_stack) = lex_state->heredoc_label_stack;
245
246    SCNG(yy_in) = lex_state->in;
247    YYSETCONDITION(lex_state->yy_state);
248    CG(zend_lineno) = lex_state->lineno;
249    zend_restore_compiled_filename(lex_state->filename);
250
251    if (SCNG(script_filtered)) {
252        efree(SCNG(script_filtered));
253        SCNG(script_filtered) = NULL;
254    }
255    SCNG(script_org) = lex_state->script_org;
256    SCNG(script_org_size) = lex_state->script_org_size;
257    SCNG(script_filtered) = lex_state->script_filtered;
258    SCNG(script_filtered_size) = lex_state->script_filtered_size;
259    SCNG(input_filter) = lex_state->input_filter;
260    SCNG(output_filter) = lex_state->output_filter;
261    SCNG(script_encoding) = lex_state->script_encoding;
262
263    CG(ast) = lex_state->ast;
264    CG(ast_arena) = lex_state->ast_arena;
265
266    RESET_DOC_COMMENT();
267}
268
269ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle)
270{
271    zend_llist_del_element(&CG(open_files), file_handle, (int (*)(void *, void *)) zend_compare_file_handles);
272    /* zend_file_handle_dtor() operates on the copy, so we have to NULLify the original here */
273    file_handle->opened_path = NULL;
274    if (file_handle->free_filename) {
275        file_handle->filename = NULL;
276    }
277}
278
279#define BOM_UTF32_BE    "\x00\x00\xfe\xff"
280#define BOM_UTF32_LE    "\xff\xfe\x00\x00"
281#define BOM_UTF16_BE    "\xfe\xff"
282#define BOM_UTF16_LE    "\xff\xfe"
283#define BOM_UTF8        "\xef\xbb\xbf"
284
285static const zend_encoding *zend_multibyte_detect_utf_encoding(const unsigned char *script, size_t script_size)
286{
287    const unsigned char *p;
288    int wchar_size = 2;
289    int le = 0;
290
291    /* utf-16 or utf-32? */
292    p = script;
293    assert(p >= script);
294    while ((size_t)(p-script) < script_size) {
295        p = memchr(p, 0, script_size-(p-script)-2);
296        if (!p) {
297            break;
298        }
299        if (*(p+1) == '\0' && *(p+2) == '\0') {
300            wchar_size = 4;
301            break;
302        }
303
304        /* searching for UTF-32 specific byte orders, so this will do */
305        p += 4;
306    }
307
308    /* BE or LE? */
309    p = script;
310    assert(p >= script);
311    while ((size_t)(p-script) < script_size) {
312        if (*p == '\0' && *(p+wchar_size-1) != '\0') {
313            /* BE */
314            le = 0;
315            break;
316        } else if (*p != '\0' && *(p+wchar_size-1) == '\0') {
317            /* LE* */
318            le = 1;
319            break;
320        }
321        p += wchar_size;
322    }
323
324    if (wchar_size == 2) {
325        return le ? zend_multibyte_encoding_utf16le : zend_multibyte_encoding_utf16be;
326    } else {
327        return le ? zend_multibyte_encoding_utf32le : zend_multibyte_encoding_utf32be;
328    }
329
330    return NULL;
331}
332
333static const zend_encoding* zend_multibyte_detect_unicode(void)
334{
335    const zend_encoding *script_encoding = NULL;
336    int bom_size;
337    unsigned char *pos1, *pos2;
338
339    if (LANG_SCNG(script_org_size) < sizeof(BOM_UTF32_LE)-1) {
340        return NULL;
341    }
342
343    /* check out BOM */
344    if (!memcmp(LANG_SCNG(script_org), BOM_UTF32_BE, sizeof(BOM_UTF32_BE)-1)) {
345        script_encoding = zend_multibyte_encoding_utf32be;
346        bom_size = sizeof(BOM_UTF32_BE)-1;
347    } else if (!memcmp(LANG_SCNG(script_org), BOM_UTF32_LE, sizeof(BOM_UTF32_LE)-1)) {
348        script_encoding = zend_multibyte_encoding_utf32le;
349        bom_size = sizeof(BOM_UTF32_LE)-1;
350    } else if (!memcmp(LANG_SCNG(script_org), BOM_UTF16_BE, sizeof(BOM_UTF16_BE)-1)) {
351        script_encoding = zend_multibyte_encoding_utf16be;
352        bom_size = sizeof(BOM_UTF16_BE)-1;
353    } else if (!memcmp(LANG_SCNG(script_org), BOM_UTF16_LE, sizeof(BOM_UTF16_LE)-1)) {
354        script_encoding = zend_multibyte_encoding_utf16le;
355        bom_size = sizeof(BOM_UTF16_LE)-1;
356    } else if (!memcmp(LANG_SCNG(script_org), BOM_UTF8, sizeof(BOM_UTF8)-1)) {
357        script_encoding = zend_multibyte_encoding_utf8;
358        bom_size = sizeof(BOM_UTF8)-1;
359    }
360
361    if (script_encoding) {
362        /* remove BOM */
363        LANG_SCNG(script_org) += bom_size;
364        LANG_SCNG(script_org_size) -= bom_size;
365
366        return script_encoding;
367    }
368
369    /* script contains NULL bytes -> auto-detection */
370    if ((pos1 = memchr(LANG_SCNG(script_org), 0, LANG_SCNG(script_org_size)))) {
371        /* check if the NULL byte is after the __HALT_COMPILER(); */
372        pos2 = LANG_SCNG(script_org);
373
374        while (pos1 - pos2 >= sizeof("__HALT_COMPILER();")-1) {
375            pos2 = memchr(pos2, '_', pos1 - pos2);
376            if (!pos2) break;
377            pos2++;
378            if (strncasecmp((char*)pos2, "_HALT_COMPILER", sizeof("_HALT_COMPILER")-1) == 0) {
379                pos2 += sizeof("_HALT_COMPILER")-1;
380                while (*pos2 == ' '  ||
381                       *pos2 == '\t' ||
382                       *pos2 == '\r' ||
383                       *pos2 == '\n') {
384                    pos2++;
385                }
386                if (*pos2 == '(') {
387                    pos2++;
388                    while (*pos2 == ' '  ||
389                           *pos2 == '\t' ||
390                           *pos2 == '\r' ||
391                           *pos2 == '\n') {
392                        pos2++;
393                    }
394                    if (*pos2 == ')') {
395                        pos2++;
396                        while (*pos2 == ' '  ||
397                               *pos2 == '\t' ||
398                               *pos2 == '\r' ||
399                               *pos2 == '\n') {
400                            pos2++;
401                        }
402                        if (*pos2 == ';') {
403                            return NULL;
404                        }
405                    }
406                }
407            }
408        }
409        /* make best effort if BOM is missing */
410        return zend_multibyte_detect_utf_encoding(LANG_SCNG(script_org), LANG_SCNG(script_org_size));
411    }
412
413    return NULL;
414}
415
416static const zend_encoding* zend_multibyte_find_script_encoding(void)
417{
418    const zend_encoding *script_encoding;
419
420    if (CG(detect_unicode)) {
421        /* check out bom(byte order mark) and see if containing wchars */
422        script_encoding = zend_multibyte_detect_unicode();
423        if (script_encoding != NULL) {
424            /* bom or wchar detection is prior to 'script_encoding' option */
425            return script_encoding;
426        }
427    }
428
429    /* if no script_encoding specified, just leave alone */
430    if (!CG(script_encoding_list) || !CG(script_encoding_list_size)) {
431        return NULL;
432    }
433
434    /* if multiple encodings specified, detect automagically */
435    if (CG(script_encoding_list_size) > 1) {
436        return zend_multibyte_encoding_detector(LANG_SCNG(script_org), LANG_SCNG(script_org_size), CG(script_encoding_list), CG(script_encoding_list_size));
437    }
438
439    return CG(script_encoding_list)[0];
440}
441
442ZEND_API int zend_multibyte_set_filter(const zend_encoding *onetime_encoding)
443{
444    const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding();
445    const zend_encoding *script_encoding = onetime_encoding ? onetime_encoding: zend_multibyte_find_script_encoding();
446
447    if (!script_encoding) {
448        return FAILURE;
449    }
450
451    /* judge input/output filter */
452    LANG_SCNG(script_encoding) = script_encoding;
453    LANG_SCNG(input_filter) = NULL;
454    LANG_SCNG(output_filter) = NULL;
455
456    if (!internal_encoding || LANG_SCNG(script_encoding) == internal_encoding) {
457        if (!zend_multibyte_check_lexer_compatibility(LANG_SCNG(script_encoding))) {
458            /* and if not, work around w/ script_encoding -> utf-8 -> script_encoding conversion */
459            LANG_SCNG(input_filter) = encoding_filter_script_to_intermediate;
460            LANG_SCNG(output_filter) = encoding_filter_intermediate_to_script;
461        } else {
462            LANG_SCNG(input_filter) = NULL;
463            LANG_SCNG(output_filter) = NULL;
464        }
465        return SUCCESS;
466    }
467
468    if (zend_multibyte_check_lexer_compatibility(internal_encoding)) {
469        LANG_SCNG(input_filter) = encoding_filter_script_to_internal;
470        LANG_SCNG(output_filter) = NULL;
471    } else if (zend_multibyte_check_lexer_compatibility(LANG_SCNG(script_encoding))) {
472        LANG_SCNG(input_filter) = NULL;
473        LANG_SCNG(output_filter) = encoding_filter_script_to_internal;
474    } else {
475        /* both script and internal encodings are incompatible w/ flex */
476        LANG_SCNG(input_filter) = encoding_filter_script_to_intermediate;
477        LANG_SCNG(output_filter) = encoding_filter_intermediate_to_internal;
478    }
479
480    return 0;
481}
482
483ZEND_API int open_file_for_scanning(zend_file_handle *file_handle)
484{
485    const char *file_path = NULL;
486    char *buf;
487    size_t size, offset = 0;
488    zend_string *compiled_filename;
489
490    /* The shebang line was read, get the current position to obtain the buffer start */
491    if (CG(start_lineno) == 2 && file_handle->type == ZEND_HANDLE_FP && file_handle->handle.fp) {
492        if ((offset = ftell(file_handle->handle.fp)) == -1) {
493            offset = 0;
494        }
495    }
496
497    if (zend_stream_fixup(file_handle, &buf, &size) == FAILURE) {
498        return FAILURE;
499    }
500
501    zend_llist_add_element(&CG(open_files), file_handle);
502    if (file_handle->handle.stream.handle >= (void*)file_handle && file_handle->handle.stream.handle <= (void*)(file_handle+1)) {
503        zend_file_handle *fh = (zend_file_handle*)zend_llist_get_last(&CG(open_files));
504        size_t diff = (char*)file_handle->handle.stream.handle - (char*)file_handle;
505        fh->handle.stream.handle = (void*)(((char*)fh) + diff);
506        file_handle->handle.stream.handle = fh->handle.stream.handle;
507    }
508
509    /* Reset the scanner for scanning the new file */
510    SCNG(yy_in) = file_handle;
511    SCNG(yy_start) = NULL;
512
513    if (size != -1) {
514        if (CG(multibyte)) {
515            SCNG(script_org) = (unsigned char*)buf;
516            SCNG(script_org_size) = size;
517            SCNG(script_filtered) = NULL;
518
519            zend_multibyte_set_filter(NULL);
520
521            if (SCNG(input_filter)) {
522                if ((size_t)-1 == SCNG(input_filter)(&SCNG(script_filtered), &SCNG(script_filtered_size), SCNG(script_org), SCNG(script_org_size))) {
523                    zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
524                            "encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
525                }
526                buf = (char*)SCNG(script_filtered);
527                size = SCNG(script_filtered_size);
528            }
529        }
530        SCNG(yy_start) = (unsigned char *)buf - offset;
531        yy_scan_buffer(buf, (unsigned int)size);
532    } else {
533        zend_error_noreturn(E_COMPILE_ERROR, "zend_stream_mmap() failed");
534    }
535
536    BEGIN(INITIAL);
537
538    if (file_handle->opened_path) {
539        file_path = file_handle->opened_path;
540    } else {
541        file_path = file_handle->filename;
542    }
543
544    compiled_filename = zend_string_init(file_path, strlen(file_path), 0);
545    zend_set_compiled_filename(compiled_filename);
546    zend_string_release(compiled_filename);
547
548    if (CG(start_lineno)) {
549        CG(zend_lineno) = CG(start_lineno);
550        CG(start_lineno) = 0;
551    } else {
552        CG(zend_lineno) = 1;
553    }
554
555    RESET_DOC_COMMENT();
556    CG(increment_lineno) = 0;
557    return SUCCESS;
558}
559END_EXTERN_C()
560
561
562ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type)
563{
564    zend_lex_state original_lex_state;
565    zend_op_array *op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
566    zend_op_array *original_active_op_array = CG(active_op_array);
567    int compiler_result;
568    zend_bool compilation_successful=0;
569    zval retval_zv;
570    zend_bool original_in_compilation = CG(in_compilation);
571
572    ZVAL_LONG(&retval_zv, 1);
573
574    zend_save_lexical_state(&original_lex_state);
575
576    if (open_file_for_scanning(file_handle)==FAILURE) {
577        if (type==ZEND_REQUIRE) {
578            zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle->filename);
579            zend_bailout();
580        } else {
581            zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file_handle->filename);
582        }
583        compilation_successful=0;
584    } else {
585        init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
586        CG(in_compilation) = 1;
587        CG(active_op_array) = op_array;
588        zend_stack_push(&CG(context_stack), (void *) &CG(context));
589        zend_init_compiler_context();
590        CG(ast_arena) = zend_arena_create(1024 * 32);
591        compiler_result = zendparse();
592        if (compiler_result != 0) { /* parser error */
593            zend_bailout();
594        }
595        if (zend_ast_process) {
596            zend_ast_process(CG(ast));
597        }
598        zend_compile_top_stmt(CG(ast));
599        zend_ast_destroy(CG(ast));
600        zend_arena_destroy(CG(ast_arena));
601        zend_do_end_compilation();
602        zend_emit_final_return(&retval_zv);
603        CG(in_compilation) = original_in_compilation;
604        compilation_successful=1;
605    }
606
607    CG(active_op_array) = original_active_op_array;
608    if (compilation_successful) {
609        pass_two(op_array);
610        zend_release_labels(0);
611    } else {
612        efree_size(op_array, sizeof(zend_op_array));
613        op_array = NULL;
614    }
615
616    zend_restore_lexical_state(&original_lex_state);
617    return op_array;
618}
619
620
621zend_op_array *compile_filename(int type, zval *filename)
622{
623    zend_file_handle file_handle;
624    zval tmp;
625    zend_op_array *retval;
626    char *opened_path = NULL;
627
628    if (Z_TYPE_P(filename) != IS_STRING) {
629        tmp = *filename;
630        zval_copy_ctor(&tmp);
631        convert_to_string(&tmp);
632        filename = &tmp;
633    }
634    file_handle.filename = Z_STRVAL_P(filename);
635    file_handle.free_filename = 0;
636    file_handle.type = ZEND_HANDLE_FILENAME;
637    file_handle.opened_path = NULL;
638    file_handle.handle.fp = NULL;
639
640    retval = zend_compile_file(&file_handle, type);
641    if (retval && file_handle.handle.stream.handle) {
642        if (!file_handle.opened_path) {
643            file_handle.opened_path = opened_path = estrndup(Z_STRVAL_P(filename), Z_STRLEN_P(filename));
644        }
645
646        zend_hash_str_add_empty_element(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path));
647
648        if (opened_path) {
649            efree(opened_path);
650        }
651    }
652    zend_destroy_file_handle(&file_handle);
653
654    if (filename==&tmp) {
655        zval_dtor(&tmp);
656    }
657    return retval;
658}
659
660ZEND_API int zend_prepare_string_for_scanning(zval *str, char *filename)
661{
662    char *buf;
663    size_t size, old_len;
664    zend_string *new_compiled_filename;
665
666    /* enforce ZEND_MMAP_AHEAD trailing NULLs for flex... */
667    old_len = Z_STRLEN_P(str);
668    Z_STR_P(str) = zend_string_realloc(Z_STR_P(str), old_len + ZEND_MMAP_AHEAD, 0);
669    Z_TYPE_INFO_P(str) = IS_STRING_EX;
670    memset(Z_STRVAL_P(str) + old_len, 0, ZEND_MMAP_AHEAD + 1);
671
672    SCNG(yy_in) = NULL;
673    SCNG(yy_start) = NULL;
674
675    buf = Z_STRVAL_P(str);
676    size = old_len;
677
678    if (CG(multibyte)) {
679        SCNG(script_org) = (unsigned char*)buf;
680        SCNG(script_org_size) = size;
681        SCNG(script_filtered) = NULL;
682
683        zend_multibyte_set_filter(zend_multibyte_get_internal_encoding());
684
685        if (SCNG(input_filter)) {
686            if ((size_t)-1 == SCNG(input_filter)(&SCNG(script_filtered), &SCNG(script_filtered_size), SCNG(script_org), SCNG(script_org_size))) {
687                zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
688                        "encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
689            }
690            buf = (char*)SCNG(script_filtered);
691            size = SCNG(script_filtered_size);
692        }
693    }
694
695    yy_scan_buffer(buf, (unsigned int)size);
696
697    new_compiled_filename = zend_string_init(filename, strlen(filename), 0);
698    zend_set_compiled_filename(new_compiled_filename);
699    zend_string_release(new_compiled_filename);
700    CG(zend_lineno) = 1;
701    CG(increment_lineno) = 0;
702    RESET_DOC_COMMENT();
703    return SUCCESS;
704}
705
706
707ZEND_API size_t zend_get_scanned_file_offset(void)
708{
709    size_t offset = SCNG(yy_cursor) - SCNG(yy_start);
710    if (SCNG(input_filter)) {
711        size_t original_offset = offset, length = 0;
712        do {
713            unsigned char *p = NULL;
714            if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), offset)) {
715                return (size_t)-1;
716            }
717            efree(p);
718            if (length > original_offset) {
719                offset--;
720            } else if (length < original_offset) {
721                offset++;
722            }
723        } while (original_offset != length);
724    }
725    return offset;
726}
727
728
729zend_op_array *compile_string(zval *source_string, char *filename)
730{
731    zend_lex_state original_lex_state;
732    zend_op_array *op_array = NULL;
733    zval tmp;
734    zend_bool original_in_compilation = CG(in_compilation);
735
736    if (Z_STRLEN_P(source_string)==0) {
737        return NULL;
738    }
739
740    ZVAL_DUP(&tmp, source_string);
741    convert_to_string(&tmp);
742    source_string = &tmp;
743
744    CG(in_compilation) = 1;
745    zend_save_lexical_state(&original_lex_state);
746    if (zend_prepare_string_for_scanning(source_string, filename) == SUCCESS) {
747        CG(ast) = NULL;
748        CG(ast_arena) = zend_arena_create(1024 * 32);
749        BEGIN(ST_IN_SCRIPTING);
750
751        if (!zendparse()) {
752            zend_op_array *original_active_op_array = CG(active_op_array);
753            op_array = emalloc(sizeof(zend_op_array));
754            init_op_array(op_array, ZEND_EVAL_CODE, INITIAL_OP_ARRAY_SIZE);
755            CG(active_op_array) = op_array;
756
757            zend_stack_push(&CG(context_stack), (void *) &CG(context));
758            zend_init_compiler_context();
759            if (zend_ast_process) {
760                zend_ast_process(CG(ast));
761            }
762            zend_compile_top_stmt(CG(ast));
763            zend_do_end_compilation();
764            zend_emit_final_return(NULL);
765            pass_two(op_array);
766            zend_release_labels(0);
767
768            CG(active_op_array) = original_active_op_array;
769        }
770
771        zend_ast_destroy(CG(ast));
772        zend_arena_destroy(CG(ast_arena));
773    }
774
775    zend_restore_lexical_state(&original_lex_state);
776    zval_dtor(&tmp);
777    CG(in_compilation) = original_in_compilation;
778    return op_array;
779}
780
781
782BEGIN_EXTERN_C()
783int highlight_file(char *filename, zend_syntax_highlighter_ini *syntax_highlighter_ini)
784{
785    zend_lex_state original_lex_state;
786    zend_file_handle file_handle;
787
788    file_handle.type = ZEND_HANDLE_FILENAME;
789    file_handle.filename = filename;
790    file_handle.free_filename = 0;
791    file_handle.opened_path = NULL;
792    zend_save_lexical_state(&original_lex_state);
793    if (open_file_for_scanning(&file_handle)==FAILURE) {
794        zend_message_dispatcher(ZMSG_FAILED_HIGHLIGHT_FOPEN, filename);
795        zend_restore_lexical_state(&original_lex_state);
796        return FAILURE;
797    }
798    zend_highlight(syntax_highlighter_ini);
799    if (SCNG(script_filtered)) {
800        efree(SCNG(script_filtered));
801        SCNG(script_filtered) = NULL;
802    }
803    zend_destroy_file_handle(&file_handle);
804    zend_restore_lexical_state(&original_lex_state);
805    return SUCCESS;
806}
807
808int highlight_string(zval *str, zend_syntax_highlighter_ini *syntax_highlighter_ini, char *str_name)
809{
810    zend_lex_state original_lex_state;
811    zval tmp = *str;
812
813    str = &tmp;
814    zval_copy_ctor(str);
815    zend_save_lexical_state(&original_lex_state);
816    if (zend_prepare_string_for_scanning(str, str_name)==FAILURE) {
817        zend_restore_lexical_state(&original_lex_state);
818        return FAILURE;
819    }
820    BEGIN(INITIAL);
821    zend_highlight(syntax_highlighter_ini);
822    if (SCNG(script_filtered)) {
823        efree(SCNG(script_filtered));
824        SCNG(script_filtered) = NULL;
825    }
826    zend_restore_lexical_state(&original_lex_state);
827    zval_dtor(str);
828    return SUCCESS;
829}
830
831ZEND_API void zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter, const zend_encoding *old_encoding)
832{
833    size_t length;
834    unsigned char *new_yy_start;
835
836    /* convert and set */
837    if (!SCNG(input_filter)) {
838        if (SCNG(script_filtered)) {
839            efree(SCNG(script_filtered));
840            SCNG(script_filtered) = NULL;
841        }
842        SCNG(script_filtered_size) = 0;
843        length = SCNG(script_org_size);
844        new_yy_start = SCNG(script_org);
845    } else {
846        if ((size_t)-1 == SCNG(input_filter)(&new_yy_start, &length, SCNG(script_org), SCNG(script_org_size))) {
847            zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
848                    "encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
849        }
850        if (SCNG(script_filtered)) {
851            efree(SCNG(script_filtered));
852        }
853        SCNG(script_filtered) = new_yy_start;
854        SCNG(script_filtered_size) = length;
855    }
856
857    SCNG(yy_cursor) = new_yy_start + (SCNG(yy_cursor) - SCNG(yy_start));
858    SCNG(yy_marker) = new_yy_start + (SCNG(yy_marker) - SCNG(yy_start));
859    SCNG(yy_text) = new_yy_start + (SCNG(yy_text) - SCNG(yy_start));
860    SCNG(yy_limit) = new_yy_start + length;
861
862    SCNG(yy_start) = new_yy_start;
863}
864
865
866// TODO: avoid reallocation ???
867# define zend_copy_value(zendlval, yytext, yyleng) \
868    if (SCNG(output_filter)) { \
869        size_t sz = 0; \
870        char *s = NULL; \
871        SCNG(output_filter)((unsigned char **)&s, &sz, (unsigned char *)yytext, (size_t)yyleng); \
872        ZVAL_STRINGL(zendlval, s, sz); \
873        efree(s); \
874    } else { \
875        ZVAL_STRINGL(zendlval, yytext, yyleng); \
876    }
877
878static void zend_scan_escape_string(zval *zendlval, char *str, int len, char quote_type)
879{
880    register char *s, *t;
881    char *end;
882
883    ZVAL_STRINGL(zendlval, str, len);
884
885    /* convert escape sequences */
886    s = t = Z_STRVAL_P(zendlval);
887    end = s+Z_STRLEN_P(zendlval);
888    while (s<end) {
889        if (*s=='\\') {
890            s++;
891            if (s >= end) {
892                *t++ = '\\';
893                break;
894            }
895
896            switch(*s) {
897                case 'n':
898                    *t++ = '\n';
899                    Z_STRLEN_P(zendlval)--;
900                    break;
901                case 'r':
902                    *t++ = '\r';
903                    Z_STRLEN_P(zendlval)--;
904                    break;
905                case 't':
906                    *t++ = '\t';
907                    Z_STRLEN_P(zendlval)--;
908                    break;
909                case 'f':
910                    *t++ = '\f';
911                    Z_STRLEN_P(zendlval)--;
912                    break;
913                case 'v':
914                    *t++ = '\v';
915                    Z_STRLEN_P(zendlval)--;
916                    break;
917                case 'e':
918#ifdef PHP_WIN32
919                    *t++ = VK_ESCAPE;
920#else
921                    *t++ = '\e';
922#endif
923                    Z_STRLEN_P(zendlval)--;
924                    break;
925                case '"':
926                case '`':
927                    if (*s != quote_type) {
928                        *t++ = '\\';
929                        *t++ = *s;
930                        break;
931                    }
932                case '\\':
933                case '$':
934                    *t++ = *s;
935                    Z_STRLEN_P(zendlval)--;
936                    break;
937                case 'x':
938                case 'X':
939                    if (ZEND_IS_HEX(*(s+1))) {
940                        char hex_buf[3] = { 0, 0, 0 };
941
942                        Z_STRLEN_P(zendlval)--; /* for the 'x' */
943
944                        hex_buf[0] = *(++s);
945                        Z_STRLEN_P(zendlval)--;
946                        if (ZEND_IS_HEX(*(s+1))) {
947                            hex_buf[1] = *(++s);
948                            Z_STRLEN_P(zendlval)--;
949                        }
950                        *t++ = (char) ZEND_STRTOL(hex_buf, NULL, 16);
951                    } else {
952                        *t++ = '\\';
953                        *t++ = *s;
954                    }
955                    break;
956                /* UTF-8 codepoint escape, format: /\\u\{\x+\}/ */
957                case 'u':
958                    {
959                        /* cache where we started so we can parse after validating */
960                        char *start = s + 1;
961                        size_t len = 0;
962                        zend_bool valid = 1;
963                        unsigned long codepoint;
964                        size_t byte_len = 0;
965
966                        if (*start != '{') {
967                            /* we silently let this pass to avoid breaking code
968                             * with JSON in string literals (e.g. "\"\u202e\""
969                             */
970                            *t++ = '\\';
971                            *t++ = 'u';
972                            break;
973                        } else {
974                            /* on the other hand, invalid \u{blah} errors */
975                            s++;
976                            len++;
977                            s++;
978                            while (*s != '}') {
979                                if (!ZEND_IS_HEX(*s)) {
980                                    valid = 0;
981                                    break;
982                                } else {
983                                    len++;
984                                }
985                                s++;
986                            }
987                            if (*s == '}') {
988                                valid = 1;
989                                len++;
990                            }
991                        }
992
993                        /* \u{} is invalid */
994                        if (len <= 2) {
995                            valid = 0;
996                        }
997
998                        if (!valid) {
999                            zend_error(E_COMPILE_ERROR, "Invalid UTF-8 codepoint escape sequence");
1000                        }
1001
1002                        errno = 0;
1003                        codepoint = strtoul(start + 1, NULL, 16);
1004
1005                        /* per RFC 3629, UTF-8 can only represent 21 bits */
1006                        if (codepoint > 0x10FFFF || errno) {
1007                            zend_error_noreturn(E_COMPILE_ERROR, "Invalid UTF-8 codepoint escape sequence: Codepoint too large");
1008                        }
1009
1010                        /* based on https://en.wikipedia.org/wiki/UTF-8#Sample_code */
1011                        if (codepoint < 0x80) {
1012                            byte_len = 1;
1013                            *t++ = codepoint;
1014                        } else if (codepoint <= 0x7FF) {
1015                            byte_len = 2;
1016                            *t++ = (codepoint >> 6) + 0xC0;
1017                            *t++ = (codepoint & 0x3F) + 0x80;
1018                        } else if (codepoint <= 0xFFFF) {
1019                            byte_len = 3;
1020                            *t++ = (codepoint >> 12) + 0xE0;
1021                            *t++ = ((codepoint >> 6) & 0x3F) + 0x80;
1022                            *t++ = (codepoint & 0x3F) + 0x80;
1023                        } else if (codepoint <= 0x10FFFF) {
1024                            byte_len = 4;
1025                            *t++ = (codepoint >> 18) + 0xF0;
1026                            *t++ = ((codepoint >> 12) & 0x3F) + 0x80;
1027                            *t++ = ((codepoint >> 6) & 0x3F) + 0x80;
1028                            *t++ = (codepoint & 0x3F) + 0x80;
1029                        }
1030
1031                        Z_STRLEN_P(zendlval) -= 2; /* \u */
1032                        Z_STRLEN_P(zendlval) -= (len - byte_len);
1033                    }
1034                    break;
1035                default:
1036                    /* check for an octal */
1037                    if (ZEND_IS_OCT(*s)) {
1038                        char octal_buf[4] = { 0, 0, 0, 0 };
1039
1040                        octal_buf[0] = *s;
1041                        Z_STRLEN_P(zendlval)--;
1042                        if (ZEND_IS_OCT(*(s+1))) {
1043                            octal_buf[1] = *(++s);
1044                            Z_STRLEN_P(zendlval)--;
1045                            if (ZEND_IS_OCT(*(s+1))) {
1046                                octal_buf[2] = *(++s);
1047                                Z_STRLEN_P(zendlval)--;
1048                            }
1049                        }
1050                        *t++ = (char) ZEND_STRTOL(octal_buf, NULL, 8);
1051                    } else {
1052                        *t++ = '\\';
1053                        *t++ = *s;
1054                    }
1055                    break;
1056            }
1057        } else {
1058            *t++ = *s;
1059        }
1060
1061        if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
1062            CG(zend_lineno)++;
1063        }
1064        s++;
1065    }
1066    *t = 0;
1067    if (SCNG(output_filter)) {
1068        size_t sz = 0;
1069        unsigned char *str;
1070        // TODO: avoid realocation ???
1071        s = Z_STRVAL_P(zendlval);
1072        SCNG(output_filter)(&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
1073        zval_ptr_dtor(zendlval);
1074        ZVAL_STRINGL(zendlval, (char *) str, sz);
1075        efree(str);
1076    }
1077}
1078
1079
1080int lex_scan(zval *zendlval)
1081{
1082restart:
1083    SCNG(yy_text) = YYCURSOR;
1084
1085/*!re2c
1086re2c:yyfill:check = 0;
1087LNUM    [0-9]+
1088DNUM    ([0-9]*"."[0-9]+)|([0-9]+"."[0-9]*)
1089EXPONENT_DNUM   (({LNUM}|{DNUM})[eE][+-]?{LNUM})
1090HNUM    "0x"[0-9a-fA-F]+
1091BNUM    "0b"[01]+
1092LABEL   [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
1093WHITESPACE [ \n\r\t]+
1094TABS_AND_SPACES [ \t]*
1095TOKENS [;:,.\[\]()|^&+-/*=%!~$<>?@]
1096ANY_CHAR [^]
1097NEWLINE ("\r"|"\n"|"\r\n")
1098
1099/* compute yyleng before each rule */
1100<!*> := yyleng = YYCURSOR - SCNG(yy_text);
1101
1102<ST_IN_SCRIPTING>"exit" {
1103    return T_EXIT;
1104}
1105
1106<ST_IN_SCRIPTING>"die" {
1107    return T_EXIT;
1108}
1109
1110<ST_IN_SCRIPTING>"function" {
1111    return T_FUNCTION;
1112}
1113
1114<ST_IN_SCRIPTING>"const" {
1115    return T_CONST;
1116}
1117
1118<ST_IN_SCRIPTING>"return" {
1119    return T_RETURN;
1120}
1121
1122<ST_IN_SCRIPTING>"yield" {
1123    return T_YIELD;
1124}
1125
1126<ST_IN_SCRIPTING>"try" {
1127    return T_TRY;
1128}
1129
1130<ST_IN_SCRIPTING>"catch" {
1131    return T_CATCH;
1132}
1133
1134<ST_IN_SCRIPTING>"finally" {
1135    return T_FINALLY;
1136}
1137
1138<ST_IN_SCRIPTING>"throw" {
1139    return T_THROW;
1140}
1141
1142<ST_IN_SCRIPTING>"if" {
1143    return T_IF;
1144}
1145
1146<ST_IN_SCRIPTING>"elseif" {
1147    return T_ELSEIF;
1148}
1149
1150<ST_IN_SCRIPTING>"endif" {
1151    return T_ENDIF;
1152}
1153
1154<ST_IN_SCRIPTING>"else" {
1155    return T_ELSE;
1156}
1157
1158<ST_IN_SCRIPTING>"while" {
1159    return T_WHILE;
1160}
1161
1162<ST_IN_SCRIPTING>"endwhile" {
1163    return T_ENDWHILE;
1164}
1165
1166<ST_IN_SCRIPTING>"do" {
1167    return T_DO;
1168}
1169
1170<ST_IN_SCRIPTING>"for" {
1171    return T_FOR;
1172}
1173
1174<ST_IN_SCRIPTING>"endfor" {
1175    return T_ENDFOR;
1176}
1177
1178<ST_IN_SCRIPTING>"foreach" {
1179    return T_FOREACH;
1180}
1181
1182<ST_IN_SCRIPTING>"endforeach" {
1183    return T_ENDFOREACH;
1184}
1185
1186<ST_IN_SCRIPTING>"declare" {
1187    return T_DECLARE;
1188}
1189
1190<ST_IN_SCRIPTING>"enddeclare" {
1191    return T_ENDDECLARE;
1192}
1193
1194<ST_IN_SCRIPTING>"instanceof" {
1195    return T_INSTANCEOF;
1196}
1197
1198<ST_IN_SCRIPTING>"as" {
1199    return T_AS;
1200}
1201
1202<ST_IN_SCRIPTING>"switch" {
1203    return T_SWITCH;
1204}
1205
1206<ST_IN_SCRIPTING>"endswitch" {
1207    return T_ENDSWITCH;
1208}
1209
1210<ST_IN_SCRIPTING>"case" {
1211    return T_CASE;
1212}
1213
1214<ST_IN_SCRIPTING>"default" {
1215    return T_DEFAULT;
1216}
1217
1218<ST_IN_SCRIPTING>"break" {
1219    return T_BREAK;
1220}
1221
1222<ST_IN_SCRIPTING>"continue" {
1223    return T_CONTINUE;
1224}
1225
1226<ST_IN_SCRIPTING>"goto" {
1227    return T_GOTO;
1228}
1229
1230<ST_IN_SCRIPTING>"echo" {
1231    return T_ECHO;
1232}
1233
1234<ST_IN_SCRIPTING>"print" {
1235    return T_PRINT;
1236}
1237
1238<ST_IN_SCRIPTING>"class" {
1239    return T_CLASS;
1240}
1241
1242<ST_IN_SCRIPTING>"interface" {
1243    return T_INTERFACE;
1244}
1245
1246<ST_IN_SCRIPTING>"trait" {
1247    return T_TRAIT;
1248}
1249
1250<ST_IN_SCRIPTING>"extends" {
1251    return T_EXTENDS;
1252}
1253
1254<ST_IN_SCRIPTING>"implements" {
1255    return T_IMPLEMENTS;
1256}
1257
1258<ST_IN_SCRIPTING>"->" {
1259    yy_push_state(ST_LOOKING_FOR_PROPERTY);
1260    return T_OBJECT_OPERATOR;
1261}
1262
1263<ST_IN_SCRIPTING,ST_LOOKING_FOR_PROPERTY>{WHITESPACE}+ {
1264    HANDLE_NEWLINES(yytext, yyleng);
1265    return T_WHITESPACE;
1266}
1267
1268<ST_LOOKING_FOR_PROPERTY>"->" {
1269    return T_OBJECT_OPERATOR;
1270}
1271
1272<ST_LOOKING_FOR_PROPERTY>{LABEL} {
1273    yy_pop_state();
1274    zend_copy_value(zendlval, yytext, yyleng);
1275    return T_STRING;
1276}
1277
1278<ST_LOOKING_FOR_PROPERTY>{ANY_CHAR} {
1279    yyless(0);
1280    yy_pop_state();
1281    goto restart;
1282}
1283
1284<ST_IN_SCRIPTING>"::" {
1285    return T_PAAMAYIM_NEKUDOTAYIM;
1286}
1287
1288<ST_IN_SCRIPTING>"\\" {
1289    return T_NS_SEPARATOR;
1290}
1291
1292<ST_IN_SCRIPTING>"..." {
1293    return T_ELLIPSIS;
1294}
1295
1296<ST_IN_SCRIPTING>"??" {
1297    return T_COALESCE;
1298}
1299
1300<ST_IN_SCRIPTING>"new" {
1301    return T_NEW;
1302}
1303
1304<ST_IN_SCRIPTING>"clone" {
1305    return T_CLONE;
1306}
1307
1308<ST_IN_SCRIPTING>"var" {
1309    return T_VAR;
1310}
1311
1312<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"|"integer"){TABS_AND_SPACES}")" {
1313    return T_INT_CAST;
1314}
1315
1316<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("real"|"double"|"float"){TABS_AND_SPACES}")" {
1317    return T_DOUBLE_CAST;
1318}
1319
1320<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"|"binary"){TABS_AND_SPACES}")" {
1321    return T_STRING_CAST;
1322}
1323
1324<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"array"{TABS_AND_SPACES}")" {
1325    return T_ARRAY_CAST;
1326}
1327
1328<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"object"{TABS_AND_SPACES}")" {
1329    return T_OBJECT_CAST;
1330}
1331
1332<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"|"boolean"){TABS_AND_SPACES}")" {
1333    return T_BOOL_CAST;
1334}
1335
1336<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("unset"){TABS_AND_SPACES}")" {
1337    return T_UNSET_CAST;
1338}
1339
1340<ST_IN_SCRIPTING>"eval" {
1341    return T_EVAL;
1342}
1343
1344<ST_IN_SCRIPTING>"include" {
1345    return T_INCLUDE;
1346}
1347
1348<ST_IN_SCRIPTING>"include_once" {
1349    return T_INCLUDE_ONCE;
1350}
1351
1352<ST_IN_SCRIPTING>"require" {
1353    return T_REQUIRE;
1354}
1355
1356<ST_IN_SCRIPTING>"require_once" {
1357    return T_REQUIRE_ONCE;
1358}
1359
1360<ST_IN_SCRIPTING>"namespace" {
1361    return T_NAMESPACE;
1362}
1363
1364<ST_IN_SCRIPTING>"use" {
1365    return T_USE;
1366}
1367
1368<ST_IN_SCRIPTING>"insteadof" {
1369        return T_INSTEADOF;
1370}
1371
1372<ST_IN_SCRIPTING>"global" {
1373    return T_GLOBAL;
1374}
1375
1376<ST_IN_SCRIPTING>"isset" {
1377    return T_ISSET;
1378}
1379
1380<ST_IN_SCRIPTING>"empty" {
1381    return T_EMPTY;
1382}
1383
1384<ST_IN_SCRIPTING>"__halt_compiler" {
1385    return T_HALT_COMPILER;
1386}
1387
1388<ST_IN_SCRIPTING>"static" {
1389    return T_STATIC;
1390}
1391
1392<ST_IN_SCRIPTING>"abstract" {
1393    return T_ABSTRACT;
1394}
1395
1396<ST_IN_SCRIPTING>"final" {
1397    return T_FINAL;
1398}
1399
1400<ST_IN_SCRIPTING>"private" {
1401    return T_PRIVATE;
1402}
1403
1404<ST_IN_SCRIPTING>"protected" {
1405    return T_PROTECTED;
1406}
1407
1408<ST_IN_SCRIPTING>"public" {
1409    return T_PUBLIC;
1410}
1411
1412<ST_IN_SCRIPTING>"unset" {
1413    return T_UNSET;
1414}
1415
1416<ST_IN_SCRIPTING>"=>" {
1417    return T_DOUBLE_ARROW;
1418}
1419
1420<ST_IN_SCRIPTING>"list" {
1421    return T_LIST;
1422}
1423
1424<ST_IN_SCRIPTING>"array" {
1425    return T_ARRAY;
1426}
1427
1428<ST_IN_SCRIPTING>"callable" {
1429 return T_CALLABLE;
1430}
1431
1432<ST_IN_SCRIPTING>"++" {
1433    return T_INC;
1434}
1435
1436<ST_IN_SCRIPTING>"--" {
1437    return T_DEC;
1438}
1439
1440<ST_IN_SCRIPTING>"===" {
1441    return T_IS_IDENTICAL;
1442}
1443
1444<ST_IN_SCRIPTING>"!==" {
1445    return T_IS_NOT_IDENTICAL;
1446}
1447
1448<ST_IN_SCRIPTING>"==" {
1449    return T_IS_EQUAL;
1450}
1451
1452<ST_IN_SCRIPTING>"!="|"<>" {
1453    return T_IS_NOT_EQUAL;
1454}
1455
1456<ST_IN_SCRIPTING>"<=" {
1457    return T_IS_SMALLER_OR_EQUAL;
1458}
1459
1460<ST_IN_SCRIPTING>">=" {
1461    return T_IS_GREATER_OR_EQUAL;
1462}
1463
1464<ST_IN_SCRIPTING>"+=" {
1465    return T_PLUS_EQUAL;
1466}
1467
1468<ST_IN_SCRIPTING>"-=" {
1469    return T_MINUS_EQUAL;
1470}
1471
1472<ST_IN_SCRIPTING>"*=" {
1473    return T_MUL_EQUAL;
1474}
1475
1476<ST_IN_SCRIPTING>"*\*" {
1477    return T_POW;
1478}
1479
1480<ST_IN_SCRIPTING>"*\*=" {
1481    return T_POW_EQUAL;
1482}
1483
1484<ST_IN_SCRIPTING>"/=" {
1485    return T_DIV_EQUAL;
1486}
1487
1488<ST_IN_SCRIPTING>".=" {
1489    return T_CONCAT_EQUAL;
1490}
1491
1492<ST_IN_SCRIPTING>"%=" {
1493    return T_MOD_EQUAL;
1494}
1495
1496<ST_IN_SCRIPTING>"<<=" {
1497    return T_SL_EQUAL;
1498}
1499
1500<ST_IN_SCRIPTING>">>=" {
1501    return T_SR_EQUAL;
1502}
1503
1504<ST_IN_SCRIPTING>"&=" {
1505    return T_AND_EQUAL;
1506}
1507
1508<ST_IN_SCRIPTING>"|=" {
1509    return T_OR_EQUAL;
1510}
1511
1512<ST_IN_SCRIPTING>"^=" {
1513    return T_XOR_EQUAL;
1514}
1515
1516<ST_IN_SCRIPTING>"||" {
1517    return T_BOOLEAN_OR;
1518}
1519
1520<ST_IN_SCRIPTING>"&&" {
1521    return T_BOOLEAN_AND;
1522}
1523
1524<ST_IN_SCRIPTING>"OR" {
1525    return T_LOGICAL_OR;
1526}
1527
1528<ST_IN_SCRIPTING>"AND" {
1529    return T_LOGICAL_AND;
1530}
1531
1532<ST_IN_SCRIPTING>"XOR" {
1533    return T_LOGICAL_XOR;
1534}
1535
1536<ST_IN_SCRIPTING>"<<" {
1537    return T_SL;
1538}
1539
1540<ST_IN_SCRIPTING>">>" {
1541    return T_SR;
1542}
1543
1544<ST_IN_SCRIPTING>{TOKENS} {
1545    return yytext[0];
1546}
1547
1548
1549<ST_IN_SCRIPTING>"{" {
1550    yy_push_state(ST_IN_SCRIPTING);
1551    return '{';
1552}
1553
1554
1555<ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"${" {
1556    yy_push_state(ST_LOOKING_FOR_VARNAME);
1557    return T_DOLLAR_OPEN_CURLY_BRACES;
1558}
1559
1560
1561<ST_IN_SCRIPTING>"}" {
1562    RESET_DOC_COMMENT();
1563    if (!zend_stack_is_empty(&SCNG(state_stack))) {
1564        yy_pop_state();
1565    }
1566    return '}';
1567}
1568
1569
1570<ST_LOOKING_FOR_VARNAME>{LABEL}[[}] {
1571    yyless(yyleng - 1);
1572    zend_copy_value(zendlval, yytext, yyleng);
1573    yy_pop_state();
1574    yy_push_state(ST_IN_SCRIPTING);
1575    return T_STRING_VARNAME;
1576}
1577
1578
1579<ST_LOOKING_FOR_VARNAME>{ANY_CHAR} {
1580    yyless(0);
1581    yy_pop_state();
1582    yy_push_state(ST_IN_SCRIPTING);
1583    goto restart;
1584}
1585
1586<ST_IN_SCRIPTING>{BNUM} {
1587    char *bin = yytext + 2; /* Skip "0b" */
1588    int len = yyleng - 2;
1589    char *end;
1590
1591    /* Skip any leading 0s */
1592    while (*bin == '0') {
1593        ++bin;
1594        --len;
1595    }
1596
1597    if (len < SIZEOF_ZEND_LONG * 8) {
1598        if (len == 0) {
1599            ZVAL_LONG(zendlval, 0);
1600        } else {
1601            errno = 0;
1602            ZVAL_LONG(zendlval, ZEND_STRTOL(bin, &end, 2));
1603            ZEND_ASSERT(!errno && end == yytext + yyleng);
1604        }
1605        return T_LNUMBER;
1606    } else {
1607        ZVAL_DOUBLE(zendlval, zend_bin_strtod(bin, (const char **)&end));
1608        /* errno isn't checked since we allow HUGE_VAL/INF overflow */
1609        ZEND_ASSERT(end == yytext + yyleng);
1610        return T_DNUMBER;
1611    }
1612}
1613
1614<ST_IN_SCRIPTING>{LNUM} {
1615    char *end;
1616    if (yyleng < MAX_LENGTH_OF_LONG - 1) { /* Won't overflow */
1617        errno = 0;
1618        ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 0));
1619        /* This isn't an assert, we need to ensure 019 isn't valid octal
1620         * Because the lexing itself doesn't do that for us
1621         */
1622        if (end != yytext + yyleng) {
1623            zend_error_noreturn(E_COMPILE_ERROR, "Invalid numeric literal");
1624        }
1625        ZEND_ASSERT(!errno);
1626    } else {
1627        errno = 0;
1628        ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 0));
1629        if (errno == ERANGE) { /* Overflow */
1630            errno = 0;
1631            if (yytext[0] == '0') { /* octal overflow */
1632                errno = 0;
1633                ZVAL_DOUBLE(zendlval, zend_oct_strtod(yytext, (const char **)&end));
1634            } else {
1635                ZVAL_DOUBLE(zendlval, zend_strtod(yytext, (const char **)&end));
1636            }
1637            /* Also not an assert for the same reason */
1638            if (end != yytext + yyleng) {
1639                zend_error_noreturn(E_COMPILE_ERROR, "Invalid numeric literal");
1640            }
1641            ZEND_ASSERT(!errno);
1642            return T_DNUMBER;
1643        }
1644        /* Also not an assert for the same reason */
1645        if (end != yytext + yyleng) {
1646            zend_error_noreturn(E_COMPILE_ERROR, "Invalid numeric literal");
1647        }
1648        ZEND_ASSERT(!errno);
1649    }
1650    return T_LNUMBER;
1651}
1652
1653<ST_IN_SCRIPTING>{HNUM} {
1654    char *hex = yytext + 2; /* Skip "0x" */
1655    int len = yyleng - 2;
1656    char *end;
1657
1658    /* Skip any leading 0s */
1659    while (*hex == '0') {
1660        hex++;
1661        len--;
1662    }
1663
1664    if (len < SIZEOF_ZEND_LONG * 2 || (len == SIZEOF_ZEND_LONG * 2 && *hex <= '7')) {
1665        if (len == 0) {
1666            ZVAL_LONG(zendlval, 0);
1667        } else {
1668            errno = 0;
1669            ZVAL_LONG(zendlval, ZEND_STRTOL(hex, &end, 16));
1670            ZEND_ASSERT(!errno && end == hex + len);
1671        }
1672        return T_LNUMBER;
1673    } else {
1674        ZVAL_DOUBLE(zendlval, zend_hex_strtod(hex, (const char **)&end));
1675        /* errno isn't checked since we allow HUGE_VAL/INF overflow */
1676        ZEND_ASSERT(end == hex + len);
1677        return T_DNUMBER;
1678    }
1679}
1680
1681<ST_VAR_OFFSET>[0]|([1-9][0-9]*) { /* Offset could be treated as a long */
1682    if (yyleng < MAX_LENGTH_OF_LONG - 1 || (yyleng == MAX_LENGTH_OF_LONG - 1 && strcmp(yytext, long_min_digits) < 0)) {
1683        char *end;
1684        errno = 0;
1685        ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 10));
1686        if (errno == ERANGE) {
1687            goto string;
1688        }
1689        ZEND_ASSERT(end == yytext + yyleng);
1690    } else {
1691string:
1692        ZVAL_STRINGL(zendlval, yytext, yyleng);
1693    }
1694    return T_NUM_STRING;
1695}
1696
1697<ST_VAR_OFFSET>{LNUM}|{HNUM}|{BNUM} { /* Offset must be treated as a string */
1698    ZVAL_STRINGL(zendlval, yytext, yyleng);
1699    return T_NUM_STRING;
1700}
1701
1702<ST_IN_SCRIPTING>{DNUM}|{EXPONENT_DNUM} {
1703    const char *end;
1704
1705    ZVAL_DOUBLE(zendlval, zend_strtod(yytext, &end));
1706    /* errno isn't checked since we allow HUGE_VAL/INF overflow */
1707    ZEND_ASSERT(end == yytext + yyleng);
1708    return T_DNUMBER;
1709}
1710
1711<ST_IN_SCRIPTING>"__CLASS__" {
1712    return T_CLASS_C;
1713}
1714
1715<ST_IN_SCRIPTING>"__TRAIT__" {
1716    return T_TRAIT_C;
1717}
1718
1719<ST_IN_SCRIPTING>"__FUNCTION__" {
1720    return T_FUNC_C;
1721}
1722
1723<ST_IN_SCRIPTING>"__METHOD__" {
1724    return T_METHOD_C;
1725}
1726
1727<ST_IN_SCRIPTING>"__LINE__" {
1728    return T_LINE;
1729}
1730
1731<ST_IN_SCRIPTING>"__FILE__" {
1732    return T_FILE;
1733}
1734
1735<ST_IN_SCRIPTING>"__DIR__" {
1736    return T_DIR;
1737}
1738
1739<ST_IN_SCRIPTING>"__NAMESPACE__" {
1740    return T_NS_C;
1741}
1742
1743
1744<INITIAL>"<?=" {
1745    BEGIN(ST_IN_SCRIPTING);
1746    return T_OPEN_TAG_WITH_ECHO;
1747}
1748
1749
1750<INITIAL>"<?php"([ \t]|{NEWLINE}) {
1751    HANDLE_NEWLINE(yytext[yyleng-1]);
1752    BEGIN(ST_IN_SCRIPTING);
1753    return T_OPEN_TAG;
1754}
1755
1756
1757<INITIAL>"<?" {
1758    if (CG(short_tags)) {
1759        BEGIN(ST_IN_SCRIPTING);
1760        return T_OPEN_TAG;
1761    } else {
1762        goto inline_char_handler;
1763    }
1764}
1765
1766<INITIAL>{ANY_CHAR} {
1767    if (YYCURSOR > YYLIMIT) {
1768        return 0;
1769    }
1770
1771inline_char_handler:
1772
1773    while (1) {
1774        YYCTYPE *ptr = memchr(YYCURSOR, '<', YYLIMIT - YYCURSOR);
1775
1776        YYCURSOR = ptr ? ptr + 1 : YYLIMIT;
1777
1778        if (YYCURSOR >= YYLIMIT) {
1779            break;
1780        }
1781
1782        if (*YYCURSOR == '?') {
1783            if (CG(short_tags) || !strncasecmp((char*)YYCURSOR + 1, "php", 3) || (*(YYCURSOR + 1) == '=')) { /* Assume [ \t\n\r] follows "php" */
1784
1785                YYCURSOR--;
1786                break;
1787            }
1788        }
1789    }
1790
1791    yyleng = YYCURSOR - SCNG(yy_text);
1792
1793    if (SCNG(output_filter)) {
1794        size_t readsize;
1795        char *s = NULL;
1796        size_t sz = 0;
1797        // TODO: avoid reallocation ???
1798        readsize = SCNG(output_filter)((unsigned char **)&s, &sz, (unsigned char *)yytext, (size_t)yyleng);
1799        ZVAL_STRINGL(zendlval, s, sz);
1800        efree(s);
1801        if (readsize < yyleng) {
1802            yyless(readsize);
1803        }
1804    } else {
1805      ZVAL_STRINGL(zendlval, yytext, yyleng);
1806    }
1807    HANDLE_NEWLINES(yytext, yyleng);
1808    return T_INLINE_HTML;
1809}
1810
1811
1812/* Make sure a label character follows "->", otherwise there is no property
1813 * and "->" will be taken literally
1814 */
1815<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"->"[a-zA-Z_\x7f-\xff] {
1816    yyless(yyleng - 3);
1817    yy_push_state(ST_LOOKING_FOR_PROPERTY);
1818    zend_copy_value(zendlval, (yytext+1), (yyleng-1));
1819    return T_VARIABLE;
1820}
1821
1822/* A [ always designates a variable offset, regardless of what follows
1823 */
1824<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"[" {
1825    yyless(yyleng - 1);
1826    yy_push_state(ST_VAR_OFFSET);
1827    zend_copy_value(zendlval, (yytext+1), (yyleng-1));
1828    return T_VARIABLE;
1829}
1830
1831<ST_IN_SCRIPTING,ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE,ST_VAR_OFFSET>"$"{LABEL} {
1832    zend_copy_value(zendlval, (yytext+1), (yyleng-1));
1833    return T_VARIABLE;
1834}
1835
1836<ST_VAR_OFFSET>"]" {
1837    yy_pop_state();
1838    return ']';
1839}
1840
1841<ST_VAR_OFFSET>{TOKENS}|[{}"`] {
1842    /* Only '[' can be valid, but returning other tokens will allow a more explicit parse error */
1843    return yytext[0];
1844}
1845
1846<ST_VAR_OFFSET>[ \n\r\t\\'#] {
1847    /* Invalid rule to return a more explicit parse error with proper line number */
1848    yyless(0);
1849    yy_pop_state();
1850    ZVAL_NULL(zendlval);
1851    return T_ENCAPSED_AND_WHITESPACE;
1852}
1853
1854<ST_IN_SCRIPTING,ST_VAR_OFFSET>{LABEL} {
1855    zend_copy_value(zendlval, yytext, yyleng);
1856    return T_STRING;
1857}
1858
1859
1860<ST_IN_SCRIPTING>"#"|"//" {
1861    while (YYCURSOR < YYLIMIT) {
1862        switch (*YYCURSOR++) {
1863            case '\r':
1864                if (*YYCURSOR == '\n') {
1865                    YYCURSOR++;
1866                }
1867                /* fall through */
1868            case '\n':
1869                CG(zend_lineno)++;
1870                break;
1871            case '?':
1872                if (*YYCURSOR == '>') {
1873                    YYCURSOR--;
1874                    break;
1875                }
1876                /* fall through */
1877            default:
1878                continue;
1879        }
1880
1881        break;
1882    }
1883
1884    yyleng = YYCURSOR - SCNG(yy_text);
1885
1886    return T_COMMENT;
1887}
1888
1889<ST_IN_SCRIPTING>"/*"|"/**"{WHITESPACE} {
1890    int doc_com;
1891
1892    if (yyleng > 2) {
1893        doc_com = 1;
1894        RESET_DOC_COMMENT();
1895    } else {
1896        doc_com = 0;
1897    }
1898
1899    while (YYCURSOR < YYLIMIT) {
1900        if (*YYCURSOR++ == '*' && *YYCURSOR == '/') {
1901            break;
1902        }
1903    }
1904
1905    if (YYCURSOR < YYLIMIT) {
1906        YYCURSOR++;
1907    } else {
1908        zend_error(E_COMPILE_WARNING, "Unterminated comment starting line %d", CG(zend_lineno));
1909    }
1910
1911    yyleng = YYCURSOR - SCNG(yy_text);
1912    HANDLE_NEWLINES(yytext, yyleng);
1913
1914    if (doc_com) {
1915        CG(doc_comment) = zend_string_init(yytext, yyleng, 0);
1916        return T_DOC_COMMENT;
1917    }
1918
1919    return T_COMMENT;
1920}
1921
1922<ST_IN_SCRIPTING>"?>"{NEWLINE}? {
1923    BEGIN(INITIAL);
1924    return T_CLOSE_TAG;  /* implicit ';' at php-end tag */
1925}
1926
1927
1928<ST_IN_SCRIPTING>b?['] {
1929    register char *s, *t;
1930    char *end;
1931    int bprefix = (yytext[0] != '\'') ? 1 : 0;
1932
1933    while (1) {
1934        if (YYCURSOR < YYLIMIT) {
1935            if (*YYCURSOR == '\'') {
1936                YYCURSOR++;
1937                yyleng = YYCURSOR - SCNG(yy_text);
1938
1939                break;
1940            } else if (*YYCURSOR++ == '\\' && YYCURSOR < YYLIMIT) {
1941                YYCURSOR++;
1942            }
1943        } else {
1944            yyleng = YYLIMIT - SCNG(yy_text);
1945
1946            /* Unclosed single quotes; treat similar to double quotes, but without a separate token
1947             * for ' (unrecognized by parser), instead of old flex fallback to "Unexpected character..."
1948             * rule, which continued in ST_IN_SCRIPTING state after the quote */
1949            ZVAL_NULL(zendlval);
1950            return T_ENCAPSED_AND_WHITESPACE;
1951        }
1952    }
1953
1954    ZVAL_STRINGL(zendlval, yytext+bprefix+1, yyleng-bprefix-2);
1955
1956    /* convert escape sequences */
1957    s = t = Z_STRVAL_P(zendlval);
1958    end = s+Z_STRLEN_P(zendlval);
1959    while (s<end) {
1960        if (*s=='\\') {
1961            s++;
1962
1963            switch(*s) {
1964                case '\\':
1965                case '\'':
1966                    *t++ = *s;
1967                    Z_STRLEN_P(zendlval)--;
1968                    break;
1969                default:
1970                    *t++ = '\\';
1971                    *t++ = *s;
1972                    break;
1973            }
1974        } else {
1975            *t++ = *s;
1976        }
1977
1978        if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
1979            CG(zend_lineno)++;
1980        }
1981        s++;
1982    }
1983    *t = 0;
1984
1985    if (SCNG(output_filter)) {
1986        size_t sz = 0;
1987        char *str = NULL;
1988        s = Z_STRVAL_P(zendlval);
1989        // TODO: avoid reallocation ???
1990        SCNG(output_filter)((unsigned char **)&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
1991        ZVAL_STRINGL(zendlval, str, sz);
1992    }
1993    return T_CONSTANT_ENCAPSED_STRING;
1994}
1995
1996
1997<ST_IN_SCRIPTING>b?["] {
1998    int bprefix = (yytext[0] != '"') ? 1 : 0;
1999
2000    while (YYCURSOR < YYLIMIT) {
2001        switch (*YYCURSOR++) {
2002            case '"':
2003                yyleng = YYCURSOR - SCNG(yy_text);
2004                zend_scan_escape_string(zendlval, yytext+bprefix+1, yyleng-bprefix-2, '"');
2005                return T_CONSTANT_ENCAPSED_STRING;
2006            case '$':
2007                if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2008                    break;
2009                }
2010                continue;
2011            case '{':
2012                if (*YYCURSOR == '$') {
2013                    break;
2014                }
2015                continue;
2016            case '\\':
2017                if (YYCURSOR < YYLIMIT) {
2018                    YYCURSOR++;
2019                }
2020                /* fall through */
2021            default:
2022                continue;
2023        }
2024
2025        YYCURSOR--;
2026        break;
2027    }
2028
2029    /* Remember how much was scanned to save rescanning */
2030    SET_DOUBLE_QUOTES_SCANNED_LENGTH(YYCURSOR - SCNG(yy_text) - yyleng);
2031
2032    YYCURSOR = SCNG(yy_text) + yyleng;
2033
2034    BEGIN(ST_DOUBLE_QUOTES);
2035    return '"';
2036}
2037
2038
2039<ST_IN_SCRIPTING>b?"<<<"{TABS_AND_SPACES}({LABEL}|([']{LABEL}['])|(["]{LABEL}["])){NEWLINE} {
2040    char *s;
2041    int bprefix = (yytext[0] != '<') ? 1 : 0;
2042    zend_heredoc_label *heredoc_label = emalloc(sizeof(zend_heredoc_label));
2043
2044    CG(zend_lineno)++;
2045    heredoc_label->length = yyleng-bprefix-3-1-(yytext[yyleng-2]=='\r'?1:0);
2046    s = yytext+bprefix+3;
2047    while ((*s == ' ') || (*s == '\t')) {
2048        s++;
2049        heredoc_label->length--;
2050    }
2051
2052    if (*s == '\'') {
2053        s++;
2054        heredoc_label->length -= 2;
2055
2056        BEGIN(ST_NOWDOC);
2057    } else {
2058        if (*s == '"') {
2059            s++;
2060            heredoc_label->length -= 2;
2061        }
2062
2063        BEGIN(ST_HEREDOC);
2064    }
2065
2066    heredoc_label->label = estrndup(s, heredoc_label->length);
2067
2068    /* Check for ending label on the next line */
2069    if (heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, s, heredoc_label->length)) {
2070        YYCTYPE *end = YYCURSOR + heredoc_label->length;
2071
2072        if (*end == ';') {
2073            end++;
2074        }
2075
2076        if (*end == '\n' || *end == '\r') {
2077            BEGIN(ST_END_HEREDOC);
2078        }
2079    }
2080
2081    zend_ptr_stack_push(&SCNG(heredoc_label_stack), (void *) heredoc_label);
2082
2083    return T_START_HEREDOC;
2084}
2085
2086
2087<ST_IN_SCRIPTING>[`] {
2088    BEGIN(ST_BACKQUOTE);
2089    return '`';
2090}
2091
2092
2093<ST_END_HEREDOC>{ANY_CHAR} {
2094    zend_heredoc_label *heredoc_label = zend_ptr_stack_pop(&SCNG(heredoc_label_stack));
2095
2096    YYCURSOR += heredoc_label->length - 1;
2097    yyleng = heredoc_label->length;
2098
2099    heredoc_label_dtor(heredoc_label);
2100    efree(heredoc_label);
2101
2102    BEGIN(ST_IN_SCRIPTING);
2103    return T_END_HEREDOC;
2104}
2105
2106
2107<ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"{$" {
2108    Z_LVAL_P(zendlval) = (zend_long) '{';
2109    yy_push_state(ST_IN_SCRIPTING);
2110    yyless(1);
2111    return T_CURLY_OPEN;
2112}
2113
2114
2115<ST_DOUBLE_QUOTES>["] {
2116    BEGIN(ST_IN_SCRIPTING);
2117    return '"';
2118}
2119
2120<ST_BACKQUOTE>[`] {
2121    BEGIN(ST_IN_SCRIPTING);
2122    return '`';
2123}
2124
2125
2126<ST_DOUBLE_QUOTES>{ANY_CHAR} {
2127    if (GET_DOUBLE_QUOTES_SCANNED_LENGTH()) {
2128        YYCURSOR += GET_DOUBLE_QUOTES_SCANNED_LENGTH() - 1;
2129        SET_DOUBLE_QUOTES_SCANNED_LENGTH(0);
2130
2131        goto double_quotes_scan_done;
2132    }
2133
2134    if (YYCURSOR > YYLIMIT) {
2135        return 0;
2136    }
2137    if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2138        YYCURSOR++;
2139    }
2140
2141    while (YYCURSOR < YYLIMIT) {
2142        switch (*YYCURSOR++) {
2143            case '"':
2144                break;
2145            case '$':
2146                if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2147                    break;
2148                }
2149                continue;
2150            case '{':
2151                if (*YYCURSOR == '$') {
2152                    break;
2153                }
2154                continue;
2155            case '\\':
2156                if (YYCURSOR < YYLIMIT) {
2157                    YYCURSOR++;
2158                }
2159                /* fall through */
2160            default:
2161                continue;
2162        }
2163
2164        YYCURSOR--;
2165        break;
2166    }
2167
2168double_quotes_scan_done:
2169    yyleng = YYCURSOR - SCNG(yy_text);
2170
2171    zend_scan_escape_string(zendlval, yytext, yyleng, '"');
2172    return T_ENCAPSED_AND_WHITESPACE;
2173}
2174
2175
2176<ST_BACKQUOTE>{ANY_CHAR} {
2177    if (YYCURSOR > YYLIMIT) {
2178        return 0;
2179    }
2180    if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2181        YYCURSOR++;
2182    }
2183
2184    while (YYCURSOR < YYLIMIT) {
2185        switch (*YYCURSOR++) {
2186            case '`':
2187                break;
2188            case '$':
2189                if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2190                    break;
2191                }
2192                continue;
2193            case '{':
2194                if (*YYCURSOR == '$') {
2195                    break;
2196                }
2197                continue;
2198            case '\\':
2199                if (YYCURSOR < YYLIMIT) {
2200                    YYCURSOR++;
2201                }
2202                /* fall through */
2203            default:
2204                continue;
2205        }
2206
2207        YYCURSOR--;
2208        break;
2209    }
2210
2211    yyleng = YYCURSOR - SCNG(yy_text);
2212
2213    zend_scan_escape_string(zendlval, yytext, yyleng, '`');
2214    return T_ENCAPSED_AND_WHITESPACE;
2215}
2216
2217
2218<ST_HEREDOC>{ANY_CHAR} {
2219    int newline = 0;
2220
2221    zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2222
2223    if (YYCURSOR > YYLIMIT) {
2224        return 0;
2225    }
2226
2227    YYCURSOR--;
2228
2229    while (YYCURSOR < YYLIMIT) {
2230        switch (*YYCURSOR++) {
2231            case '\r':
2232                if (*YYCURSOR == '\n') {
2233                    YYCURSOR++;
2234                }
2235                /* fall through */
2236            case '\n':
2237                /* Check for ending label on the next line */
2238                if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
2239                    YYCTYPE *end = YYCURSOR + heredoc_label->length;
2240
2241                    if (*end == ';') {
2242                        end++;
2243                    }
2244
2245                    if (*end == '\n' || *end == '\r') {
2246                        /* newline before label will be subtracted from returned text, but
2247                         * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
2248                        if (YYCURSOR[-2] == '\r' && YYCURSOR[-1] == '\n') {
2249                            newline = 2; /* Windows newline */
2250                        } else {
2251                            newline = 1;
2252                        }
2253
2254                        CG(increment_lineno) = 1; /* For newline before label */
2255                        BEGIN(ST_END_HEREDOC);
2256
2257                        goto heredoc_scan_done;
2258                    }
2259                }
2260                continue;
2261            case '$':
2262                if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2263                    break;
2264                }
2265                continue;
2266            case '{':
2267                if (*YYCURSOR == '$') {
2268                    break;
2269                }
2270                continue;
2271            case '\\':
2272                if (YYCURSOR < YYLIMIT && *YYCURSOR != '\n' && *YYCURSOR != '\r') {
2273                    YYCURSOR++;
2274                }
2275                /* fall through */
2276            default:
2277                continue;
2278        }
2279
2280        YYCURSOR--;
2281        break;
2282    }
2283
2284heredoc_scan_done:
2285    yyleng = YYCURSOR - SCNG(yy_text);
2286
2287    zend_scan_escape_string(zendlval, yytext, yyleng - newline, 0);
2288    return T_ENCAPSED_AND_WHITESPACE;
2289}
2290
2291
2292<ST_NOWDOC>{ANY_CHAR} {
2293    int newline = 0;
2294
2295    zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2296
2297    if (YYCURSOR > YYLIMIT) {
2298        return 0;
2299    }
2300
2301    YYCURSOR--;
2302
2303    while (YYCURSOR < YYLIMIT) {
2304        switch (*YYCURSOR++) {
2305            case '\r':
2306                if (*YYCURSOR == '\n') {
2307                    YYCURSOR++;
2308                }
2309                /* fall through */
2310            case '\n':
2311                /* Check for ending label on the next line */
2312                if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
2313                    YYCTYPE *end = YYCURSOR + heredoc_label->length;
2314
2315                    if (*end == ';') {
2316                        end++;
2317                    }
2318
2319                    if (*end == '\n' || *end == '\r') {
2320                        /* newline before label will be subtracted from returned text, but
2321                         * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
2322                        if (YYCURSOR[-2] == '\r' && YYCURSOR[-1] == '\n') {
2323                            newline = 2; /* Windows newline */
2324                        } else {
2325                            newline = 1;
2326                        }
2327
2328                        CG(increment_lineno) = 1; /* For newline before label */
2329                        BEGIN(ST_END_HEREDOC);
2330
2331                        goto nowdoc_scan_done;
2332                    }
2333                }
2334                /* fall through */
2335            default:
2336                continue;
2337        }
2338    }
2339
2340nowdoc_scan_done:
2341    yyleng = YYCURSOR - SCNG(yy_text);
2342
2343    zend_copy_value(zendlval, yytext, yyleng - newline);
2344    HANDLE_NEWLINES(yytext, yyleng - newline);
2345    return T_ENCAPSED_AND_WHITESPACE;
2346}
2347
2348
2349<ST_IN_SCRIPTING,ST_VAR_OFFSET>{ANY_CHAR} {
2350    if (YYCURSOR > YYLIMIT) {
2351        return 0;
2352    }
2353
2354    zend_error(E_COMPILE_WARNING,"Unexpected character in input:  '%c' (ASCII=%d) state=%d", yytext[0], yytext[0], YYSTATE);
2355    goto restart;
2356}
2357
2358*/
2359}
2360