1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2016 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
571static zend_op_array *zend_compile(int type)
572{
573	zend_op_array *op_array = NULL;
574	zend_bool original_in_compilation = CG(in_compilation);
575
576	CG(in_compilation) = 1;
577	CG(ast) = NULL;
578	CG(ast_arena) = zend_arena_create(1024 * 32);
579
580	if (!zendparse()) {
581		zend_file_context original_file_context;
582		zend_oparray_context original_oparray_context;
583		zend_op_array *original_active_op_array = CG(active_op_array);
584
585		op_array = emalloc(sizeof(zend_op_array));
586		init_op_array(op_array, type, INITIAL_OP_ARRAY_SIZE);
587		CG(active_op_array) = op_array;
588
589		if (zend_ast_process) {
590			zend_ast_process(CG(ast));
591		}
592
593		zend_file_context_begin(&original_file_context);
594		zend_oparray_context_begin(&original_oparray_context);
595		zend_compile_top_stmt(CG(ast));
596		zend_emit_final_return(type == ZEND_USER_FUNCTION);
597		op_array->line_start = 1;
598		op_array->line_end = CG(zend_lineno);
599		pass_two(op_array);
600		zend_oparray_context_end(&original_oparray_context);
601		zend_file_context_end(&original_file_context);
602
603		CG(active_op_array) = original_active_op_array;
604	}
605
606	zend_ast_destroy(CG(ast));
607	zend_arena_destroy(CG(ast_arena));
608
609	CG(in_compilation) = original_in_compilation;
610
611	return op_array;
612}
613
614ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type)
615{
616	zend_lex_state original_lex_state;
617	zend_op_array *op_array = NULL;
618	zend_save_lexical_state(&original_lex_state);
619
620	if (open_file_for_scanning(file_handle)==FAILURE) {
621		if (type==ZEND_REQUIRE) {
622			zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle->filename);
623			zend_bailout();
624		} else {
625			zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file_handle->filename);
626		}
627	} else {
628		op_array = zend_compile(ZEND_USER_FUNCTION);
629	}
630
631	zend_restore_lexical_state(&original_lex_state);
632	return op_array;
633}
634
635
636zend_op_array *compile_filename(int type, zval *filename)
637{
638	zend_file_handle file_handle;
639	zval tmp;
640	zend_op_array *retval;
641	zend_string *opened_path = NULL;
642
643	if (Z_TYPE_P(filename) != IS_STRING) {
644		tmp = *filename;
645		zval_copy_ctor(&tmp);
646		convert_to_string(&tmp);
647		filename = &tmp;
648	}
649	file_handle.filename = Z_STRVAL_P(filename);
650	file_handle.free_filename = 0;
651	file_handle.type = ZEND_HANDLE_FILENAME;
652	file_handle.opened_path = NULL;
653	file_handle.handle.fp = NULL;
654
655	retval = zend_compile_file(&file_handle, type);
656	if (retval && file_handle.handle.stream.handle) {
657		if (!file_handle.opened_path) {
658			file_handle.opened_path = opened_path = zend_string_copy(Z_STR_P(filename));
659		}
660
661		zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path);
662
663		if (opened_path) {
664			zend_string_release(opened_path);
665		}
666	}
667	zend_destroy_file_handle(&file_handle);
668
669	if (filename==&tmp) {
670		zval_dtor(&tmp);
671	}
672	return retval;
673}
674
675ZEND_API int zend_prepare_string_for_scanning(zval *str, char *filename)
676{
677	char *buf;
678	size_t size, old_len;
679	zend_string *new_compiled_filename;
680
681	/* enforce ZEND_MMAP_AHEAD trailing NULLs for flex... */
682	old_len = Z_STRLEN_P(str);
683	Z_STR_P(str) = zend_string_extend(Z_STR_P(str), old_len + ZEND_MMAP_AHEAD, 0);
684	Z_TYPE_INFO_P(str) = IS_STRING_EX;
685	memset(Z_STRVAL_P(str) + old_len, 0, ZEND_MMAP_AHEAD + 1);
686
687	SCNG(yy_in) = NULL;
688	SCNG(yy_start) = NULL;
689
690	buf = Z_STRVAL_P(str);
691	size = old_len;
692
693	if (CG(multibyte)) {
694		SCNG(script_org) = (unsigned char*)buf;
695		SCNG(script_org_size) = size;
696		SCNG(script_filtered) = NULL;
697
698		zend_multibyte_set_filter(zend_multibyte_get_internal_encoding());
699
700		if (SCNG(input_filter)) {
701			if ((size_t)-1 == SCNG(input_filter)(&SCNG(script_filtered), &SCNG(script_filtered_size), SCNG(script_org), SCNG(script_org_size))) {
702				zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
703						"encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
704			}
705			buf = (char*)SCNG(script_filtered);
706			size = SCNG(script_filtered_size);
707		}
708	}
709
710	yy_scan_buffer(buf, (unsigned int)size);
711
712	new_compiled_filename = zend_string_init(filename, strlen(filename), 0);
713	zend_set_compiled_filename(new_compiled_filename);
714	zend_string_release(new_compiled_filename);
715	CG(zend_lineno) = 1;
716	CG(increment_lineno) = 0;
717	RESET_DOC_COMMENT();
718	return SUCCESS;
719}
720
721
722ZEND_API size_t zend_get_scanned_file_offset(void)
723{
724	size_t offset = SCNG(yy_cursor) - SCNG(yy_start);
725	if (SCNG(input_filter)) {
726		size_t original_offset = offset, length = 0;
727		do {
728			unsigned char *p = NULL;
729			if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), offset)) {
730				return (size_t)-1;
731			}
732			efree(p);
733			if (length > original_offset) {
734				offset--;
735			} else if (length < original_offset) {
736				offset++;
737			}
738		} while (original_offset != length);
739	}
740	return offset;
741}
742
743zend_op_array *compile_string(zval *source_string, char *filename)
744{
745	zend_lex_state original_lex_state;
746	zend_op_array *op_array = NULL;
747	zval tmp;
748
749	if (Z_STRLEN_P(source_string)==0) {
750		return NULL;
751	}
752
753	ZVAL_DUP(&tmp, source_string);
754	convert_to_string(&tmp);
755	source_string = &tmp;
756
757	zend_save_lexical_state(&original_lex_state);
758	if (zend_prepare_string_for_scanning(source_string, filename) == SUCCESS) {
759		BEGIN(ST_IN_SCRIPTING);
760		op_array = zend_compile(ZEND_EVAL_CODE);
761	}
762
763	zend_restore_lexical_state(&original_lex_state);
764	zval_dtor(&tmp);
765
766	return op_array;
767}
768
769
770BEGIN_EXTERN_C()
771int highlight_file(char *filename, zend_syntax_highlighter_ini *syntax_highlighter_ini)
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);
781	if (open_file_for_scanning(&file_handle)==FAILURE) {
782		zend_message_dispatcher(ZMSG_FAILED_HIGHLIGHT_FOPEN, filename);
783		zend_restore_lexical_state(&original_lex_state);
784		return FAILURE;
785	}
786	zend_highlight(syntax_highlighter_ini);
787	if (SCNG(script_filtered)) {
788		efree(SCNG(script_filtered));
789		SCNG(script_filtered) = NULL;
790	}
791	zend_destroy_file_handle(&file_handle);
792	zend_restore_lexical_state(&original_lex_state);
793	return SUCCESS;
794}
795
796int highlight_string(zval *str, zend_syntax_highlighter_ini *syntax_highlighter_ini, char *str_name)
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);
804	if (zend_prepare_string_for_scanning(str, str_name)==FAILURE) {
805		zend_restore_lexical_state(&original_lex_state);
806		return FAILURE;
807	}
808	BEGIN(INITIAL);
809	zend_highlight(syntax_highlighter_ini);
810	if (SCNG(script_filtered)) {
811		efree(SCNG(script_filtered));
812		SCNG(script_filtered) = NULL;
813	}
814	zend_restore_lexical_state(&original_lex_state);
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)
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))) {
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); \
860		ZVAL_STRINGL(zendlval, s, sz); \
861		efree(s); \
862	} else { \
863		ZVAL_STRINGL(zendlval, yytext, yyleng); \
864	}
865
866static int zend_scan_escape_string(zval *zendlval, char *str, int len, char quote_type)
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 ZEND_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				/* UTF-8 codepoint escape, format: /\\u\{\x+\}/ */
945				case 'u':
946					{
947						/* cache where we started so we can parse after validating */
948						char *start = s + 1;
949						size_t len = 0;
950						zend_bool valid = 1;
951						unsigned long codepoint;
952						size_t byte_len = 0;
953
954						if (*start != '{') {
955							/* we silently let this pass to avoid breaking code
956							 * with JSON in string literals (e.g. "\"\u202e\""
957							 */
958							*t++ = '\\';
959							*t++ = 'u';
960							break;
961						} else {
962							/* on the other hand, invalid \u{blah} errors */
963							s++;
964							len++;
965							s++;
966							while (*s != '}') {
967								if (!ZEND_IS_HEX(*s)) {
968									valid = 0;
969									break;
970								} else {
971									len++;
972								}
973								s++;
974							}
975							if (*s == '}') {
976								valid = 1;
977								len++;
978							}
979						}
980
981						/* \u{} is invalid */
982						if (len <= 2) {
983							valid = 0;
984						}
985
986						if (!valid) {
987							zend_throw_exception(zend_ce_parse_error,
988								"Invalid UTF-8 codepoint escape sequence", 0);
989							zval_ptr_dtor(zendlval);
990							ZVAL_UNDEF(zendlval);
991							return FAILURE;
992						}
993
994						errno = 0;
995						codepoint = strtoul(start + 1, NULL, 16);
996
997						/* per RFC 3629, UTF-8 can only represent 21 bits */
998						if (codepoint > 0x10FFFF || errno) {
999							zend_throw_exception(zend_ce_parse_error,
1000								"Invalid UTF-8 codepoint escape sequence: Codepoint too large", 0);
1001							zval_ptr_dtor(zendlval);
1002							ZVAL_UNDEF(zendlval);
1003							return FAILURE;
1004						}
1005
1006						/* based on https://en.wikipedia.org/wiki/UTF-8#Sample_code */
1007						if (codepoint < 0x80) {
1008							byte_len = 1;
1009							*t++ = codepoint;
1010						} else if (codepoint <= 0x7FF) {
1011							byte_len = 2;
1012							*t++ = (codepoint >> 6) + 0xC0;
1013							*t++ = (codepoint & 0x3F) + 0x80;
1014						} else if (codepoint <= 0xFFFF) {
1015							byte_len = 3;
1016							*t++ = (codepoint >> 12) + 0xE0;
1017							*t++ = ((codepoint >> 6) & 0x3F) + 0x80;
1018							*t++ = (codepoint & 0x3F) + 0x80;
1019						} else if (codepoint <= 0x10FFFF) {
1020							byte_len = 4;
1021							*t++ = (codepoint >> 18) + 0xF0;
1022							*t++ = ((codepoint >> 12) & 0x3F) + 0x80;
1023							*t++ = ((codepoint >> 6) & 0x3F) + 0x80;
1024							*t++ = (codepoint & 0x3F) + 0x80;
1025						}
1026
1027						Z_STRLEN_P(zendlval) -= 2; /* \u */
1028						Z_STRLEN_P(zendlval) -= (len - byte_len);
1029					}
1030					break;
1031				default:
1032					/* check for an octal */
1033					if (ZEND_IS_OCT(*s)) {
1034						char octal_buf[4] = { 0, 0, 0, 0 };
1035
1036						octal_buf[0] = *s;
1037						Z_STRLEN_P(zendlval)--;
1038						if (ZEND_IS_OCT(*(s+1))) {
1039							octal_buf[1] = *(++s);
1040							Z_STRLEN_P(zendlval)--;
1041							if (ZEND_IS_OCT(*(s+1))) {
1042								octal_buf[2] = *(++s);
1043								Z_STRLEN_P(zendlval)--;
1044							}
1045						}
1046						*t++ = (char) ZEND_STRTOL(octal_buf, NULL, 8);
1047					} else {
1048						*t++ = '\\';
1049						*t++ = *s;
1050					}
1051					break;
1052			}
1053		} else {
1054			*t++ = *s;
1055		}
1056
1057		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
1058			CG(zend_lineno)++;
1059		}
1060		s++;
1061	}
1062	*t = 0;
1063	if (SCNG(output_filter)) {
1064		size_t sz = 0;
1065		unsigned char *str;
1066		// TODO: avoid realocation ???
1067		s = Z_STRVAL_P(zendlval);
1068		SCNG(output_filter)(&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
1069		zval_ptr_dtor(zendlval);
1070		ZVAL_STRINGL(zendlval, (char *) str, sz);
1071		efree(str);
1072	}
1073	return SUCCESS;
1074}
1075
1076static zend_always_inline int emit_token(int token, int token_line)
1077{
1078	if(SCNG(on_event)) SCNG(on_event)(ON_TOKEN, token, token_line);
1079
1080	return token;
1081}
1082
1083#define RETURN_TOKEN(token) return emit_token(token, start_line);
1084
1085int lex_scan(zval *zendlval)
1086{
1087
1088int start_line = CG(zend_lineno);
1089
1090restart:
1091	SCNG(yy_text) = YYCURSOR;
1092
1093/*!re2c
1094re2c:yyfill:check = 0;
1095LNUM	[0-9]+
1096DNUM	([0-9]*"."[0-9]+)|([0-9]+"."[0-9]*)
1097EXPONENT_DNUM	(({LNUM}|{DNUM})[eE][+-]?{LNUM})
1098HNUM	"0x"[0-9a-fA-F]+
1099BNUM	"0b"[01]+
1100LABEL	[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
1101WHITESPACE [ \n\r\t]+
1102TABS_AND_SPACES [ \t]*
1103TOKENS [;:,.\[\]()|^&+-/*=%!~$<>?@]
1104ANY_CHAR [^]
1105NEWLINE ("\r"|"\n"|"\r\n")
1106
1107/* compute yyleng before each rule */
1108<!*> := yyleng = YYCURSOR - SCNG(yy_text);
1109
1110<ST_IN_SCRIPTING>"exit" {
1111	RETURN_TOKEN(T_EXIT);
1112}
1113
1114<ST_IN_SCRIPTING>"die" {
1115	RETURN_TOKEN(T_EXIT);
1116}
1117
1118<ST_IN_SCRIPTING>"function" {
1119	RETURN_TOKEN(T_FUNCTION);
1120}
1121
1122<ST_IN_SCRIPTING>"const" {
1123	RETURN_TOKEN(T_CONST);
1124}
1125
1126<ST_IN_SCRIPTING>"return" {
1127	RETURN_TOKEN(T_RETURN);
1128}
1129
1130<ST_IN_SCRIPTING>"yield"{WHITESPACE}"from" {
1131	RETURN_TOKEN(T_YIELD_FROM);
1132}
1133
1134<ST_IN_SCRIPTING>"yield" {
1135	RETURN_TOKEN(T_YIELD);
1136}
1137
1138<ST_IN_SCRIPTING>"try" {
1139	RETURN_TOKEN(T_TRY);
1140}
1141
1142<ST_IN_SCRIPTING>"catch" {
1143	RETURN_TOKEN(T_CATCH);
1144}
1145
1146<ST_IN_SCRIPTING>"finally" {
1147	RETURN_TOKEN(T_FINALLY);
1148}
1149
1150<ST_IN_SCRIPTING>"throw" {
1151	RETURN_TOKEN(T_THROW);
1152}
1153
1154<ST_IN_SCRIPTING>"if" {
1155	RETURN_TOKEN(T_IF);
1156}
1157
1158<ST_IN_SCRIPTING>"elseif" {
1159	RETURN_TOKEN(T_ELSEIF);
1160}
1161
1162<ST_IN_SCRIPTING>"endif" {
1163	RETURN_TOKEN(T_ENDIF);
1164}
1165
1166<ST_IN_SCRIPTING>"else" {
1167	RETURN_TOKEN(T_ELSE);
1168}
1169
1170<ST_IN_SCRIPTING>"while" {
1171	RETURN_TOKEN(T_WHILE);
1172}
1173
1174<ST_IN_SCRIPTING>"endwhile" {
1175	RETURN_TOKEN(T_ENDWHILE);
1176}
1177
1178<ST_IN_SCRIPTING>"do" {
1179	RETURN_TOKEN(T_DO);
1180}
1181
1182<ST_IN_SCRIPTING>"for" {
1183	RETURN_TOKEN(T_FOR);
1184}
1185
1186<ST_IN_SCRIPTING>"endfor" {
1187	RETURN_TOKEN(T_ENDFOR);
1188}
1189
1190<ST_IN_SCRIPTING>"foreach" {
1191	RETURN_TOKEN(T_FOREACH);
1192}
1193
1194<ST_IN_SCRIPTING>"endforeach" {
1195	RETURN_TOKEN(T_ENDFOREACH);
1196}
1197
1198<ST_IN_SCRIPTING>"declare" {
1199	RETURN_TOKEN(T_DECLARE);
1200}
1201
1202<ST_IN_SCRIPTING>"enddeclare" {
1203	RETURN_TOKEN(T_ENDDECLARE);
1204}
1205
1206<ST_IN_SCRIPTING>"instanceof" {
1207	RETURN_TOKEN(T_INSTANCEOF);
1208}
1209
1210<ST_IN_SCRIPTING>"as" {
1211	RETURN_TOKEN(T_AS);
1212}
1213
1214<ST_IN_SCRIPTING>"switch" {
1215	RETURN_TOKEN(T_SWITCH);
1216}
1217
1218<ST_IN_SCRIPTING>"endswitch" {
1219	RETURN_TOKEN(T_ENDSWITCH);
1220}
1221
1222<ST_IN_SCRIPTING>"case" {
1223	RETURN_TOKEN(T_CASE);
1224}
1225
1226<ST_IN_SCRIPTING>"default" {
1227	RETURN_TOKEN(T_DEFAULT);
1228}
1229
1230<ST_IN_SCRIPTING>"break" {
1231	RETURN_TOKEN(T_BREAK);
1232}
1233
1234<ST_IN_SCRIPTING>"continue" {
1235	RETURN_TOKEN(T_CONTINUE);
1236}
1237
1238<ST_IN_SCRIPTING>"goto" {
1239	RETURN_TOKEN(T_GOTO);
1240}
1241
1242<ST_IN_SCRIPTING>"echo" {
1243	RETURN_TOKEN(T_ECHO);
1244}
1245
1246<ST_IN_SCRIPTING>"print" {
1247	RETURN_TOKEN(T_PRINT);
1248}
1249
1250<ST_IN_SCRIPTING>"class" {
1251	RETURN_TOKEN(T_CLASS);
1252}
1253
1254<ST_IN_SCRIPTING>"interface" {
1255	RETURN_TOKEN(T_INTERFACE);
1256}
1257
1258<ST_IN_SCRIPTING>"trait" {
1259	RETURN_TOKEN(T_TRAIT);
1260}
1261
1262<ST_IN_SCRIPTING>"extends" {
1263	RETURN_TOKEN(T_EXTENDS);
1264}
1265
1266<ST_IN_SCRIPTING>"implements" {
1267	RETURN_TOKEN(T_IMPLEMENTS);
1268}
1269
1270<ST_IN_SCRIPTING>"->" {
1271	yy_push_state(ST_LOOKING_FOR_PROPERTY);
1272	RETURN_TOKEN(T_OBJECT_OPERATOR);
1273}
1274
1275<ST_IN_SCRIPTING,ST_LOOKING_FOR_PROPERTY>{WHITESPACE}+ {
1276	HANDLE_NEWLINES(yytext, yyleng);
1277	RETURN_TOKEN(T_WHITESPACE);
1278}
1279
1280<ST_LOOKING_FOR_PROPERTY>"->" {
1281	RETURN_TOKEN(T_OBJECT_OPERATOR);
1282}
1283
1284<ST_LOOKING_FOR_PROPERTY>{LABEL} {
1285	yy_pop_state();
1286	zend_copy_value(zendlval, yytext, yyleng);
1287	RETURN_TOKEN(T_STRING);
1288}
1289
1290<ST_LOOKING_FOR_PROPERTY>{ANY_CHAR} {
1291	yyless(0);
1292	yy_pop_state();
1293	goto restart;
1294}
1295
1296<ST_IN_SCRIPTING>"::" {
1297	RETURN_TOKEN(T_PAAMAYIM_NEKUDOTAYIM);
1298}
1299
1300<ST_IN_SCRIPTING>"\\" {
1301	RETURN_TOKEN(T_NS_SEPARATOR);
1302}
1303
1304<ST_IN_SCRIPTING>"..." {
1305	RETURN_TOKEN(T_ELLIPSIS);
1306}
1307
1308<ST_IN_SCRIPTING>"??" {
1309	RETURN_TOKEN(T_COALESCE);
1310}
1311
1312<ST_IN_SCRIPTING>"new" {
1313	RETURN_TOKEN(T_NEW);
1314}
1315
1316<ST_IN_SCRIPTING>"clone" {
1317	RETURN_TOKEN(T_CLONE);
1318}
1319
1320<ST_IN_SCRIPTING>"var" {
1321	RETURN_TOKEN(T_VAR);
1322}
1323
1324<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"|"integer"){TABS_AND_SPACES}")" {
1325	RETURN_TOKEN(T_INT_CAST);
1326}
1327
1328<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("real"|"double"|"float"){TABS_AND_SPACES}")" {
1329	RETURN_TOKEN(T_DOUBLE_CAST);
1330}
1331
1332<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"|"binary"){TABS_AND_SPACES}")" {
1333	RETURN_TOKEN(T_STRING_CAST);
1334}
1335
1336<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"array"{TABS_AND_SPACES}")" {
1337	RETURN_TOKEN(T_ARRAY_CAST);
1338}
1339
1340<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"object"{TABS_AND_SPACES}")" {
1341	RETURN_TOKEN(T_OBJECT_CAST);
1342}
1343
1344<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"|"boolean"){TABS_AND_SPACES}")" {
1345	RETURN_TOKEN(T_BOOL_CAST);
1346}
1347
1348<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("unset"){TABS_AND_SPACES}")" {
1349	RETURN_TOKEN(T_UNSET_CAST);
1350}
1351
1352<ST_IN_SCRIPTING>"eval" {
1353	RETURN_TOKEN(T_EVAL);
1354}
1355
1356<ST_IN_SCRIPTING>"include" {
1357	RETURN_TOKEN(T_INCLUDE);
1358}
1359
1360<ST_IN_SCRIPTING>"include_once" {
1361	RETURN_TOKEN(T_INCLUDE_ONCE);
1362}
1363
1364<ST_IN_SCRIPTING>"require" {
1365	RETURN_TOKEN(T_REQUIRE);
1366}
1367
1368<ST_IN_SCRIPTING>"require_once" {
1369	RETURN_TOKEN(T_REQUIRE_ONCE);
1370}
1371
1372<ST_IN_SCRIPTING>"namespace" {
1373	RETURN_TOKEN(T_NAMESPACE);
1374}
1375
1376<ST_IN_SCRIPTING>"use" {
1377	RETURN_TOKEN(T_USE);
1378}
1379
1380<ST_IN_SCRIPTING>"insteadof" {
1381    RETURN_TOKEN(T_INSTEADOF);
1382}
1383
1384<ST_IN_SCRIPTING>"global" {
1385	RETURN_TOKEN(T_GLOBAL);
1386}
1387
1388<ST_IN_SCRIPTING>"isset" {
1389	RETURN_TOKEN(T_ISSET);
1390}
1391
1392<ST_IN_SCRIPTING>"empty" {
1393	RETURN_TOKEN(T_EMPTY);
1394}
1395
1396<ST_IN_SCRIPTING>"__halt_compiler" {
1397	RETURN_TOKEN(T_HALT_COMPILER);
1398}
1399
1400<ST_IN_SCRIPTING>"static" {
1401	RETURN_TOKEN(T_STATIC);
1402}
1403
1404<ST_IN_SCRIPTING>"abstract" {
1405	RETURN_TOKEN(T_ABSTRACT);
1406}
1407
1408<ST_IN_SCRIPTING>"final" {
1409	RETURN_TOKEN(T_FINAL);
1410}
1411
1412<ST_IN_SCRIPTING>"private" {
1413	RETURN_TOKEN(T_PRIVATE);
1414}
1415
1416<ST_IN_SCRIPTING>"protected" {
1417	RETURN_TOKEN(T_PROTECTED);
1418}
1419
1420<ST_IN_SCRIPTING>"public" {
1421	RETURN_TOKEN(T_PUBLIC);
1422}
1423
1424<ST_IN_SCRIPTING>"unset" {
1425	RETURN_TOKEN(T_UNSET);
1426}
1427
1428<ST_IN_SCRIPTING>"=>" {
1429	RETURN_TOKEN(T_DOUBLE_ARROW);
1430}
1431
1432<ST_IN_SCRIPTING>"list" {
1433	RETURN_TOKEN(T_LIST);
1434}
1435
1436<ST_IN_SCRIPTING>"array" {
1437	RETURN_TOKEN(T_ARRAY);
1438}
1439
1440<ST_IN_SCRIPTING>"callable" {
1441	RETURN_TOKEN(T_CALLABLE);
1442}
1443
1444<ST_IN_SCRIPTING>"++" {
1445	RETURN_TOKEN(T_INC);
1446}
1447
1448<ST_IN_SCRIPTING>"--" {
1449	RETURN_TOKEN(T_DEC);
1450}
1451
1452<ST_IN_SCRIPTING>"===" {
1453	RETURN_TOKEN(T_IS_IDENTICAL);
1454}
1455
1456<ST_IN_SCRIPTING>"!==" {
1457	RETURN_TOKEN(T_IS_NOT_IDENTICAL);
1458}
1459
1460<ST_IN_SCRIPTING>"==" {
1461	RETURN_TOKEN(T_IS_EQUAL);
1462}
1463
1464<ST_IN_SCRIPTING>"!="|"<>" {
1465	RETURN_TOKEN(T_IS_NOT_EQUAL);
1466}
1467
1468<ST_IN_SCRIPTING>"<=>" {
1469	RETURN_TOKEN(T_SPACESHIP);
1470}
1471
1472<ST_IN_SCRIPTING>"<=" {
1473	RETURN_TOKEN(T_IS_SMALLER_OR_EQUAL);
1474}
1475
1476<ST_IN_SCRIPTING>">=" {
1477	RETURN_TOKEN(T_IS_GREATER_OR_EQUAL);
1478}
1479
1480<ST_IN_SCRIPTING>"+=" {
1481	RETURN_TOKEN(T_PLUS_EQUAL);
1482}
1483
1484<ST_IN_SCRIPTING>"-=" {
1485	RETURN_TOKEN(T_MINUS_EQUAL);
1486}
1487
1488<ST_IN_SCRIPTING>"*=" {
1489	RETURN_TOKEN(T_MUL_EQUAL);
1490}
1491
1492<ST_IN_SCRIPTING>"*\*" {
1493	RETURN_TOKEN(T_POW);
1494}
1495
1496<ST_IN_SCRIPTING>"*\*=" {
1497	RETURN_TOKEN(T_POW_EQUAL);
1498}
1499
1500<ST_IN_SCRIPTING>"/=" {
1501	RETURN_TOKEN(T_DIV_EQUAL);
1502}
1503
1504<ST_IN_SCRIPTING>".=" {
1505	RETURN_TOKEN(T_CONCAT_EQUAL);
1506}
1507
1508<ST_IN_SCRIPTING>"%=" {
1509	RETURN_TOKEN(T_MOD_EQUAL);
1510}
1511
1512<ST_IN_SCRIPTING>"<<=" {
1513	RETURN_TOKEN(T_SL_EQUAL);
1514}
1515
1516<ST_IN_SCRIPTING>">>=" {
1517	RETURN_TOKEN(T_SR_EQUAL);
1518}
1519
1520<ST_IN_SCRIPTING>"&=" {
1521	RETURN_TOKEN(T_AND_EQUAL);
1522}
1523
1524<ST_IN_SCRIPTING>"|=" {
1525	RETURN_TOKEN(T_OR_EQUAL);
1526}
1527
1528<ST_IN_SCRIPTING>"^=" {
1529	RETURN_TOKEN(T_XOR_EQUAL);
1530}
1531
1532<ST_IN_SCRIPTING>"||" {
1533	RETURN_TOKEN(T_BOOLEAN_OR);
1534}
1535
1536<ST_IN_SCRIPTING>"&&" {
1537	RETURN_TOKEN(T_BOOLEAN_AND);
1538}
1539
1540<ST_IN_SCRIPTING>"OR" {
1541	RETURN_TOKEN(T_LOGICAL_OR);
1542}
1543
1544<ST_IN_SCRIPTING>"AND" {
1545	RETURN_TOKEN(T_LOGICAL_AND);
1546}
1547
1548<ST_IN_SCRIPTING>"XOR" {
1549	RETURN_TOKEN(T_LOGICAL_XOR);
1550}
1551
1552<ST_IN_SCRIPTING>"<<" {
1553	RETURN_TOKEN(T_SL);
1554}
1555
1556<ST_IN_SCRIPTING>">>" {
1557	RETURN_TOKEN(T_SR);
1558}
1559
1560<ST_IN_SCRIPTING>{TOKENS} {
1561	RETURN_TOKEN(yytext[0]);
1562}
1563
1564
1565<ST_IN_SCRIPTING>"{" {
1566	yy_push_state(ST_IN_SCRIPTING);
1567	RETURN_TOKEN('{');
1568}
1569
1570
1571<ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"${" {
1572	yy_push_state(ST_LOOKING_FOR_VARNAME);
1573	RETURN_TOKEN(T_DOLLAR_OPEN_CURLY_BRACES);
1574}
1575
1576
1577<ST_IN_SCRIPTING>"}" {
1578	RESET_DOC_COMMENT();
1579	if (!zend_stack_is_empty(&SCNG(state_stack))) {
1580		yy_pop_state();
1581	}
1582	RETURN_TOKEN('}');
1583}
1584
1585
1586<ST_LOOKING_FOR_VARNAME>{LABEL}[[}] {
1587	yyless(yyleng - 1);
1588	zend_copy_value(zendlval, yytext, yyleng);
1589	yy_pop_state();
1590	yy_push_state(ST_IN_SCRIPTING);
1591	RETURN_TOKEN(T_STRING_VARNAME);
1592}
1593
1594
1595<ST_LOOKING_FOR_VARNAME>{ANY_CHAR} {
1596	yyless(0);
1597	yy_pop_state();
1598	yy_push_state(ST_IN_SCRIPTING);
1599	goto restart;
1600}
1601
1602<ST_IN_SCRIPTING>{BNUM} {
1603	char *bin = yytext + 2; /* Skip "0b" */
1604	int len = yyleng - 2;
1605	char *end;
1606
1607	/* Skip any leading 0s */
1608	while (*bin == '0') {
1609		++bin;
1610		--len;
1611	}
1612
1613	if (len < SIZEOF_ZEND_LONG * 8) {
1614		if (len == 0) {
1615			ZVAL_LONG(zendlval, 0);
1616		} else {
1617			errno = 0;
1618			ZVAL_LONG(zendlval, ZEND_STRTOL(bin, &end, 2));
1619			ZEND_ASSERT(!errno && end == yytext + yyleng);
1620		}
1621		RETURN_TOKEN(T_LNUMBER);
1622	} else {
1623		ZVAL_DOUBLE(zendlval, zend_bin_strtod(bin, (const char **)&end));
1624		/* errno isn't checked since we allow HUGE_VAL/INF overflow */
1625		ZEND_ASSERT(end == yytext + yyleng);
1626		RETURN_TOKEN(T_DNUMBER);
1627	}
1628}
1629
1630<ST_IN_SCRIPTING>{LNUM} {
1631	char *end;
1632	if (yyleng < MAX_LENGTH_OF_LONG - 1) { /* Won't overflow */
1633		errno = 0;
1634		ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 0));
1635		/* This isn't an assert, we need to ensure 019 isn't valid octal
1636		 * Because the lexing itself doesn't do that for us
1637		 */
1638		if (end != yytext + yyleng) {
1639			zend_throw_exception(zend_ce_parse_error, "Invalid numeric literal", 0);
1640			ZVAL_UNDEF(zendlval);
1641			RETURN_TOKEN(T_LNUMBER);
1642		}
1643	} else {
1644		errno = 0;
1645		ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 0));
1646		if (errno == ERANGE) { /* Overflow */
1647			errno = 0;
1648			if (yytext[0] == '0') { /* octal overflow */
1649				errno = 0;
1650				ZVAL_DOUBLE(zendlval, zend_oct_strtod(yytext, (const char **)&end));
1651			} else {
1652				ZVAL_DOUBLE(zendlval, zend_strtod(yytext, (const char **)&end));
1653			}
1654			/* Also not an assert for the same reason */
1655			if (end != yytext + yyleng) {
1656				zend_throw_exception(zend_ce_parse_error,
1657					"Invalid numeric literal", 0);
1658				ZVAL_UNDEF(zendlval);
1659				RETURN_TOKEN(T_DNUMBER);
1660			}
1661			ZEND_ASSERT(!errno);
1662			RETURN_TOKEN(T_DNUMBER);
1663		}
1664		/* Also not an assert for the same reason */
1665		if (end != yytext + yyleng) {
1666			zend_throw_exception(zend_ce_parse_error, "Invalid numeric literal", 0);
1667			ZVAL_UNDEF(zendlval);
1668			RETURN_TOKEN(T_DNUMBER);
1669		}
1670	}
1671	ZEND_ASSERT(!errno);
1672	RETURN_TOKEN(T_LNUMBER);
1673}
1674
1675<ST_IN_SCRIPTING>{HNUM} {
1676	char *hex = yytext + 2; /* Skip "0x" */
1677	int len = yyleng - 2;
1678	char *end;
1679
1680	/* Skip any leading 0s */
1681	while (*hex == '0') {
1682		hex++;
1683		len--;
1684	}
1685
1686	if (len < SIZEOF_ZEND_LONG * 2 || (len == SIZEOF_ZEND_LONG * 2 && *hex <= '7')) {
1687		if (len == 0) {
1688			ZVAL_LONG(zendlval, 0);
1689		} else {
1690			errno = 0;
1691			ZVAL_LONG(zendlval, ZEND_STRTOL(hex, &end, 16));
1692			ZEND_ASSERT(!errno && end == hex + len);
1693		}
1694		RETURN_TOKEN(T_LNUMBER);
1695	} else {
1696		ZVAL_DOUBLE(zendlval, zend_hex_strtod(hex, (const char **)&end));
1697		/* errno isn't checked since we allow HUGE_VAL/INF overflow */
1698		ZEND_ASSERT(end == hex + len);
1699		RETURN_TOKEN(T_DNUMBER);
1700	}
1701}
1702
1703<ST_VAR_OFFSET>[0]|([1-9][0-9]*) { /* Offset could be treated as a long */
1704	if (yyleng < MAX_LENGTH_OF_LONG - 1 || (yyleng == MAX_LENGTH_OF_LONG - 1 && strcmp(yytext, long_min_digits) < 0)) {
1705		char *end;
1706		errno = 0;
1707		ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 10));
1708		if (errno == ERANGE) {
1709			goto string;
1710		}
1711		ZEND_ASSERT(end == yytext + yyleng);
1712	} else {
1713string:
1714		ZVAL_STRINGL(zendlval, yytext, yyleng);
1715	}
1716	RETURN_TOKEN(T_NUM_STRING);
1717}
1718
1719<ST_VAR_OFFSET>{LNUM}|{HNUM}|{BNUM} { /* Offset must be treated as a string */
1720	ZVAL_STRINGL(zendlval, yytext, yyleng);
1721	RETURN_TOKEN(T_NUM_STRING);
1722}
1723
1724<ST_IN_SCRIPTING>{DNUM}|{EXPONENT_DNUM} {
1725	const char *end;
1726
1727	ZVAL_DOUBLE(zendlval, zend_strtod(yytext, &end));
1728	/* errno isn't checked since we allow HUGE_VAL/INF overflow */
1729	ZEND_ASSERT(end == yytext + yyleng);
1730	RETURN_TOKEN(T_DNUMBER);
1731}
1732
1733<ST_IN_SCRIPTING>"__CLASS__" {
1734	RETURN_TOKEN(T_CLASS_C);
1735}
1736
1737<ST_IN_SCRIPTING>"__TRAIT__" {
1738	RETURN_TOKEN(T_TRAIT_C);
1739}
1740
1741<ST_IN_SCRIPTING>"__FUNCTION__" {
1742	RETURN_TOKEN(T_FUNC_C);
1743}
1744
1745<ST_IN_SCRIPTING>"__METHOD__" {
1746	RETURN_TOKEN(T_METHOD_C);
1747}
1748
1749<ST_IN_SCRIPTING>"__LINE__" {
1750	RETURN_TOKEN(T_LINE);
1751}
1752
1753<ST_IN_SCRIPTING>"__FILE__" {
1754	RETURN_TOKEN(T_FILE);
1755}
1756
1757<ST_IN_SCRIPTING>"__DIR__" {
1758	RETURN_TOKEN(T_DIR);
1759}
1760
1761<ST_IN_SCRIPTING>"__NAMESPACE__" {
1762	RETURN_TOKEN(T_NS_C);
1763}
1764
1765
1766<INITIAL>"<?=" {
1767	BEGIN(ST_IN_SCRIPTING);
1768	RETURN_TOKEN(T_OPEN_TAG_WITH_ECHO);
1769}
1770
1771
1772<INITIAL>"<?php"([ \t]|{NEWLINE}) {
1773	HANDLE_NEWLINE(yytext[yyleng-1]);
1774	BEGIN(ST_IN_SCRIPTING);
1775	RETURN_TOKEN(T_OPEN_TAG);
1776}
1777
1778
1779<INITIAL>"<?" {
1780	if (CG(short_tags)) {
1781		BEGIN(ST_IN_SCRIPTING);
1782		RETURN_TOKEN(T_OPEN_TAG);
1783	} else {
1784		goto inline_char_handler;
1785	}
1786}
1787
1788<INITIAL>{ANY_CHAR} {
1789	if (YYCURSOR > YYLIMIT) {
1790		RETURN_TOKEN(END);
1791	}
1792
1793inline_char_handler:
1794
1795	while (1) {
1796		YYCTYPE *ptr = memchr(YYCURSOR, '<', YYLIMIT - YYCURSOR);
1797
1798		YYCURSOR = ptr ? ptr + 1 : YYLIMIT;
1799
1800		if (YYCURSOR >= YYLIMIT) {
1801			break;
1802		}
1803
1804		if (*YYCURSOR == '?') {
1805			if (CG(short_tags) || !strncasecmp((char*)YYCURSOR + 1, "php", 3) || (*(YYCURSOR + 1) == '=')) { /* Assume [ \t\n\r] follows "php" */
1806
1807				YYCURSOR--;
1808				break;
1809			}
1810		}
1811	}
1812
1813	yyleng = YYCURSOR - SCNG(yy_text);
1814
1815	if (SCNG(output_filter)) {
1816		size_t readsize;
1817		char *s = NULL;
1818		size_t sz = 0;
1819		// TODO: avoid reallocation ???
1820		readsize = SCNG(output_filter)((unsigned char **)&s, &sz, (unsigned char *)yytext, (size_t)yyleng);
1821		ZVAL_STRINGL(zendlval, s, sz);
1822		efree(s);
1823		if (readsize < yyleng) {
1824			yyless(readsize);
1825		}
1826	} else {
1827	  ZVAL_STRINGL(zendlval, yytext, yyleng);
1828	}
1829	HANDLE_NEWLINES(yytext, yyleng);
1830	RETURN_TOKEN(T_INLINE_HTML);
1831}
1832
1833
1834/* Make sure a label character follows "->", otherwise there is no property
1835 * and "->" will be taken literally
1836 */
1837<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"->"[a-zA-Z_\x7f-\xff] {
1838	yyless(yyleng - 3);
1839	yy_push_state(ST_LOOKING_FOR_PROPERTY);
1840	zend_copy_value(zendlval, (yytext+1), (yyleng-1));
1841	RETURN_TOKEN(T_VARIABLE);
1842}
1843
1844/* A [ always designates a variable offset, regardless of what follows
1845 */
1846<ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"[" {
1847	yyless(yyleng - 1);
1848	yy_push_state(ST_VAR_OFFSET);
1849	zend_copy_value(zendlval, (yytext+1), (yyleng-1));
1850	RETURN_TOKEN(T_VARIABLE);
1851}
1852
1853<ST_IN_SCRIPTING,ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE,ST_VAR_OFFSET>"$"{LABEL} {
1854	zend_copy_value(zendlval, (yytext+1), (yyleng-1));
1855	RETURN_TOKEN(T_VARIABLE);
1856}
1857
1858<ST_VAR_OFFSET>"]" {
1859	yy_pop_state();
1860	RETURN_TOKEN(']');
1861}
1862
1863<ST_VAR_OFFSET>{TOKENS}|[{}"`] {
1864	/* Only '[' can be valid, but returning other tokens will allow a more explicit parse error */
1865	RETURN_TOKEN(yytext[0]);
1866}
1867
1868<ST_VAR_OFFSET>[ \n\r\t\\'#] {
1869	/* Invalid rule to return a more explicit parse error with proper line number */
1870	yyless(0);
1871	yy_pop_state();
1872	ZVAL_NULL(zendlval);
1873	RETURN_TOKEN(T_ENCAPSED_AND_WHITESPACE);
1874}
1875
1876<ST_IN_SCRIPTING,ST_VAR_OFFSET>{LABEL} {
1877	zend_copy_value(zendlval, yytext, yyleng);
1878	RETURN_TOKEN(T_STRING);
1879}
1880
1881
1882<ST_IN_SCRIPTING>"#"|"//" {
1883	while (YYCURSOR < YYLIMIT) {
1884		switch (*YYCURSOR++) {
1885			case '\r':
1886				if (*YYCURSOR == '\n') {
1887					YYCURSOR++;
1888				}
1889				/* fall through */
1890			case '\n':
1891				CG(zend_lineno)++;
1892				break;
1893			case '?':
1894				if (*YYCURSOR == '>') {
1895					YYCURSOR--;
1896					break;
1897				}
1898				/* fall through */
1899			default:
1900				continue;
1901		}
1902
1903		break;
1904	}
1905
1906	yyleng = YYCURSOR - SCNG(yy_text);
1907
1908	RETURN_TOKEN(T_COMMENT);
1909}
1910
1911<ST_IN_SCRIPTING>"/*"|"/**"{WHITESPACE} {
1912	int doc_com;
1913
1914	if (yyleng > 2) {
1915		doc_com = 1;
1916		RESET_DOC_COMMENT();
1917	} else {
1918		doc_com = 0;
1919	}
1920
1921	while (YYCURSOR < YYLIMIT) {
1922		if (*YYCURSOR++ == '*' && *YYCURSOR == '/') {
1923			break;
1924		}
1925	}
1926
1927	if (YYCURSOR < YYLIMIT) {
1928		YYCURSOR++;
1929	} else {
1930		zend_error(E_COMPILE_WARNING, "Unterminated comment starting line %d", CG(zend_lineno));
1931	}
1932
1933	yyleng = YYCURSOR - SCNG(yy_text);
1934	HANDLE_NEWLINES(yytext, yyleng);
1935
1936	if (doc_com) {
1937		CG(doc_comment) = zend_string_init(yytext, yyleng, 0);
1938		RETURN_TOKEN(T_DOC_COMMENT);
1939	}
1940
1941	RETURN_TOKEN(T_COMMENT);
1942}
1943
1944<ST_IN_SCRIPTING>"?>"{NEWLINE}? {
1945	BEGIN(INITIAL);
1946	RETURN_TOKEN(T_CLOSE_TAG);  /* implicit ';' at php-end tag */
1947}
1948
1949
1950<ST_IN_SCRIPTING>b?['] {
1951	register char *s, *t;
1952	char *end;
1953	int bprefix = (yytext[0] != '\'') ? 1 : 0;
1954
1955	while (1) {
1956		if (YYCURSOR < YYLIMIT) {
1957			if (*YYCURSOR == '\'') {
1958				YYCURSOR++;
1959				yyleng = YYCURSOR - SCNG(yy_text);
1960
1961				break;
1962			} else if (*YYCURSOR++ == '\\' && YYCURSOR < YYLIMIT) {
1963				YYCURSOR++;
1964			}
1965		} else {
1966			yyleng = YYLIMIT - SCNG(yy_text);
1967
1968			/* Unclosed single quotes; treat similar to double quotes, but without a separate token
1969			 * for ' (unrecognized by parser), instead of old flex fallback to "Unexpected character..."
1970			 * rule, which continued in ST_IN_SCRIPTING state after the quote */
1971			ZVAL_NULL(zendlval);
1972			RETURN_TOKEN(T_ENCAPSED_AND_WHITESPACE);
1973		}
1974	}
1975
1976	ZVAL_STRINGL(zendlval, yytext+bprefix+1, yyleng-bprefix-2);
1977
1978	/* convert escape sequences */
1979	s = t = Z_STRVAL_P(zendlval);
1980	end = s+Z_STRLEN_P(zendlval);
1981	while (s<end) {
1982		if (*s=='\\') {
1983			s++;
1984
1985			switch(*s) {
1986				case '\\':
1987				case '\'':
1988					*t++ = *s;
1989					Z_STRLEN_P(zendlval)--;
1990					break;
1991				default:
1992					*t++ = '\\';
1993					*t++ = *s;
1994					break;
1995			}
1996		} else {
1997			*t++ = *s;
1998		}
1999
2000		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
2001			CG(zend_lineno)++;
2002		}
2003		s++;
2004	}
2005	*t = 0;
2006
2007	if (SCNG(output_filter)) {
2008		size_t sz = 0;
2009		char *str = NULL;
2010		s = Z_STRVAL_P(zendlval);
2011		// TODO: avoid reallocation ???
2012		SCNG(output_filter)((unsigned char **)&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
2013		ZVAL_STRINGL(zendlval, str, sz);
2014	}
2015	RETURN_TOKEN(T_CONSTANT_ENCAPSED_STRING);
2016}
2017
2018
2019<ST_IN_SCRIPTING>b?["] {
2020	int bprefix = (yytext[0] != '"') ? 1 : 0;
2021
2022	while (YYCURSOR < YYLIMIT) {
2023		switch (*YYCURSOR++) {
2024			case '"':
2025				yyleng = YYCURSOR - SCNG(yy_text);
2026				zend_scan_escape_string(zendlval, yytext+bprefix+1, yyleng-bprefix-2, '"');
2027				RETURN_TOKEN(T_CONSTANT_ENCAPSED_STRING);
2028			case '$':
2029				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2030					break;
2031				}
2032				continue;
2033			case '{':
2034				if (*YYCURSOR == '$') {
2035					break;
2036				}
2037				continue;
2038			case '\\':
2039				if (YYCURSOR < YYLIMIT) {
2040					YYCURSOR++;
2041				}
2042				/* fall through */
2043			default:
2044				continue;
2045		}
2046
2047		YYCURSOR--;
2048		break;
2049	}
2050
2051	/* Remember how much was scanned to save rescanning */
2052	SET_DOUBLE_QUOTES_SCANNED_LENGTH(YYCURSOR - SCNG(yy_text) - yyleng);
2053
2054	YYCURSOR = SCNG(yy_text) + yyleng;
2055
2056	BEGIN(ST_DOUBLE_QUOTES);
2057	RETURN_TOKEN('"');
2058}
2059
2060
2061<ST_IN_SCRIPTING>b?"<<<"{TABS_AND_SPACES}({LABEL}|([']{LABEL}['])|(["]{LABEL}["])){NEWLINE} {
2062	char *s;
2063	int bprefix = (yytext[0] != '<') ? 1 : 0;
2064	zend_heredoc_label *heredoc_label = emalloc(sizeof(zend_heredoc_label));
2065
2066	CG(zend_lineno)++;
2067	heredoc_label->length = yyleng-bprefix-3-1-(yytext[yyleng-2]=='\r'?1:0);
2068	s = yytext+bprefix+3;
2069	while ((*s == ' ') || (*s == '\t')) {
2070		s++;
2071		heredoc_label->length--;
2072	}
2073
2074	if (*s == '\'') {
2075		s++;
2076		heredoc_label->length -= 2;
2077
2078		BEGIN(ST_NOWDOC);
2079	} else {
2080		if (*s == '"') {
2081			s++;
2082			heredoc_label->length -= 2;
2083		}
2084
2085		BEGIN(ST_HEREDOC);
2086	}
2087
2088	heredoc_label->label = estrndup(s, heredoc_label->length);
2089
2090	/* Check for ending label on the next line */
2091	if (heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, s, heredoc_label->length)) {
2092		YYCTYPE *end = YYCURSOR + heredoc_label->length;
2093
2094		if (*end == ';') {
2095			end++;
2096		}
2097
2098		if (*end == '\n' || *end == '\r') {
2099			BEGIN(ST_END_HEREDOC);
2100		}
2101	}
2102
2103	zend_ptr_stack_push(&SCNG(heredoc_label_stack), (void *) heredoc_label);
2104
2105	RETURN_TOKEN(T_START_HEREDOC);
2106}
2107
2108
2109<ST_IN_SCRIPTING>[`] {
2110	BEGIN(ST_BACKQUOTE);
2111	RETURN_TOKEN('`');
2112}
2113
2114
2115<ST_END_HEREDOC>{ANY_CHAR} {
2116	zend_heredoc_label *heredoc_label = zend_ptr_stack_pop(&SCNG(heredoc_label_stack));
2117
2118	YYCURSOR += heredoc_label->length - 1;
2119	yyleng = heredoc_label->length;
2120
2121	heredoc_label_dtor(heredoc_label);
2122	efree(heredoc_label);
2123
2124	BEGIN(ST_IN_SCRIPTING);
2125	RETURN_TOKEN(T_END_HEREDOC);
2126}
2127
2128
2129<ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"{$" {
2130	Z_LVAL_P(zendlval) = (zend_long) '{';
2131	yy_push_state(ST_IN_SCRIPTING);
2132	yyless(1);
2133	RETURN_TOKEN(T_CURLY_OPEN);
2134}
2135
2136
2137<ST_DOUBLE_QUOTES>["] {
2138	BEGIN(ST_IN_SCRIPTING);
2139	RETURN_TOKEN('"');
2140}
2141
2142<ST_BACKQUOTE>[`] {
2143	BEGIN(ST_IN_SCRIPTING);
2144	RETURN_TOKEN('`');
2145}
2146
2147
2148<ST_DOUBLE_QUOTES>{ANY_CHAR} {
2149	if (GET_DOUBLE_QUOTES_SCANNED_LENGTH()) {
2150		YYCURSOR += GET_DOUBLE_QUOTES_SCANNED_LENGTH() - 1;
2151		SET_DOUBLE_QUOTES_SCANNED_LENGTH(0);
2152
2153		goto double_quotes_scan_done;
2154	}
2155
2156	if (YYCURSOR > YYLIMIT) {
2157		RETURN_TOKEN(END);
2158	}
2159	if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2160		YYCURSOR++;
2161	}
2162
2163	while (YYCURSOR < YYLIMIT) {
2164		switch (*YYCURSOR++) {
2165			case '"':
2166				break;
2167			case '$':
2168				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2169					break;
2170				}
2171				continue;
2172			case '{':
2173				if (*YYCURSOR == '$') {
2174					break;
2175				}
2176				continue;
2177			case '\\':
2178				if (YYCURSOR < YYLIMIT) {
2179					YYCURSOR++;
2180				}
2181				/* fall through */
2182			default:
2183				continue;
2184		}
2185
2186		YYCURSOR--;
2187		break;
2188	}
2189
2190double_quotes_scan_done:
2191	yyleng = YYCURSOR - SCNG(yy_text);
2192
2193	zend_scan_escape_string(zendlval, yytext, yyleng, '"');
2194	RETURN_TOKEN(T_ENCAPSED_AND_WHITESPACE);
2195}
2196
2197
2198<ST_BACKQUOTE>{ANY_CHAR} {
2199	if (YYCURSOR > YYLIMIT) {
2200		RETURN_TOKEN(END);
2201	}
2202	if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2203		YYCURSOR++;
2204	}
2205
2206	while (YYCURSOR < YYLIMIT) {
2207		switch (*YYCURSOR++) {
2208			case '`':
2209				break;
2210			case '$':
2211				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2212					break;
2213				}
2214				continue;
2215			case '{':
2216				if (*YYCURSOR == '$') {
2217					break;
2218				}
2219				continue;
2220			case '\\':
2221				if (YYCURSOR < YYLIMIT) {
2222					YYCURSOR++;
2223				}
2224				/* fall through */
2225			default:
2226				continue;
2227		}
2228
2229		YYCURSOR--;
2230		break;
2231	}
2232
2233	yyleng = YYCURSOR - SCNG(yy_text);
2234
2235	zend_scan_escape_string(zendlval, yytext, yyleng, '`');
2236	RETURN_TOKEN(T_ENCAPSED_AND_WHITESPACE);
2237}
2238
2239
2240<ST_HEREDOC>{ANY_CHAR} {
2241	int newline = 0;
2242
2243	zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2244
2245	if (YYCURSOR > YYLIMIT) {
2246		RETURN_TOKEN(END);
2247	}
2248
2249	YYCURSOR--;
2250
2251	while (YYCURSOR < YYLIMIT) {
2252		switch (*YYCURSOR++) {
2253			case '\r':
2254				if (*YYCURSOR == '\n') {
2255					YYCURSOR++;
2256				}
2257				/* fall through */
2258			case '\n':
2259				/* Check for ending label on the next line */
2260				if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
2261					YYCTYPE *end = YYCURSOR + heredoc_label->length;
2262
2263					if (*end == ';') {
2264						end++;
2265					}
2266
2267					if (*end == '\n' || *end == '\r') {
2268						/* newline before label will be subtracted from returned text, but
2269						 * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
2270						if (YYCURSOR[-2] == '\r' && YYCURSOR[-1] == '\n') {
2271							newline = 2; /* Windows newline */
2272						} else {
2273							newline = 1;
2274						}
2275
2276						CG(increment_lineno) = 1; /* For newline before label */
2277						BEGIN(ST_END_HEREDOC);
2278
2279						goto heredoc_scan_done;
2280					}
2281				}
2282				continue;
2283			case '$':
2284				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2285					break;
2286				}
2287				continue;
2288			case '{':
2289				if (*YYCURSOR == '$') {
2290					break;
2291				}
2292				continue;
2293			case '\\':
2294				if (YYCURSOR < YYLIMIT && *YYCURSOR != '\n' && *YYCURSOR != '\r') {
2295					YYCURSOR++;
2296				}
2297				/* fall through */
2298			default:
2299				continue;
2300		}
2301
2302		YYCURSOR--;
2303		break;
2304	}
2305
2306heredoc_scan_done:
2307	yyleng = YYCURSOR - SCNG(yy_text);
2308
2309	zend_scan_escape_string(zendlval, yytext, yyleng - newline, 0);
2310	RETURN_TOKEN(T_ENCAPSED_AND_WHITESPACE);
2311}
2312
2313
2314<ST_NOWDOC>{ANY_CHAR} {
2315	int newline = 0;
2316
2317	zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2318
2319	if (YYCURSOR > YYLIMIT) {
2320		RETURN_TOKEN(END);
2321	}
2322
2323	YYCURSOR--;
2324
2325	while (YYCURSOR < YYLIMIT) {
2326		switch (*YYCURSOR++) {
2327			case '\r':
2328				if (*YYCURSOR == '\n') {
2329					YYCURSOR++;
2330				}
2331				/* fall through */
2332			case '\n':
2333				/* Check for ending label on the next line */
2334				if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
2335					YYCTYPE *end = YYCURSOR + heredoc_label->length;
2336
2337					if (*end == ';') {
2338						end++;
2339					}
2340
2341					if (*end == '\n' || *end == '\r') {
2342						/* newline before label will be subtracted from returned text, but
2343						 * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
2344						if (YYCURSOR[-2] == '\r' && YYCURSOR[-1] == '\n') {
2345							newline = 2; /* Windows newline */
2346						} else {
2347							newline = 1;
2348						}
2349
2350						CG(increment_lineno) = 1; /* For newline before label */
2351						BEGIN(ST_END_HEREDOC);
2352
2353						goto nowdoc_scan_done;
2354					}
2355				}
2356				/* fall through */
2357			default:
2358				continue;
2359		}
2360	}
2361
2362nowdoc_scan_done:
2363	yyleng = YYCURSOR - SCNG(yy_text);
2364
2365	zend_copy_value(zendlval, yytext, yyleng - newline);
2366	HANDLE_NEWLINES(yytext, yyleng - newline);
2367	RETURN_TOKEN(T_ENCAPSED_AND_WHITESPACE);
2368}
2369
2370
2371<ST_IN_SCRIPTING,ST_VAR_OFFSET>{ANY_CHAR} {
2372	if (YYCURSOR > YYLIMIT) {
2373		RETURN_TOKEN(END);
2374	}
2375
2376	zend_error(E_COMPILE_WARNING,"Unexpected character in input:  '%c' (ASCII=%d) state=%d", yytext[0], yytext[0], YYSTATE);
2377	goto restart;
2378}
2379
2380*/
2381}
2382