xref: /PHP-8.3/Zend/zend_language_scanner.l (revision e01e2bb5)
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)) {
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);
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" {
1729 	RETURN_TOKEN_WITH_IDENT(T_PUBLIC);
1730 }
1731 
1732 <ST_IN_SCRIPTING>"readonly" {
1733 	RETURN_TOKEN_WITH_IDENT(T_READONLY);
1734 }
1735 
1736 <ST_IN_SCRIPTING>"unset" {
1737 	RETURN_TOKEN_WITH_IDENT(T_UNSET);
1738 }
1739 
1740 <ST_IN_SCRIPTING>"=>" {
1741 	RETURN_TOKEN(T_DOUBLE_ARROW);
1742 }
1743 
1744 <ST_IN_SCRIPTING>"list" {
1745 	RETURN_TOKEN_WITH_IDENT(T_LIST);
1746 }
1747 
1748 <ST_IN_SCRIPTING>"array" {
1749 	RETURN_TOKEN_WITH_IDENT(T_ARRAY);
1750 }
1751 
1752 <ST_IN_SCRIPTING>"callable" {
1753 	RETURN_TOKEN_WITH_IDENT(T_CALLABLE);
1754 }
1755 
1756 <ST_IN_SCRIPTING>"++" {
1757 	RETURN_TOKEN(T_INC);
1758 }
1759 
1760 <ST_IN_SCRIPTING>"--" {
1761 	RETURN_TOKEN(T_DEC);
1762 }
1763 
1764 <ST_IN_SCRIPTING>"===" {
1765 	RETURN_TOKEN(T_IS_IDENTICAL);
1766 }
1767 
1768 <ST_IN_SCRIPTING>"!==" {
1769 	RETURN_TOKEN(T_IS_NOT_IDENTICAL);
1770 }
1771 
1772 <ST_IN_SCRIPTING>"==" {
1773 	RETURN_TOKEN(T_IS_EQUAL);
1774 }
1775 
1776 <ST_IN_SCRIPTING>"!="|"<>" {
1777 	RETURN_TOKEN(T_IS_NOT_EQUAL);
1778 }
1779 
1780 <ST_IN_SCRIPTING>"<=>" {
1781 	RETURN_TOKEN(T_SPACESHIP);
1782 }
1783 
1784 <ST_IN_SCRIPTING>"<=" {
1785 	RETURN_TOKEN(T_IS_SMALLER_OR_EQUAL);
1786 }
1787 
1788 <ST_IN_SCRIPTING>">=" {
1789 	RETURN_TOKEN(T_IS_GREATER_OR_EQUAL);
1790 }
1791 
1792 <ST_IN_SCRIPTING>"+=" {
1793 	RETURN_TOKEN(T_PLUS_EQUAL);
1794 }
1795 
1796 <ST_IN_SCRIPTING>"-=" {
1797 	RETURN_TOKEN(T_MINUS_EQUAL);
1798 }
1799 
1800 <ST_IN_SCRIPTING>"*=" {
1801 	RETURN_TOKEN(T_MUL_EQUAL);
1802 }
1803 
1804 <ST_IN_SCRIPTING>"*\*" {
1805 	RETURN_TOKEN(T_POW);
1806 }
1807 
1808 <ST_IN_SCRIPTING>"*\*=" {
1809 	RETURN_TOKEN(T_POW_EQUAL);
1810 }
1811 
1812 <ST_IN_SCRIPTING>"/=" {
1813 	RETURN_TOKEN(T_DIV_EQUAL);
1814 }
1815 
1816 <ST_IN_SCRIPTING>".=" {
1817 	RETURN_TOKEN(T_CONCAT_EQUAL);
1818 }
1819 
1820 <ST_IN_SCRIPTING>"%=" {
1821 	RETURN_TOKEN(T_MOD_EQUAL);
1822 }
1823 
1824 <ST_IN_SCRIPTING>"<<=" {
1825 	RETURN_TOKEN(T_SL_EQUAL);
1826 }
1827 
1828 <ST_IN_SCRIPTING>">>=" {
1829 	RETURN_TOKEN(T_SR_EQUAL);
1830 }
1831 
1832 <ST_IN_SCRIPTING>"&=" {
1833 	RETURN_TOKEN(T_AND_EQUAL);
1834 }
1835 
1836 <ST_IN_SCRIPTING>"|=" {
1837 	RETURN_TOKEN(T_OR_EQUAL);
1838 }
1839 
1840 <ST_IN_SCRIPTING>"^=" {
1841 	RETURN_TOKEN(T_XOR_EQUAL);
1842 }
1843 
1844 <ST_IN_SCRIPTING>"??=" {
1845 	RETURN_TOKEN(T_COALESCE_EQUAL);
1846 }
1847 
1848 <ST_IN_SCRIPTING>"||" {
1849 	RETURN_TOKEN(T_BOOLEAN_OR);
1850 }
1851 
1852 <ST_IN_SCRIPTING>"&&" {
1853 	RETURN_TOKEN(T_BOOLEAN_AND);
1854 }
1855 
1856 <ST_IN_SCRIPTING>"OR" {
1857 	RETURN_TOKEN_WITH_IDENT(T_LOGICAL_OR);
1858 }
1859 
1860 <ST_IN_SCRIPTING>"AND" {
1861 	RETURN_TOKEN_WITH_IDENT(T_LOGICAL_AND);
1862 }
1863 
1864 <ST_IN_SCRIPTING>"XOR" {
1865 	RETURN_TOKEN_WITH_IDENT(T_LOGICAL_XOR);
1866 }
1867 
1868 <ST_IN_SCRIPTING>"<<" {
1869 	RETURN_TOKEN(T_SL);
1870 }
1871 
1872 <ST_IN_SCRIPTING>">>" {
1873 	RETURN_TOKEN(T_SR);
1874 }
1875 
1876 <ST_IN_SCRIPTING>"&"{OPTIONAL_WHITESPACE_OR_COMMENTS}("$"|"...") {
1877 	yyless(1);
1878 	RETURN_TOKEN(T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG);
1879 }
1880 
1881 <ST_IN_SCRIPTING>"&" {
1882 	RETURN_TOKEN(T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG);
1883 }
1884 
1885 <ST_IN_SCRIPTING>"]"|")" {
1886 	/* Check that ] and ) match up properly with a preceding [ or ( */
1887 	RETURN_EXIT_NESTING_TOKEN(yytext[0]);
1888 }
1889 
1890 <ST_IN_SCRIPTING>"["|"(" {
1891 	enter_nesting(yytext[0]);
1892 	RETURN_TOKEN(yytext[0]);
1893 }
1894 
1895 <ST_IN_SCRIPTING>{TOKENS} {
1896 	RETURN_TOKEN(yytext[0]);
1897 }
1898 
1899 
1900 <ST_IN_SCRIPTING>"{" {
1901 	yy_push_state(ST_IN_SCRIPTING);
1902 	enter_nesting('{');
1903 	RETURN_TOKEN('{');
1904 }
1905 
1906 
1907 <ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"${" {
1908 	yy_push_state(ST_LOOKING_FOR_VARNAME);
1909 	enter_nesting('{');
1910 	RETURN_TOKEN(T_DOLLAR_OPEN_CURLY_BRACES);
1911 }
1912 
1913 <ST_IN_SCRIPTING>"}" {
1914 	RESET_DOC_COMMENT();
1915 	if (!zend_stack_is_empty(&SCNG(state_stack))) {
1916 		yy_pop_state();
1917 	}
1918 	RETURN_EXIT_NESTING_TOKEN('}');
1919 }
1920 
1921 
1922 <ST_LOOKING_FOR_VARNAME>{LABEL}[[}] {
1923 	yyless(yyleng - 1);
1924 	yy_pop_state();
1925 	yy_push_state(ST_IN_SCRIPTING);
1926 	RETURN_TOKEN_WITH_STR(T_STRING_VARNAME, 0);
1927 }
1928 
1929 
1930 <ST_LOOKING_FOR_VARNAME>{ANY_CHAR} {
1931 	yyless(0);
1932 	yy_pop_state();
1933 	yy_push_state(ST_IN_SCRIPTING);
1934 	goto restart;
1935 }
1936 
1937 <ST_IN_SCRIPTING>{BNUM} {
1938 	/* The +/- 2 skips "0b" */
1939 	size_t len = yyleng - 2;
1940 	char *end, *bin = yytext + 2;
1941 	bool contains_underscores;
1942 
1943 	/* Skip any leading 0s */
1944 	while (len > 0 && (*bin == '0' || *bin == '_')) {
1945 		++bin;
1946 		--len;
1947 	}
1948 
1949 	contains_underscores = (memchr(bin, '_', len) != NULL);
1950 
1951 	if (contains_underscores) {
1952 		bin = estrndup(bin, len);
1953 		strip_underscores(bin, &len);
1954 	}
1955 
1956 	if (len < SIZEOF_ZEND_LONG * 8) {
1957 		if (len == 0) {
1958 			ZVAL_LONG(zendlval, 0);
1959 		} else {
1960 			errno = 0;
1961 			ZVAL_LONG(zendlval, ZEND_STRTOL(bin, &end, 2));
1962 			ZEND_ASSERT(!errno && end == bin + len);
1963 		}
1964 		if (contains_underscores) {
1965 			efree(bin);
1966 		}
1967 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
1968 	} else {
1969 		ZVAL_DOUBLE(zendlval, zend_bin_strtod(bin, (const char **)&end));
1970 		/* errno isn't checked since we allow HUGE_VAL/INF overflow */
1971 		ZEND_ASSERT(end == bin + len);
1972 		if (contains_underscores) {
1973 			efree(bin);
1974 		}
1975 		RETURN_TOKEN_WITH_VAL(T_DNUMBER);
1976 	}
1977 }
1978 
1979 <ST_IN_SCRIPTING>{ONUM} {
1980 	/* The +/- 2 skips "0o" */
1981 	size_t len = yyleng - 2;
1982 	char *end, *octal = yytext + 2;
1983 	bool contains_underscores = (memchr(octal, '_', len) != NULL);
1984 
1985 	/* Skip any leading 0s */
1986 	while (len > 0 && (*octal == '0' || *octal == '_')) {
1987 		++octal;
1988 		--len;
1989 	}
1990 
1991 	if (len == 0) {
1992 		ZVAL_LONG(zendlval, 0);
1993 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
1994 	}
1995 
1996 	if (contains_underscores) {
1997 		octal = estrndup(octal, len);
1998 		strip_underscores(octal, &len);
1999 	}
2000 
2001 	errno = 0;
2002 
2003 	ZVAL_LONG(zendlval, ZEND_STRTOL(octal, &end, 8));
2004 
2005 	ZEND_ASSERT(end == octal + len);
2006 
2007 	if (!errno) {
2008 		if (contains_underscores) {
2009 			efree(octal);
2010 		}
2011 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2012 	}
2013 
2014 	/* Overflow */
2015 	ZEND_ASSERT(errno == ERANGE);
2016 	/* Reset errno */
2017 	errno = 0;
2018 
2019 	/* zend_oct_strtod skips leading '0' */
2020 	ZVAL_DOUBLE(zendlval, zend_oct_strtod(octal, (const char **)&end));
2021 	ZEND_ASSERT(!errno);
2022 	ZEND_ASSERT(end == octal + len);
2023 	if (contains_underscores) {
2024 		efree(octal);
2025 	}
2026 	RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2027 }
2028 
2029 <ST_IN_SCRIPTING>{LNUM} {
2030 	size_t len = yyleng;
2031 	char *end, *lnum = yytext;
2032 	bool is_octal = lnum[0] == '0';
2033 	bool contains_underscores = (memchr(lnum, '_', len) != NULL);
2034 
2035 	if (contains_underscores) {
2036 		lnum = estrndup(lnum, len);
2037 		strip_underscores(lnum, &len);
2038 	}
2039 
2040 	/* Digits 8 and 9 are illegal in octal literals. */
2041 	if (is_octal) {
2042 		size_t i;
2043 		for (i = 0; i < len; i++) {
2044 			if (lnum[i] == '8' || lnum[i] == '9') {
2045 				zend_throw_exception(zend_ce_parse_error, "Invalid numeric literal", 0);
2046 				if (PARSER_MODE()) {
2047 					if (contains_underscores) {
2048 						efree(lnum);
2049 					}
2050 					ZVAL_UNDEF(zendlval);
2051 					RETURN_TOKEN(T_ERROR);
2052 				}
2053 
2054 				/* Continue in order to determine if this is T_LNUMBER or T_DNUMBER. */
2055 				len = i;
2056 				break;
2057 			}
2058 		}
2059 	}
2060 
2061 
2062 	if (len < MAX_LENGTH_OF_LONG - 1) { /* Won't overflow */
2063 		errno = 0;
2064 		/* base must be passed explicitly for correct parse error on Windows */
2065 		ZVAL_LONG(zendlval, ZEND_STRTOL(lnum, &end, is_octal ? 8 : 10));
2066 		ZEND_ASSERT(end == lnum + len);
2067 	} else {
2068 		errno = 0;
2069 		ZVAL_LONG(zendlval, ZEND_STRTOL(lnum, &end, 0));
2070 		if (errno == ERANGE) { /* Overflow */
2071 			errno = 0;
2072 			if (is_octal) { /* octal overflow */
2073 				ZVAL_DOUBLE(zendlval, zend_oct_strtod(lnum, (const char **)&end));
2074 			} else {
2075 				ZVAL_DOUBLE(zendlval, zend_strtod(lnum, (const char **)&end));
2076 			}
2077 			ZEND_ASSERT(end == lnum + len);
2078 			if (contains_underscores) {
2079 				efree(lnum);
2080 			}
2081 			RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2082 		}
2083 		ZEND_ASSERT(end == lnum + len);
2084 	}
2085 	ZEND_ASSERT(!errno);
2086 	if (contains_underscores) {
2087 		efree(lnum);
2088 	}
2089 	RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2090 }
2091 
2092 <ST_IN_SCRIPTING>{HNUM} {
2093 	/* The +/- 2 skips "0x" */
2094 	size_t len = yyleng - 2;
2095 	char *end, *hex = yytext + 2;
2096 	bool contains_underscores;
2097 
2098 	/* Skip any leading 0s */
2099 	while (len > 0 && (*hex == '0' || *hex == '_')) {
2100 		++hex;
2101 		--len;
2102 	}
2103 
2104 	contains_underscores = (memchr(hex, '_', len) != NULL);
2105 
2106 	if (contains_underscores) {
2107 		hex = estrndup(hex, len);
2108 		strip_underscores(hex, &len);
2109 	}
2110 
2111 	if (len < SIZEOF_ZEND_LONG * 2 || (len == SIZEOF_ZEND_LONG * 2 && *hex <= '7')) {
2112 		if (len == 0) {
2113 			ZVAL_LONG(zendlval, 0);
2114 		} else {
2115 			errno = 0;
2116 			ZVAL_LONG(zendlval, ZEND_STRTOL(hex, &end, 16));
2117 			ZEND_ASSERT(!errno && end == hex + len);
2118 		}
2119 		if (contains_underscores) {
2120 			efree(hex);
2121 		}
2122 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2123 	} else {
2124 		ZVAL_DOUBLE(zendlval, zend_hex_strtod(hex, (const char **)&end));
2125 		/* errno isn't checked since we allow HUGE_VAL/INF overflow */
2126 		ZEND_ASSERT(end == hex + len);
2127 		if (contains_underscores) {
2128 			efree(hex);
2129 		}
2130 		RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2131 	}
2132 }
2133 
2134 <ST_VAR_OFFSET>[0]|([1-9][0-9]*) { /* Offset could be treated as a long */
2135 	if (yyleng < MAX_LENGTH_OF_LONG - 1 || (yyleng == MAX_LENGTH_OF_LONG - 1 && strcmp(yytext, long_min_digits) < 0)) {
2136 		char *end;
2137 		errno = 0;
2138 		ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 10));
2139 		if (errno == ERANGE) {
2140 			goto string;
2141 		}
2142 		ZEND_ASSERT(end == yytext + yyleng);
2143 	} else {
2144 string:
2145 		ZVAL_STRINGL(zendlval, yytext, yyleng);
2146 	}
2147 	RETURN_TOKEN_WITH_VAL(T_NUM_STRING);
2148 }
2149 
2150 <ST_VAR_OFFSET>{LNUM}|{HNUM}|{BNUM}|{ONUM} { /* Offset must be treated as a string */
2151 	if (yyleng == 1) {
2152 		ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR((zend_uchar)*(yytext)));
2153 	} else {
2154 		ZVAL_STRINGL(zendlval, yytext, yyleng);
2155 	}
2156 	RETURN_TOKEN_WITH_VAL(T_NUM_STRING);
2157 }
2158 
2159 <ST_IN_SCRIPTING>{DNUM}|{EXPONENT_DNUM} {
2160 	const char *end;
2161 	size_t len = yyleng;
2162 	char *dnum = yytext;
2163 	bool contains_underscores = (memchr(dnum, '_', len) != NULL);
2164 
2165 	if (contains_underscores) {
2166 		dnum = estrndup(dnum, len);
2167 		strip_underscores(dnum, &len);
2168 	}
2169 
2170 	ZVAL_DOUBLE(zendlval, zend_strtod(dnum, &end));
2171 	/* errno isn't checked since we allow HUGE_VAL/INF overflow */
2172 	ZEND_ASSERT(end == dnum + len);
2173 	if (contains_underscores) {
2174 		efree(dnum);
2175 	}
2176 	RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2177 }
2178 
2179 <ST_IN_SCRIPTING>"__CLASS__" {
2180 	RETURN_TOKEN_WITH_IDENT(T_CLASS_C);
2181 }
2182 
2183 <ST_IN_SCRIPTING>"__TRAIT__" {
2184 	RETURN_TOKEN_WITH_IDENT(T_TRAIT_C);
2185 }
2186 
2187 <ST_IN_SCRIPTING>"__FUNCTION__" {
2188 	RETURN_TOKEN_WITH_IDENT(T_FUNC_C);
2189 }
2190 
2191 <ST_IN_SCRIPTING>"__METHOD__" {
2192 	RETURN_TOKEN_WITH_IDENT(T_METHOD_C);
2193 }
2194 
2195 <ST_IN_SCRIPTING>"__LINE__" {
2196 	RETURN_TOKEN_WITH_IDENT(T_LINE);
2197 }
2198 
2199 <ST_IN_SCRIPTING>"__FILE__" {
2200 	RETURN_TOKEN_WITH_IDENT(T_FILE);
2201 }
2202 
2203 <ST_IN_SCRIPTING>"__DIR__" {
2204 	RETURN_TOKEN_WITH_IDENT(T_DIR);
2205 }
2206 
2207 <ST_IN_SCRIPTING>"__NAMESPACE__" {
2208 	RETURN_TOKEN_WITH_IDENT(T_NS_C);
2209 }
2210 
2211 <SHEBANG>"#!" .* {NEWLINE} {
2212 	CG(zend_lineno)++;
2213 	BEGIN(INITIAL);
2214 	goto restart;
2215 }
2216 
2217 <SHEBANG>{ANY_CHAR} {
2218 	yyless(0);
2219 	BEGIN(INITIAL);
2220 	goto restart;
2221 }
2222 
2223 <INITIAL>"<?=" {
2224 	BEGIN(ST_IN_SCRIPTING);
2225 	if (PARSER_MODE()) {
2226 		/* We'll reject this as an identifier in zend_lex_tstring. */
2227 		RETURN_TOKEN_WITH_IDENT(T_ECHO);
2228 	}
2229 	RETURN_TOKEN(T_OPEN_TAG_WITH_ECHO);
2230 }
2231 
2232 
2233 <INITIAL>"<?php"([ \t]|{NEWLINE}) {
2234 	HANDLE_NEWLINE(yytext[yyleng-1]);
2235 	BEGIN(ST_IN_SCRIPTING);
2236 	RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2237 }
2238 
2239 <INITIAL>"<?php" {
2240 	/* Allow <?php followed by end of file. */
2241 	if (YYCURSOR == YYLIMIT) {
2242 		BEGIN(ST_IN_SCRIPTING);
2243 		RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2244 	}
2245 	/* Degenerate case: <?phpX is interpreted as <? phpX with short tags. */
2246 	if (CG(short_tags)) {
2247 		yyless(2);
2248 		BEGIN(ST_IN_SCRIPTING);
2249 		RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2250 	}
2251 	goto inline_char_handler;
2252 }
2253 
2254 <INITIAL>"<?" {
2255 	if (CG(short_tags)) {
2256 		BEGIN(ST_IN_SCRIPTING);
2257 		RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2258 	} else {
2259 		goto inline_char_handler;
2260 	}
2261 }
2262 
2263 <INITIAL>{ANY_CHAR} {
2264 	if (YYCURSOR > YYLIMIT) {
2265 		RETURN_END_TOKEN;
2266 	}
2267 
2268 inline_char_handler:
2269 
2270 	while (1) {
2271 		YYCTYPE *ptr = memchr(YYCURSOR, '<', YYLIMIT - YYCURSOR);
2272 
2273 		YYCURSOR = ptr ? ptr + 1 : YYLIMIT;
2274 
2275 		if (YYCURSOR >= YYLIMIT) {
2276 			break;
2277 		}
2278 
2279 		if (*YYCURSOR == '?') {
2280 			if (CG(short_tags) /* <? */
2281 				|| (*(YYCURSOR + 1) == '=') /* <?= */
2282 				|| (!strncasecmp((char*)YYCURSOR + 1, "php", 3) && /* <?php[ \t\r\n] */
2283 					(YYCURSOR + 4 == YYLIMIT ||
2284 					YYCURSOR[4] == ' ' || YYCURSOR[4] == '\t' ||
2285 					YYCURSOR[4] == '\n' || YYCURSOR[4] == '\r'))
2286 			) {
2287 				YYCURSOR--;
2288 				break;
2289 			}
2290 		}
2291 	}
2292 
2293 	yyleng = YYCURSOR - SCNG(yy_text);
2294 
2295 	if (SCNG(output_filter)) {
2296 		size_t readsize;
2297 		char *s = NULL;
2298 		size_t sz = 0;
2299 		// TODO: avoid reallocation ???
2300 		readsize = SCNG(output_filter)((unsigned char **)&s, &sz, (unsigned char *)yytext, (size_t)yyleng);
2301 		ZVAL_STRINGL(zendlval, s, sz);
2302 		efree(s);
2303 		if (readsize < yyleng) {
2304 			yyless(readsize);
2305 		}
2306 	} else if (yyleng == 1) {
2307 		ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR((zend_uchar)*yytext));
2308 	} else {
2309 		ZVAL_STRINGL(zendlval, yytext, yyleng);
2310 	}
2311 	HANDLE_NEWLINES(yytext, yyleng);
2312 	RETURN_TOKEN_WITH_VAL(T_INLINE_HTML);
2313 }
2314 
2315 
2316 /* Make sure a label character follows "->" or "?->", otherwise there is no property
2317  * and "->"/"?->" will be taken literally
2318  */
2319 <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"->"[a-zA-Z_\x80-\xff] {
2320 	yyless(yyleng - 3);
2321 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
2322 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2323 }
2324 
2325 <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"?->"[a-zA-Z_\x80-\xff] {
2326 	yyless(yyleng - 4);
2327 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
2328 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2329 }
2330 
2331 /* A [ always designates a variable offset, regardless of what follows
2332  */
2333 <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"[" {
2334 	yyless(yyleng - 1);
2335 	yy_push_state(ST_VAR_OFFSET);
2336 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2337 }
2338 
2339 <ST_IN_SCRIPTING,ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE,ST_VAR_OFFSET>"$"{LABEL} {
2340 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2341 }
2342 
2343 <ST_VAR_OFFSET>"]" {
2344 	yy_pop_state();
2345 	RETURN_TOKEN(']');
2346 }
2347 
2348 <ST_VAR_OFFSET>{TOKENS}|[[(){}"`] {
2349 	/* Only '[' or '-' can be valid, but returning other tokens will allow a more explicit parse error */
2350 	RETURN_TOKEN(yytext[0]);
2351 }
2352 
2353 <ST_VAR_OFFSET>[ \n\r\t\\'#] {
2354 	/* Invalid rule to return a more explicit parse error with proper line number */
2355 	yyless(0);
2356 	yy_pop_state();
2357 	ZVAL_NULL(zendlval);
2358 	RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2359 }
2360 
2361 <ST_IN_SCRIPTING>"namespace"("\\"{LABEL})+ {
2362 	RETURN_TOKEN_WITH_STR(T_NAME_RELATIVE, sizeof("namespace\\") - 1);
2363 }
2364 
2365 <ST_IN_SCRIPTING>{LABEL}("\\"{LABEL})+ {
2366 	RETURN_TOKEN_WITH_STR(T_NAME_QUALIFIED, 0);
2367 }
2368 
2369 <ST_IN_SCRIPTING>"\\"{LABEL}("\\"{LABEL})* {
2370 	RETURN_TOKEN_WITH_STR(T_NAME_FULLY_QUALIFIED, 1);
2371 }
2372 
2373 <ST_IN_SCRIPTING>"\\" {
2374 	RETURN_TOKEN(T_NS_SEPARATOR);
2375 }
2376 
2377 <ST_IN_SCRIPTING,ST_VAR_OFFSET>{LABEL} {
2378 	RETURN_TOKEN_WITH_STR(T_STRING, 0);
2379 }
2380 
2381 
2382 <ST_IN_SCRIPTING,ST_LOOKING_FOR_PROPERTY>"#"|"//" {
2383 	while (YYCURSOR < YYLIMIT) {
2384 		switch (*YYCURSOR++) {
2385 			case '\r':
2386 			case '\n':
2387 				YYCURSOR--;
2388 				break;
2389 			case '?':
2390 				if (*YYCURSOR == '>') {
2391 					YYCURSOR--;
2392 					break;
2393 				}
2394 				ZEND_FALLTHROUGH;
2395 			default:
2396 				continue;
2397 		}
2398 
2399 		break;
2400 	}
2401 
2402 	yyleng = YYCURSOR - SCNG(yy_text);
2403 	RETURN_OR_SKIP_TOKEN(T_COMMENT);
2404 }
2405 
2406 <ST_IN_SCRIPTING,ST_LOOKING_FOR_PROPERTY>"/*"|"/**"{WHITESPACE} {
2407 	int doc_com;
2408 
2409 	if (yyleng > 2) {
2410 		doc_com = 1;
2411 		RESET_DOC_COMMENT();
2412 	} else {
2413 		doc_com = 0;
2414 	}
2415 
2416 	while (YYCURSOR < YYLIMIT) {
2417 		if (*YYCURSOR++ == '*' && *YYCURSOR == '/') {
2418 			break;
2419 		}
2420 	}
2421 
2422 	if (YYCURSOR < YYLIMIT) {
2423 		YYCURSOR++;
2424 	} else {
2425 		zend_throw_exception_ex(zend_ce_parse_error, 0, "Unterminated comment starting line %d", CG(zend_lineno));
2426 		if (PARSER_MODE()) {
2427 			RETURN_TOKEN(T_ERROR);
2428 		}
2429 	}
2430 
2431 	yyleng = YYCURSOR - SCNG(yy_text);
2432 	HANDLE_NEWLINES(yytext, yyleng);
2433 
2434 	if (doc_com) {
2435 		CG(doc_comment) = zend_string_init(yytext, yyleng, 0);
2436 		RETURN_OR_SKIP_TOKEN(T_DOC_COMMENT);
2437 	}
2438 
2439 	RETURN_OR_SKIP_TOKEN(T_COMMENT);
2440 }
2441 
2442 <ST_LOOKING_FOR_PROPERTY>{ANY_CHAR} {
2443 	yyless(0);
2444 	yy_pop_state();
2445 	goto restart;
2446 }
2447 
2448 <ST_IN_SCRIPTING>"?>"{NEWLINE}? {
2449 	BEGIN(INITIAL);
2450 	if (yytext[yyleng-1] != '>') {
2451 		CG(increment_lineno) = 1;
2452 	}
2453 	if (PARSER_MODE()) {
2454 		RETURN_TOKEN(';');  /* implicit ';' at php-end tag */
2455 	}
2456 	RETURN_TOKEN(T_CLOSE_TAG);
2457 }
2458 
2459 
2460 <ST_IN_SCRIPTING>b?['] {
2461 	char *s, *t;
2462 	char *end;
2463 	int bprefix = (yytext[0] != '\'') ? 1 : 0;
2464 
2465 	while (1) {
2466 		if (YYCURSOR < YYLIMIT) {
2467 			if (*YYCURSOR == '\'') {
2468 				YYCURSOR++;
2469 				yyleng = YYCURSOR - SCNG(yy_text);
2470 
2471 				break;
2472 			} else if (*YYCURSOR++ == '\\' && YYCURSOR < YYLIMIT) {
2473 				YYCURSOR++;
2474 			}
2475 		} else {
2476 			yyleng = YYLIMIT - SCNG(yy_text);
2477 
2478 			/* Unclosed single quotes; treat similar to double quotes, but without a separate token
2479 			 * for ' (unrecognized by parser), instead of old flex fallback to "Unexpected character..."
2480 			 * rule, which continued in ST_IN_SCRIPTING state after the quote */
2481 			ZVAL_NULL(zendlval);
2482 			RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2483 		}
2484 	}
2485 
2486 	if (yyleng-bprefix-2 <= 1) {
2487 		if (yyleng-bprefix-2 < 1) {
2488 			ZVAL_EMPTY_STRING(zendlval);
2489 		} else {
2490 			zend_uchar c = (zend_uchar)*(yytext+bprefix+1);
2491 			if (c == '\n' || c == '\r') {
2492 				CG(zend_lineno)++;
2493 			}
2494 			ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR(c));
2495 		}
2496 		goto skip_escape_conversion;
2497 	}
2498 	ZVAL_STRINGL(zendlval, yytext+bprefix+1, yyleng-bprefix-2);
2499 
2500 	/* convert escape sequences */
2501 	s = Z_STRVAL_P(zendlval);
2502 	end = s+Z_STRLEN_P(zendlval);
2503 	while (1) {
2504 		if (UNEXPECTED(*s=='\\')) {
2505 			break;
2506 		}
2507 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
2508 			CG(zend_lineno)++;
2509 		}
2510 		s++;
2511 		if (s == end) {
2512 			goto skip_escape_conversion;
2513 		}
2514 	}
2515 
2516 	t = s;
2517 	while (s<end) {
2518 		if (*s=='\\') {
2519 			s++;
2520 			if (*s == '\\' || *s == '\'') {
2521 				*t++ = *s;
2522 			} else {
2523 				*t++ = '\\';
2524 				*t++ = *s;
2525 			}
2526 		} else {
2527 			*t++ = *s;
2528 		}
2529 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
2530 			CG(zend_lineno)++;
2531 		}
2532 		s++;
2533 	}
2534 	*t = 0;
2535 	Z_STRLEN_P(zendlval) = t - Z_STRVAL_P(zendlval);
2536 
2537 skip_escape_conversion:
2538 	if (SCNG(output_filter)) {
2539 		size_t sz = 0;
2540 		char *str = NULL;
2541 		zend_string *new_str;
2542 		s = Z_STRVAL_P(zendlval);
2543 		// TODO: avoid reallocation ???
2544 		SCNG(output_filter)((unsigned char **)&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
2545 		new_str = zend_string_init(str, sz, 0);
2546 		if (str != s) {
2547 			efree(str);
2548 		}
2549 		zend_string_release_ex(Z_STR_P(zendlval), 0);
2550 		ZVAL_STR(zendlval, new_str);
2551 	}
2552 	RETURN_TOKEN_WITH_VAL(T_CONSTANT_ENCAPSED_STRING);
2553 }
2554 
2555 
2556 <ST_IN_SCRIPTING>b?["] {
2557 	int bprefix = (yytext[0] != '"') ? 1 : 0;
2558 
2559 	while (YYCURSOR < YYLIMIT) {
2560 		switch (*YYCURSOR++) {
2561 			case '"':
2562 				yyleng = YYCURSOR - SCNG(yy_text);
2563 				if (EXPECTED(zend_scan_escape_string(zendlval, yytext+bprefix+1, yyleng-bprefix-2, '"') == SUCCESS)
2564 				 || !PARSER_MODE()) {
2565 					RETURN_TOKEN_WITH_VAL(T_CONSTANT_ENCAPSED_STRING);
2566 				} else {
2567 					RETURN_TOKEN(T_ERROR);
2568 				}
2569 			case '$':
2570 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2571 					break;
2572 				}
2573 				continue;
2574 			case '{':
2575 				if (*YYCURSOR == '$') {
2576 					break;
2577 				}
2578 				continue;
2579 			case '\\':
2580 				if (YYCURSOR < YYLIMIT) {
2581 					YYCURSOR++;
2582 				}
2583 				ZEND_FALLTHROUGH;
2584 			default:
2585 				continue;
2586 		}
2587 
2588 		YYCURSOR--;
2589 		break;
2590 	}
2591 
2592 	/* Remember how much was scanned to save rescanning */
2593 	SET_DOUBLE_QUOTES_SCANNED_LENGTH(YYCURSOR - SCNG(yy_text) - yyleng);
2594 
2595 	YYCURSOR = SCNG(yy_text) + yyleng;
2596 
2597 	BEGIN(ST_DOUBLE_QUOTES);
2598 	RETURN_TOKEN('"');
2599 }
2600 
2601 
2602 <ST_IN_SCRIPTING>b?"<<<"{TABS_AND_SPACES}({LABEL}|([']{LABEL}['])|(["]{LABEL}["])){NEWLINE} {
2603 	char *s;
2604 	unsigned char *saved_cursor;
2605 	int bprefix = (yytext[0] != '<') ? 1 : 0, spacing = 0, indentation = 0;
2606 	zend_heredoc_label *heredoc_label = emalloc(sizeof(zend_heredoc_label));
2607 	bool is_heredoc = 1;
2608 
2609 	CG(zend_lineno)++;
2610 	heredoc_label->length = yyleng-bprefix-3-1-(yytext[yyleng-2]=='\r'?1:0);
2611 	s = yytext+bprefix+3;
2612 	while ((*s == ' ') || (*s == '\t')) {
2613 		s++;
2614 		heredoc_label->length--;
2615 	}
2616 
2617 	if (*s == '\'') {
2618 		s++;
2619 		heredoc_label->length -= 2;
2620 		is_heredoc = 0;
2621 
2622 		BEGIN(ST_NOWDOC);
2623 	} else {
2624 		if (*s == '"') {
2625 			s++;
2626 			heredoc_label->length -= 2;
2627 		}
2628 
2629 		BEGIN(ST_HEREDOC);
2630 	}
2631 
2632 	heredoc_label->label = estrndup(s, heredoc_label->length);
2633 	heredoc_label->indentation_uses_spaces = 0;
2634 	heredoc_label->indentation = 0;
2635 	saved_cursor = YYCURSOR;
2636 
2637 	zend_ptr_stack_push(&SCNG(heredoc_label_stack), (void *) heredoc_label);
2638 
2639 	while (YYCURSOR < YYLIMIT && (*YYCURSOR == ' ' || *YYCURSOR == '\t')) {
2640 		if (*YYCURSOR == '\t') {
2641 			spacing |= HEREDOC_USING_TABS;
2642 		} else {
2643 			spacing |= HEREDOC_USING_SPACES;
2644 		}
2645 		++YYCURSOR;
2646 		++indentation;
2647 	}
2648 
2649 	if (YYCURSOR == YYLIMIT) {
2650 		YYCURSOR = saved_cursor;
2651 		RETURN_TOKEN(T_START_HEREDOC);
2652 	}
2653 
2654 	/* Check for ending label on the next line */
2655 	if (heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, s, heredoc_label->length)) {
2656 		if (!IS_LABEL_SUCCESSOR(YYCURSOR[heredoc_label->length])) {
2657 			if (spacing == (HEREDOC_USING_SPACES | HEREDOC_USING_TABS)) {
2658 				zend_throw_exception(zend_ce_parse_error, "Invalid indentation - tabs and spaces cannot be mixed", 0);
2659 				if (PARSER_MODE()) {
2660 					RETURN_TOKEN(T_ERROR);
2661 				}
2662 			}
2663 
2664 			YYCURSOR = saved_cursor;
2665 			heredoc_label->indentation = indentation;
2666 
2667 			BEGIN(ST_END_HEREDOC);
2668 			RETURN_TOKEN(T_START_HEREDOC);
2669 		}
2670 	}
2671 
2672 	YYCURSOR = saved_cursor;
2673 
2674 	if (is_heredoc && !SCNG(heredoc_scan_ahead)) {
2675 		zend_lex_state current_state;
2676 		zend_string *saved_doc_comment = CG(doc_comment);
2677 		int heredoc_nesting_level = 1;
2678 		int first_token = 0;
2679 		int error = 0;
2680 
2681 		zend_save_lexical_state(&current_state);
2682 
2683 		SCNG(heredoc_scan_ahead) = 1;
2684 		SCNG(heredoc_indentation) = 0;
2685 		SCNG(heredoc_indentation_uses_spaces) = 0;
2686 		LANG_SCNG(on_event) = NULL;
2687 		CG(doc_comment) = NULL;
2688 
2689 		zend_ptr_stack_reverse_apply(&current_state.heredoc_label_stack, copy_heredoc_label_stack);
2690 
2691 		zend_exception_save();
2692 		while (heredoc_nesting_level) {
2693 			zval zv;
2694 			int retval;
2695 
2696 			ZVAL_UNDEF(&zv);
2697 			retval = lex_scan(&zv, NULL);
2698 			zval_ptr_dtor_nogc(&zv);
2699 
2700 			if (EG(exception)) {
2701 				zend_clear_exception();
2702 				break;
2703 			}
2704 
2705 			if (!first_token) {
2706 				first_token = retval;
2707 			}
2708 
2709 			switch (retval) {
2710 				case T_START_HEREDOC:
2711 					++heredoc_nesting_level;
2712 					break;
2713 				case T_END_HEREDOC:
2714 					--heredoc_nesting_level;
2715 					break;
2716 				case END:
2717 					heredoc_nesting_level = 0;
2718 			}
2719 		}
2720 		zend_exception_restore();
2721 
2722 		if (
2723 		    (first_token == T_VARIABLE
2724 		     || first_token == T_DOLLAR_OPEN_CURLY_BRACES
2725 		     || first_token == T_CURLY_OPEN
2726 		    ) && SCNG(heredoc_indentation)) {
2727 			zend_throw_exception_ex(zend_ce_parse_error, 0, "Invalid body indentation level (expecting an indentation level of at least %d)", SCNG(heredoc_indentation));
2728 			error = 1;
2729 		}
2730 
2731 		heredoc_label->indentation = SCNG(heredoc_indentation);
2732 		heredoc_label->indentation_uses_spaces = SCNG(heredoc_indentation_uses_spaces);
2733 
2734 		zend_restore_lexical_state(&current_state);
2735 		SCNG(heredoc_scan_ahead) = 0;
2736 		CG(increment_lineno) = 0;
2737 		CG(doc_comment) = saved_doc_comment;
2738 
2739 		if (PARSER_MODE() && error) {
2740 			RETURN_TOKEN(T_ERROR);
2741 		}
2742 	}
2743 
2744 	RETURN_TOKEN(T_START_HEREDOC);
2745 }
2746 
2747 
2748 <ST_IN_SCRIPTING>[`] {
2749 	BEGIN(ST_BACKQUOTE);
2750 	RETURN_TOKEN('`');
2751 }
2752 
2753 
2754 <ST_END_HEREDOC>{ANY_CHAR} {
2755 	zend_heredoc_label *heredoc_label = zend_ptr_stack_pop(&SCNG(heredoc_label_stack));
2756 
2757 	yyleng = heredoc_label->indentation + heredoc_label->length;
2758 	YYCURSOR += yyleng - 1;
2759 
2760 	heredoc_label_dtor(heredoc_label);
2761 	efree(heredoc_label);
2762 
2763 	BEGIN(ST_IN_SCRIPTING);
2764 	RETURN_TOKEN(T_END_HEREDOC);
2765 }
2766 
2767 
2768 <ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"{$" {
2769 	yy_push_state(ST_IN_SCRIPTING);
2770 	yyless(1);
2771 	enter_nesting('{');
2772 	RETURN_TOKEN(T_CURLY_OPEN);
2773 }
2774 
2775 
2776 <ST_DOUBLE_QUOTES>["] {
2777 	BEGIN(ST_IN_SCRIPTING);
2778 	RETURN_TOKEN('"');
2779 }
2780 
2781 <ST_BACKQUOTE>[`] {
2782 	BEGIN(ST_IN_SCRIPTING);
2783 	RETURN_TOKEN('`');
2784 }
2785 
2786 
2787 <ST_DOUBLE_QUOTES>{ANY_CHAR} {
2788 	if (GET_DOUBLE_QUOTES_SCANNED_LENGTH()) {
2789 		YYCURSOR += GET_DOUBLE_QUOTES_SCANNED_LENGTH() - 1;
2790 		SET_DOUBLE_QUOTES_SCANNED_LENGTH(0);
2791 
2792 		goto double_quotes_scan_done;
2793 	}
2794 
2795 	if (YYCURSOR > YYLIMIT) {
2796 		RETURN_END_TOKEN;
2797 	}
2798 	if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2799 		YYCURSOR++;
2800 	}
2801 
2802 	while (YYCURSOR < YYLIMIT) {
2803 		switch (*YYCURSOR++) {
2804 			case '"':
2805 				break;
2806 			case '$':
2807 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2808 					break;
2809 				}
2810 				continue;
2811 			case '{':
2812 				if (*YYCURSOR == '$') {
2813 					break;
2814 				}
2815 				continue;
2816 			case '\\':
2817 				if (YYCURSOR < YYLIMIT) {
2818 					YYCURSOR++;
2819 				}
2820 				ZEND_FALLTHROUGH;
2821 			default:
2822 				continue;
2823 		}
2824 
2825 		YYCURSOR--;
2826 		break;
2827 	}
2828 
2829 double_quotes_scan_done:
2830 	yyleng = YYCURSOR - SCNG(yy_text);
2831 
2832 	if (EXPECTED(zend_scan_escape_string(zendlval, yytext, yyleng, '"') == SUCCESS)
2833 	 || !PARSER_MODE()) {
2834 		RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2835 	} else {
2836 		RETURN_TOKEN(T_ERROR);
2837 	}
2838 }
2839 
2840 
2841 <ST_BACKQUOTE>{ANY_CHAR} {
2842 	if (YYCURSOR > YYLIMIT) {
2843 		RETURN_END_TOKEN;
2844 	}
2845 	if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2846 		YYCURSOR++;
2847 	}
2848 
2849 	while (YYCURSOR < YYLIMIT) {
2850 		switch (*YYCURSOR++) {
2851 			case '`':
2852 				break;
2853 			case '$':
2854 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2855 					break;
2856 				}
2857 				continue;
2858 			case '{':
2859 				if (*YYCURSOR == '$') {
2860 					break;
2861 				}
2862 				continue;
2863 			case '\\':
2864 				if (YYCURSOR < YYLIMIT) {
2865 					YYCURSOR++;
2866 				}
2867 				ZEND_FALLTHROUGH;
2868 			default:
2869 				continue;
2870 		}
2871 
2872 		YYCURSOR--;
2873 		break;
2874 	}
2875 
2876 	yyleng = YYCURSOR - SCNG(yy_text);
2877 
2878 	if (EXPECTED(zend_scan_escape_string(zendlval, yytext, yyleng, '`') == SUCCESS)
2879 	 || !PARSER_MODE()) {
2880 		RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2881 	} else {
2882 		RETURN_TOKEN(T_ERROR);
2883 	}
2884 }
2885 
2886 
2887 <ST_HEREDOC>{ANY_CHAR} {
2888 	zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2889 	int newline = 0, indentation = 0, spacing = 0;
2890 
2891 	if (YYCURSOR > YYLIMIT) {
2892 		RETURN_END_TOKEN;
2893 	}
2894 
2895 	YYCURSOR--;
2896 
2897 	while (YYCURSOR < YYLIMIT) {
2898 		switch (*YYCURSOR++) {
2899 			case '\r':
2900 				if (*YYCURSOR == '\n') {
2901 					YYCURSOR++;
2902 				}
2903 				ZEND_FALLTHROUGH;
2904 			case '\n':
2905 				indentation = spacing = 0;
2906 
2907 				while (YYCURSOR < YYLIMIT && (*YYCURSOR == ' ' || *YYCURSOR == '\t')) {
2908 					if (*YYCURSOR == '\t') {
2909 						spacing |= HEREDOC_USING_TABS;
2910 					} else {
2911 						spacing |= HEREDOC_USING_SPACES;
2912 					}
2913 					++YYCURSOR;
2914 					++indentation;
2915 				}
2916 
2917 				if (YYCURSOR == YYLIMIT) {
2918 					yyleng = YYCURSOR - SCNG(yy_text);
2919 					HANDLE_NEWLINES(yytext, yyleng);
2920 					ZVAL_NULL(zendlval);
2921 					RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2922 				}
2923 
2924 				/* Check for ending label on the next line */
2925 				if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
2926 					if (IS_LABEL_SUCCESSOR(YYCURSOR[heredoc_label->length])) {
2927 						continue;
2928 					}
2929 
2930 					if (spacing == (HEREDOC_USING_SPACES | HEREDOC_USING_TABS)) {
2931 						zend_throw_exception(zend_ce_parse_error, "Invalid indentation - tabs and spaces cannot be mixed", 0);
2932 						if (PARSER_MODE()) {
2933 							RETURN_TOKEN(T_ERROR);
2934 						}
2935 					}
2936 
2937 					/* newline before label will be subtracted from returned text, but
2938 					 * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
2939 					if (YYCURSOR[-indentation - 2] == '\r' && YYCURSOR[-indentation - 1] == '\n') {
2940 						newline = 2; /* Windows newline */
2941 					} else {
2942 						newline = 1;
2943 					}
2944 
2945 					CG(increment_lineno) = 1; /* For newline before label */
2946 
2947 					if (SCNG(heredoc_scan_ahead)) {
2948 						SCNG(heredoc_indentation) = indentation;
2949 						SCNG(heredoc_indentation_uses_spaces) = (spacing == HEREDOC_USING_SPACES);
2950 					} else {
2951 						YYCURSOR -= indentation;
2952 					}
2953 
2954 					BEGIN(ST_END_HEREDOC);
2955 
2956 					goto heredoc_scan_done;
2957 				}
2958 				continue;
2959 			case '$':
2960 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2961 					break;
2962 				}
2963 				continue;
2964 			case '{':
2965 				if (*YYCURSOR == '$') {
2966 					break;
2967 				}
2968 				continue;
2969 			case '\\':
2970 				if (YYCURSOR < YYLIMIT && *YYCURSOR != '\n' && *YYCURSOR != '\r') {
2971 					YYCURSOR++;
2972 				}
2973 				ZEND_FALLTHROUGH;
2974 			default:
2975 				continue;
2976 		}
2977 
2978 		YYCURSOR--;
2979 		break;
2980 	}
2981 
2982 heredoc_scan_done:
2983 
2984 	yyleng = YYCURSOR - SCNG(yy_text);
2985 	ZVAL_STRINGL(zendlval, yytext, yyleng - newline);
2986 
2987 	if (!SCNG(heredoc_scan_ahead) && !EG(exception) && PARSER_MODE()) {
2988 		bool newline_at_start = *(yytext - 1) == '\n' || *(yytext - 1) == '\r';
2989 		zend_string *copy = Z_STR_P(zendlval);
2990 
2991 		if (!strip_multiline_string_indentation(
2992 				zendlval, heredoc_label->indentation, heredoc_label->indentation_uses_spaces,
2993 				newline_at_start, newline != 0)) {
2994 			RETURN_TOKEN(T_ERROR);
2995 		}
2996 
2997 		if (UNEXPECTED(zend_scan_escape_string(zendlval, ZSTR_VAL(copy), ZSTR_LEN(copy), 0) != SUCCESS)) {
2998 			zend_string_efree(copy);
2999 			RETURN_TOKEN(T_ERROR);
3000 		}
3001 
3002 		zend_string_efree(copy);
3003 	} else {
3004 		HANDLE_NEWLINES(yytext, yyleng - newline);
3005 	}
3006 
3007 	RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
3008 }
3009 
3010 
3011 <ST_NOWDOC>{ANY_CHAR} {
3012 	zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
3013 	int newline = 0, indentation = 0, spacing = -1;
3014 
3015 	if (YYCURSOR > YYLIMIT) {
3016 		RETURN_END_TOKEN;
3017 	}
3018 
3019 	YYCURSOR--;
3020 
3021 	while (YYCURSOR < YYLIMIT) {
3022 		switch (*YYCURSOR++) {
3023 			case '\r':
3024 				if (*YYCURSOR == '\n') {
3025 					YYCURSOR++;
3026 				}
3027 				ZEND_FALLTHROUGH;
3028 			case '\n':
3029 				indentation = spacing = 0;
3030 
3031 				while (YYCURSOR < YYLIMIT && (*YYCURSOR == ' ' || *YYCURSOR == '\t')) {
3032 					if (*YYCURSOR == '\t') {
3033 						spacing |= HEREDOC_USING_TABS;
3034 					} else {
3035 						spacing |= HEREDOC_USING_SPACES;
3036 					}
3037 					++YYCURSOR;
3038 					++indentation;
3039 				}
3040 
3041 				if (YYCURSOR == YYLIMIT) {
3042 					yyleng = YYCURSOR - SCNG(yy_text);
3043 					HANDLE_NEWLINES(yytext, yyleng);
3044 					ZVAL_NULL(zendlval);
3045 					RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
3046 				}
3047 
3048 				/* Check for ending label on the next line */
3049 				if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
3050 					if (IS_LABEL_SUCCESSOR(YYCURSOR[heredoc_label->length])) {
3051 						continue;
3052 					}
3053 
3054 					if (spacing == (HEREDOC_USING_SPACES | HEREDOC_USING_TABS)) {
3055 						zend_throw_exception(zend_ce_parse_error, "Invalid indentation - tabs and spaces cannot be mixed", 0);
3056 						if (PARSER_MODE()) {
3057 							RETURN_TOKEN(T_ERROR);
3058 						}
3059 					}
3060 
3061 					/* newline before label will be subtracted from returned text, but
3062 					 * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
3063 					if (YYCURSOR[-indentation - 2] == '\r' && YYCURSOR[-indentation - 1] == '\n') {
3064 						newline = 2; /* Windows newline */
3065 					} else {
3066 						newline = 1;
3067 					}
3068 
3069 					CG(increment_lineno) = 1; /* For newline before label */
3070 
3071 					YYCURSOR -= indentation;
3072 					heredoc_label->indentation = indentation;
3073 
3074 					BEGIN(ST_END_HEREDOC);
3075 
3076 					goto nowdoc_scan_done;
3077 				}
3078 				ZEND_FALLTHROUGH;
3079 			default:
3080 				continue;
3081 		}
3082 	}
3083 
3084 nowdoc_scan_done:
3085 	yyleng = YYCURSOR - SCNG(yy_text);
3086 	ZVAL_STRINGL(zendlval, yytext, yyleng - newline);
3087 
3088 	if (!EG(exception) && spacing != -1 && PARSER_MODE()) {
3089 		bool newline_at_start = *(yytext - 1) == '\n' || *(yytext - 1) == '\r';
3090 		if (!strip_multiline_string_indentation(
3091 				zendlval, indentation, spacing == HEREDOC_USING_SPACES,
3092 				newline_at_start, newline != 0)) {
3093 			RETURN_TOKEN(T_ERROR);
3094 		}
3095 	}
3096 
3097 	HANDLE_NEWLINES(yytext, yyleng - newline);
3098 	RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
3099 }
3100 
3101 
3102 <ST_IN_SCRIPTING,ST_VAR_OFFSET>{ANY_CHAR} {
3103 	if (YYCURSOR > YYLIMIT) {
3104 		RETURN_END_TOKEN;
3105 	}
3106 
3107 	RETURN_TOKEN(T_BAD_CHARACTER);
3108 }
3109 
3110 */
3111 
3112 emit_token_with_str:
3113 	zend_copy_value(zendlval, (yytext + offset), (yyleng - offset));
3114 
3115 emit_token_with_val:
3116 	if (PARSER_MODE()) {
3117 		ZEND_ASSERT(Z_TYPE_P(zendlval) != IS_UNDEF);
3118 		elem->ast = zend_ast_create_zval_with_lineno(zendlval, start_line);
3119 	}
3120 
3121 emit_token:
3122 	if (SCNG(on_event)) {
3123 		SCNG(on_event)(ON_TOKEN, token, start_line, yytext, yyleng, SCNG(on_event_context));
3124 	}
3125 	return token;
3126 
3127 emit_token_with_ident:
3128 	if (PARSER_MODE()) {
3129 		elem->ident = SCNG(yy_text);
3130 	}
3131 	if (SCNG(on_event)) {
3132 		SCNG(on_event)(ON_TOKEN, token, start_line, yytext, yyleng, SCNG(on_event_context));
3133 	}
3134 	return token;
3135 
3136 return_whitespace:
3137 	HANDLE_NEWLINES(yytext, yyleng);
3138 	if (SCNG(on_event)) {
3139 		SCNG(on_event)(ON_TOKEN, T_WHITESPACE, start_line, yytext, yyleng, SCNG(on_event_context));
3140 	}
3141 	if (PARSER_MODE()) {
3142 		start_line = CG(zend_lineno);
3143 		goto restart;
3144 	} else {
3145 		return T_WHITESPACE;
3146 	}
3147 
3148 skip_token:
3149 	if (SCNG(on_event)) {
3150 		SCNG(on_event)(ON_TOKEN, token, start_line, yytext, yyleng, SCNG(on_event_context));
3151 	}
3152 	start_line = CG(zend_lineno);
3153 	goto restart;
3154 }
3155