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