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