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