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_SPACESHIP;
1458}
1459
1460<ST_IN_SCRIPTING>"<=" {
1461    return T_IS_SMALLER_OR_EQUAL;
1462}
1463
1464<ST_IN_SCRIPTING>">=" {
1465    return T_IS_GREATER_OR_EQUAL;
1466}
1467
1468<ST_IN_SCRIPTING>"+=" {
1469    return T_PLUS_EQUAL;
1470}
1471
1472<ST_IN_SCRIPTING>"-=" {
1473    return T_MINUS_EQUAL;
1474}
1475
1476<ST_IN_SCRIPTING>"*=" {
1477    return T_MUL_EQUAL;
1478}
1479
1480<ST_IN_SCRIPTING>"*\*" {
1481    return T_POW;
1482}
1483
1484<ST_IN_SCRIPTING>"*\*=" {
1485    return T_POW_EQUAL;
1486}
1487
1488<ST_IN_SCRIPTING>"/=" {
1489    return T_DIV_EQUAL;
1490}
1491
1492<ST_IN_SCRIPTING>".=" {
1493    return T_CONCAT_EQUAL;
1494}
1495
1496<ST_IN_SCRIPTING>"%=" {
1497    return T_MOD_EQUAL;
1498}
1499
1500<ST_IN_SCRIPTING>"<<=" {
1501    return T_SL_EQUAL;
1502}
1503
1504<ST_IN_SCRIPTING>">>=" {
1505    return T_SR_EQUAL;
1506}
1507
1508<ST_IN_SCRIPTING>"&=" {
1509    return T_AND_EQUAL;
1510}
1511
1512<ST_IN_SCRIPTING>"|=" {
1513    return T_OR_EQUAL;
1514}
1515
1516<ST_IN_SCRIPTING>"^=" {
1517    return T_XOR_EQUAL;
1518}
1519
1520<ST_IN_SCRIPTING>"||" {
1521    return T_BOOLEAN_OR;
1522}
1523
1524<ST_IN_SCRIPTING>"&&" {
1525    return T_BOOLEAN_AND;
1526}
1527
1528<ST_IN_SCRIPTING>"OR" {
1529    return T_LOGICAL_OR;
1530}
1531
1532<ST_IN_SCRIPTING>"AND" {
1533    return T_LOGICAL_AND;
1534}
1535
1536<ST_IN_SCRIPTING>"XOR" {
1537    return T_LOGICAL_XOR;
1538}
1539
1540<ST_IN_SCRIPTING>"<<" {
1541    return T_SL;
1542}
1543
1544<ST_IN_SCRIPTING>">>" {
1545    return T_SR;
1546}
1547
1548<ST_IN_SCRIPTING>{TOKENS} {
1549    return yytext[0];
1550}
1551
1552
1553<ST_IN_SCRIPTING>"{" {
1554    yy_push_state(ST_IN_SCRIPTING);
1555    return '{';
1556}
1557
1558
1559<ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"${" {
1560    yy_push_state(ST_LOOKING_FOR_VARNAME);
1561    return T_DOLLAR_OPEN_CURLY_BRACES;
1562}
1563
1564
1565<ST_IN_SCRIPTING>"}" {
1566    RESET_DOC_COMMENT();
1567    if (!zend_stack_is_empty(&SCNG(state_stack))) {
1568        yy_pop_state();
1569    }
1570    return '}';
1571}
1572
1573
1574<ST_LOOKING_FOR_VARNAME>{LABEL}[[}] {
1575    yyless(yyleng - 1);
1576    zend_copy_value(zendlval, yytext, yyleng);
1577    yy_pop_state();
1578    yy_push_state(ST_IN_SCRIPTING);
1579    return T_STRING_VARNAME;
1580}
1581
1582
1583<ST_LOOKING_FOR_VARNAME>{ANY_CHAR} {
1584    yyless(0);
1585    yy_pop_state();
1586    yy_push_state(ST_IN_SCRIPTING);
1587    goto restart;
1588}
1589
1590<ST_IN_SCRIPTING>{BNUM} {
1591    char *bin = yytext + 2; /* Skip "0b" */
1592    int len = yyleng - 2;
1593    char *end;
1594
1595    /* Skip any leading 0s */
1596    while (*bin == '0') {
1597        ++bin;
1598        --len;
1599    }
1600
1601    if (len < SIZEOF_ZEND_LONG * 8) {
1602        if (len == 0) {
1603            ZVAL_LONG(zendlval, 0);
1604        } else {
1605            errno = 0;
1606            ZVAL_LONG(zendlval, ZEND_STRTOL(bin, &end, 2));
1607            ZEND_ASSERT(!errno && end == yytext + yyleng);
1608        }
1609        return T_LNUMBER;
1610    } else {
1611        ZVAL_DOUBLE(zendlval, zend_bin_strtod(bin, (const char **)&end));
1612        /* errno isn't checked since we allow HUGE_VAL/INF overflow */
1613        ZEND_ASSERT(end == yytext + yyleng);
1614        return T_DNUMBER;
1615    }
1616}
1617
1618<ST_IN_SCRIPTING>{LNUM} {
1619    char *end;
1620    if (yyleng < MAX_LENGTH_OF_LONG - 1) { /* Won't overflow */
1621        errno = 0;
1622        ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 0));
1623        /* This isn't an assert, we need to ensure 019 isn't valid octal
1624         * Because the lexing itself doesn't do that for us
1625         */
1626        if (end != yytext + yyleng) {
1627            zend_error_noreturn(E_COMPILE_ERROR, "Invalid numeric literal");
1628        }
1629        ZEND_ASSERT(!errno);
1630    } else {
1631        errno = 0;
1632        ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 0));
1633        if (errno == ERANGE) { /* Overflow */
1634            errno = 0;
1635            if (yytext[0] == '0') { /* octal overflow */
1636                errno = 0;
1637                ZVAL_DOUBLE(zendlval, zend_oct_strtod(yytext, (const char **)&end));
1638            } else {
1639                ZVAL_DOUBLE(zendlval, zend_strtod(yytext, (const char **)&end));
1640            }
1641            /* Also not an assert for the same reason */
1642            if (end != yytext + yyleng) {
1643                zend_error_noreturn(E_COMPILE_ERROR, "Invalid numeric literal");
1644            }
1645            ZEND_ASSERT(!errno);
1646            return T_DNUMBER;
1647        }
1648        /* Also not an assert for the same reason */
1649        if (end != yytext + yyleng) {
1650            zend_error_noreturn(E_COMPILE_ERROR, "Invalid numeric literal");
1651        }
1652        ZEND_ASSERT(!errno);
1653    }
1654    return T_LNUMBER;
1655}
1656
1657<ST_IN_SCRIPTING>{HNUM} {
1658    char *hex = yytext + 2; /* Skip "0x" */
1659    int len = yyleng - 2;
1660    char *end;
1661
1662    /* Skip any leading 0s */
1663    while (*hex == '0') {
1664        hex++;
1665        len--;
1666    }
1667
1668    if (len < SIZEOF_ZEND_LONG * 2 || (len == SIZEOF_ZEND_LONG * 2 && *hex <= '7')) {
1669        if (len == 0) {
1670            ZVAL_LONG(zendlval, 0);
1671        } else {
1672            errno = 0;
1673            ZVAL_LONG(zendlval, ZEND_STRTOL(hex, &end, 16));
1674            ZEND_ASSERT(!errno && end == hex + len);
1675        }
1676        return T_LNUMBER;
1677    } else {
1678        ZVAL_DOUBLE(zendlval, zend_hex_strtod(hex, (const char **)&end));
1679        /* errno isn't checked since we allow HUGE_VAL/INF overflow */
1680        ZEND_ASSERT(end == hex + len);
1681        return T_DNUMBER;
1682    }
1683}
1684
1685<ST_VAR_OFFSET>[0]|([1-9][0-9]*) { /* Offset could be treated as a long */
1686    if (yyleng < MAX_LENGTH_OF_LONG - 1 || (yyleng == MAX_LENGTH_OF_LONG - 1 && strcmp(yytext, long_min_digits) < 0)) {
1687        char *end;
1688        errno = 0;
1689        ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 10));
1690        if (errno == ERANGE) {
1691            goto string;
1692        }
1693        ZEND_ASSERT(end == yytext + yyleng);
1694    } else {
1695string:
1696        ZVAL_STRINGL(zendlval, yytext, yyleng);
1697    }
1698    return T_NUM_STRING;
1699}
1700
1701<ST_VAR_OFFSET>{LNUM}|{HNUM}|{BNUM} { /* Offset must be treated as a string */
1702    ZVAL_STRINGL(zendlval, yytext, yyleng);
1703    return T_NUM_STRING;
1704}
1705
1706<ST_IN_SCRIPTING>{DNUM}|{EXPONENT_DNUM} {
1707    const char *end;
1708
1709    ZVAL_DOUBLE(zendlval, zend_strtod(yytext, &end));
1710    /* errno isn't checked since we allow HUGE_VAL/INF overflow */
1711    ZEND_ASSERT(end == yytext + yyleng);
1712    return T_DNUMBER;
1713}
1714
1715<ST_IN_SCRIPTING>"__CLASS__" {
1716    return T_CLASS_C;
1717}
1718
1719<ST_IN_SCRIPTING>"__TRAIT__" {
1720    return T_TRAIT_C;
1721}
1722
1723<ST_IN_SCRIPTING>"__FUNCTION__" {
1724    return T_FUNC_C;
1725}
1726
1727<ST_IN_SCRIPTING>"__METHOD__" {
1728    return T_METHOD_C;
1729}
1730
1731<ST_IN_SCRIPTING>"__LINE__" {
1732    return T_LINE;
1733}
1734
1735<ST_IN_SCRIPTING>"__FILE__" {
1736    return T_FILE;
1737}
1738
1739<ST_IN_SCRIPTING>"__DIR__" {
1740    return T_DIR;
1741}
1742
1743<ST_IN_SCRIPTING>"__NAMESPACE__" {
1744    return T_NS_C;
1745}
1746
1747
1748<INITIAL>"<?=" {
1749    BEGIN(ST_IN_SCRIPTING);
1750    return T_OPEN_TAG_WITH_ECHO;
1751}
1752
1753
1754<INITIAL>"<?php"([ \t]|{NEWLINE}) {
1755    HANDLE_NEWLINE(yytext[yyleng-1]);
1756    BEGIN(ST_IN_SCRIPTING);
1757    return T_OPEN_TAG;
1758}
1759
1760
1761<INITIAL>"<?" {
1762    if (CG(short_tags)) {
1763        BEGIN(ST_IN_SCRIPTING);
1764        return T_OPEN_TAG;
1765    } else {
1766        goto inline_char_handler;
1767    }
1768}
1769
1770<INITIAL>{ANY_CHAR} {
1771    if (YYCURSOR > YYLIMIT) {
1772        return 0;
1773    }
1774
1775inline_char_handler:
1776
1777    while (1) {
1778        YYCTYPE *ptr = memchr(YYCURSOR, '<', YYLIMIT - YYCURSOR);
1779
1780        YYCURSOR = ptr ? ptr + 1 : YYLIMIT;
1781
1782        if (YYCURSOR >= YYLIMIT) {
1783            break;
1784        }
1785
1786        if (*YYCURSOR == '?') {
1787            if (CG(short_tags) || !strncasecmp((char*)YYCURSOR + 1, "php", 3) || (*(YYCURSOR + 1) == '=')) { /* Assume [ \t\n\r] follows "php" */
1788
1789                YYCURSOR--;
1790                break;
1791            }
1792        }
1793    }
1794
1795    yyleng = YYCURSOR - SCNG(yy_text);
1796
1797    if (SCNG(output_filter)) {
1798        size_t readsize;
1799        char *s = NULL;
1800        size_t sz = 0;
1801        // TODO: avoid reallocation ???
1802        readsize = SCNG(output_filter)((unsigned char **)&s, &sz, (unsigned char *)yytext, (size_t)yyleng);
1803        ZVAL_STRINGL(zendlval, s, sz);
1804        efree(s);
1805        if (readsize < yyleng) {
1806            yyless(readsize);
1807        }
1808    } else {
1809      ZVAL_STRINGL(zendlval, yytext, yyleng);
1810    }
1811    HANDLE_NEWLINES(yytext, yyleng);
1812    return T_INLINE_HTML;
1813}
1814
1815
1816/* Make sure a label character follows "->", otherwise there is no property
1817 * and "->" will be taken literally
1818 */
1819<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"->"[a-zA-Z_\x7f-\xff] {
1820    yyless(yyleng - 3);
1821    yy_push_state(ST_LOOKING_FOR_PROPERTY);
1822    zend_copy_value(zendlval, (yytext+1), (yyleng-1));
1823    return T_VARIABLE;
1824}
1825
1826/* A [ always designates a variable offset, regardless of what follows
1827 */
1828<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"[" {
1829    yyless(yyleng - 1);
1830    yy_push_state(ST_VAR_OFFSET);
1831    zend_copy_value(zendlval, (yytext+1), (yyleng-1));
1832    return T_VARIABLE;
1833}
1834
1835<ST_IN_SCRIPTING,ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE,ST_VAR_OFFSET>"$"{LABEL} {
1836    zend_copy_value(zendlval, (yytext+1), (yyleng-1));
1837    return T_VARIABLE;
1838}
1839
1840<ST_VAR_OFFSET>"]" {
1841    yy_pop_state();
1842    return ']';
1843}
1844
1845<ST_VAR_OFFSET>{TOKENS}|[{}"`] {
1846    /* Only '[' can be valid, but returning other tokens will allow a more explicit parse error */
1847    return yytext[0];
1848}
1849
1850<ST_VAR_OFFSET>[ \n\r\t\\'#] {
1851    /* Invalid rule to return a more explicit parse error with proper line number */
1852    yyless(0);
1853    yy_pop_state();
1854    ZVAL_NULL(zendlval);
1855    return T_ENCAPSED_AND_WHITESPACE;
1856}
1857
1858<ST_IN_SCRIPTING,ST_VAR_OFFSET>{LABEL} {
1859    zend_copy_value(zendlval, yytext, yyleng);
1860    return T_STRING;
1861}
1862
1863
1864<ST_IN_SCRIPTING>"#"|"//" {
1865    while (YYCURSOR < YYLIMIT) {
1866        switch (*YYCURSOR++) {
1867            case '\r':
1868                if (*YYCURSOR == '\n') {
1869                    YYCURSOR++;
1870                }
1871                /* fall through */
1872            case '\n':
1873                CG(zend_lineno)++;
1874                break;
1875            case '?':
1876                if (*YYCURSOR == '>') {
1877                    YYCURSOR--;
1878                    break;
1879                }
1880                /* fall through */
1881            default:
1882                continue;
1883        }
1884
1885        break;
1886    }
1887
1888    yyleng = YYCURSOR - SCNG(yy_text);
1889
1890    return T_COMMENT;
1891}
1892
1893<ST_IN_SCRIPTING>"/*"|"/**"{WHITESPACE} {
1894    int doc_com;
1895
1896    if (yyleng > 2) {
1897        doc_com = 1;
1898        RESET_DOC_COMMENT();
1899    } else {
1900        doc_com = 0;
1901    }
1902
1903    while (YYCURSOR < YYLIMIT) {
1904        if (*YYCURSOR++ == '*' && *YYCURSOR == '/') {
1905            break;
1906        }
1907    }
1908
1909    if (YYCURSOR < YYLIMIT) {
1910        YYCURSOR++;
1911    } else {
1912        zend_error(E_COMPILE_WARNING, "Unterminated comment starting line %d", CG(zend_lineno));
1913    }
1914
1915    yyleng = YYCURSOR - SCNG(yy_text);
1916    HANDLE_NEWLINES(yytext, yyleng);
1917
1918    if (doc_com) {
1919        CG(doc_comment) = zend_string_init(yytext, yyleng, 0);
1920        return T_DOC_COMMENT;
1921    }
1922
1923    return T_COMMENT;
1924}
1925
1926<ST_IN_SCRIPTING>"?>"{NEWLINE}? {
1927    BEGIN(INITIAL);
1928    return T_CLOSE_TAG;  /* implicit ';' at php-end tag */
1929}
1930
1931
1932<ST_IN_SCRIPTING>b?['] {
1933    register char *s, *t;
1934    char *end;
1935    int bprefix = (yytext[0] != '\'') ? 1 : 0;
1936
1937    while (1) {
1938        if (YYCURSOR < YYLIMIT) {
1939            if (*YYCURSOR == '\'') {
1940                YYCURSOR++;
1941                yyleng = YYCURSOR - SCNG(yy_text);
1942
1943                break;
1944            } else if (*YYCURSOR++ == '\\' && YYCURSOR < YYLIMIT) {
1945                YYCURSOR++;
1946            }
1947        } else {
1948            yyleng = YYLIMIT - SCNG(yy_text);
1949
1950            /* Unclosed single quotes; treat similar to double quotes, but without a separate token
1951             * for ' (unrecognized by parser), instead of old flex fallback to "Unexpected character..."
1952             * rule, which continued in ST_IN_SCRIPTING state after the quote */
1953            ZVAL_NULL(zendlval);
1954            return T_ENCAPSED_AND_WHITESPACE;
1955        }
1956    }
1957
1958    ZVAL_STRINGL(zendlval, yytext+bprefix+1, yyleng-bprefix-2);
1959
1960    /* convert escape sequences */
1961    s = t = Z_STRVAL_P(zendlval);
1962    end = s+Z_STRLEN_P(zendlval);
1963    while (s<end) {
1964        if (*s=='\\') {
1965            s++;
1966
1967            switch(*s) {
1968                case '\\':
1969                case '\'':
1970                    *t++ = *s;
1971                    Z_STRLEN_P(zendlval)--;
1972                    break;
1973                default:
1974                    *t++ = '\\';
1975                    *t++ = *s;
1976                    break;
1977            }
1978        } else {
1979            *t++ = *s;
1980        }
1981
1982        if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
1983            CG(zend_lineno)++;
1984        }
1985        s++;
1986    }
1987    *t = 0;
1988
1989    if (SCNG(output_filter)) {
1990        size_t sz = 0;
1991        char *str = NULL;
1992        s = Z_STRVAL_P(zendlval);
1993        // TODO: avoid reallocation ???
1994        SCNG(output_filter)((unsigned char **)&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
1995        ZVAL_STRINGL(zendlval, str, sz);
1996    }
1997    return T_CONSTANT_ENCAPSED_STRING;
1998}
1999
2000
2001<ST_IN_SCRIPTING>b?["] {
2002    int bprefix = (yytext[0] != '"') ? 1 : 0;
2003
2004    while (YYCURSOR < YYLIMIT) {
2005        switch (*YYCURSOR++) {
2006            case '"':
2007                yyleng = YYCURSOR - SCNG(yy_text);
2008                zend_scan_escape_string(zendlval, yytext+bprefix+1, yyleng-bprefix-2, '"');
2009                return T_CONSTANT_ENCAPSED_STRING;
2010            case '$':
2011                if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2012                    break;
2013                }
2014                continue;
2015            case '{':
2016                if (*YYCURSOR == '$') {
2017                    break;
2018                }
2019                continue;
2020            case '\\':
2021                if (YYCURSOR < YYLIMIT) {
2022                    YYCURSOR++;
2023                }
2024                /* fall through */
2025            default:
2026                continue;
2027        }
2028
2029        YYCURSOR--;
2030        break;
2031    }
2032
2033    /* Remember how much was scanned to save rescanning */
2034    SET_DOUBLE_QUOTES_SCANNED_LENGTH(YYCURSOR - SCNG(yy_text) - yyleng);
2035
2036    YYCURSOR = SCNG(yy_text) + yyleng;
2037
2038    BEGIN(ST_DOUBLE_QUOTES);
2039    return '"';
2040}
2041
2042
2043<ST_IN_SCRIPTING>b?"<<<"{TABS_AND_SPACES}({LABEL}|([']{LABEL}['])|(["]{LABEL}["])){NEWLINE} {
2044    char *s;
2045    int bprefix = (yytext[0] != '<') ? 1 : 0;
2046    zend_heredoc_label *heredoc_label = emalloc(sizeof(zend_heredoc_label));
2047
2048    CG(zend_lineno)++;
2049    heredoc_label->length = yyleng-bprefix-3-1-(yytext[yyleng-2]=='\r'?1:0);
2050    s = yytext+bprefix+3;
2051    while ((*s == ' ') || (*s == '\t')) {
2052        s++;
2053        heredoc_label->length--;
2054    }
2055
2056    if (*s == '\'') {
2057        s++;
2058        heredoc_label->length -= 2;
2059
2060        BEGIN(ST_NOWDOC);
2061    } else {
2062        if (*s == '"') {
2063            s++;
2064            heredoc_label->length -= 2;
2065        }
2066
2067        BEGIN(ST_HEREDOC);
2068    }
2069
2070    heredoc_label->label = estrndup(s, heredoc_label->length);
2071
2072    /* Check for ending label on the next line */
2073    if (heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, s, heredoc_label->length)) {
2074        YYCTYPE *end = YYCURSOR + heredoc_label->length;
2075
2076        if (*end == ';') {
2077            end++;
2078        }
2079
2080        if (*end == '\n' || *end == '\r') {
2081            BEGIN(ST_END_HEREDOC);
2082        }
2083    }
2084
2085    zend_ptr_stack_push(&SCNG(heredoc_label_stack), (void *) heredoc_label);
2086
2087    return T_START_HEREDOC;
2088}
2089
2090
2091<ST_IN_SCRIPTING>[`] {
2092    BEGIN(ST_BACKQUOTE);
2093    return '`';
2094}
2095
2096
2097<ST_END_HEREDOC>{ANY_CHAR} {
2098    zend_heredoc_label *heredoc_label = zend_ptr_stack_pop(&SCNG(heredoc_label_stack));
2099
2100    YYCURSOR += heredoc_label->length - 1;
2101    yyleng = heredoc_label->length;
2102
2103    heredoc_label_dtor(heredoc_label);
2104    efree(heredoc_label);
2105
2106    BEGIN(ST_IN_SCRIPTING);
2107    return T_END_HEREDOC;
2108}
2109
2110
2111<ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"{$" {
2112    Z_LVAL_P(zendlval) = (zend_long) '{';
2113    yy_push_state(ST_IN_SCRIPTING);
2114    yyless(1);
2115    return T_CURLY_OPEN;
2116}
2117
2118
2119<ST_DOUBLE_QUOTES>["] {
2120    BEGIN(ST_IN_SCRIPTING);
2121    return '"';
2122}
2123
2124<ST_BACKQUOTE>[`] {
2125    BEGIN(ST_IN_SCRIPTING);
2126    return '`';
2127}
2128
2129
2130<ST_DOUBLE_QUOTES>{ANY_CHAR} {
2131    if (GET_DOUBLE_QUOTES_SCANNED_LENGTH()) {
2132        YYCURSOR += GET_DOUBLE_QUOTES_SCANNED_LENGTH() - 1;
2133        SET_DOUBLE_QUOTES_SCANNED_LENGTH(0);
2134
2135        goto double_quotes_scan_done;
2136    }
2137
2138    if (YYCURSOR > YYLIMIT) {
2139        return 0;
2140    }
2141    if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2142        YYCURSOR++;
2143    }
2144
2145    while (YYCURSOR < YYLIMIT) {
2146        switch (*YYCURSOR++) {
2147            case '"':
2148                break;
2149            case '$':
2150                if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2151                    break;
2152                }
2153                continue;
2154            case '{':
2155                if (*YYCURSOR == '$') {
2156                    break;
2157                }
2158                continue;
2159            case '\\':
2160                if (YYCURSOR < YYLIMIT) {
2161                    YYCURSOR++;
2162                }
2163                /* fall through */
2164            default:
2165                continue;
2166        }
2167
2168        YYCURSOR--;
2169        break;
2170    }
2171
2172double_quotes_scan_done:
2173    yyleng = YYCURSOR - SCNG(yy_text);
2174
2175    zend_scan_escape_string(zendlval, yytext, yyleng, '"');
2176    return T_ENCAPSED_AND_WHITESPACE;
2177}
2178
2179
2180<ST_BACKQUOTE>{ANY_CHAR} {
2181    if (YYCURSOR > YYLIMIT) {
2182        return 0;
2183    }
2184    if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2185        YYCURSOR++;
2186    }
2187
2188    while (YYCURSOR < YYLIMIT) {
2189        switch (*YYCURSOR++) {
2190            case '`':
2191                break;
2192            case '$':
2193                if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2194                    break;
2195                }
2196                continue;
2197            case '{':
2198                if (*YYCURSOR == '$') {
2199                    break;
2200                }
2201                continue;
2202            case '\\':
2203                if (YYCURSOR < YYLIMIT) {
2204                    YYCURSOR++;
2205                }
2206                /* fall through */
2207            default:
2208                continue;
2209        }
2210
2211        YYCURSOR--;
2212        break;
2213    }
2214
2215    yyleng = YYCURSOR - SCNG(yy_text);
2216
2217    zend_scan_escape_string(zendlval, yytext, yyleng, '`');
2218    return T_ENCAPSED_AND_WHITESPACE;
2219}
2220
2221
2222<ST_HEREDOC>{ANY_CHAR} {
2223    int newline = 0;
2224
2225    zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2226
2227    if (YYCURSOR > YYLIMIT) {
2228        return 0;
2229    }
2230
2231    YYCURSOR--;
2232
2233    while (YYCURSOR < YYLIMIT) {
2234        switch (*YYCURSOR++) {
2235            case '\r':
2236                if (*YYCURSOR == '\n') {
2237                    YYCURSOR++;
2238                }
2239                /* fall through */
2240            case '\n':
2241                /* Check for ending label on the next line */
2242                if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
2243                    YYCTYPE *end = YYCURSOR + heredoc_label->length;
2244
2245                    if (*end == ';') {
2246                        end++;
2247                    }
2248
2249                    if (*end == '\n' || *end == '\r') {
2250                        /* newline before label will be subtracted from returned text, but
2251                         * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
2252                        if (YYCURSOR[-2] == '\r' && YYCURSOR[-1] == '\n') {
2253                            newline = 2; /* Windows newline */
2254                        } else {
2255                            newline = 1;
2256                        }
2257
2258                        CG(increment_lineno) = 1; /* For newline before label */
2259                        BEGIN(ST_END_HEREDOC);
2260
2261                        goto heredoc_scan_done;
2262                    }
2263                }
2264                continue;
2265            case '$':
2266                if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2267                    break;
2268                }
2269                continue;
2270            case '{':
2271                if (*YYCURSOR == '$') {
2272                    break;
2273                }
2274                continue;
2275            case '\\':
2276                if (YYCURSOR < YYLIMIT && *YYCURSOR != '\n' && *YYCURSOR != '\r') {
2277                    YYCURSOR++;
2278                }
2279                /* fall through */
2280            default:
2281                continue;
2282        }
2283
2284        YYCURSOR--;
2285        break;
2286    }
2287
2288heredoc_scan_done:
2289    yyleng = YYCURSOR - SCNG(yy_text);
2290
2291    zend_scan_escape_string(zendlval, yytext, yyleng - newline, 0);
2292    return T_ENCAPSED_AND_WHITESPACE;
2293}
2294
2295
2296<ST_NOWDOC>{ANY_CHAR} {
2297    int newline = 0;
2298
2299    zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2300
2301    if (YYCURSOR > YYLIMIT) {
2302        return 0;
2303    }
2304
2305    YYCURSOR--;
2306
2307    while (YYCURSOR < YYLIMIT) {
2308        switch (*YYCURSOR++) {
2309            case '\r':
2310                if (*YYCURSOR == '\n') {
2311                    YYCURSOR++;
2312                }
2313                /* fall through */
2314            case '\n':
2315                /* Check for ending label on the next line */
2316                if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
2317                    YYCTYPE *end = YYCURSOR + heredoc_label->length;
2318
2319                    if (*end == ';') {
2320                        end++;
2321                    }
2322
2323                    if (*end == '\n' || *end == '\r') {
2324                        /* newline before label will be subtracted from returned text, but
2325                         * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
2326                        if (YYCURSOR[-2] == '\r' && YYCURSOR[-1] == '\n') {
2327                            newline = 2; /* Windows newline */
2328                        } else {
2329                            newline = 1;
2330                        }
2331
2332                        CG(increment_lineno) = 1; /* For newline before label */
2333                        BEGIN(ST_END_HEREDOC);
2334
2335                        goto nowdoc_scan_done;
2336                    }
2337                }
2338                /* fall through */
2339            default:
2340                continue;
2341        }
2342    }
2343
2344nowdoc_scan_done:
2345    yyleng = YYCURSOR - SCNG(yy_text);
2346
2347    zend_copy_value(zendlval, yytext, yyleng - newline);
2348    HANDLE_NEWLINES(yytext, yyleng - newline);
2349    return T_ENCAPSED_AND_WHITESPACE;
2350}
2351
2352
2353<ST_IN_SCRIPTING,ST_VAR_OFFSET>{ANY_CHAR} {
2354    if (YYCURSOR > YYLIMIT) {
2355        return 0;
2356    }
2357
2358    zend_error(E_COMPILE_WARNING,"Unexpected character in input:  '%c' (ASCII=%d) state=%d", yytext[0], yytext[0], YYSTATE);
2359    goto restart;
2360}
2361
2362*/
2363}
2364