xref: /php-src/Zend/zend_language_scanner.l (revision fc1db70f)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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@php.net>                                 |
20    |          Zeev Suraski <zeev@php.net>                                 |
21    +----------------------------------------------------------------------+
22 */
23 
24 #if 0
25 # define YYDEBUG(s, c) printf("state: %d char: %c\n", s, c)
26 #else
27 # define YYDEBUG(s, c)
28 #endif
29 
30 #include "zend_language_scanner_defs.h"
31 
32 #include <errno.h>
33 #include "zend.h"
34 #ifdef ZEND_WIN32
35 # include <Winuser.h>
36 #endif
37 #include "zend_alloc.h"
38 #include <zend_language_parser.h>
39 #include "zend_compile.h"
40 #include "zend_language_scanner.h"
41 #include "zend_highlight.h"
42 #include "zend_constants.h"
43 #include "zend_variables.h"
44 #include "zend_operators.h"
45 #include "zend_API.h"
46 #include "zend_strtod.h"
47 #include "zend_exceptions.h"
48 #include "zend_virtual_cwd.h"
49 
50 #define YYCTYPE   unsigned char
51 #define YYFILL(n) { if ((YYCURSOR + n) >= (YYLIMIT + ZEND_MMAP_AHEAD)) { return 0; } }
52 #define YYCURSOR  SCNG(yy_cursor)
53 #define YYLIMIT   SCNG(yy_limit)
54 #define YYMARKER  SCNG(yy_marker)
55 
56 #define YYGETCONDITION()  SCNG(yy_state)
57 #define YYSETCONDITION(s) SCNG(yy_state) = s
58 
59 #define STATE(name)  yyc##name
60 
61 /* emulate flex constructs */
62 #define BEGIN(state) YYSETCONDITION(STATE(state))
63 #define YYSTATE      YYGETCONDITION()
64 #define yytext       ((char*)SCNG(yy_text))
65 #define yyleng       SCNG(yy_leng)
66 #define yyless(x)    do { YYCURSOR = (unsigned char*)yytext + x; \
67                           yyleng   = (unsigned int)x; } while(0)
68 #define yymore()     goto yymore_restart
69 
70 /* perform sanity check. If this message is triggered you should
71    increase the ZEND_MMAP_AHEAD value in the zend_streams.h file */
72 /*!max:re2c */
73 #if ZEND_MMAP_AHEAD < YYMAXFILL
74 # error ZEND_MMAP_AHEAD should be greater than or equal to YYMAXFILL
75 #endif
76 
77 #include <stdarg.h>
78 
79 #ifdef HAVE_UNISTD_H
80 # include <unistd.h>
81 #endif
82 
83 /* Globals Macros */
84 #define SCNG	LANG_SCNG
85 #ifdef ZTS
86 ZEND_API ts_rsrc_id language_scanner_globals_id;
87 ZEND_API size_t language_scanner_globals_offset;
88 #else
89 ZEND_API zend_php_scanner_globals language_scanner_globals;
90 #endif
91 
92 #define HANDLE_NEWLINES(s, l)													\
93 do {																			\
94 	char *p = (s), *boundary = p+(l);											\
95 																				\
96 	while (p<boundary) {														\
97 		if (*p == '\n' || (*p == '\r' && (*(p+1) != '\n'))) {					\
98 			CG(zend_lineno)++;													\
99 		}																		\
100 		p++;																	\
101 	}																			\
102 } while (0)
103 
104 #define HANDLE_NEWLINE(c) \
105 { \
106 	if (c == '\n' || c == '\r') { \
107 		CG(zend_lineno)++; \
108 	} \
109 }
110 
111 /* To save initial string length after scanning to first variable */
112 #define SET_DOUBLE_QUOTES_SCANNED_LENGTH(len) SCNG(scanned_string_len) = (len)
113 #define GET_DOUBLE_QUOTES_SCANNED_LENGTH()    SCNG(scanned_string_len)
114 
115 #define IS_LABEL_START(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z') || (c) == '_' || (c) >= 0x80)
116 #define IS_LABEL_SUCCESSOR(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z') || ((c) >= '0' && (c) <= '9') || (c) == '_' || (c) >= 0x80)
117 
118 #define ZEND_IS_OCT(c)  ((c)>='0' && (c)<='7')
119 #define ZEND_IS_HEX(c)  (((c)>='0' && (c)<='9') || ((c)>='a' && (c)<='f') || ((c)>='A' && (c)<='F'))
120 
121 
strip_underscores(char * str,size_t * len)122 static void strip_underscores(char *str, size_t *len)
123 {
124 	char *src = str, *dest = str;
125 	while (*src != '\0') {
126 		if (*src != '_') {
127 			*dest = *src;
128 			dest++;
129 		} else {
130 			--(*len);
131 		}
132 		src++;
133 	}
134 	*dest = '\0';
135 }
136 
encoding_filter_script_to_internal(unsigned char ** to,size_t * to_length,const unsigned char * from,size_t from_length)137 static size_t encoding_filter_script_to_internal(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length)
138 {
139 	const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding();
140 	ZEND_ASSERT(internal_encoding);
141 	return zend_multibyte_encoding_converter(to, to_length, from, from_length, internal_encoding, LANG_SCNG(script_encoding));
142 }
143 
encoding_filter_script_to_intermediate(unsigned char ** to,size_t * to_length,const unsigned char * from,size_t from_length)144 static size_t encoding_filter_script_to_intermediate(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length)
145 {
146 	return zend_multibyte_encoding_converter(to, to_length, from, from_length, zend_multibyte_encoding_utf8, LANG_SCNG(script_encoding));
147 }
148 
encoding_filter_intermediate_to_script(unsigned char ** to,size_t * to_length,const unsigned char * from,size_t from_length)149 static size_t encoding_filter_intermediate_to_script(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length)
150 {
151 	return zend_multibyte_encoding_converter(to, to_length, from, from_length,
152 LANG_SCNG(script_encoding), zend_multibyte_encoding_utf8);
153 }
154 
encoding_filter_intermediate_to_internal(unsigned char ** to,size_t * to_length,const unsigned char * from,size_t from_length)155 static size_t encoding_filter_intermediate_to_internal(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length)
156 {
157 	const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding();
158 	ZEND_ASSERT(internal_encoding);
159 	return zend_multibyte_encoding_converter(to, to_length, from, from_length,
160 internal_encoding, zend_multibyte_encoding_utf8);
161 }
162 
163 
_yy_push_state(int new_state)164 static void _yy_push_state(int new_state)
165 {
166 	zend_stack_push(&SCNG(state_stack), (void *) &YYGETCONDITION());
167 	YYSETCONDITION(new_state);
168 }
169 
170 #define yy_push_state(state_and_tsrm) _yy_push_state(yyc##state_and_tsrm)
171 
yy_pop_state(void)172 static void yy_pop_state(void)
173 {
174 	int *stack_state = zend_stack_top(&SCNG(state_stack));
175 	YYSETCONDITION(*stack_state);
176 	zend_stack_del_top(&SCNG(state_stack));
177 }
178 
yy_scan_buffer(char * str,size_t len)179 static void yy_scan_buffer(char *str, size_t len)
180 {
181 	YYCURSOR       = (YYCTYPE*)str;
182 	YYLIMIT        = YYCURSOR + len;
183 	if (!SCNG(yy_start)) {
184 		SCNG(yy_start) = YYCURSOR;
185 	}
186 }
187 
startup_scanner(void)188 void startup_scanner(void)
189 {
190 	CG(parse_error) = 0;
191 	CG(doc_comment) = NULL;
192 	CG(extra_fn_flags) = 0;
193 	zend_stack_init(&SCNG(state_stack), sizeof(int));
194 	zend_stack_init(&SCNG(nest_location_stack), sizeof(zend_nest_location));
195 	zend_ptr_stack_init(&SCNG(heredoc_label_stack));
196 	SCNG(heredoc_scan_ahead) = 0;
197 }
198 
heredoc_label_dtor(zend_heredoc_label * heredoc_label)199 static void heredoc_label_dtor(zend_heredoc_label *heredoc_label) {
200     efree(heredoc_label->label);
201 }
202 
shutdown_scanner(void)203 void shutdown_scanner(void)
204 {
205 	CG(parse_error) = 0;
206 	RESET_DOC_COMMENT();
207 	zend_stack_destroy(&SCNG(state_stack));
208 	zend_stack_destroy(&SCNG(nest_location_stack));
209 	zend_ptr_stack_clean(&SCNG(heredoc_label_stack), (void (*)(void *)) &heredoc_label_dtor, 1);
210 	zend_ptr_stack_destroy(&SCNG(heredoc_label_stack));
211 	SCNG(heredoc_scan_ahead) = 0;
212 	SCNG(on_event) = NULL;
213 }
214 
zend_save_lexical_state(zend_lex_state * lex_state)215 ZEND_API void zend_save_lexical_state(zend_lex_state *lex_state)
216 {
217 	lex_state->yy_leng   = SCNG(yy_leng);
218 	lex_state->yy_start  = SCNG(yy_start);
219 	lex_state->yy_text   = SCNG(yy_text);
220 	lex_state->yy_cursor = SCNG(yy_cursor);
221 	lex_state->yy_marker = SCNG(yy_marker);
222 	lex_state->yy_limit  = SCNG(yy_limit);
223 
224 	lex_state->state_stack = SCNG(state_stack);
225 	zend_stack_init(&SCNG(state_stack), sizeof(int));
226 
227 	lex_state->nest_location_stack = SCNG(nest_location_stack);
228 	zend_stack_init(&SCNG(nest_location_stack), sizeof(zend_nest_location));
229 
230 	lex_state->heredoc_label_stack = SCNG(heredoc_label_stack);
231 	zend_ptr_stack_init(&SCNG(heredoc_label_stack));
232 
233 	lex_state->in = SCNG(yy_in);
234 	lex_state->yy_state = YYSTATE;
235 	lex_state->filename = CG(compiled_filename);
236 	lex_state->lineno = CG(zend_lineno);
237 	CG(compiled_filename) = NULL;
238 
239 	lex_state->script_org = SCNG(script_org);
240 	lex_state->script_org_size = SCNG(script_org_size);
241 	lex_state->script_filtered = SCNG(script_filtered);
242 	lex_state->script_filtered_size = SCNG(script_filtered_size);
243 	lex_state->input_filter = SCNG(input_filter);
244 	lex_state->output_filter = SCNG(output_filter);
245 	lex_state->script_encoding = SCNG(script_encoding);
246 
247 	lex_state->on_event = SCNG(on_event);
248 	lex_state->on_event_context = SCNG(on_event_context);
249 
250 	lex_state->ast = CG(ast);
251 	lex_state->ast_arena = CG(ast_arena);
252 }
253 
zend_restore_lexical_state(zend_lex_state * lex_state)254 ZEND_API void zend_restore_lexical_state(zend_lex_state *lex_state)
255 {
256 	SCNG(yy_leng)   = lex_state->yy_leng;
257 	SCNG(yy_start)  = lex_state->yy_start;
258 	SCNG(yy_text)   = lex_state->yy_text;
259 	SCNG(yy_cursor) = lex_state->yy_cursor;
260 	SCNG(yy_marker) = lex_state->yy_marker;
261 	SCNG(yy_limit)  = lex_state->yy_limit;
262 
263 	zend_stack_destroy(&SCNG(state_stack));
264 	SCNG(state_stack) = lex_state->state_stack;
265 
266 	zend_stack_destroy(&SCNG(nest_location_stack));
267 	SCNG(nest_location_stack) = lex_state->nest_location_stack;
268 
269 	zend_ptr_stack_clean(&SCNG(heredoc_label_stack), (void (*)(void *)) &heredoc_label_dtor, 1);
270 	zend_ptr_stack_destroy(&SCNG(heredoc_label_stack));
271 	SCNG(heredoc_label_stack) = lex_state->heredoc_label_stack;
272 
273 	SCNG(yy_in) = lex_state->in;
274 	YYSETCONDITION(lex_state->yy_state);
275 	CG(zend_lineno) = lex_state->lineno;
276 	zend_restore_compiled_filename(lex_state->filename);
277 
278 	if (SCNG(script_filtered) && SCNG(script_filtered) != lex_state->script_filtered) {
279 		efree(SCNG(script_filtered));
280 		SCNG(script_filtered) = NULL;
281 	}
282 	SCNG(script_org) = lex_state->script_org;
283 	SCNG(script_org_size) = lex_state->script_org_size;
284 	SCNG(script_filtered) = lex_state->script_filtered;
285 	SCNG(script_filtered_size) = lex_state->script_filtered_size;
286 	SCNG(input_filter) = lex_state->input_filter;
287 	SCNG(output_filter) = lex_state->output_filter;
288 	SCNG(script_encoding) = lex_state->script_encoding;
289 
290 	SCNG(on_event) = lex_state->on_event;
291 	SCNG(on_event_context) = lex_state->on_event_context;
292 
293 	CG(ast) = lex_state->ast;
294 	CG(ast_arena) = lex_state->ast_arena;
295 
296 	RESET_DOC_COMMENT();
297 }
298 
zend_lex_tstring(zval * zv,unsigned char * ident)299 ZEND_API zend_result zend_lex_tstring(zval *zv, unsigned char *ident)
300 {
301 	unsigned char *end = ident;
302 	while ((*end >= 'a' && *end <= 'z') || (*end >= 'A' && *end <= 'Z') || *end == '_') {
303 		end++;
304 	}
305 
306 	size_t length = end - ident;
307 	if (length == 0) {
308 		ZEND_ASSERT(ident[0] == '<' && ident[1] == '?' && ident[2] == '=');
309 		zend_throw_exception(zend_ce_parse_error, "Cannot use \"<?=\" as an identifier", 0);
310 		return FAILURE;
311 	}
312 
313 	if (SCNG(on_event)) {
314 		SCNG(on_event)(ON_FEEDBACK, T_STRING, 0, (char *) ident, length, SCNG(on_event_context));
315 	}
316 
317 	ZVAL_STRINGL(zv, (char *) ident, length);
318 	return SUCCESS;
319 }
320 
321 #define BOM_UTF32_BE	"\x00\x00\xfe\xff"
322 #define	BOM_UTF32_LE	"\xff\xfe\x00\x00"
323 #define	BOM_UTF16_BE	"\xfe\xff"
324 #define	BOM_UTF16_LE	"\xff\xfe"
325 #define	BOM_UTF8		"\xef\xbb\xbf"
326 
zend_multibyte_detect_utf_encoding(const unsigned char * script,size_t script_size)327 static const zend_encoding *zend_multibyte_detect_utf_encoding(const unsigned char *script, size_t script_size)
328 {
329 	const unsigned char *p;
330 	int wchar_size = 2;
331 	int le = 0;
332 
333 	/* utf-16 or utf-32? */
334 	p = script;
335 	assert(p >= script);
336 	while ((size_t)(p-script) < script_size) {
337 		p = memchr(p, 0, script_size-(p-script)-2);
338 		if (!p) {
339 			break;
340 		}
341 		if (*(p+1) == '\0' && *(p+2) == '\0') {
342 			wchar_size = 4;
343 			break;
344 		}
345 
346 		/* searching for UTF-32 specific byte orders, so this will do */
347 		p += 4;
348 	}
349 
350 	/* BE or LE? */
351 	p = script;
352 	assert(p >= script);
353 	while ((size_t)(p-script) < script_size) {
354 		if (*p == '\0' && *(p+wchar_size-1) != '\0') {
355 			/* BE */
356 			le = 0;
357 			break;
358 		} else if (*p != '\0' && *(p+wchar_size-1) == '\0') {
359 			/* LE* */
360 			le = 1;
361 			break;
362 		}
363 		p += wchar_size;
364 	}
365 
366 	if (wchar_size == 2) {
367 		return le ? zend_multibyte_encoding_utf16le : zend_multibyte_encoding_utf16be;
368 	} else {
369 		return le ? zend_multibyte_encoding_utf32le : zend_multibyte_encoding_utf32be;
370 	}
371 
372 	return NULL;
373 }
374 
zend_multibyte_detect_unicode(void)375 static const zend_encoding* zend_multibyte_detect_unicode(void)
376 {
377 	const zend_encoding *script_encoding = NULL;
378 	int bom_size;
379 	unsigned char *pos1, *pos2;
380 
381 	if (LANG_SCNG(script_org_size) < sizeof(BOM_UTF32_LE)-1) {
382 		return NULL;
383 	}
384 
385 	/* check out BOM */
386 	if (!memcmp(LANG_SCNG(script_org), BOM_UTF32_BE, sizeof(BOM_UTF32_BE)-1)) {
387 		script_encoding = zend_multibyte_encoding_utf32be;
388 		bom_size = sizeof(BOM_UTF32_BE)-1;
389 	} else if (!memcmp(LANG_SCNG(script_org), BOM_UTF32_LE, sizeof(BOM_UTF32_LE)-1)) {
390 		script_encoding = zend_multibyte_encoding_utf32le;
391 		bom_size = sizeof(BOM_UTF32_LE)-1;
392 	} else if (!memcmp(LANG_SCNG(script_org), BOM_UTF16_BE, sizeof(BOM_UTF16_BE)-1)) {
393 		script_encoding = zend_multibyte_encoding_utf16be;
394 		bom_size = sizeof(BOM_UTF16_BE)-1;
395 	} else if (!memcmp(LANG_SCNG(script_org), BOM_UTF16_LE, sizeof(BOM_UTF16_LE)-1)) {
396 		script_encoding = zend_multibyte_encoding_utf16le;
397 		bom_size = sizeof(BOM_UTF16_LE)-1;
398 	} else if (!memcmp(LANG_SCNG(script_org), BOM_UTF8, sizeof(BOM_UTF8)-1)) {
399 		script_encoding = zend_multibyte_encoding_utf8;
400 		bom_size = sizeof(BOM_UTF8)-1;
401 	}
402 
403 	if (script_encoding) {
404 		/* remove BOM */
405 		LANG_SCNG(script_org) += bom_size;
406 		LANG_SCNG(script_org_size) -= bom_size;
407 
408 		return script_encoding;
409 	}
410 
411 	/* script contains NULL bytes -> auto-detection */
412 	if ((pos1 = memchr(LANG_SCNG(script_org), 0, LANG_SCNG(script_org_size)))) {
413 		/* check if the NULL byte is after the __HALT_COMPILER(); */
414 		pos2 = LANG_SCNG(script_org);
415 
416 		while ((size_t)(pos1 - pos2) >= sizeof("__HALT_COMPILER();")-1) {
417 			pos2 = memchr(pos2, '_', pos1 - pos2);
418 			if (!pos2) break;
419 			pos2++;
420 			if (strncasecmp((char*)pos2, "_HALT_COMPILER", sizeof("_HALT_COMPILER")-1) == 0) {
421 				pos2 += sizeof("_HALT_COMPILER")-1;
422 				while (*pos2 == ' '  ||
423 					   *pos2 == '\t' ||
424 					   *pos2 == '\r' ||
425 					   *pos2 == '\n') {
426 					pos2++;
427 				}
428 				if (*pos2 == '(') {
429 					pos2++;
430 					while (*pos2 == ' '  ||
431 						   *pos2 == '\t' ||
432 						   *pos2 == '\r' ||
433 						   *pos2 == '\n') {
434 						pos2++;
435 					}
436 					if (*pos2 == ')') {
437 						pos2++;
438 						while (*pos2 == ' '  ||
439 							   *pos2 == '\t' ||
440 							   *pos2 == '\r' ||
441 							   *pos2 == '\n') {
442 							pos2++;
443 						}
444 						if (*pos2 == ';') {
445 							return NULL;
446 						}
447 					}
448 				}
449 			}
450 		}
451 		/* make best effort if BOM is missing */
452 		return zend_multibyte_detect_utf_encoding(LANG_SCNG(script_org), LANG_SCNG(script_org_size));
453 	}
454 
455 	return NULL;
456 }
457 
zend_multibyte_find_script_encoding(void)458 static const zend_encoding* zend_multibyte_find_script_encoding(void)
459 {
460 	const zend_encoding *script_encoding;
461 
462 	if (CG(detect_unicode)) {
463 		/* check out bom(byte order mark) and see if containing wchars */
464 		script_encoding = zend_multibyte_detect_unicode();
465 		if (script_encoding != NULL) {
466 			/* bom or wchar detection is prior to 'script_encoding' option */
467 			return script_encoding;
468 		}
469 	}
470 
471 	/* if no script_encoding specified, just leave alone */
472 	if (!CG(script_encoding_list) || !CG(script_encoding_list_size)) {
473 		return NULL;
474 	}
475 
476 	/* if multiple encodings specified, detect automagically */
477 	if (CG(script_encoding_list_size) > 1) {
478 		return zend_multibyte_encoding_detector(LANG_SCNG(script_org), LANG_SCNG(script_org_size), CG(script_encoding_list), CG(script_encoding_list_size));
479 	}
480 
481 	return CG(script_encoding_list)[0];
482 }
483 
zend_multibyte_set_filter(const zend_encoding * onetime_encoding)484 ZEND_API zend_result zend_multibyte_set_filter(const zend_encoding *onetime_encoding)
485 {
486 	const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding();
487 	const zend_encoding *script_encoding = onetime_encoding ? onetime_encoding: zend_multibyte_find_script_encoding();
488 
489 	if (!script_encoding) {
490 		return FAILURE;
491 	}
492 
493 	/* judge input/output filter */
494 	LANG_SCNG(script_encoding) = script_encoding;
495 	LANG_SCNG(input_filter) = NULL;
496 	LANG_SCNG(output_filter) = NULL;
497 
498 	if (!internal_encoding || LANG_SCNG(script_encoding) == internal_encoding) {
499 		if (!zend_multibyte_check_lexer_compatibility(LANG_SCNG(script_encoding))) {
500 			/* and if not, work around w/ script_encoding -> utf-8 -> script_encoding conversion */
501 			LANG_SCNG(input_filter) = encoding_filter_script_to_intermediate;
502 			LANG_SCNG(output_filter) = encoding_filter_intermediate_to_script;
503 		} else {
504 			LANG_SCNG(input_filter) = NULL;
505 			LANG_SCNG(output_filter) = NULL;
506 		}
507 		return SUCCESS;
508 	}
509 
510 	if (zend_multibyte_check_lexer_compatibility(internal_encoding)) {
511 		LANG_SCNG(input_filter) = encoding_filter_script_to_internal;
512 		LANG_SCNG(output_filter) = NULL;
513 	} else if (zend_multibyte_check_lexer_compatibility(LANG_SCNG(script_encoding))) {
514 		LANG_SCNG(input_filter) = NULL;
515 		LANG_SCNG(output_filter) = encoding_filter_script_to_internal;
516 	} else {
517 		/* both script and internal encodings are incompatible w/ flex */
518 		LANG_SCNG(input_filter) = encoding_filter_script_to_intermediate;
519 		LANG_SCNG(output_filter) = encoding_filter_intermediate_to_internal;
520 	}
521 
522 	return SUCCESS;
523 }
524 
open_file_for_scanning(zend_file_handle * file_handle)525 ZEND_API zend_result open_file_for_scanning(zend_file_handle *file_handle)
526 {
527 	char *buf;
528 	size_t size;
529 	zend_string *compiled_filename;
530 
531 	if (zend_stream_fixup(file_handle, &buf, &size) == FAILURE) {
532 		/* Still add it to open_files to make destroy_file_handle work */
533 		zend_llist_add_element(&CG(open_files), file_handle);
534 		file_handle->in_list = 1;
535 		return FAILURE;
536 	}
537 
538 	ZEND_ASSERT(!EG(exception) && "stream_fixup() should have failed");
539 	zend_llist_add_element(&CG(open_files), file_handle);
540 	file_handle->in_list = 1;
541 
542 	/* Reset the scanner for scanning the new file */
543 	SCNG(yy_in) = file_handle;
544 	SCNG(yy_start) = NULL;
545 
546 	if (size != (size_t)-1) {
547 		if (CG(multibyte)) {
548 			SCNG(script_org) = (unsigned char*)buf;
549 			SCNG(script_org_size) = size;
550 			SCNG(script_filtered) = NULL;
551 
552 			zend_multibyte_set_filter(NULL);
553 
554 			if (SCNG(input_filter)) {
555 				if ((size_t)-1 == SCNG(input_filter)(&SCNG(script_filtered), &SCNG(script_filtered_size), SCNG(script_org), SCNG(script_org_size))) {
556 					zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
557 							"encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
558 				}
559 				buf = (char*)SCNG(script_filtered);
560 				size = SCNG(script_filtered_size);
561 			}
562 		}
563 		SCNG(yy_start) = (unsigned char *)buf;
564 		yy_scan_buffer(buf, size);
565 	} else {
566 		zend_error_noreturn(E_COMPILE_ERROR, "zend_stream_mmap() failed");
567 	}
568 
569 	if (CG(skip_shebang)) {
570 		BEGIN(SHEBANG);
571 	} else {
572 		BEGIN(INITIAL);
573 	}
574 
575 	if (file_handle->opened_path) {
576 		compiled_filename = zend_string_copy(file_handle->opened_path);
577 	} else {
578 		compiled_filename = zend_string_copy(file_handle->filename);
579 	}
580 
581 	zend_set_compiled_filename(compiled_filename);
582 	zend_string_release_ex(compiled_filename, 0);
583 
584 	RESET_DOC_COMMENT();
585 	CG(zend_lineno) = 1;
586 	CG(increment_lineno) = 0;
587 	return SUCCESS;
588 }
589 
zend_compile(int type)590 static zend_op_array *zend_compile(int type)
591 {
592 	zend_op_array *op_array = NULL;
593 	bool original_in_compilation = CG(in_compilation);
594 
595 	CG(in_compilation) = 1;
596 	CG(ast) = NULL;
597 	CG(ast_arena) = zend_arena_create(1024 * 32);
598 
599 	if (!zendparse()) {
600 		int last_lineno = CG(zend_lineno);
601 		zend_file_context original_file_context;
602 		zend_oparray_context original_oparray_context;
603 		zend_op_array *original_active_op_array = CG(active_op_array);
604 
605 		op_array = emalloc(sizeof(zend_op_array));
606 		init_op_array(op_array, type, INITIAL_OP_ARRAY_SIZE);
607 		CG(active_op_array) = op_array;
608 
609 		/* Use heap to not waste arena memory */
610 		op_array->fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
611 
612 		if (zend_ast_process) {
613 			zend_ast_process(CG(ast));
614 		}
615 
616 		zend_file_context_begin(&original_file_context);
617 		zend_oparray_context_begin(&original_oparray_context, op_array);
618 		zend_compile_top_stmt(CG(ast));
619 		CG(zend_lineno) = last_lineno;
620 		zend_emit_final_return(type == ZEND_USER_FUNCTION);
621 		op_array->line_start = 1;
622 		op_array->line_end = last_lineno;
623 		pass_two(op_array);
624 		zend_oparray_context_end(&original_oparray_context);
625 		zend_file_context_end(&original_file_context);
626 
627 		CG(active_op_array) = original_active_op_array;
628 	}
629 
630 	zend_ast_destroy(CG(ast));
631 	zend_arena_destroy(CG(ast_arena));
632 
633 	CG(in_compilation) = original_in_compilation;
634 
635 	return op_array;
636 }
637 
compile_file(zend_file_handle * file_handle,int type)638 ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type)
639 {
640 	zend_lex_state original_lex_state;
641 	zend_op_array *op_array = NULL;
642 	zend_save_lexical_state(&original_lex_state);
643 
644 	if (open_file_for_scanning(file_handle)==FAILURE) {
645 		if (!EG(exception)) {
646 			if (type==ZEND_REQUIRE) {
647 				zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, ZSTR_VAL(file_handle->filename));
648 			} else {
649 				zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, ZSTR_VAL(file_handle->filename));
650 			}
651 		}
652 	} else {
653 		op_array = zend_compile(ZEND_USER_FUNCTION);
654 	}
655 
656 	zend_restore_lexical_state(&original_lex_state);
657 	return op_array;
658 }
659 
zend_compile_string_to_ast(zend_string * code,zend_arena ** ast_arena,zend_string * filename)660 ZEND_API zend_ast *zend_compile_string_to_ast(
661 		zend_string *code, zend_arena **ast_arena, zend_string *filename) {
662 	zval code_zv;
663 	bool original_in_compilation;
664 	zend_lex_state original_lex_state;
665 	zend_ast *ast;
666 
667 	ZVAL_STR_COPY(&code_zv, code);
668 
669 	original_in_compilation = CG(in_compilation);
670 	CG(in_compilation) = 1;
671 
672 	zend_save_lexical_state(&original_lex_state);
673 	zend_prepare_string_for_scanning(&code_zv, filename);
674 	CG(ast) = NULL;
675 	CG(ast_arena) = zend_arena_create(1024 * 32);
676 	LANG_SCNG(yy_state) = yycINITIAL;
677 
678 	if (zendparse() != 0) {
679 		zend_ast_destroy(CG(ast));
680 		zend_arena_destroy(CG(ast_arena));
681 		CG(ast) = NULL;
682 	}
683 
684 	/* restore_lexical_state changes CG(ast) and CG(ast_arena) */
685 	ast = CG(ast);
686 	*ast_arena = CG(ast_arena);
687 
688 	zend_restore_lexical_state(&original_lex_state);
689 	CG(in_compilation) = original_in_compilation;
690 
691 	zval_ptr_dtor_str(&code_zv);
692 
693 	return ast;
694 }
695 
compile_filename(int type,zend_string * filename)696 zend_op_array *compile_filename(int type, zend_string *filename)
697 {
698 	zend_file_handle file_handle;
699 	zend_op_array *retval;
700 	zend_string *opened_path = NULL;
701 
702 	zend_stream_init_filename_ex(&file_handle, filename);
703 
704 	retval = zend_compile_file(&file_handle, type);
705 	if (retval && file_handle.handle.stream.handle) {
706 		if (!file_handle.opened_path) {
707 			file_handle.opened_path = opened_path = zend_string_copy(filename);
708 		}
709 
710 		zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path);
711 
712 		if (opened_path) {
713 			zend_string_release_ex(opened_path, 0);
714 		}
715 	}
716 	zend_destroy_file_handle(&file_handle);
717 
718 	return retval;
719 }
720 
zend_prepare_string_for_scanning(zval * str,zend_string * filename)721 ZEND_API void zend_prepare_string_for_scanning(zval *str, zend_string *filename)
722 {
723 	char *buf;
724 	size_t size, old_len;
725 
726 	/* enforce ZEND_MMAP_AHEAD trailing NULLs for flex... */
727 	old_len = Z_STRLEN_P(str);
728 	Z_STR_P(str) = zend_string_extend(Z_STR_P(str), old_len + ZEND_MMAP_AHEAD, 0);
729 	Z_TYPE_INFO_P(str) = IS_STRING_EX;
730 	memset(Z_STRVAL_P(str) + old_len, 0, ZEND_MMAP_AHEAD + 1);
731 
732 	SCNG(yy_in) = NULL;
733 	SCNG(yy_start) = NULL;
734 
735 	buf = Z_STRVAL_P(str);
736 	size = old_len;
737 
738 	if (CG(multibyte)) {
739 		SCNG(script_org) = (unsigned char*)buf;
740 		SCNG(script_org_size) = size;
741 		SCNG(script_filtered) = NULL;
742 
743 		zend_multibyte_set_filter(zend_multibyte_get_internal_encoding());
744 
745 		if (SCNG(input_filter)) {
746 			if ((size_t)-1 == SCNG(input_filter)(&SCNG(script_filtered), &SCNG(script_filtered_size), SCNG(script_org), SCNG(script_org_size))) {
747 				zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
748 						"encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
749 			}
750 			buf = (char*)SCNG(script_filtered);
751 			size = SCNG(script_filtered_size);
752 		}
753 	}
754 
755 	yy_scan_buffer(buf, size);
756 	zend_set_compiled_filename(filename);
757 	CG(zend_lineno) = 1;
758 	CG(increment_lineno) = 0;
759 	RESET_DOC_COMMENT();
760 }
761 
762 
zend_get_scanned_file_offset(void)763 ZEND_API size_t zend_get_scanned_file_offset(void)
764 {
765 	size_t offset = SCNG(yy_cursor) - SCNG(yy_start);
766 	if (SCNG(input_filter)) {
767 		size_t original_offset = offset, length = 0;
768 		do {
769 			unsigned char *p = NULL;
770 			if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), offset)) {
771 				return (size_t)-1;
772 			}
773 			efree(p);
774 			if (length > original_offset) {
775 				offset--;
776 			} else if (length < original_offset) {
777 				offset++;
778 			}
779 		} while (original_offset != length);
780 	}
781 	return offset;
782 }
783 
compile_string(zend_string * source_string,const char * filename,zend_compile_position position)784 zend_op_array *compile_string(zend_string *source_string, const char *filename, zend_compile_position position)
785 {
786 	zend_lex_state original_lex_state;
787 	zend_op_array *op_array = NULL;
788 	zval tmp;
789 	zend_string *filename_str;
790 
791 	if (ZSTR_LEN(source_string) == 0) {
792 		return NULL;
793 	}
794 
795 	ZVAL_STR_COPY(&tmp, source_string);
796 
797 	zend_save_lexical_state(&original_lex_state);
798 	filename_str = zend_string_init(filename, strlen(filename), 0);
799 	zend_prepare_string_for_scanning(&tmp, filename_str);
800 	zend_string_release(filename_str);
801 
802 	switch (position) {
803 		case ZEND_COMPILE_POSITION_AT_SHEBANG:
804 			BEGIN(SHEBANG);
805 			break;
806 		case ZEND_COMPILE_POSITION_AT_OPEN_TAG:
807 			BEGIN(INITIAL);
808 			break;
809 		case ZEND_COMPILE_POSITION_AFTER_OPEN_TAG:
810 			BEGIN(ST_IN_SCRIPTING);
811 			break;
812 	}
813 
814 	op_array = zend_compile(ZEND_EVAL_CODE);
815 
816 	zend_restore_lexical_state(&original_lex_state);
817 	zval_ptr_dtor(&tmp);
818 
819 	return op_array;
820 }
821 
822 
highlight_file(const char * filename,zend_syntax_highlighter_ini * syntax_highlighter_ini)823 zend_result highlight_file(const char *filename, zend_syntax_highlighter_ini *syntax_highlighter_ini)
824 {
825 	zend_lex_state original_lex_state;
826 	zend_file_handle file_handle;
827 
828 	zend_stream_init_filename(&file_handle, filename);
829 	zend_save_lexical_state(&original_lex_state);
830 	if (open_file_for_scanning(&file_handle)==FAILURE) {
831 		zend_message_dispatcher(ZMSG_FAILED_HIGHLIGHT_FOPEN, filename);
832 		zend_destroy_file_handle(&file_handle);
833 		zend_restore_lexical_state(&original_lex_state);
834 		return FAILURE;
835 	}
836 	zend_highlight(syntax_highlighter_ini);
837 	if (SCNG(script_filtered)) {
838 		efree(SCNG(script_filtered));
839 		SCNG(script_filtered) = NULL;
840 	}
841 	zend_destroy_file_handle(&file_handle);
842 	zend_restore_lexical_state(&original_lex_state);
843 	return SUCCESS;
844 }
845 
highlight_string(zend_string * str,zend_syntax_highlighter_ini * syntax_highlighter_ini,const char * filename)846 void highlight_string(zend_string *str, zend_syntax_highlighter_ini *syntax_highlighter_ini, const char *filename)
847 {
848 	zend_lex_state original_lex_state;
849 	zval str_zv;
850 	zend_string *filename_str = zend_string_init(filename, strlen(filename), 0);
851 	ZVAL_STR_COPY(&str_zv, str);
852 	zend_save_lexical_state(&original_lex_state);
853 	zend_prepare_string_for_scanning(&str_zv, filename_str);
854 	zend_string_release(filename_str);
855 	BEGIN(INITIAL);
856 	zend_highlight(syntax_highlighter_ini);
857 	if (SCNG(script_filtered)) {
858 		efree(SCNG(script_filtered));
859 		SCNG(script_filtered) = NULL;
860 	}
861 	zend_restore_lexical_state(&original_lex_state);
862 	zval_ptr_dtor(&str_zv);
863 }
864 
zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter,const zend_encoding * old_encoding)865 ZEND_API void zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter, const zend_encoding *old_encoding)
866 {
867 	size_t length;
868 	unsigned char *new_yy_start;
869 
870 	/* convert and set */
871 	if (!SCNG(input_filter)) {
872 		if (SCNG(script_filtered)) {
873 			efree(SCNG(script_filtered));
874 			SCNG(script_filtered) = NULL;
875 		}
876 		SCNG(script_filtered_size) = 0;
877 		length = SCNG(script_org_size);
878 		new_yy_start = SCNG(script_org);
879 	} else {
880 		if ((size_t)-1 == SCNG(input_filter)(&new_yy_start, &length, SCNG(script_org), SCNG(script_org_size))) {
881 			zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
882 					"encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
883 		}
884 		if (SCNG(script_filtered)) {
885 			efree(SCNG(script_filtered));
886 		}
887 		SCNG(script_filtered) = new_yy_start;
888 		SCNG(script_filtered_size) = length;
889 	}
890 
891 	SCNG(yy_cursor) = new_yy_start + (SCNG(yy_cursor) - SCNG(yy_start));
892 	SCNG(yy_marker) = new_yy_start + (SCNG(yy_marker) - SCNG(yy_start));
893 	SCNG(yy_text) = new_yy_start + (SCNG(yy_text) - SCNG(yy_start));
894 	SCNG(yy_limit) = new_yy_start + length;
895 
896 	SCNG(yy_start) = new_yy_start;
897 }
898 
899 
900 // TODO: avoid reallocation ???
901 # define zend_copy_value(zendlval, yytext, yyleng) \
902 	if (SCNG(output_filter)) { \
903 		size_t sz = 0; \
904 		char *s = NULL; \
905 		SCNG(output_filter)((unsigned char **)&s, &sz, (unsigned char *)yytext, (size_t)yyleng); \
906 		ZVAL_STRINGL(zendlval, s, sz); \
907 		efree(s); \
908 	} else if (yyleng == 1) { \
909 		ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR((zend_uchar)*(yytext))); \
910 	} else { \
911 		ZVAL_STRINGL(zendlval, yytext, yyleng); \
912 	}
913 
zend_scan_escape_string(zval * zendlval,char * str,int len,char quote_type)914 static zend_result zend_scan_escape_string(zval *zendlval, char *str, int len, char quote_type)
915 {
916 	char *s, *t;
917 	char *end;
918 
919 	if (len <= 1) {
920 		if (len < 1) {
921 			ZVAL_EMPTY_STRING(zendlval);
922 		} else {
923 			zend_uchar c = (zend_uchar)*str;
924 			if (c == '\n' || c == '\r') {
925 				CG(zend_lineno)++;
926 			}
927 			ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR(c));
928 		}
929 		goto skip_escape_conversion;
930 	}
931 
932 	ZVAL_STRINGL(zendlval, str, len);
933 
934 	/* convert escape sequences */
935 	s = Z_STRVAL_P(zendlval);
936 	end = s+Z_STRLEN_P(zendlval);
937 	while (1) {
938 		if (UNEXPECTED(*s=='\\')) {
939 			break;
940 		}
941 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
942 			CG(zend_lineno)++;
943 		}
944 		s++;
945 		if (s == end) {
946 			goto skip_escape_conversion;
947 		}
948 	}
949 
950 	t = s;
951 	while (s<end) {
952 		if (*s=='\\') {
953 			s++;
954 			if (s >= end) {
955 				*t++ = '\\';
956 				break;
957 			}
958 
959 			switch(*s) {
960 				case 'n':
961 					*t++ = '\n';
962 					break;
963 				case 'r':
964 					*t++ = '\r';
965 					break;
966 				case 't':
967 					*t++ = '\t';
968 					break;
969 				case 'f':
970 					*t++ = '\f';
971 					break;
972 				case 'v':
973 					*t++ = '\v';
974 					break;
975 				case 'e':
976 #ifdef ZEND_WIN32
977 					*t++ = VK_ESCAPE;
978 #else
979 					*t++ = '\e';
980 #endif
981 					break;
982 				case '"':
983 				case '`':
984 					if (*s != quote_type) {
985 						*t++ = '\\';
986 						*t++ = *s;
987 						break;
988 					}
989 					ZEND_FALLTHROUGH;
990 				case '\\':
991 				case '$':
992 					*t++ = *s;
993 					break;
994 				case 'x':
995 				case 'X':
996 					if (ZEND_IS_HEX(*(s+1))) {
997 						char hex_buf[3] = { 0, 0, 0 };
998 
999 						hex_buf[0] = *(++s);
1000 						if (ZEND_IS_HEX(*(s+1))) {
1001 							hex_buf[1] = *(++s);
1002 						}
1003 						*t++ = (char) ZEND_STRTOL(hex_buf, NULL, 16);
1004 					} else {
1005 						*t++ = '\\';
1006 						*t++ = *s;
1007 					}
1008 					break;
1009 				/* UTF-8 codepoint escape, format: /\\u\{\x+\}/ */
1010 				case 'u':
1011 					{
1012 						/* cache where we started so we can parse after validating */
1013 						char *start = s + 1;
1014 						size_t len = 0;
1015 						bool valid = 1;
1016 						unsigned long codepoint;
1017 
1018 						if (*start != '{') {
1019 							/* we silently let this pass to avoid breaking code
1020 							 * with JSON in string literals (e.g. "\"\u202e\""
1021 							 */
1022 							*t++ = '\\';
1023 							*t++ = 'u';
1024 							break;
1025 						} else {
1026 							/* on the other hand, invalid \u{blah} errors */
1027 							s++;
1028 							len++;
1029 							s++;
1030 							while (*s != '}') {
1031 								if (!ZEND_IS_HEX(*s)) {
1032 									valid = 0;
1033 									break;
1034 								} else {
1035 									len++;
1036 								}
1037 								s++;
1038 							}
1039 							if (*s == '}') {
1040 								valid = 1;
1041 								len++;
1042 							}
1043 						}
1044 
1045 						/* \u{} is invalid */
1046 						if (len <= 2) {
1047 							valid = 0;
1048 						}
1049 
1050 						if (!valid) {
1051 							zend_throw_exception(zend_ce_parse_error,
1052 								"Invalid UTF-8 codepoint escape sequence", 0);
1053 							zval_ptr_dtor(zendlval);
1054 							ZVAL_UNDEF(zendlval);
1055 							return FAILURE;
1056 						}
1057 
1058 						errno = 0;
1059 						codepoint = strtoul(start + 1, NULL, 16);
1060 
1061 						/* per RFC 3629, UTF-8 can only represent 21 bits */
1062 						if (codepoint > 0x10FFFF || errno) {
1063 							zend_throw_exception(zend_ce_parse_error,
1064 								"Invalid UTF-8 codepoint escape sequence: Codepoint too large", 0);
1065 							zval_ptr_dtor(zendlval);
1066 							ZVAL_UNDEF(zendlval);
1067 							return FAILURE;
1068 						}
1069 
1070 						/* based on https://en.wikipedia.org/wiki/UTF-8#Sample_code */
1071 						if (codepoint < 0x80) {
1072 							*t++ = codepoint;
1073 						} else if (codepoint <= 0x7FF) {
1074 							*t++ = (codepoint >> 6) + 0xC0;
1075 							*t++ = (codepoint & 0x3F) + 0x80;
1076 						} else if (codepoint <= 0xFFFF) {
1077 							*t++ = (codepoint >> 12) + 0xE0;
1078 							*t++ = ((codepoint >> 6) & 0x3F) + 0x80;
1079 							*t++ = (codepoint & 0x3F) + 0x80;
1080 						} else if (codepoint <= 0x10FFFF) {
1081 							*t++ = (codepoint >> 18) + 0xF0;
1082 							*t++ = ((codepoint >> 12) & 0x3F) + 0x80;
1083 							*t++ = ((codepoint >> 6) & 0x3F) + 0x80;
1084 							*t++ = (codepoint & 0x3F) + 0x80;
1085 						}
1086 					}
1087 					break;
1088 				default:
1089 					/* check for an octal */
1090 					if (ZEND_IS_OCT(*s)) {
1091 						char octal_buf[4] = { 0, 0, 0, 0 };
1092 
1093 						octal_buf[0] = *s;
1094 						if (ZEND_IS_OCT(*(s+1))) {
1095 							octal_buf[1] = *(++s);
1096 							if (ZEND_IS_OCT(*(s+1))) {
1097 								octal_buf[2] = *(++s);
1098 							}
1099 						}
1100 						if (octal_buf[2] && (octal_buf[0] > '3') && !SCNG(heredoc_scan_ahead)) {
1101 							/* 3 octit values must not overflow 0xFF (\377) */
1102 							zend_error(E_COMPILE_WARNING, "Octal escape sequence overflow \\%s is greater than \\377", octal_buf);
1103 						}
1104 
1105 						*t++ = (char) ZEND_STRTOL(octal_buf, NULL, 8);
1106 					} else {
1107 						*t++ = '\\';
1108 						*t++ = *s;
1109 					}
1110 					break;
1111 			}
1112 		} else {
1113 			*t++ = *s;
1114 		}
1115 
1116 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
1117 			CG(zend_lineno)++;
1118 		}
1119 		s++;
1120 	}
1121 	*t = 0;
1122 	Z_STRLEN_P(zendlval) = t - Z_STRVAL_P(zendlval);
1123 
1124 skip_escape_conversion:
1125 	if (SCNG(output_filter)) {
1126 		size_t sz = 0;
1127 		unsigned char *str;
1128 		// TODO: avoid realocation ???
1129 		s = Z_STRVAL_P(zendlval);
1130 		SCNG(output_filter)(&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
1131 		zval_ptr_dtor(zendlval);
1132 		ZVAL_STRINGL(zendlval, (char *) str, sz);
1133 		efree(str);
1134 	}
1135 	return SUCCESS;
1136 }
1137 
1138 #define HEREDOC_USING_SPACES 1
1139 #define HEREDOC_USING_TABS 2
1140 
next_newline(const char * str,const char * end,size_t * newline_len)1141 static const char *next_newline(const char *str, const char *end, size_t *newline_len) {
1142 	for (; str < end; str++) {
1143 		if (*str == '\r') {
1144 			*newline_len = str + 1 < end && *(str + 1) == '\n' ? 2 : 1;
1145 			return str;
1146 		} else if (*str == '\n') {
1147 			*newline_len = 1;
1148 			return str;
1149 		}
1150 	}
1151 	*newline_len = 0;
1152 	return NULL;
1153 }
1154 
strip_multiline_string_indentation(zval * zendlval,int indentation,bool using_spaces,bool newline_at_start,bool newline_at_end)1155 static bool strip_multiline_string_indentation(
1156 	zval *zendlval, int indentation, bool using_spaces,
1157 	bool newline_at_start, bool newline_at_end)
1158 {
1159 	const char *str = Z_STRVAL_P(zendlval), *end = str + Z_STRLEN_P(zendlval);
1160 	char *copy = Z_STRVAL_P(zendlval);
1161 
1162 	int newline_count = 0;
1163 	size_t newline_len;
1164 	const char *nl;
1165 
1166 	if (!newline_at_start) {
1167 		nl = next_newline(str, end, &newline_len);
1168 		if (!nl) {
1169 			return 1;
1170 		}
1171 
1172 		str = nl + newline_len;
1173 		copy = (char *) nl + newline_len;
1174 		newline_count++;
1175 	} else {
1176 		nl = str;
1177 	}
1178 
1179 	/* <= intentional */
1180 	while (str <= end && nl) {
1181 		size_t skip;
1182 		nl = next_newline(str, end, &newline_len);
1183 		if (!nl && newline_at_end) {
1184 			nl = end;
1185 		}
1186 
1187 		/* Try to skip indentation */
1188 		for (skip = 0; skip < indentation; skip++, str++) {
1189 			if (str == nl) {
1190 				/* Don't require full indentation on whitespace-only lines */
1191 				break;
1192 			}
1193 
1194 			if (str == end || (*str != ' ' && *str != '\t')) {
1195 				CG(zend_lineno) += newline_count;
1196 				zend_throw_exception_ex(zend_ce_parse_error, 0,
1197 					"Invalid body indentation level (expecting an indentation level of at least %d)", indentation);
1198 				goto error;
1199 			}
1200 
1201 			if ((!using_spaces && *str == ' ') || (using_spaces && *str == '\t')) {
1202 				CG(zend_lineno) += newline_count;
1203 				zend_throw_exception(zend_ce_parse_error,
1204 					"Invalid indentation - tabs and spaces cannot be mixed", 0);
1205 				goto error;
1206 			}
1207 		}
1208 
1209 		if (str == end) {
1210 			break;
1211 		}
1212 
1213 		size_t len = nl ? (nl - str + newline_len) : (end - str);
1214 		memmove(copy, str, len);
1215 		str += len;
1216 		copy += len;
1217 		newline_count++;
1218 	}
1219 
1220 	*copy = '\0';
1221 	Z_STRLEN_P(zendlval) = copy - Z_STRVAL_P(zendlval);
1222 	return 1;
1223 
1224 error:
1225 	zval_ptr_dtor_str(zendlval);
1226 	ZVAL_UNDEF(zendlval);
1227 
1228 	return 0;
1229 }
1230 
copy_heredoc_label_stack(void * void_heredoc_label)1231 static void copy_heredoc_label_stack(void *void_heredoc_label)
1232 {
1233 	zend_heredoc_label *heredoc_label = void_heredoc_label;
1234 	zend_heredoc_label *new_heredoc_label = emalloc(sizeof(zend_heredoc_label));
1235 
1236 	*new_heredoc_label = *heredoc_label;
1237 	new_heredoc_label->label = estrndup(heredoc_label->label, heredoc_label->length);
1238 
1239 	zend_ptr_stack_push(&SCNG(heredoc_label_stack), (void *) new_heredoc_label);
1240 }
1241 
1242 /* Check that { }, [ ], ( ) are nested correctly */
report_bad_nesting(char opening,int opening_lineno,char closing)1243 static void report_bad_nesting(char opening, int opening_lineno, char closing)
1244 {
1245 	char   buf[256];
1246 	size_t used = 0;
1247 
1248 	used = snprintf(buf, sizeof(buf), "Unclosed '%c'", opening);
1249 
1250 	if (opening_lineno != CG(zend_lineno)) {
1251 		used += snprintf(buf + used, sizeof(buf) - used, " on line %d", opening_lineno);
1252 	}
1253 
1254 	if (closing) { 	/* 'closing' will be 0 if at end of file */
1255 		used += snprintf(buf + used, sizeof(buf) - used, " does not match '%c'", closing);
1256 	}
1257 
1258 	zend_throw_exception(zend_ce_parse_error, buf, 0);
1259 }
1260 
enter_nesting(char opening)1261 static void enter_nesting(char opening)
1262 {
1263 	zend_nest_location nest_loc = {opening, CG(zend_lineno)};
1264 	zend_stack_push(&SCNG(nest_location_stack), &nest_loc);
1265 }
1266 
exit_nesting(char closing)1267 static zend_result exit_nesting(char closing)
1268 {
1269 	if (zend_stack_is_empty(&SCNG(nest_location_stack))) {
1270 		zend_throw_exception_ex(zend_ce_parse_error, 0, "Unmatched '%c'", closing);
1271 		return FAILURE;
1272 	}
1273 
1274 	zend_nest_location *nest_loc = zend_stack_top(&SCNG(nest_location_stack));
1275 	char opening = nest_loc->text;
1276 
1277 	if ((opening == '{' && closing != '}') ||
1278 	    (opening == '[' && closing != ']') ||
1279 	    (opening == '(' && closing != ')')) {
1280 		report_bad_nesting(opening, nest_loc->lineno, closing);
1281 		return FAILURE;
1282 	}
1283 
1284 	zend_stack_del_top(&SCNG(nest_location_stack));
1285 	return SUCCESS;
1286 }
1287 
check_nesting_at_end(void)1288 static zend_result check_nesting_at_end(void)
1289 {
1290 	if (!zend_stack_is_empty(&SCNG(nest_location_stack))) {
1291 		zend_nest_location *nest_loc = zend_stack_top(&SCNG(nest_location_stack));
1292 		report_bad_nesting(nest_loc->text, nest_loc->lineno, 0);
1293 		return FAILURE;
1294 	}
1295 
1296 	return SUCCESS;
1297 }
1298 
1299 #define PARSER_MODE() \
1300 	EXPECTED(elem != NULL)
1301 
1302 #define RETURN_TOKEN(_token) do { \
1303 		token = _token; \
1304 		goto emit_token; \
1305 	} while (0)
1306 
1307 #define RETURN_TOKEN_WITH_VAL(_token) do { \
1308 		token = _token; \
1309 		goto emit_token_with_val; \
1310 	} while (0)
1311 
1312 #define RETURN_TOKEN_WITH_STR(_token, _offset) do { \
1313 		token = _token; \
1314 		offset = _offset; \
1315 		goto emit_token_with_str; \
1316 	} while (0)
1317 
1318 #define RETURN_TOKEN_WITH_IDENT(_token) do { \
1319 		token = _token; \
1320 		goto emit_token_with_ident; \
1321 	} while (0)
1322 
1323 #define RETURN_OR_SKIP_TOKEN(_token) do { \
1324 		token = _token; \
1325 		if (PARSER_MODE()) { \
1326 			goto skip_token; \
1327 		} \
1328 		goto emit_token; \
1329 	} while (0)
1330 
1331 #define RETURN_EXIT_NESTING_TOKEN(_token) do { \
1332 		if (exit_nesting(_token) != SUCCESS && PARSER_MODE()) { \
1333 			RETURN_TOKEN(T_ERROR); \
1334 		} else { \
1335 			RETURN_TOKEN(_token); \
1336 		} \
1337 	} while(0)
1338 
1339 #define RETURN_END_TOKEN do { \
1340 		if (check_nesting_at_end() != SUCCESS && PARSER_MODE()) { \
1341 			RETURN_TOKEN(T_ERROR); \
1342 		} else { \
1343 			RETURN_TOKEN(END); \
1344 		} \
1345 	} while (0)
1346 
lex_scan(zval * zendlval,zend_parser_stack_elem * elem)1347 int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem)
1348 {
1349 int token;
1350 int offset;
1351 int start_line = CG(zend_lineno);
1352 
1353 	ZVAL_UNDEF(zendlval);
1354 restart:
1355 	SCNG(yy_text) = YYCURSOR;
1356 
1357 /*!re2c
1358 re2c:yyfill:check = 0;
1359 LNUM	[0-9]+(_[0-9]+)*
1360 DNUM	({LNUM}?"."{LNUM})|({LNUM}"."{LNUM}?)
1361 EXPONENT_DNUM	(({LNUM}|{DNUM})[eE][+-]?{LNUM})
1362 HNUM	"0x"[0-9a-fA-F]+(_[0-9a-fA-F]+)*
1363 BNUM	"0b"[01]+(_[01]+)*
1364 ONUM	"0o"[0-7]+(_[0-7]+)*
1365 LABEL	[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*
1366 WHITESPACE [ \n\r\t]+
1367 TABS_AND_SPACES [ \t]*
1368 TOKENS [;:,.|^&+-/*=%!~$<>?@]
1369 ANY_CHAR [^]
1370 NEWLINE ("\r"|"\n"|"\r\n")
1371 OPTIONAL_WHITESPACE [ \n\r\t]*
1372 /* We don't use re2c with bounds checking, we just return 0 bytes if we read past the input.
1373  * If we use wildcard matching for comments, we can read past the input, which crashes
1374  * once we try to report a syntax error because the 0 bytes are not actually part of
1375  * the token. We prevent this by not allowing 0 bytes, which already aren't valid anyway. */
1376 MULTI_LINE_COMMENT "/*"([^*\x00]*"*"+)([^*/\x00][^*\x00]*"*"+)*"/"
1377 SINGLE_LINE_COMMENT "//"[^\x00\n\r]*[\n\r]
1378 HASH_COMMENT "#"(([^[\x00][^\x00\n\r]*[\n\r])|[\n\r])
1379 WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_COMMENT}|{HASH_COMMENT})+
1380 OPTIONAL_WHITESPACE_OR_COMMENTS ({WHITESPACE}|{MULTI_LINE_COMMENT}|{SINGLE_LINE_COMMENT}|{HASH_COMMENT})*
1381 
1382 /* compute yyleng before each rule */
1383 <!*> := yyleng = YYCURSOR - SCNG(yy_text);
1384 
1385 <ST_IN_SCRIPTING>"exit" {
1386 	RETURN_TOKEN_WITH_IDENT(T_EXIT);
1387 }
1388 
1389 <ST_IN_SCRIPTING>"die" {
1390 	RETURN_TOKEN_WITH_IDENT(T_EXIT);
1391 }
1392 
1393 <ST_IN_SCRIPTING>"fn" {
1394 	RETURN_TOKEN_WITH_IDENT(T_FN);
1395 }
1396 
1397 <ST_IN_SCRIPTING>"function" {
1398 	RETURN_TOKEN_WITH_IDENT(T_FUNCTION);
1399 }
1400 
1401 <ST_IN_SCRIPTING>"const" {
1402 	RETURN_TOKEN_WITH_IDENT(T_CONST);
1403 }
1404 
1405 <ST_IN_SCRIPTING>"return" {
1406 	RETURN_TOKEN_WITH_IDENT(T_RETURN);
1407 }
1408 
1409 <ST_IN_SCRIPTING>"#[" {
1410 	enter_nesting('[');
1411 	RETURN_TOKEN(T_ATTRIBUTE);
1412 }
1413 
1414 <ST_IN_SCRIPTING>"yield"{WHITESPACE_OR_COMMENTS}"from"[^a-zA-Z0-9_\x80-\xff] {
1415 	yyless(yyleng - 1);
1416 	HANDLE_NEWLINES(yytext, yyleng);
1417 	RETURN_TOKEN_WITH_IDENT(T_YIELD_FROM);
1418 }
1419 
1420 <ST_IN_SCRIPTING>"yield" {
1421 	RETURN_TOKEN_WITH_IDENT(T_YIELD);
1422 }
1423 
1424 <ST_IN_SCRIPTING>"try" {
1425 	RETURN_TOKEN_WITH_IDENT(T_TRY);
1426 }
1427 
1428 <ST_IN_SCRIPTING>"catch" {
1429 	RETURN_TOKEN_WITH_IDENT(T_CATCH);
1430 }
1431 
1432 <ST_IN_SCRIPTING>"finally" {
1433 	RETURN_TOKEN_WITH_IDENT(T_FINALLY);
1434 }
1435 
1436 <ST_IN_SCRIPTING>"throw" {
1437 	RETURN_TOKEN_WITH_IDENT(T_THROW);
1438 }
1439 
1440 <ST_IN_SCRIPTING>"if" {
1441 	RETURN_TOKEN_WITH_IDENT(T_IF);
1442 }
1443 
1444 <ST_IN_SCRIPTING>"elseif" {
1445 	RETURN_TOKEN_WITH_IDENT(T_ELSEIF);
1446 }
1447 
1448 <ST_IN_SCRIPTING>"endif" {
1449 	RETURN_TOKEN_WITH_IDENT(T_ENDIF);
1450 }
1451 
1452 <ST_IN_SCRIPTING>"else" {
1453 	RETURN_TOKEN_WITH_IDENT(T_ELSE);
1454 }
1455 
1456 <ST_IN_SCRIPTING>"while" {
1457 	RETURN_TOKEN_WITH_IDENT(T_WHILE);
1458 }
1459 
1460 <ST_IN_SCRIPTING>"endwhile" {
1461 	RETURN_TOKEN_WITH_IDENT(T_ENDWHILE);
1462 }
1463 
1464 <ST_IN_SCRIPTING>"do" {
1465 	RETURN_TOKEN_WITH_IDENT(T_DO);
1466 }
1467 
1468 <ST_IN_SCRIPTING>"for" {
1469 	RETURN_TOKEN_WITH_IDENT(T_FOR);
1470 }
1471 
1472 <ST_IN_SCRIPTING>"endfor" {
1473 	RETURN_TOKEN_WITH_IDENT(T_ENDFOR);
1474 }
1475 
1476 <ST_IN_SCRIPTING>"foreach" {
1477 	RETURN_TOKEN_WITH_IDENT(T_FOREACH);
1478 }
1479 
1480 <ST_IN_SCRIPTING>"endforeach" {
1481 	RETURN_TOKEN_WITH_IDENT(T_ENDFOREACH);
1482 }
1483 
1484 <ST_IN_SCRIPTING>"declare" {
1485 	RETURN_TOKEN_WITH_IDENT(T_DECLARE);
1486 }
1487 
1488 <ST_IN_SCRIPTING>"enddeclare" {
1489 	RETURN_TOKEN_WITH_IDENT(T_ENDDECLARE);
1490 }
1491 
1492 <ST_IN_SCRIPTING>"instanceof" {
1493 	RETURN_TOKEN_WITH_IDENT(T_INSTANCEOF);
1494 }
1495 
1496 <ST_IN_SCRIPTING>"as" {
1497 	RETURN_TOKEN_WITH_IDENT(T_AS);
1498 }
1499 
1500 <ST_IN_SCRIPTING>"switch" {
1501 	RETURN_TOKEN_WITH_IDENT(T_SWITCH);
1502 }
1503 
1504 <ST_IN_SCRIPTING>"match" {
1505 	RETURN_TOKEN_WITH_IDENT(T_MATCH);
1506 }
1507 
1508 <ST_IN_SCRIPTING>"endswitch" {
1509 	RETURN_TOKEN_WITH_IDENT(T_ENDSWITCH);
1510 }
1511 
1512 <ST_IN_SCRIPTING>"case" {
1513 	RETURN_TOKEN_WITH_IDENT(T_CASE);
1514 }
1515 
1516 <ST_IN_SCRIPTING>"default" {
1517 	RETURN_TOKEN_WITH_IDENT(T_DEFAULT);
1518 }
1519 
1520 <ST_IN_SCRIPTING>"break" {
1521 	RETURN_TOKEN_WITH_IDENT(T_BREAK);
1522 }
1523 
1524 <ST_IN_SCRIPTING>"continue" {
1525 	RETURN_TOKEN_WITH_IDENT(T_CONTINUE);
1526 }
1527 
1528 <ST_IN_SCRIPTING>"goto" {
1529 	RETURN_TOKEN_WITH_IDENT(T_GOTO);
1530 }
1531 
1532 <ST_IN_SCRIPTING>"echo" {
1533 	RETURN_TOKEN_WITH_IDENT(T_ECHO);
1534 }
1535 
1536 <ST_IN_SCRIPTING>"print" {
1537 	RETURN_TOKEN_WITH_IDENT(T_PRINT);
1538 }
1539 
1540 <ST_IN_SCRIPTING>"class" {
1541 	RETURN_TOKEN_WITH_IDENT(T_CLASS);
1542 }
1543 
1544 <ST_IN_SCRIPTING>"interface" {
1545 	RETURN_TOKEN_WITH_IDENT(T_INTERFACE);
1546 }
1547 
1548 <ST_IN_SCRIPTING>"trait" {
1549 	RETURN_TOKEN_WITH_IDENT(T_TRAIT);
1550 }
1551 
1552 /*
1553  * The enum keyword must be followed by whitespace and another identifier.
1554  * This avoids the BC break of using enum in classes, namespaces, functions and constants.
1555  */
1556 <ST_IN_SCRIPTING>"enum"{WHITESPACE_OR_COMMENTS}("extends"|"implements") {
1557 	yyless(4);
1558 	RETURN_TOKEN_WITH_STR(T_STRING, 0);
1559 }
1560 <ST_IN_SCRIPTING>"enum"{WHITESPACE_OR_COMMENTS}[a-zA-Z_\x80-\xff] {
1561 	yyless(4);
1562 	RETURN_TOKEN_WITH_IDENT(T_ENUM);
1563 }
1564 
1565 <ST_IN_SCRIPTING>"extends" {
1566 	RETURN_TOKEN_WITH_IDENT(T_EXTENDS);
1567 }
1568 
1569 <ST_IN_SCRIPTING>"implements" {
1570 	RETURN_TOKEN_WITH_IDENT(T_IMPLEMENTS);
1571 }
1572 
1573 <ST_IN_SCRIPTING>"->" {
1574 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
1575 	RETURN_TOKEN(T_OBJECT_OPERATOR);
1576 }
1577 
1578 <ST_IN_SCRIPTING>"?->" {
1579 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
1580 	RETURN_TOKEN(T_NULLSAFE_OBJECT_OPERATOR);
1581 }
1582 
1583 <ST_IN_SCRIPTING,ST_LOOKING_FOR_PROPERTY>{WHITESPACE}+ {
1584 	goto return_whitespace;
1585 }
1586 
1587 <ST_LOOKING_FOR_PROPERTY>"->" {
1588 	RETURN_TOKEN(T_OBJECT_OPERATOR);
1589 }
1590 
1591 <ST_LOOKING_FOR_PROPERTY>"?->" {
1592 	RETURN_TOKEN(T_NULLSAFE_OBJECT_OPERATOR);
1593 }
1594 
1595 <ST_LOOKING_FOR_PROPERTY>{LABEL} {
1596 	yy_pop_state();
1597 	RETURN_TOKEN_WITH_STR(T_STRING, 0);
1598 }
1599 
1600 <ST_IN_SCRIPTING>"::" {
1601 	RETURN_TOKEN(T_PAAMAYIM_NEKUDOTAYIM);
1602 }
1603 
1604 <ST_IN_SCRIPTING>"..." {
1605 	RETURN_TOKEN(T_ELLIPSIS);
1606 }
1607 
1608 <ST_IN_SCRIPTING>"??" {
1609 	RETURN_TOKEN(T_COALESCE);
1610 }
1611 
1612 <ST_IN_SCRIPTING>"new" {
1613 	RETURN_TOKEN_WITH_IDENT(T_NEW);
1614 }
1615 
1616 <ST_IN_SCRIPTING>"clone" {
1617 	RETURN_TOKEN_WITH_IDENT(T_CLONE);
1618 }
1619 
1620 <ST_IN_SCRIPTING>"var" {
1621 	RETURN_TOKEN_WITH_IDENT(T_VAR);
1622 }
1623 
1624 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"|"integer"){TABS_AND_SPACES}")" {
1625 	RETURN_TOKEN(T_INT_CAST);
1626 }
1627 
1628 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("double"|"float"){TABS_AND_SPACES}")" {
1629 	RETURN_TOKEN(T_DOUBLE_CAST);
1630 }
1631 
1632 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"real"{TABS_AND_SPACES}")" {
1633 	if (PARSER_MODE()) {
1634 		zend_throw_exception(zend_ce_parse_error, "The (real) cast has been removed, use (float) instead", 0);
1635 		RETURN_TOKEN(T_ERROR);
1636 	}
1637 	RETURN_TOKEN(T_DOUBLE_CAST);
1638 }
1639 
1640 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"|"binary"){TABS_AND_SPACES}")" {
1641 	RETURN_TOKEN(T_STRING_CAST);
1642 }
1643 
1644 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"array"{TABS_AND_SPACES}")" {
1645 	RETURN_TOKEN(T_ARRAY_CAST);
1646 }
1647 
1648 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"object"{TABS_AND_SPACES}")" {
1649 	RETURN_TOKEN(T_OBJECT_CAST);
1650 }
1651 
1652 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"|"boolean"){TABS_AND_SPACES}")" {
1653 	RETURN_TOKEN(T_BOOL_CAST);
1654 }
1655 
1656 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("unset"){TABS_AND_SPACES}")" {
1657 	RETURN_TOKEN(T_UNSET_CAST);
1658 }
1659 
1660 <ST_IN_SCRIPTING>"eval" {
1661 	RETURN_TOKEN_WITH_IDENT(T_EVAL);
1662 }
1663 
1664 <ST_IN_SCRIPTING>"include" {
1665 	RETURN_TOKEN_WITH_IDENT(T_INCLUDE);
1666 }
1667 
1668 <ST_IN_SCRIPTING>"include_once" {
1669 	RETURN_TOKEN_WITH_IDENT(T_INCLUDE_ONCE);
1670 }
1671 
1672 <ST_IN_SCRIPTING>"require" {
1673 	RETURN_TOKEN_WITH_IDENT(T_REQUIRE);
1674 }
1675 
1676 <ST_IN_SCRIPTING>"require_once" {
1677 	RETURN_TOKEN_WITH_IDENT(T_REQUIRE_ONCE);
1678 }
1679 
1680 <ST_IN_SCRIPTING>"namespace" {
1681 	RETURN_TOKEN_WITH_IDENT(T_NAMESPACE);
1682 }
1683 
1684 <ST_IN_SCRIPTING>"use" {
1685 	RETURN_TOKEN_WITH_IDENT(T_USE);
1686 }
1687 
1688 <ST_IN_SCRIPTING>"insteadof" {
1689     RETURN_TOKEN_WITH_IDENT(T_INSTEADOF);
1690 }
1691 
1692 <ST_IN_SCRIPTING>"global" {
1693 	RETURN_TOKEN_WITH_IDENT(T_GLOBAL);
1694 }
1695 
1696 <ST_IN_SCRIPTING>"isset" {
1697 	RETURN_TOKEN_WITH_IDENT(T_ISSET);
1698 }
1699 
1700 <ST_IN_SCRIPTING>"empty" {
1701 	RETURN_TOKEN_WITH_IDENT(T_EMPTY);
1702 }
1703 
1704 <ST_IN_SCRIPTING>"__halt_compiler" {
1705 	RETURN_TOKEN_WITH_IDENT(T_HALT_COMPILER);
1706 }
1707 
1708 <ST_IN_SCRIPTING>"static" {
1709 	RETURN_TOKEN_WITH_IDENT(T_STATIC);
1710 }
1711 
1712 <ST_IN_SCRIPTING>"abstract" {
1713 	RETURN_TOKEN_WITH_IDENT(T_ABSTRACT);
1714 }
1715 
1716 <ST_IN_SCRIPTING>"final" {
1717 	RETURN_TOKEN_WITH_IDENT(T_FINAL);
1718 }
1719 
1720 <ST_IN_SCRIPTING>"private" {
1721 	RETURN_TOKEN_WITH_IDENT(T_PRIVATE);
1722 }
1723 
1724 <ST_IN_SCRIPTING>"protected" {
1725 	RETURN_TOKEN_WITH_IDENT(T_PROTECTED);
1726 }
1727 
1728 <ST_IN_SCRIPTING>"public(set)" {
1729 	RETURN_TOKEN_WITH_IDENT(T_PUBLIC_SET);
1730 }
1731 
1732 <ST_IN_SCRIPTING>"protected(set)" {
1733 	RETURN_TOKEN_WITH_IDENT(T_PROTECTED_SET);
1734 }
1735 
1736 <ST_IN_SCRIPTING>"private(set)" {
1737 	RETURN_TOKEN_WITH_IDENT(T_PRIVATE_SET);
1738 }
1739 
1740 <ST_IN_SCRIPTING>"public" {
1741 	RETURN_TOKEN_WITH_IDENT(T_PUBLIC);
1742 }
1743 
1744 <ST_IN_SCRIPTING>"readonly" {
1745 	RETURN_TOKEN_WITH_IDENT(T_READONLY);
1746 }
1747 
1748 <ST_IN_SCRIPTING>"unset" {
1749 	RETURN_TOKEN_WITH_IDENT(T_UNSET);
1750 }
1751 
1752 <ST_IN_SCRIPTING>"=>" {
1753 	RETURN_TOKEN(T_DOUBLE_ARROW);
1754 }
1755 
1756 <ST_IN_SCRIPTING>"list" {
1757 	RETURN_TOKEN_WITH_IDENT(T_LIST);
1758 }
1759 
1760 <ST_IN_SCRIPTING>"array" {
1761 	RETURN_TOKEN_WITH_IDENT(T_ARRAY);
1762 }
1763 
1764 <ST_IN_SCRIPTING>"callable" {
1765 	RETURN_TOKEN_WITH_IDENT(T_CALLABLE);
1766 }
1767 
1768 <ST_IN_SCRIPTING>"++" {
1769 	RETURN_TOKEN(T_INC);
1770 }
1771 
1772 <ST_IN_SCRIPTING>"--" {
1773 	RETURN_TOKEN(T_DEC);
1774 }
1775 
1776 <ST_IN_SCRIPTING>"===" {
1777 	RETURN_TOKEN(T_IS_IDENTICAL);
1778 }
1779 
1780 <ST_IN_SCRIPTING>"!==" {
1781 	RETURN_TOKEN(T_IS_NOT_IDENTICAL);
1782 }
1783 
1784 <ST_IN_SCRIPTING>"==" {
1785 	RETURN_TOKEN(T_IS_EQUAL);
1786 }
1787 
1788 <ST_IN_SCRIPTING>"!="|"<>" {
1789 	RETURN_TOKEN(T_IS_NOT_EQUAL);
1790 }
1791 
1792 <ST_IN_SCRIPTING>"<=>" {
1793 	RETURN_TOKEN(T_SPACESHIP);
1794 }
1795 
1796 <ST_IN_SCRIPTING>"<=" {
1797 	RETURN_TOKEN(T_IS_SMALLER_OR_EQUAL);
1798 }
1799 
1800 <ST_IN_SCRIPTING>">=" {
1801 	RETURN_TOKEN(T_IS_GREATER_OR_EQUAL);
1802 }
1803 
1804 <ST_IN_SCRIPTING>"+=" {
1805 	RETURN_TOKEN(T_PLUS_EQUAL);
1806 }
1807 
1808 <ST_IN_SCRIPTING>"-=" {
1809 	RETURN_TOKEN(T_MINUS_EQUAL);
1810 }
1811 
1812 <ST_IN_SCRIPTING>"*=" {
1813 	RETURN_TOKEN(T_MUL_EQUAL);
1814 }
1815 
1816 <ST_IN_SCRIPTING>"*\*" {
1817 	RETURN_TOKEN(T_POW);
1818 }
1819 
1820 <ST_IN_SCRIPTING>"*\*=" {
1821 	RETURN_TOKEN(T_POW_EQUAL);
1822 }
1823 
1824 <ST_IN_SCRIPTING>"/=" {
1825 	RETURN_TOKEN(T_DIV_EQUAL);
1826 }
1827 
1828 <ST_IN_SCRIPTING>".=" {
1829 	RETURN_TOKEN(T_CONCAT_EQUAL);
1830 }
1831 
1832 <ST_IN_SCRIPTING>"%=" {
1833 	RETURN_TOKEN(T_MOD_EQUAL);
1834 }
1835 
1836 <ST_IN_SCRIPTING>"<<=" {
1837 	RETURN_TOKEN(T_SL_EQUAL);
1838 }
1839 
1840 <ST_IN_SCRIPTING>">>=" {
1841 	RETURN_TOKEN(T_SR_EQUAL);
1842 }
1843 
1844 <ST_IN_SCRIPTING>"&=" {
1845 	RETURN_TOKEN(T_AND_EQUAL);
1846 }
1847 
1848 <ST_IN_SCRIPTING>"|=" {
1849 	RETURN_TOKEN(T_OR_EQUAL);
1850 }
1851 
1852 <ST_IN_SCRIPTING>"^=" {
1853 	RETURN_TOKEN(T_XOR_EQUAL);
1854 }
1855 
1856 <ST_IN_SCRIPTING>"??=" {
1857 	RETURN_TOKEN(T_COALESCE_EQUAL);
1858 }
1859 
1860 <ST_IN_SCRIPTING>"||" {
1861 	RETURN_TOKEN(T_BOOLEAN_OR);
1862 }
1863 
1864 <ST_IN_SCRIPTING>"&&" {
1865 	RETURN_TOKEN(T_BOOLEAN_AND);
1866 }
1867 
1868 <ST_IN_SCRIPTING>"OR" {
1869 	RETURN_TOKEN_WITH_IDENT(T_LOGICAL_OR);
1870 }
1871 
1872 <ST_IN_SCRIPTING>"AND" {
1873 	RETURN_TOKEN_WITH_IDENT(T_LOGICAL_AND);
1874 }
1875 
1876 <ST_IN_SCRIPTING>"XOR" {
1877 	RETURN_TOKEN_WITH_IDENT(T_LOGICAL_XOR);
1878 }
1879 
1880 <ST_IN_SCRIPTING>"<<" {
1881 	RETURN_TOKEN(T_SL);
1882 }
1883 
1884 <ST_IN_SCRIPTING>">>" {
1885 	RETURN_TOKEN(T_SR);
1886 }
1887 
1888 <ST_IN_SCRIPTING>"&"{OPTIONAL_WHITESPACE_OR_COMMENTS}("$"|"...") {
1889 	yyless(1);
1890 	RETURN_TOKEN(T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG);
1891 }
1892 
1893 <ST_IN_SCRIPTING>"&" {
1894 	RETURN_TOKEN(T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG);
1895 }
1896 
1897 <ST_IN_SCRIPTING>"]"|")" {
1898 	/* Check that ] and ) match up properly with a preceding [ or ( */
1899 	RETURN_EXIT_NESTING_TOKEN(yytext[0]);
1900 }
1901 
1902 <ST_IN_SCRIPTING>"["|"(" {
1903 	enter_nesting(yytext[0]);
1904 	RETURN_TOKEN(yytext[0]);
1905 }
1906 
1907 <ST_IN_SCRIPTING>{TOKENS} {
1908 	RETURN_TOKEN(yytext[0]);
1909 }
1910 
1911 
1912 <ST_IN_SCRIPTING>"{" {
1913 	yy_push_state(ST_IN_SCRIPTING);
1914 	enter_nesting('{');
1915 	RETURN_TOKEN('{');
1916 }
1917 
1918 
1919 <ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"${" {
1920 	yy_push_state(ST_LOOKING_FOR_VARNAME);
1921 	enter_nesting('{');
1922 	RETURN_TOKEN(T_DOLLAR_OPEN_CURLY_BRACES);
1923 }
1924 
1925 <ST_IN_SCRIPTING>"}" {
1926 	RESET_DOC_COMMENT();
1927 	if (!zend_stack_is_empty(&SCNG(state_stack))) {
1928 		yy_pop_state();
1929 	}
1930 	RETURN_EXIT_NESTING_TOKEN('}');
1931 }
1932 
1933 
1934 <ST_LOOKING_FOR_VARNAME>{LABEL}[[}] {
1935 	yyless(yyleng - 1);
1936 	yy_pop_state();
1937 	yy_push_state(ST_IN_SCRIPTING);
1938 	RETURN_TOKEN_WITH_STR(T_STRING_VARNAME, 0);
1939 }
1940 
1941 
1942 <ST_LOOKING_FOR_VARNAME>{ANY_CHAR} {
1943 	yyless(0);
1944 	yy_pop_state();
1945 	yy_push_state(ST_IN_SCRIPTING);
1946 	goto restart;
1947 }
1948 
1949 <ST_IN_SCRIPTING>{BNUM} {
1950 	/* The +/- 2 skips "0b" */
1951 	size_t len = yyleng - 2;
1952 	char *end, *bin = yytext + 2;
1953 	bool contains_underscores;
1954 
1955 	/* Skip any leading 0s */
1956 	while (len > 0 && (*bin == '0' || *bin == '_')) {
1957 		++bin;
1958 		--len;
1959 	}
1960 
1961 	contains_underscores = (memchr(bin, '_', len) != NULL);
1962 
1963 	if (contains_underscores) {
1964 		bin = estrndup(bin, len);
1965 		strip_underscores(bin, &len);
1966 	}
1967 
1968 	if (len < SIZEOF_ZEND_LONG * 8) {
1969 		if (len == 0) {
1970 			ZVAL_LONG(zendlval, 0);
1971 		} else {
1972 			errno = 0;
1973 			ZVAL_LONG(zendlval, ZEND_STRTOL(bin, &end, 2));
1974 			ZEND_ASSERT(!errno && end == bin + len);
1975 		}
1976 		if (contains_underscores) {
1977 			efree(bin);
1978 		}
1979 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
1980 	} else {
1981 		ZVAL_DOUBLE(zendlval, zend_bin_strtod(bin, (const char **)&end));
1982 		/* errno isn't checked since we allow HUGE_VAL/INF overflow */
1983 		ZEND_ASSERT(end == bin + len);
1984 		if (contains_underscores) {
1985 			efree(bin);
1986 		}
1987 		RETURN_TOKEN_WITH_VAL(T_DNUMBER);
1988 	}
1989 }
1990 
1991 <ST_IN_SCRIPTING>{ONUM} {
1992 	/* The +/- 2 skips "0o" */
1993 	size_t len = yyleng - 2;
1994 	char *end, *octal = yytext + 2;
1995 	bool contains_underscores = (memchr(octal, '_', len) != NULL);
1996 
1997 	/* Skip any leading 0s */
1998 	while (len > 0 && (*octal == '0' || *octal == '_')) {
1999 		++octal;
2000 		--len;
2001 	}
2002 
2003 	if (len == 0) {
2004 		ZVAL_LONG(zendlval, 0);
2005 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2006 	}
2007 
2008 	if (contains_underscores) {
2009 		octal = estrndup(octal, len);
2010 		strip_underscores(octal, &len);
2011 	}
2012 
2013 	errno = 0;
2014 
2015 	ZVAL_LONG(zendlval, ZEND_STRTOL(octal, &end, 8));
2016 
2017 	ZEND_ASSERT(end == octal + len);
2018 
2019 	if (!errno) {
2020 		if (contains_underscores) {
2021 			efree(octal);
2022 		}
2023 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2024 	}
2025 
2026 	/* Overflow */
2027 	ZEND_ASSERT(errno == ERANGE);
2028 	/* Reset errno */
2029 	errno = 0;
2030 
2031 	/* zend_oct_strtod skips leading '0' */
2032 	ZVAL_DOUBLE(zendlval, zend_oct_strtod(octal, (const char **)&end));
2033 	ZEND_ASSERT(!errno);
2034 	ZEND_ASSERT(end == octal + len);
2035 	if (contains_underscores) {
2036 		efree(octal);
2037 	}
2038 	RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2039 }
2040 
2041 <ST_IN_SCRIPTING>{LNUM} {
2042 	size_t len = yyleng;
2043 	char *end, *lnum = yytext;
2044 	bool is_octal = lnum[0] == '0';
2045 	bool contains_underscores = (memchr(lnum, '_', len) != NULL);
2046 
2047 	if (contains_underscores) {
2048 		lnum = estrndup(lnum, len);
2049 		strip_underscores(lnum, &len);
2050 	}
2051 
2052 	/* Digits 8 and 9 are illegal in octal literals. */
2053 	if (is_octal) {
2054 		size_t i;
2055 		for (i = 0; i < len; i++) {
2056 			if (lnum[i] == '8' || lnum[i] == '9') {
2057 				zend_throw_exception(zend_ce_parse_error, "Invalid numeric literal", 0);
2058 				if (PARSER_MODE()) {
2059 					if (contains_underscores) {
2060 						efree(lnum);
2061 					}
2062 					ZVAL_UNDEF(zendlval);
2063 					RETURN_TOKEN(T_ERROR);
2064 				}
2065 
2066 				/* Continue in order to determine if this is T_LNUMBER or T_DNUMBER. */
2067 				len = i;
2068 				break;
2069 			}
2070 		}
2071 	}
2072 
2073 
2074 	if (len < MAX_LENGTH_OF_LONG - 1) { /* Won't overflow */
2075 		errno = 0;
2076 		/* base must be passed explicitly for correct parse error on Windows */
2077 		ZVAL_LONG(zendlval, ZEND_STRTOL(lnum, &end, is_octal ? 8 : 10));
2078 		ZEND_ASSERT(end == lnum + len);
2079 	} else {
2080 		errno = 0;
2081 		ZVAL_LONG(zendlval, ZEND_STRTOL(lnum, &end, 0));
2082 		if (errno == ERANGE) { /* Overflow */
2083 			errno = 0;
2084 			if (is_octal) { /* octal overflow */
2085 				ZVAL_DOUBLE(zendlval, zend_oct_strtod(lnum, (const char **)&end));
2086 			} else {
2087 				ZVAL_DOUBLE(zendlval, zend_strtod(lnum, (const char **)&end));
2088 			}
2089 			ZEND_ASSERT(end == lnum + len);
2090 			if (contains_underscores) {
2091 				efree(lnum);
2092 			}
2093 			RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2094 		}
2095 		ZEND_ASSERT(end == lnum + len);
2096 	}
2097 	ZEND_ASSERT(!errno);
2098 	if (contains_underscores) {
2099 		efree(lnum);
2100 	}
2101 	RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2102 }
2103 
2104 <ST_IN_SCRIPTING>{HNUM} {
2105 	/* The +/- 2 skips "0x" */
2106 	size_t len = yyleng - 2;
2107 	char *end, *hex = yytext + 2;
2108 	bool contains_underscores;
2109 
2110 	/* Skip any leading 0s */
2111 	while (len > 0 && (*hex == '0' || *hex == '_')) {
2112 		++hex;
2113 		--len;
2114 	}
2115 
2116 	contains_underscores = (memchr(hex, '_', len) != NULL);
2117 
2118 	if (contains_underscores) {
2119 		hex = estrndup(hex, len);
2120 		strip_underscores(hex, &len);
2121 	}
2122 
2123 	if (len < SIZEOF_ZEND_LONG * 2 || (len == SIZEOF_ZEND_LONG * 2 && *hex <= '7')) {
2124 		if (len == 0) {
2125 			ZVAL_LONG(zendlval, 0);
2126 		} else {
2127 			errno = 0;
2128 			ZVAL_LONG(zendlval, ZEND_STRTOL(hex, &end, 16));
2129 			ZEND_ASSERT(!errno && end == hex + len);
2130 		}
2131 		if (contains_underscores) {
2132 			efree(hex);
2133 		}
2134 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2135 	} else {
2136 		ZVAL_DOUBLE(zendlval, zend_hex_strtod(hex, (const char **)&end));
2137 		/* errno isn't checked since we allow HUGE_VAL/INF overflow */
2138 		ZEND_ASSERT(end == hex + len);
2139 		if (contains_underscores) {
2140 			efree(hex);
2141 		}
2142 		RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2143 	}
2144 }
2145 
2146 <ST_VAR_OFFSET>[0]|([1-9][0-9]*) { /* Offset could be treated as a long */
2147 	if (yyleng < MAX_LENGTH_OF_LONG - 1 || (yyleng == MAX_LENGTH_OF_LONG - 1 && strcmp(yytext, long_min_digits) < 0)) {
2148 		char *end;
2149 		errno = 0;
2150 		ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 10));
2151 		if (errno == ERANGE) {
2152 			goto string;
2153 		}
2154 		ZEND_ASSERT(end == yytext + yyleng);
2155 	} else {
2156 string:
2157 		ZVAL_STRINGL(zendlval, yytext, yyleng);
2158 	}
2159 	RETURN_TOKEN_WITH_VAL(T_NUM_STRING);
2160 }
2161 
2162 <ST_VAR_OFFSET>{LNUM}|{HNUM}|{BNUM}|{ONUM} { /* Offset must be treated as a string */
2163 	if (yyleng == 1) {
2164 		ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR((zend_uchar)*(yytext)));
2165 	} else {
2166 		ZVAL_STRINGL(zendlval, yytext, yyleng);
2167 	}
2168 	RETURN_TOKEN_WITH_VAL(T_NUM_STRING);
2169 }
2170 
2171 <ST_IN_SCRIPTING>{DNUM}|{EXPONENT_DNUM} {
2172 	const char *end;
2173 	size_t len = yyleng;
2174 	char *dnum = yytext;
2175 	bool contains_underscores = (memchr(dnum, '_', len) != NULL);
2176 
2177 	if (contains_underscores) {
2178 		dnum = estrndup(dnum, len);
2179 		strip_underscores(dnum, &len);
2180 	}
2181 
2182 	ZVAL_DOUBLE(zendlval, zend_strtod(dnum, &end));
2183 	/* errno isn't checked since we allow HUGE_VAL/INF overflow */
2184 	ZEND_ASSERT(end == dnum + len);
2185 	if (contains_underscores) {
2186 		efree(dnum);
2187 	}
2188 	RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2189 }
2190 
2191 <ST_IN_SCRIPTING>"__CLASS__" {
2192 	RETURN_TOKEN_WITH_IDENT(T_CLASS_C);
2193 }
2194 
2195 <ST_IN_SCRIPTING>"__TRAIT__" {
2196 	RETURN_TOKEN_WITH_IDENT(T_TRAIT_C);
2197 }
2198 
2199 <ST_IN_SCRIPTING>"__FUNCTION__" {
2200 	RETURN_TOKEN_WITH_IDENT(T_FUNC_C);
2201 }
2202 
2203 <ST_IN_SCRIPTING>"__PROPERTY__" {
2204 	RETURN_TOKEN_WITH_IDENT(T_PROPERTY_C);
2205 }
2206 
2207 <ST_IN_SCRIPTING>"__METHOD__" {
2208 	RETURN_TOKEN_WITH_IDENT(T_METHOD_C);
2209 }
2210 
2211 <ST_IN_SCRIPTING>"__LINE__" {
2212 	RETURN_TOKEN_WITH_IDENT(T_LINE);
2213 }
2214 
2215 <ST_IN_SCRIPTING>"__FILE__" {
2216 	RETURN_TOKEN_WITH_IDENT(T_FILE);
2217 }
2218 
2219 <ST_IN_SCRIPTING>"__DIR__" {
2220 	RETURN_TOKEN_WITH_IDENT(T_DIR);
2221 }
2222 
2223 <ST_IN_SCRIPTING>"__NAMESPACE__" {
2224 	RETURN_TOKEN_WITH_IDENT(T_NS_C);
2225 }
2226 
2227 <SHEBANG>"#!" .* {NEWLINE} {
2228 	CG(zend_lineno)++;
2229 	BEGIN(INITIAL);
2230 	goto restart;
2231 }
2232 
2233 <SHEBANG>{ANY_CHAR} {
2234 	yyless(0);
2235 	BEGIN(INITIAL);
2236 	goto restart;
2237 }
2238 
2239 <INITIAL>"<?=" {
2240 	BEGIN(ST_IN_SCRIPTING);
2241 	if (PARSER_MODE()) {
2242 		/* We'll reject this as an identifier in zend_lex_tstring. */
2243 		RETURN_TOKEN_WITH_IDENT(T_ECHO);
2244 	}
2245 	RETURN_TOKEN(T_OPEN_TAG_WITH_ECHO);
2246 }
2247 
2248 
2249 <INITIAL>"<?php"([ \t]|{NEWLINE}) {
2250 	HANDLE_NEWLINE(yytext[yyleng-1]);
2251 	BEGIN(ST_IN_SCRIPTING);
2252 	RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2253 }
2254 
2255 <INITIAL>"<?php" {
2256 	/* Allow <?php followed by end of file. */
2257 	if (YYCURSOR == YYLIMIT) {
2258 		BEGIN(ST_IN_SCRIPTING);
2259 		RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2260 	}
2261 	/* Degenerate case: <?phpX is interpreted as <? phpX with short tags. */
2262 	if (CG(short_tags)) {
2263 		yyless(2);
2264 		BEGIN(ST_IN_SCRIPTING);
2265 		RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2266 	}
2267 	goto inline_char_handler;
2268 }
2269 
2270 <INITIAL>"<?" {
2271 	if (CG(short_tags)) {
2272 		BEGIN(ST_IN_SCRIPTING);
2273 		RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2274 	} else {
2275 		goto inline_char_handler;
2276 	}
2277 }
2278 
2279 <INITIAL>{ANY_CHAR} {
2280 	if (YYCURSOR > YYLIMIT) {
2281 		RETURN_END_TOKEN;
2282 	}
2283 
2284 inline_char_handler:
2285 
2286 	while (1) {
2287 		YYCTYPE *ptr = memchr(YYCURSOR, '<', YYLIMIT - YYCURSOR);
2288 
2289 		YYCURSOR = ptr ? ptr + 1 : YYLIMIT;
2290 
2291 		if (YYCURSOR >= YYLIMIT) {
2292 			break;
2293 		}
2294 
2295 		if (*YYCURSOR == '?') {
2296 			if (CG(short_tags) /* <? */
2297 				|| (*(YYCURSOR + 1) == '=') /* <?= */
2298 				|| (!strncasecmp((char*)YYCURSOR + 1, "php", 3) && /* <?php[ \t\r\n] */
2299 					(YYCURSOR + 4 == YYLIMIT ||
2300 					YYCURSOR[4] == ' ' || YYCURSOR[4] == '\t' ||
2301 					YYCURSOR[4] == '\n' || YYCURSOR[4] == '\r'))
2302 			) {
2303 				YYCURSOR--;
2304 				break;
2305 			}
2306 		}
2307 	}
2308 
2309 	yyleng = YYCURSOR - SCNG(yy_text);
2310 
2311 	if (SCNG(output_filter)) {
2312 		size_t readsize;
2313 		char *s = NULL;
2314 		size_t sz = 0;
2315 		// TODO: avoid reallocation ???
2316 		readsize = SCNG(output_filter)((unsigned char **)&s, &sz, (unsigned char *)yytext, (size_t)yyleng);
2317 		ZVAL_STRINGL(zendlval, s, sz);
2318 		efree(s);
2319 		if (readsize < yyleng) {
2320 			yyless(readsize);
2321 		}
2322 	} else if (yyleng == 1) {
2323 		ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR((zend_uchar)*yytext));
2324 	} else {
2325 		ZVAL_STRINGL(zendlval, yytext, yyleng);
2326 	}
2327 	HANDLE_NEWLINES(yytext, yyleng);
2328 	RETURN_TOKEN_WITH_VAL(T_INLINE_HTML);
2329 }
2330 
2331 
2332 /* Make sure a label character follows "->" or "?->", otherwise there is no property
2333  * and "->"/"?->" will be taken literally
2334  */
2335 <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"->"[a-zA-Z_\x80-\xff] {
2336 	yyless(yyleng - 3);
2337 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
2338 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2339 }
2340 
2341 <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"?->"[a-zA-Z_\x80-\xff] {
2342 	yyless(yyleng - 4);
2343 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
2344 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2345 }
2346 
2347 /* A [ always designates a variable offset, regardless of what follows
2348  */
2349 <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"[" {
2350 	yyless(yyleng - 1);
2351 	yy_push_state(ST_VAR_OFFSET);
2352 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2353 }
2354 
2355 <ST_IN_SCRIPTING,ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE,ST_VAR_OFFSET>"$"{LABEL} {
2356 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2357 }
2358 
2359 <ST_VAR_OFFSET>"]" {
2360 	yy_pop_state();
2361 	RETURN_TOKEN(']');
2362 }
2363 
2364 <ST_VAR_OFFSET>{TOKENS}|[[(){}"`] {
2365 	/* Only '[' or '-' can be valid, but returning other tokens will allow a more explicit parse error */
2366 	RETURN_TOKEN(yytext[0]);
2367 }
2368 
2369 <ST_VAR_OFFSET>[ \n\r\t\\'#] {
2370 	/* Invalid rule to return a more explicit parse error with proper line number */
2371 	yyless(0);
2372 	yy_pop_state();
2373 	ZVAL_NULL(zendlval);
2374 	RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2375 }
2376 
2377 <ST_IN_SCRIPTING>"namespace"("\\"{LABEL})+ {
2378 	RETURN_TOKEN_WITH_STR(T_NAME_RELATIVE, sizeof("namespace\\") - 1);
2379 }
2380 
2381 <ST_IN_SCRIPTING>{LABEL}("\\"{LABEL})+ {
2382 	RETURN_TOKEN_WITH_STR(T_NAME_QUALIFIED, 0);
2383 }
2384 
2385 <ST_IN_SCRIPTING>"\\"{LABEL}("\\"{LABEL})* {
2386 	RETURN_TOKEN_WITH_STR(T_NAME_FULLY_QUALIFIED, 1);
2387 }
2388 
2389 <ST_IN_SCRIPTING>"\\" {
2390 	RETURN_TOKEN(T_NS_SEPARATOR);
2391 }
2392 
2393 <ST_IN_SCRIPTING,ST_VAR_OFFSET>{LABEL} {
2394 	RETURN_TOKEN_WITH_STR(T_STRING, 0);
2395 }
2396 
2397 
2398 <ST_IN_SCRIPTING,ST_LOOKING_FOR_PROPERTY>"#"|"//" {
2399 	while (YYCURSOR < YYLIMIT) {
2400 		switch (*YYCURSOR++) {
2401 			case '\r':
2402 			case '\n':
2403 				YYCURSOR--;
2404 				break;
2405 			case '?':
2406 				if (*YYCURSOR == '>') {
2407 					YYCURSOR--;
2408 					break;
2409 				}
2410 				ZEND_FALLTHROUGH;
2411 			default:
2412 				continue;
2413 		}
2414 
2415 		break;
2416 	}
2417 
2418 	yyleng = YYCURSOR - SCNG(yy_text);
2419 	RETURN_OR_SKIP_TOKEN(T_COMMENT);
2420 }
2421 
2422 <ST_IN_SCRIPTING,ST_LOOKING_FOR_PROPERTY>"/*"|"/**"{WHITESPACE} {
2423 	int doc_com;
2424 
2425 	if (yyleng > 2) {
2426 		doc_com = 1;
2427 		RESET_DOC_COMMENT();
2428 	} else {
2429 		doc_com = 0;
2430 	}
2431 
2432 	while (YYCURSOR < YYLIMIT) {
2433 		if (*YYCURSOR++ == '*' && *YYCURSOR == '/') {
2434 			break;
2435 		}
2436 	}
2437 
2438 	if (YYCURSOR < YYLIMIT) {
2439 		YYCURSOR++;
2440 	} else {
2441 		zend_throw_exception_ex(zend_ce_parse_error, 0, "Unterminated comment starting line %d", CG(zend_lineno));
2442 		if (PARSER_MODE()) {
2443 			RETURN_TOKEN(T_ERROR);
2444 		}
2445 	}
2446 
2447 	yyleng = YYCURSOR - SCNG(yy_text);
2448 	HANDLE_NEWLINES(yytext, yyleng);
2449 
2450 	if (doc_com) {
2451 		CG(doc_comment) = zend_string_init(yytext, yyleng, 0);
2452 		RETURN_OR_SKIP_TOKEN(T_DOC_COMMENT);
2453 	}
2454 
2455 	RETURN_OR_SKIP_TOKEN(T_COMMENT);
2456 }
2457 
2458 <ST_LOOKING_FOR_PROPERTY>{ANY_CHAR} {
2459 	yyless(0);
2460 	yy_pop_state();
2461 	goto restart;
2462 }
2463 
2464 <ST_IN_SCRIPTING>"?>"{NEWLINE}? {
2465 	BEGIN(INITIAL);
2466 	if (yytext[yyleng-1] != '>') {
2467 		CG(increment_lineno) = 1;
2468 	}
2469 	if (PARSER_MODE()) {
2470 		RETURN_TOKEN(';');  /* implicit ';' at php-end tag */
2471 	}
2472 	RETURN_TOKEN(T_CLOSE_TAG);
2473 }
2474 
2475 
2476 <ST_IN_SCRIPTING>b?['] {
2477 	char *s, *t;
2478 	char *end;
2479 	int bprefix = (yytext[0] != '\'') ? 1 : 0;
2480 
2481 	while (1) {
2482 		if (YYCURSOR < YYLIMIT) {
2483 			if (*YYCURSOR == '\'') {
2484 				YYCURSOR++;
2485 				yyleng = YYCURSOR - SCNG(yy_text);
2486 
2487 				break;
2488 			} else if (*YYCURSOR++ == '\\' && YYCURSOR < YYLIMIT) {
2489 				YYCURSOR++;
2490 			}
2491 		} else {
2492 			yyleng = YYLIMIT - SCNG(yy_text);
2493 
2494 			/* Unclosed single quotes; treat similar to double quotes, but without a separate token
2495 			 * for ' (unrecognized by parser), instead of old flex fallback to "Unexpected character..."
2496 			 * rule, which continued in ST_IN_SCRIPTING state after the quote */
2497 			ZVAL_NULL(zendlval);
2498 			RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2499 		}
2500 	}
2501 
2502 	if (yyleng-bprefix-2 <= 1) {
2503 		if (yyleng-bprefix-2 < 1) {
2504 			ZVAL_EMPTY_STRING(zendlval);
2505 		} else {
2506 			zend_uchar c = (zend_uchar)*(yytext+bprefix+1);
2507 			if (c == '\n' || c == '\r') {
2508 				CG(zend_lineno)++;
2509 			}
2510 			ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR(c));
2511 		}
2512 		goto skip_escape_conversion;
2513 	}
2514 	ZVAL_STRINGL(zendlval, yytext+bprefix+1, yyleng-bprefix-2);
2515 
2516 	/* convert escape sequences */
2517 	s = Z_STRVAL_P(zendlval);
2518 	end = s+Z_STRLEN_P(zendlval);
2519 	while (1) {
2520 		if (UNEXPECTED(*s=='\\')) {
2521 			break;
2522 		}
2523 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
2524 			CG(zend_lineno)++;
2525 		}
2526 		s++;
2527 		if (s == end) {
2528 			goto skip_escape_conversion;
2529 		}
2530 	}
2531 
2532 	t = s;
2533 	while (s<end) {
2534 		if (*s=='\\') {
2535 			s++;
2536 			if (*s == '\\' || *s == '\'') {
2537 				*t++ = *s;
2538 			} else {
2539 				*t++ = '\\';
2540 				*t++ = *s;
2541 			}
2542 		} else {
2543 			*t++ = *s;
2544 		}
2545 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
2546 			CG(zend_lineno)++;
2547 		}
2548 		s++;
2549 	}
2550 	*t = 0;
2551 	Z_STRLEN_P(zendlval) = t - Z_STRVAL_P(zendlval);
2552 
2553 skip_escape_conversion:
2554 	if (SCNG(output_filter)) {
2555 		size_t sz = 0;
2556 		char *str = NULL;
2557 		zend_string *new_str;
2558 		s = Z_STRVAL_P(zendlval);
2559 		// TODO: avoid reallocation ???
2560 		SCNG(output_filter)((unsigned char **)&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
2561 		new_str = zend_string_init(str, sz, 0);
2562 		if (str != s) {
2563 			efree(str);
2564 		}
2565 		zend_string_release_ex(Z_STR_P(zendlval), 0);
2566 		ZVAL_STR(zendlval, new_str);
2567 	}
2568 	RETURN_TOKEN_WITH_VAL(T_CONSTANT_ENCAPSED_STRING);
2569 }
2570 
2571 
2572 <ST_IN_SCRIPTING>b?["] {
2573 	int bprefix = (yytext[0] != '"') ? 1 : 0;
2574 
2575 	while (YYCURSOR < YYLIMIT) {
2576 		switch (*YYCURSOR++) {
2577 			case '"':
2578 				yyleng = YYCURSOR - SCNG(yy_text);
2579 				if (EXPECTED(zend_scan_escape_string(zendlval, yytext+bprefix+1, yyleng-bprefix-2, '"') == SUCCESS)
2580 				 || !PARSER_MODE()) {
2581 					RETURN_TOKEN_WITH_VAL(T_CONSTANT_ENCAPSED_STRING);
2582 				} else {
2583 					RETURN_TOKEN(T_ERROR);
2584 				}
2585 			case '$':
2586 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2587 					break;
2588 				}
2589 				continue;
2590 			case '{':
2591 				if (*YYCURSOR == '$') {
2592 					break;
2593 				}
2594 				continue;
2595 			case '\\':
2596 				if (YYCURSOR < YYLIMIT) {
2597 					YYCURSOR++;
2598 				}
2599 				ZEND_FALLTHROUGH;
2600 			default:
2601 				continue;
2602 		}
2603 
2604 		YYCURSOR--;
2605 		break;
2606 	}
2607 
2608 	/* Remember how much was scanned to save rescanning */
2609 	SET_DOUBLE_QUOTES_SCANNED_LENGTH(YYCURSOR - SCNG(yy_text) - yyleng);
2610 
2611 	YYCURSOR = SCNG(yy_text) + yyleng;
2612 
2613 	BEGIN(ST_DOUBLE_QUOTES);
2614 	RETURN_TOKEN('"');
2615 }
2616 
2617 
2618 <ST_IN_SCRIPTING>b?"<<<"{TABS_AND_SPACES}({LABEL}|([']{LABEL}['])|(["]{LABEL}["])){NEWLINE} {
2619 	char *s;
2620 	unsigned char *saved_cursor;
2621 	int bprefix = (yytext[0] != '<') ? 1 : 0, spacing = 0, indentation = 0;
2622 	zend_heredoc_label *heredoc_label = emalloc(sizeof(zend_heredoc_label));
2623 	bool is_heredoc = 1;
2624 
2625 	CG(zend_lineno)++;
2626 	heredoc_label->length = yyleng-bprefix-3-1-(yytext[yyleng-2]=='\r'?1:0);
2627 	s = yytext+bprefix+3;
2628 	while ((*s == ' ') || (*s == '\t')) {
2629 		s++;
2630 		heredoc_label->length--;
2631 	}
2632 
2633 	if (*s == '\'') {
2634 		s++;
2635 		heredoc_label->length -= 2;
2636 		is_heredoc = 0;
2637 
2638 		BEGIN(ST_NOWDOC);
2639 	} else {
2640 		if (*s == '"') {
2641 			s++;
2642 			heredoc_label->length -= 2;
2643 		}
2644 
2645 		BEGIN(ST_HEREDOC);
2646 	}
2647 
2648 	heredoc_label->label = estrndup(s, heredoc_label->length);
2649 	heredoc_label->indentation_uses_spaces = 0;
2650 	heredoc_label->indentation = 0;
2651 	saved_cursor = YYCURSOR;
2652 
2653 	zend_ptr_stack_push(&SCNG(heredoc_label_stack), (void *) heredoc_label);
2654 
2655 	while (YYCURSOR < YYLIMIT && (*YYCURSOR == ' ' || *YYCURSOR == '\t')) {
2656 		if (*YYCURSOR == '\t') {
2657 			spacing |= HEREDOC_USING_TABS;
2658 		} else {
2659 			spacing |= HEREDOC_USING_SPACES;
2660 		}
2661 		++YYCURSOR;
2662 		++indentation;
2663 	}
2664 
2665 	if (YYCURSOR == YYLIMIT) {
2666 		YYCURSOR = saved_cursor;
2667 		RETURN_TOKEN(T_START_HEREDOC);
2668 	}
2669 
2670 	/* Check for ending label on the next line */
2671 	if (heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, s, heredoc_label->length)) {
2672 		if (!IS_LABEL_SUCCESSOR(YYCURSOR[heredoc_label->length])) {
2673 			if (spacing == (HEREDOC_USING_SPACES | HEREDOC_USING_TABS)) {
2674 				zend_throw_exception(zend_ce_parse_error, "Invalid indentation - tabs and spaces cannot be mixed", 0);
2675 				if (PARSER_MODE()) {
2676 					RETURN_TOKEN(T_ERROR);
2677 				}
2678 			}
2679 
2680 			YYCURSOR = saved_cursor;
2681 			heredoc_label->indentation = indentation;
2682 
2683 			BEGIN(ST_END_HEREDOC);
2684 			RETURN_TOKEN(T_START_HEREDOC);
2685 		}
2686 	}
2687 
2688 	YYCURSOR = saved_cursor;
2689 
2690 	if (is_heredoc && !SCNG(heredoc_scan_ahead)) {
2691 		zend_lex_state current_state;
2692 		zend_string *saved_doc_comment = CG(doc_comment);
2693 		int heredoc_nesting_level = 1;
2694 		int first_token = 0;
2695 		int error = 0;
2696 
2697 		zend_save_lexical_state(&current_state);
2698 
2699 		SCNG(heredoc_scan_ahead) = 1;
2700 		SCNG(heredoc_indentation) = 0;
2701 		SCNG(heredoc_indentation_uses_spaces) = 0;
2702 		LANG_SCNG(on_event) = NULL;
2703 		CG(doc_comment) = NULL;
2704 
2705 		zend_ptr_stack_reverse_apply(&current_state.heredoc_label_stack, copy_heredoc_label_stack);
2706 
2707 		zend_exception_save();
2708 		while (heredoc_nesting_level) {
2709 			zval zv;
2710 			int retval;
2711 
2712 			ZVAL_UNDEF(&zv);
2713 			retval = lex_scan(&zv, NULL);
2714 			zval_ptr_dtor_nogc(&zv);
2715 
2716 			if (EG(exception)) {
2717 				zend_clear_exception();
2718 				break;
2719 			}
2720 
2721 			if (!first_token) {
2722 				first_token = retval;
2723 			}
2724 
2725 			switch (retval) {
2726 				case T_START_HEREDOC:
2727 					++heredoc_nesting_level;
2728 					break;
2729 				case T_END_HEREDOC:
2730 					--heredoc_nesting_level;
2731 					break;
2732 				case END:
2733 					heredoc_nesting_level = 0;
2734 			}
2735 		}
2736 		zend_exception_restore();
2737 
2738 		if (
2739 		    (first_token == T_VARIABLE
2740 		     || first_token == T_DOLLAR_OPEN_CURLY_BRACES
2741 		     || first_token == T_CURLY_OPEN
2742 		    ) && SCNG(heredoc_indentation)) {
2743 			zend_throw_exception_ex(zend_ce_parse_error, 0, "Invalid body indentation level (expecting an indentation level of at least %d)", SCNG(heredoc_indentation));
2744 			error = 1;
2745 		}
2746 
2747 		heredoc_label->indentation = SCNG(heredoc_indentation);
2748 		heredoc_label->indentation_uses_spaces = SCNG(heredoc_indentation_uses_spaces);
2749 
2750 		zend_restore_lexical_state(&current_state);
2751 		SCNG(heredoc_scan_ahead) = 0;
2752 		CG(increment_lineno) = 0;
2753 		CG(doc_comment) = saved_doc_comment;
2754 
2755 		if (PARSER_MODE() && error) {
2756 			RETURN_TOKEN(T_ERROR);
2757 		}
2758 	}
2759 
2760 	RETURN_TOKEN(T_START_HEREDOC);
2761 }
2762 
2763 
2764 <ST_IN_SCRIPTING>[`] {
2765 	BEGIN(ST_BACKQUOTE);
2766 	RETURN_TOKEN('`');
2767 }
2768 
2769 
2770 <ST_END_HEREDOC>{ANY_CHAR} {
2771 	zend_heredoc_label *heredoc_label = zend_ptr_stack_pop(&SCNG(heredoc_label_stack));
2772 
2773 	yyleng = heredoc_label->indentation + heredoc_label->length;
2774 	YYCURSOR += yyleng - 1;
2775 
2776 	heredoc_label_dtor(heredoc_label);
2777 	efree(heredoc_label);
2778 
2779 	BEGIN(ST_IN_SCRIPTING);
2780 	RETURN_TOKEN(T_END_HEREDOC);
2781 }
2782 
2783 
2784 <ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"{$" {
2785 	yy_push_state(ST_IN_SCRIPTING);
2786 	yyless(1);
2787 	enter_nesting('{');
2788 	RETURN_TOKEN(T_CURLY_OPEN);
2789 }
2790 
2791 
2792 <ST_DOUBLE_QUOTES>["] {
2793 	BEGIN(ST_IN_SCRIPTING);
2794 	RETURN_TOKEN('"');
2795 }
2796 
2797 <ST_BACKQUOTE>[`] {
2798 	BEGIN(ST_IN_SCRIPTING);
2799 	RETURN_TOKEN('`');
2800 }
2801 
2802 
2803 <ST_DOUBLE_QUOTES>{ANY_CHAR} {
2804 	if (GET_DOUBLE_QUOTES_SCANNED_LENGTH()) {
2805 		YYCURSOR += GET_DOUBLE_QUOTES_SCANNED_LENGTH() - 1;
2806 		SET_DOUBLE_QUOTES_SCANNED_LENGTH(0);
2807 
2808 		goto double_quotes_scan_done;
2809 	}
2810 
2811 	if (YYCURSOR > YYLIMIT) {
2812 		RETURN_END_TOKEN;
2813 	}
2814 	if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2815 		YYCURSOR++;
2816 	}
2817 
2818 	while (YYCURSOR < YYLIMIT) {
2819 		switch (*YYCURSOR++) {
2820 			case '"':
2821 				break;
2822 			case '$':
2823 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2824 					break;
2825 				}
2826 				continue;
2827 			case '{':
2828 				if (*YYCURSOR == '$') {
2829 					break;
2830 				}
2831 				continue;
2832 			case '\\':
2833 				if (YYCURSOR < YYLIMIT) {
2834 					YYCURSOR++;
2835 				}
2836 				ZEND_FALLTHROUGH;
2837 			default:
2838 				continue;
2839 		}
2840 
2841 		YYCURSOR--;
2842 		break;
2843 	}
2844 
2845 double_quotes_scan_done:
2846 	yyleng = YYCURSOR - SCNG(yy_text);
2847 
2848 	if (EXPECTED(zend_scan_escape_string(zendlval, yytext, yyleng, '"') == SUCCESS)
2849 	 || !PARSER_MODE()) {
2850 		RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2851 	} else {
2852 		RETURN_TOKEN(T_ERROR);
2853 	}
2854 }
2855 
2856 
2857 <ST_BACKQUOTE>{ANY_CHAR} {
2858 	if (YYCURSOR > YYLIMIT) {
2859 		RETURN_END_TOKEN;
2860 	}
2861 	if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2862 		YYCURSOR++;
2863 	}
2864 
2865 	while (YYCURSOR < YYLIMIT) {
2866 		switch (*YYCURSOR++) {
2867 			case '`':
2868 				break;
2869 			case '$':
2870 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2871 					break;
2872 				}
2873 				continue;
2874 			case '{':
2875 				if (*YYCURSOR == '$') {
2876 					break;
2877 				}
2878 				continue;
2879 			case '\\':
2880 				if (YYCURSOR < YYLIMIT) {
2881 					YYCURSOR++;
2882 				}
2883 				ZEND_FALLTHROUGH;
2884 			default:
2885 				continue;
2886 		}
2887 
2888 		YYCURSOR--;
2889 		break;
2890 	}
2891 
2892 	yyleng = YYCURSOR - SCNG(yy_text);
2893 
2894 	if (EXPECTED(zend_scan_escape_string(zendlval, yytext, yyleng, '`') == SUCCESS)
2895 	 || !PARSER_MODE()) {
2896 		RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2897 	} else {
2898 		RETURN_TOKEN(T_ERROR);
2899 	}
2900 }
2901 
2902 
2903 <ST_HEREDOC>{ANY_CHAR} {
2904 	zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2905 	int newline = 0, indentation = 0, spacing = 0;
2906 
2907 	if (YYCURSOR > YYLIMIT) {
2908 		RETURN_END_TOKEN;
2909 	}
2910 
2911 	YYCURSOR--;
2912 
2913 	while (YYCURSOR < YYLIMIT) {
2914 		switch (*YYCURSOR++) {
2915 			case '\r':
2916 				if (*YYCURSOR == '\n') {
2917 					YYCURSOR++;
2918 				}
2919 				ZEND_FALLTHROUGH;
2920 			case '\n':
2921 				indentation = spacing = 0;
2922 
2923 				while (YYCURSOR < YYLIMIT && (*YYCURSOR == ' ' || *YYCURSOR == '\t')) {
2924 					if (*YYCURSOR == '\t') {
2925 						spacing |= HEREDOC_USING_TABS;
2926 					} else {
2927 						spacing |= HEREDOC_USING_SPACES;
2928 					}
2929 					++YYCURSOR;
2930 					++indentation;
2931 				}
2932 
2933 				if (YYCURSOR == YYLIMIT) {
2934 					yyleng = YYCURSOR - SCNG(yy_text);
2935 					HANDLE_NEWLINES(yytext, yyleng);
2936 					ZVAL_NULL(zendlval);
2937 					RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2938 				}
2939 
2940 				/* Check for ending label on the next line */
2941 				if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
2942 					if (IS_LABEL_SUCCESSOR(YYCURSOR[heredoc_label->length])) {
2943 						continue;
2944 					}
2945 
2946 					if (spacing == (HEREDOC_USING_SPACES | HEREDOC_USING_TABS)) {
2947 						zend_throw_exception(zend_ce_parse_error, "Invalid indentation - tabs and spaces cannot be mixed", 0);
2948 						if (PARSER_MODE()) {
2949 							RETURN_TOKEN(T_ERROR);
2950 						}
2951 					}
2952 
2953 					/* newline before label will be subtracted from returned text, but
2954 					 * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
2955 					if (YYCURSOR[-indentation - 2] == '\r' && YYCURSOR[-indentation - 1] == '\n') {
2956 						newline = 2; /* Windows newline */
2957 					} else {
2958 						newline = 1;
2959 					}
2960 
2961 					CG(increment_lineno) = 1; /* For newline before label */
2962 
2963 					if (SCNG(heredoc_scan_ahead)) {
2964 						SCNG(heredoc_indentation) = indentation;
2965 						SCNG(heredoc_indentation_uses_spaces) = (spacing == HEREDOC_USING_SPACES);
2966 					} else {
2967 						YYCURSOR -= indentation;
2968 					}
2969 
2970 					BEGIN(ST_END_HEREDOC);
2971 
2972 					goto heredoc_scan_done;
2973 				}
2974 				continue;
2975 			case '$':
2976 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2977 					break;
2978 				}
2979 				continue;
2980 			case '{':
2981 				if (*YYCURSOR == '$') {
2982 					break;
2983 				}
2984 				continue;
2985 			case '\\':
2986 				if (YYCURSOR < YYLIMIT && *YYCURSOR != '\n' && *YYCURSOR != '\r') {
2987 					YYCURSOR++;
2988 				}
2989 				ZEND_FALLTHROUGH;
2990 			default:
2991 				continue;
2992 		}
2993 
2994 		YYCURSOR--;
2995 		break;
2996 	}
2997 
2998 heredoc_scan_done:
2999 
3000 	yyleng = YYCURSOR - SCNG(yy_text);
3001 	ZVAL_STRINGL(zendlval, yytext, yyleng - newline);
3002 
3003 	if (!SCNG(heredoc_scan_ahead) && !EG(exception) && PARSER_MODE()) {
3004 		bool newline_at_start = *(yytext - 1) == '\n' || *(yytext - 1) == '\r';
3005 		zend_string *copy = Z_STR_P(zendlval);
3006 
3007 		if (!strip_multiline_string_indentation(
3008 				zendlval, heredoc_label->indentation, heredoc_label->indentation_uses_spaces,
3009 				newline_at_start, newline != 0)) {
3010 			RETURN_TOKEN(T_ERROR);
3011 		}
3012 
3013 		if (UNEXPECTED(zend_scan_escape_string(zendlval, ZSTR_VAL(copy), ZSTR_LEN(copy), 0) != SUCCESS)) {
3014 			zend_string_efree(copy);
3015 			RETURN_TOKEN(T_ERROR);
3016 		}
3017 
3018 		zend_string_efree(copy);
3019 	} else {
3020 		HANDLE_NEWLINES(yytext, yyleng - newline);
3021 	}
3022 
3023 	RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
3024 }
3025 
3026 
3027 <ST_NOWDOC>{ANY_CHAR} {
3028 	zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
3029 	int newline = 0, indentation = 0, spacing = -1;
3030 
3031 	if (YYCURSOR > YYLIMIT) {
3032 		RETURN_END_TOKEN;
3033 	}
3034 
3035 	YYCURSOR--;
3036 
3037 	while (YYCURSOR < YYLIMIT) {
3038 		switch (*YYCURSOR++) {
3039 			case '\r':
3040 				if (*YYCURSOR == '\n') {
3041 					YYCURSOR++;
3042 				}
3043 				ZEND_FALLTHROUGH;
3044 			case '\n':
3045 				indentation = spacing = 0;
3046 
3047 				while (YYCURSOR < YYLIMIT && (*YYCURSOR == ' ' || *YYCURSOR == '\t')) {
3048 					if (*YYCURSOR == '\t') {
3049 						spacing |= HEREDOC_USING_TABS;
3050 					} else {
3051 						spacing |= HEREDOC_USING_SPACES;
3052 					}
3053 					++YYCURSOR;
3054 					++indentation;
3055 				}
3056 
3057 				if (YYCURSOR == YYLIMIT) {
3058 					yyleng = YYCURSOR - SCNG(yy_text);
3059 					HANDLE_NEWLINES(yytext, yyleng);
3060 					ZVAL_NULL(zendlval);
3061 					RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
3062 				}
3063 
3064 				/* Check for ending label on the next line */
3065 				if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
3066 					if (IS_LABEL_SUCCESSOR(YYCURSOR[heredoc_label->length])) {
3067 						continue;
3068 					}
3069 
3070 					if (spacing == (HEREDOC_USING_SPACES | HEREDOC_USING_TABS)) {
3071 						zend_throw_exception(zend_ce_parse_error, "Invalid indentation - tabs and spaces cannot be mixed", 0);
3072 						if (PARSER_MODE()) {
3073 							RETURN_TOKEN(T_ERROR);
3074 						}
3075 					}
3076 
3077 					/* newline before label will be subtracted from returned text, but
3078 					 * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
3079 					if (YYCURSOR[-indentation - 2] == '\r' && YYCURSOR[-indentation - 1] == '\n') {
3080 						newline = 2; /* Windows newline */
3081 					} else {
3082 						newline = 1;
3083 					}
3084 
3085 					CG(increment_lineno) = 1; /* For newline before label */
3086 
3087 					YYCURSOR -= indentation;
3088 					heredoc_label->indentation = indentation;
3089 
3090 					BEGIN(ST_END_HEREDOC);
3091 
3092 					goto nowdoc_scan_done;
3093 				}
3094 				ZEND_FALLTHROUGH;
3095 			default:
3096 				continue;
3097 		}
3098 	}
3099 
3100 nowdoc_scan_done:
3101 	yyleng = YYCURSOR - SCNG(yy_text);
3102 	ZVAL_STRINGL(zendlval, yytext, yyleng - newline);
3103 
3104 	if (!EG(exception) && spacing != -1 && PARSER_MODE()) {
3105 		bool newline_at_start = *(yytext - 1) == '\n' || *(yytext - 1) == '\r';
3106 		if (!strip_multiline_string_indentation(
3107 				zendlval, indentation, spacing == HEREDOC_USING_SPACES,
3108 				newline_at_start, newline != 0)) {
3109 			RETURN_TOKEN(T_ERROR);
3110 		}
3111 	}
3112 
3113 	HANDLE_NEWLINES(yytext, yyleng - newline);
3114 	RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
3115 }
3116 
3117 
3118 <ST_IN_SCRIPTING,ST_VAR_OFFSET>{ANY_CHAR} {
3119 	if (YYCURSOR > YYLIMIT) {
3120 		RETURN_END_TOKEN;
3121 	}
3122 
3123 	RETURN_TOKEN(T_BAD_CHARACTER);
3124 }
3125 
3126 */
3127 
3128 emit_token_with_str:
3129 	zend_copy_value(zendlval, (yytext + offset), (yyleng - offset));
3130 
3131 emit_token_with_val:
3132 	if (PARSER_MODE()) {
3133 		ZEND_ASSERT(Z_TYPE_P(zendlval) != IS_UNDEF);
3134 		elem->ast = zend_ast_create_zval_with_lineno(zendlval, start_line);
3135 	}
3136 
3137 emit_token:
3138 	if (SCNG(on_event)) {
3139 		SCNG(on_event)(ON_TOKEN, token, start_line, yytext, yyleng, SCNG(on_event_context));
3140 	}
3141 	return token;
3142 
3143 emit_token_with_ident:
3144 	if (PARSER_MODE()) {
3145 		elem->ident = SCNG(yy_text);
3146 	}
3147 	if (SCNG(on_event)) {
3148 		SCNG(on_event)(ON_TOKEN, token, start_line, yytext, yyleng, SCNG(on_event_context));
3149 	}
3150 	return token;
3151 
3152 return_whitespace:
3153 	HANDLE_NEWLINES(yytext, yyleng);
3154 	if (SCNG(on_event)) {
3155 		SCNG(on_event)(ON_TOKEN, T_WHITESPACE, start_line, yytext, yyleng, SCNG(on_event_context));
3156 	}
3157 	if (PARSER_MODE()) {
3158 		start_line = CG(zend_lineno);
3159 		goto restart;
3160 	} else {
3161 		return T_WHITESPACE;
3162 	}
3163 
3164 skip_token:
3165 	if (SCNG(on_event)) {
3166 		SCNG(on_event)(ON_TOKEN, token, start_line, yytext, yyleng, SCNG(on_event_context));
3167 	}
3168 	start_line = CG(zend_lineno);
3169 	goto restart;
3170 }
3171