xref: /PHP-8.1/Zend/zend_language_scanner.l (revision 76348f33)
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 		zend_init_static_variables_map_ptr(op_array);
624 		pass_two(op_array);
625 		zend_oparray_context_end(&original_oparray_context);
626 		zend_file_context_end(&original_file_context);
627 
628 		CG(active_op_array) = original_active_op_array;
629 	}
630 
631 	zend_ast_destroy(CG(ast));
632 	zend_arena_destroy(CG(ast_arena));
633 
634 	CG(in_compilation) = original_in_compilation;
635 
636 	return op_array;
637 }
638 
compile_file(zend_file_handle * file_handle,int type)639 ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type)
640 {
641 	zend_lex_state original_lex_state;
642 	zend_op_array *op_array = NULL;
643 	zend_save_lexical_state(&original_lex_state);
644 
645 	if (open_file_for_scanning(file_handle)==FAILURE) {
646 		if (!EG(exception)) {
647 			if (type==ZEND_REQUIRE) {
648 				zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, ZSTR_VAL(file_handle->filename));
649 			} else {
650 				zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, ZSTR_VAL(file_handle->filename));
651 			}
652 		}
653 	} else {
654 		op_array = zend_compile(ZEND_USER_FUNCTION);
655 	}
656 
657 	zend_restore_lexical_state(&original_lex_state);
658 	return op_array;
659 }
660 
zend_compile_string_to_ast(zend_string * code,zend_arena ** ast_arena,zend_string * filename)661 ZEND_API zend_ast *zend_compile_string_to_ast(
662 		zend_string *code, zend_arena **ast_arena, zend_string *filename) {
663 	zval code_zv;
664 	bool original_in_compilation;
665 	zend_lex_state original_lex_state;
666 	zend_ast *ast;
667 
668 	ZVAL_STR_COPY(&code_zv, code);
669 
670 	original_in_compilation = CG(in_compilation);
671 	CG(in_compilation) = 1;
672 
673 	zend_save_lexical_state(&original_lex_state);
674 	zend_prepare_string_for_scanning(&code_zv, filename);
675 	CG(ast) = NULL;
676 	CG(ast_arena) = zend_arena_create(1024 * 32);
677 	LANG_SCNG(yy_state) = yycINITIAL;
678 
679 	if (zendparse() != 0) {
680 		zend_ast_destroy(CG(ast));
681 		zend_arena_destroy(CG(ast_arena));
682 		CG(ast) = NULL;
683 	}
684 
685 	/* restore_lexical_state changes CG(ast) and CG(ast_arena) */
686 	ast = CG(ast);
687 	*ast_arena = CG(ast_arena);
688 
689 	zend_restore_lexical_state(&original_lex_state);
690 	CG(in_compilation) = original_in_compilation;
691 
692 	zval_ptr_dtor_str(&code_zv);
693 
694 	return ast;
695 }
696 
compile_filename(int type,zend_string * filename)697 zend_op_array *compile_filename(int type, zend_string *filename)
698 {
699 	zend_file_handle file_handle;
700 	zend_op_array *retval;
701 	zend_string *opened_path = NULL;
702 
703 	zend_stream_init_filename_ex(&file_handle, filename);
704 
705 	retval = zend_compile_file(&file_handle, type);
706 	if (retval && file_handle.handle.stream.handle) {
707 		if (!file_handle.opened_path) {
708 			file_handle.opened_path = opened_path = zend_string_copy(filename);
709 		}
710 
711 		zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path);
712 
713 		if (opened_path) {
714 			zend_string_release_ex(opened_path, 0);
715 		}
716 	}
717 	zend_destroy_file_handle(&file_handle);
718 
719 	return retval;
720 }
721 
zend_prepare_string_for_scanning(zval * str,zend_string * filename)722 ZEND_API void zend_prepare_string_for_scanning(zval *str, zend_string *filename)
723 {
724 	char *buf;
725 	size_t size, old_len;
726 
727 	/* enforce ZEND_MMAP_AHEAD trailing NULLs for flex... */
728 	old_len = Z_STRLEN_P(str);
729 	Z_STR_P(str) = zend_string_extend(Z_STR_P(str), old_len + ZEND_MMAP_AHEAD, 0);
730 	Z_TYPE_INFO_P(str) = IS_STRING_EX;
731 	memset(Z_STRVAL_P(str) + old_len, 0, ZEND_MMAP_AHEAD + 1);
732 
733 	SCNG(yy_in) = NULL;
734 	SCNG(yy_start) = NULL;
735 
736 	buf = Z_STRVAL_P(str);
737 	size = old_len;
738 
739 	if (CG(multibyte)) {
740 		SCNG(script_org) = (unsigned char*)buf;
741 		SCNG(script_org_size) = size;
742 		SCNG(script_filtered) = NULL;
743 
744 		zend_multibyte_set_filter(zend_multibyte_get_internal_encoding());
745 
746 		if (SCNG(input_filter)) {
747 			if ((size_t)-1 == SCNG(input_filter)(&SCNG(script_filtered), &SCNG(script_filtered_size), SCNG(script_org), SCNG(script_org_size))) {
748 				zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
749 						"encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
750 			}
751 			buf = (char*)SCNG(script_filtered);
752 			size = SCNG(script_filtered_size);
753 		}
754 	}
755 
756 	yy_scan_buffer(buf, size);
757 	zend_set_compiled_filename(filename);
758 	CG(zend_lineno) = 1;
759 	CG(increment_lineno) = 0;
760 	RESET_DOC_COMMENT();
761 }
762 
763 
zend_get_scanned_file_offset(void)764 ZEND_API size_t zend_get_scanned_file_offset(void)
765 {
766 	size_t offset = SCNG(yy_cursor) - SCNG(yy_start);
767 	if (SCNG(input_filter)) {
768 		size_t original_offset = offset, length = 0;
769 		do {
770 			unsigned char *p = NULL;
771 			if ((size_t)-1 == SCNG(input_filter)(&p, &length, SCNG(script_org), offset)) {
772 				return (size_t)-1;
773 			}
774 			efree(p);
775 			if (length > original_offset) {
776 				offset--;
777 			} else if (length < original_offset) {
778 				offset++;
779 			}
780 		} while (original_offset != length);
781 	}
782 	return offset;
783 }
784 
compile_string(zend_string * source_string,const char * filename)785 zend_op_array *compile_string(zend_string *source_string, const char *filename)
786 {
787 	zend_lex_state original_lex_state;
788 	zend_op_array *op_array = NULL;
789 	zval tmp;
790 	zend_string *filename_str;
791 
792 	if (ZSTR_LEN(source_string) == 0) {
793 		return NULL;
794 	}
795 
796 	ZVAL_STR_COPY(&tmp, source_string);
797 
798 	zend_save_lexical_state(&original_lex_state);
799 	filename_str = zend_string_init(filename, strlen(filename), 0);
800 	zend_prepare_string_for_scanning(&tmp, filename_str);
801 	zend_string_release(filename_str);
802 	BEGIN(ST_IN_SCRIPTING);
803 	op_array = zend_compile(ZEND_EVAL_CODE);
804 
805 	zend_restore_lexical_state(&original_lex_state);
806 	zval_ptr_dtor(&tmp);
807 
808 	return op_array;
809 }
810 
811 
highlight_file(const char * filename,zend_syntax_highlighter_ini * syntax_highlighter_ini)812 zend_result highlight_file(const char *filename, zend_syntax_highlighter_ini *syntax_highlighter_ini)
813 {
814 	zend_lex_state original_lex_state;
815 	zend_file_handle file_handle;
816 
817 	zend_stream_init_filename(&file_handle, filename);
818 	zend_save_lexical_state(&original_lex_state);
819 	if (open_file_for_scanning(&file_handle)==FAILURE) {
820 		zend_message_dispatcher(ZMSG_FAILED_HIGHLIGHT_FOPEN, filename);
821 		zend_destroy_file_handle(&file_handle);
822 		zend_restore_lexical_state(&original_lex_state);
823 		return FAILURE;
824 	}
825 	zend_highlight(syntax_highlighter_ini);
826 	if (SCNG(script_filtered)) {
827 		efree(SCNG(script_filtered));
828 		SCNG(script_filtered) = NULL;
829 	}
830 	zend_destroy_file_handle(&file_handle);
831 	zend_restore_lexical_state(&original_lex_state);
832 	return SUCCESS;
833 }
834 
highlight_string(zend_string * str,zend_syntax_highlighter_ini * syntax_highlighter_ini,const char * filename)835 void highlight_string(zend_string *str, zend_syntax_highlighter_ini *syntax_highlighter_ini, const char *filename)
836 {
837 	zend_lex_state original_lex_state;
838 	zval str_zv;
839 	zend_string *filename_str = zend_string_init(filename, strlen(filename), 0);
840 	ZVAL_STR_COPY(&str_zv, str);
841 	zend_save_lexical_state(&original_lex_state);
842 	zend_prepare_string_for_scanning(&str_zv, filename_str);
843 	zend_string_release(filename_str);
844 	BEGIN(INITIAL);
845 	zend_highlight(syntax_highlighter_ini);
846 	if (SCNG(script_filtered)) {
847 		efree(SCNG(script_filtered));
848 		SCNG(script_filtered) = NULL;
849 	}
850 	zend_restore_lexical_state(&original_lex_state);
851 	zval_ptr_dtor(&str_zv);
852 }
853 
zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter,const zend_encoding * old_encoding)854 ZEND_API void zend_multibyte_yyinput_again(zend_encoding_filter old_input_filter, const zend_encoding *old_encoding)
855 {
856 	size_t length;
857 	unsigned char *new_yy_start;
858 
859 	/* convert and set */
860 	if (!SCNG(input_filter)) {
861 		if (SCNG(script_filtered)) {
862 			efree(SCNG(script_filtered));
863 			SCNG(script_filtered) = NULL;
864 		}
865 		SCNG(script_filtered_size) = 0;
866 		length = SCNG(script_org_size);
867 		new_yy_start = SCNG(script_org);
868 	} else {
869 		if ((size_t)-1 == SCNG(input_filter)(&new_yy_start, &length, SCNG(script_org), SCNG(script_org_size))) {
870 			zend_error_noreturn(E_COMPILE_ERROR, "Could not convert the script from the detected "
871 					"encoding \"%s\" to a compatible encoding", zend_multibyte_get_encoding_name(LANG_SCNG(script_encoding)));
872 		}
873 		if (SCNG(script_filtered)) {
874 			efree(SCNG(script_filtered));
875 		}
876 		SCNG(script_filtered) = new_yy_start;
877 		SCNG(script_filtered_size) = length;
878 	}
879 
880 	SCNG(yy_cursor) = new_yy_start + (SCNG(yy_cursor) - SCNG(yy_start));
881 	SCNG(yy_marker) = new_yy_start + (SCNG(yy_marker) - SCNG(yy_start));
882 	SCNG(yy_text) = new_yy_start + (SCNG(yy_text) - SCNG(yy_start));
883 	SCNG(yy_limit) = new_yy_start + length;
884 
885 	SCNG(yy_start) = new_yy_start;
886 }
887 
888 
889 // TODO: avoid reallocation ???
890 # define zend_copy_value(zendlval, yytext, yyleng) \
891 	if (SCNG(output_filter)) { \
892 		size_t sz = 0; \
893 		char *s = NULL; \
894 		SCNG(output_filter)((unsigned char **)&s, &sz, (unsigned char *)yytext, (size_t)yyleng); \
895 		ZVAL_STRINGL(zendlval, s, sz); \
896 		efree(s); \
897 	} else if (yyleng == 1) { \
898 		ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR((zend_uchar)*(yytext))); \
899 	} else { \
900 		ZVAL_STRINGL(zendlval, yytext, yyleng); \
901 	}
902 
zend_scan_escape_string(zval * zendlval,char * str,int len,char quote_type)903 static zend_result zend_scan_escape_string(zval *zendlval, char *str, int len, char quote_type)
904 {
905 	char *s, *t;
906 	char *end;
907 
908 	if (len <= 1) {
909 		if (len < 1) {
910 			ZVAL_EMPTY_STRING(zendlval);
911 		} else {
912 			zend_uchar c = (zend_uchar)*str;
913 			if (c == '\n' || c == '\r') {
914 				CG(zend_lineno)++;
915 			}
916 			ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR(c));
917 		}
918 		goto skip_escape_conversion;
919 	}
920 
921 	ZVAL_STRINGL(zendlval, str, len);
922 
923 	/* convert escape sequences */
924 	s = Z_STRVAL_P(zendlval);
925 	end = s+Z_STRLEN_P(zendlval);
926 	while (1) {
927 		if (UNEXPECTED(*s=='\\')) {
928 			break;
929 		}
930 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
931 			CG(zend_lineno)++;
932 		}
933 		s++;
934 		if (s == end) {
935 			goto skip_escape_conversion;
936 		}
937 	}
938 
939 	t = s;
940 	while (s<end) {
941 		if (*s=='\\') {
942 			s++;
943 			if (s >= end) {
944 				*t++ = '\\';
945 				break;
946 			}
947 
948 			switch(*s) {
949 				case 'n':
950 					*t++ = '\n';
951 					break;
952 				case 'r':
953 					*t++ = '\r';
954 					break;
955 				case 't':
956 					*t++ = '\t';
957 					break;
958 				case 'f':
959 					*t++ = '\f';
960 					break;
961 				case 'v':
962 					*t++ = '\v';
963 					break;
964 				case 'e':
965 #ifdef ZEND_WIN32
966 					*t++ = VK_ESCAPE;
967 #else
968 					*t++ = '\e';
969 #endif
970 					break;
971 				case '"':
972 				case '`':
973 					if (*s != quote_type) {
974 						*t++ = '\\';
975 						*t++ = *s;
976 						break;
977 					}
978 					ZEND_FALLTHROUGH;
979 				case '\\':
980 				case '$':
981 					*t++ = *s;
982 					break;
983 				case 'x':
984 				case 'X':
985 					if (ZEND_IS_HEX(*(s+1))) {
986 						char hex_buf[3] = { 0, 0, 0 };
987 
988 						hex_buf[0] = *(++s);
989 						if (ZEND_IS_HEX(*(s+1))) {
990 							hex_buf[1] = *(++s);
991 						}
992 						*t++ = (char) ZEND_STRTOL(hex_buf, NULL, 16);
993 					} else {
994 						*t++ = '\\';
995 						*t++ = *s;
996 					}
997 					break;
998 				/* UTF-8 codepoint escape, format: /\\u\{\x+\}/ */
999 				case 'u':
1000 					{
1001 						/* cache where we started so we can parse after validating */
1002 						char *start = s + 1;
1003 						size_t len = 0;
1004 						bool valid = 1;
1005 						unsigned long codepoint;
1006 
1007 						if (*start != '{') {
1008 							/* we silently let this pass to avoid breaking code
1009 							 * with JSON in string literals (e.g. "\"\u202e\""
1010 							 */
1011 							*t++ = '\\';
1012 							*t++ = 'u';
1013 							break;
1014 						} else {
1015 							/* on the other hand, invalid \u{blah} errors */
1016 							s++;
1017 							len++;
1018 							s++;
1019 							while (*s != '}') {
1020 								if (!ZEND_IS_HEX(*s)) {
1021 									valid = 0;
1022 									break;
1023 								} else {
1024 									len++;
1025 								}
1026 								s++;
1027 							}
1028 							if (*s == '}') {
1029 								valid = 1;
1030 								len++;
1031 							}
1032 						}
1033 
1034 						/* \u{} is invalid */
1035 						if (len <= 2) {
1036 							valid = 0;
1037 						}
1038 
1039 						if (!valid) {
1040 							zend_throw_exception(zend_ce_parse_error,
1041 								"Invalid UTF-8 codepoint escape sequence", 0);
1042 							zval_ptr_dtor(zendlval);
1043 							ZVAL_UNDEF(zendlval);
1044 							return FAILURE;
1045 						}
1046 
1047 						errno = 0;
1048 						codepoint = strtoul(start + 1, NULL, 16);
1049 
1050 						/* per RFC 3629, UTF-8 can only represent 21 bits */
1051 						if (codepoint > 0x10FFFF || errno) {
1052 							zend_throw_exception(zend_ce_parse_error,
1053 								"Invalid UTF-8 codepoint escape sequence: Codepoint too large", 0);
1054 							zval_ptr_dtor(zendlval);
1055 							ZVAL_UNDEF(zendlval);
1056 							return FAILURE;
1057 						}
1058 
1059 						/* based on https://en.wikipedia.org/wiki/UTF-8#Sample_code */
1060 						if (codepoint < 0x80) {
1061 							*t++ = codepoint;
1062 						} else if (codepoint <= 0x7FF) {
1063 							*t++ = (codepoint >> 6) + 0xC0;
1064 							*t++ = (codepoint & 0x3F) + 0x80;
1065 						} else if (codepoint <= 0xFFFF) {
1066 							*t++ = (codepoint >> 12) + 0xE0;
1067 							*t++ = ((codepoint >> 6) & 0x3F) + 0x80;
1068 							*t++ = (codepoint & 0x3F) + 0x80;
1069 						} else if (codepoint <= 0x10FFFF) {
1070 							*t++ = (codepoint >> 18) + 0xF0;
1071 							*t++ = ((codepoint >> 12) & 0x3F) + 0x80;
1072 							*t++ = ((codepoint >> 6) & 0x3F) + 0x80;
1073 							*t++ = (codepoint & 0x3F) + 0x80;
1074 						}
1075 					}
1076 					break;
1077 				default:
1078 					/* check for an octal */
1079 					if (ZEND_IS_OCT(*s)) {
1080 						char octal_buf[4] = { 0, 0, 0, 0 };
1081 
1082 						octal_buf[0] = *s;
1083 						if (ZEND_IS_OCT(*(s+1))) {
1084 							octal_buf[1] = *(++s);
1085 							if (ZEND_IS_OCT(*(s+1))) {
1086 								octal_buf[2] = *(++s);
1087 							}
1088 						}
1089 						if (octal_buf[2] && (octal_buf[0] > '3') && !SCNG(heredoc_scan_ahead)) {
1090 							/* 3 octit values must not overflow 0xFF (\377) */
1091 							zend_error(E_COMPILE_WARNING, "Octal escape sequence overflow \\%s is greater than \\377", octal_buf);
1092 						}
1093 
1094 						*t++ = (char) ZEND_STRTOL(octal_buf, NULL, 8);
1095 					} else {
1096 						*t++ = '\\';
1097 						*t++ = *s;
1098 					}
1099 					break;
1100 			}
1101 		} else {
1102 			*t++ = *s;
1103 		}
1104 
1105 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
1106 			CG(zend_lineno)++;
1107 		}
1108 		s++;
1109 	}
1110 	*t = 0;
1111 	Z_STRLEN_P(zendlval) = t - Z_STRVAL_P(zendlval);
1112 
1113 skip_escape_conversion:
1114 	if (SCNG(output_filter)) {
1115 		size_t sz = 0;
1116 		unsigned char *str;
1117 		// TODO: avoid realocation ???
1118 		s = Z_STRVAL_P(zendlval);
1119 		SCNG(output_filter)(&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
1120 		zval_ptr_dtor(zendlval);
1121 		ZVAL_STRINGL(zendlval, (char *) str, sz);
1122 		efree(str);
1123 	}
1124 	return SUCCESS;
1125 }
1126 
1127 #define HEREDOC_USING_SPACES 1
1128 #define HEREDOC_USING_TABS 2
1129 
next_newline(const char * str,const char * end,size_t * newline_len)1130 static const char *next_newline(const char *str, const char *end, size_t *newline_len) {
1131 	for (; str < end; str++) {
1132 		if (*str == '\r') {
1133 			*newline_len = str + 1 < end && *(str + 1) == '\n' ? 2 : 1;
1134 			return str;
1135 		} else if (*str == '\n') {
1136 			*newline_len = 1;
1137 			return str;
1138 		}
1139 	}
1140 	*newline_len = 0;
1141 	return NULL;
1142 }
1143 
strip_multiline_string_indentation(zval * zendlval,int indentation,bool using_spaces,bool newline_at_start,bool newline_at_end)1144 static bool strip_multiline_string_indentation(
1145 	zval *zendlval, int indentation, bool using_spaces,
1146 	bool newline_at_start, bool newline_at_end)
1147 {
1148 	const char *str = Z_STRVAL_P(zendlval), *end = str + Z_STRLEN_P(zendlval);
1149 	char *copy = Z_STRVAL_P(zendlval);
1150 
1151 	int newline_count = 0;
1152 	size_t newline_len;
1153 	const char *nl;
1154 
1155 	if (!newline_at_start) {
1156 		nl = next_newline(str, end, &newline_len);
1157 		if (!nl) {
1158 			return 1;
1159 		}
1160 
1161 		str = nl + newline_len;
1162 		copy = (char *) nl + newline_len;
1163 		newline_count++;
1164 	} else {
1165 		nl = str;
1166 	}
1167 
1168 	/* <= intentional */
1169 	while (str <= end && nl) {
1170 		size_t skip;
1171 		nl = next_newline(str, end, &newline_len);
1172 		if (!nl && newline_at_end) {
1173 			nl = end;
1174 		}
1175 
1176 		/* Try to skip indentation */
1177 		for (skip = 0; skip < indentation; skip++, str++) {
1178 			if (str == nl) {
1179 				/* Don't require full indentation on whitespace-only lines */
1180 				break;
1181 			}
1182 
1183 			if (str == end || (*str != ' ' && *str != '\t')) {
1184 				CG(zend_lineno) += newline_count;
1185 				zend_throw_exception_ex(zend_ce_parse_error, 0,
1186 					"Invalid body indentation level (expecting an indentation level of at least %d)", indentation);
1187 				goto error;
1188 			}
1189 
1190 			if ((!using_spaces && *str == ' ') || (using_spaces && *str == '\t')) {
1191 				CG(zend_lineno) += newline_count;
1192 				zend_throw_exception(zend_ce_parse_error,
1193 					"Invalid indentation - tabs and spaces cannot be mixed", 0);
1194 				goto error;
1195 			}
1196 		}
1197 
1198 		if (str == end) {
1199 			break;
1200 		}
1201 
1202 		size_t len = nl ? (nl - str + newline_len) : (end - str);
1203 		memmove(copy, str, len);
1204 		str += len;
1205 		copy += len;
1206 		newline_count++;
1207 	}
1208 
1209 	*copy = '\0';
1210 	Z_STRLEN_P(zendlval) = copy - Z_STRVAL_P(zendlval);
1211 	return 1;
1212 
1213 error:
1214 	zval_ptr_dtor_str(zendlval);
1215 	ZVAL_UNDEF(zendlval);
1216 
1217 	return 0;
1218 }
1219 
copy_heredoc_label_stack(void * void_heredoc_label)1220 static void copy_heredoc_label_stack(void *void_heredoc_label)
1221 {
1222 	zend_heredoc_label *heredoc_label = void_heredoc_label;
1223 	zend_heredoc_label *new_heredoc_label = emalloc(sizeof(zend_heredoc_label));
1224 
1225 	*new_heredoc_label = *heredoc_label;
1226 	new_heredoc_label->label = estrndup(heredoc_label->label, heredoc_label->length);
1227 
1228 	zend_ptr_stack_push(&SCNG(heredoc_label_stack), (void *) new_heredoc_label);
1229 }
1230 
1231 /* Check that { }, [ ], ( ) are nested correctly */
report_bad_nesting(char opening,int opening_lineno,char closing)1232 static void report_bad_nesting(char opening, int opening_lineno, char closing)
1233 {
1234 	char   buf[256];
1235 	size_t used = 0;
1236 
1237 	used = snprintf(buf, sizeof(buf), "Unclosed '%c'", opening);
1238 
1239 	if (opening_lineno != CG(zend_lineno)) {
1240 		used += snprintf(buf + used, sizeof(buf) - used, " on line %d", opening_lineno);
1241 	}
1242 
1243 	if (closing) { 	/* 'closing' will be 0 if at end of file */
1244 		used += snprintf(buf + used, sizeof(buf) - used, " does not match '%c'", closing);
1245 	}
1246 
1247 	zend_throw_exception(zend_ce_parse_error, buf, 0);
1248 }
1249 
enter_nesting(char opening)1250 static void enter_nesting(char opening)
1251 {
1252 	zend_nest_location nest_loc = {opening, CG(zend_lineno)};
1253 	zend_stack_push(&SCNG(nest_location_stack), &nest_loc);
1254 }
1255 
exit_nesting(char closing)1256 static zend_result exit_nesting(char closing)
1257 {
1258 	if (zend_stack_is_empty(&SCNG(nest_location_stack))) {
1259 		zend_throw_exception_ex(zend_ce_parse_error, 0, "Unmatched '%c'", closing);
1260 		return FAILURE;
1261 	}
1262 
1263 	zend_nest_location *nest_loc = zend_stack_top(&SCNG(nest_location_stack));
1264 	char opening = nest_loc->text;
1265 
1266 	if ((opening == '{' && closing != '}') ||
1267 	    (opening == '[' && closing != ']') ||
1268 	    (opening == '(' && closing != ')')) {
1269 		report_bad_nesting(opening, nest_loc->lineno, closing);
1270 		return FAILURE;
1271 	}
1272 
1273 	zend_stack_del_top(&SCNG(nest_location_stack));
1274 	return SUCCESS;
1275 }
1276 
check_nesting_at_end(void)1277 static zend_result check_nesting_at_end(void)
1278 {
1279 	if (!zend_stack_is_empty(&SCNG(nest_location_stack))) {
1280 		zend_nest_location *nest_loc = zend_stack_top(&SCNG(nest_location_stack));
1281 		report_bad_nesting(nest_loc->text, nest_loc->lineno, 0);
1282 		return FAILURE;
1283 	}
1284 
1285 	return SUCCESS;
1286 }
1287 
1288 #define PARSER_MODE() \
1289 	EXPECTED(elem != NULL)
1290 
1291 #define RETURN_TOKEN(_token) do { \
1292 		token = _token; \
1293 		goto emit_token; \
1294 	} while (0)
1295 
1296 #define RETURN_TOKEN_WITH_VAL(_token) do { \
1297 		token = _token; \
1298 		goto emit_token_with_val; \
1299 	} while (0)
1300 
1301 #define RETURN_TOKEN_WITH_STR(_token, _offset) do { \
1302 		token = _token; \
1303 		offset = _offset; \
1304 		goto emit_token_with_str; \
1305 	} while (0)
1306 
1307 #define RETURN_TOKEN_WITH_IDENT(_token) do { \
1308 		token = _token; \
1309 		goto emit_token_with_ident; \
1310 	} while (0)
1311 
1312 #define RETURN_OR_SKIP_TOKEN(_token) do { \
1313 		token = _token; \
1314 		if (PARSER_MODE()) { \
1315 			goto skip_token; \
1316 		} \
1317 		goto emit_token; \
1318 	} while (0)
1319 
1320 #define RETURN_EXIT_NESTING_TOKEN(_token) do { \
1321 		if (exit_nesting(_token) && PARSER_MODE()) { \
1322 			RETURN_TOKEN(T_ERROR); \
1323 		} else { \
1324 			RETURN_TOKEN(_token); \
1325 		} \
1326 	} while(0)
1327 
1328 #define RETURN_END_TOKEN do { \
1329 		if (check_nesting_at_end() && PARSER_MODE()) { \
1330 			RETURN_TOKEN(T_ERROR); \
1331 		} else { \
1332 			RETURN_TOKEN(END); \
1333 		} \
1334 	} while (0)
1335 
lex_scan(zval * zendlval,zend_parser_stack_elem * elem)1336 int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem)
1337 {
1338 int token;
1339 int offset;
1340 int start_line = CG(zend_lineno);
1341 
1342 	ZVAL_UNDEF(zendlval);
1343 restart:
1344 	SCNG(yy_text) = YYCURSOR;
1345 
1346 /*!re2c
1347 re2c:yyfill:check = 0;
1348 LNUM	[0-9]+(_[0-9]+)*
1349 DNUM	({LNUM}?"."{LNUM})|({LNUM}"."{LNUM}?)
1350 EXPONENT_DNUM	(({LNUM}|{DNUM})[eE][+-]?{LNUM})
1351 HNUM	"0x"[0-9a-fA-F]+(_[0-9a-fA-F]+)*
1352 BNUM	"0b"[01]+(_[01]+)*
1353 ONUM	"0o"[0-7]+(_[0-7]+)*
1354 LABEL	[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*
1355 WHITESPACE [ \n\r\t]+
1356 TABS_AND_SPACES [ \t]*
1357 TOKENS [;:,.|^&+-/*=%!~$<>?@]
1358 ANY_CHAR [^]
1359 NEWLINE ("\r"|"\n"|"\r\n")
1360 
1361 /* compute yyleng before each rule */
1362 <!*> := yyleng = YYCURSOR - SCNG(yy_text);
1363 
1364 <ST_IN_SCRIPTING>"exit" {
1365 	RETURN_TOKEN_WITH_IDENT(T_EXIT);
1366 }
1367 
1368 <ST_IN_SCRIPTING>"die" {
1369 	RETURN_TOKEN_WITH_IDENT(T_EXIT);
1370 }
1371 
1372 <ST_IN_SCRIPTING>"fn" {
1373 	RETURN_TOKEN_WITH_IDENT(T_FN);
1374 }
1375 
1376 <ST_IN_SCRIPTING>"function" {
1377 	RETURN_TOKEN_WITH_IDENT(T_FUNCTION);
1378 }
1379 
1380 <ST_IN_SCRIPTING>"const" {
1381 	RETURN_TOKEN_WITH_IDENT(T_CONST);
1382 }
1383 
1384 <ST_IN_SCRIPTING>"return" {
1385 	RETURN_TOKEN_WITH_IDENT(T_RETURN);
1386 }
1387 
1388 <ST_IN_SCRIPTING>"#[" {
1389 	enter_nesting('[');
1390 	RETURN_TOKEN(T_ATTRIBUTE);
1391 }
1392 
1393 <ST_IN_SCRIPTING>"yield"{WHITESPACE}"from"[^a-zA-Z0-9_\x80-\xff] {
1394 	yyless(yyleng - 1);
1395 	HANDLE_NEWLINES(yytext, yyleng);
1396 	RETURN_TOKEN_WITH_IDENT(T_YIELD_FROM);
1397 }
1398 
1399 <ST_IN_SCRIPTING>"yield" {
1400 	RETURN_TOKEN_WITH_IDENT(T_YIELD);
1401 }
1402 
1403 <ST_IN_SCRIPTING>"try" {
1404 	RETURN_TOKEN_WITH_IDENT(T_TRY);
1405 }
1406 
1407 <ST_IN_SCRIPTING>"catch" {
1408 	RETURN_TOKEN_WITH_IDENT(T_CATCH);
1409 }
1410 
1411 <ST_IN_SCRIPTING>"finally" {
1412 	RETURN_TOKEN_WITH_IDENT(T_FINALLY);
1413 }
1414 
1415 <ST_IN_SCRIPTING>"throw" {
1416 	RETURN_TOKEN_WITH_IDENT(T_THROW);
1417 }
1418 
1419 <ST_IN_SCRIPTING>"if" {
1420 	RETURN_TOKEN_WITH_IDENT(T_IF);
1421 }
1422 
1423 <ST_IN_SCRIPTING>"elseif" {
1424 	RETURN_TOKEN_WITH_IDENT(T_ELSEIF);
1425 }
1426 
1427 <ST_IN_SCRIPTING>"endif" {
1428 	RETURN_TOKEN_WITH_IDENT(T_ENDIF);
1429 }
1430 
1431 <ST_IN_SCRIPTING>"else" {
1432 	RETURN_TOKEN_WITH_IDENT(T_ELSE);
1433 }
1434 
1435 <ST_IN_SCRIPTING>"while" {
1436 	RETURN_TOKEN_WITH_IDENT(T_WHILE);
1437 }
1438 
1439 <ST_IN_SCRIPTING>"endwhile" {
1440 	RETURN_TOKEN_WITH_IDENT(T_ENDWHILE);
1441 }
1442 
1443 <ST_IN_SCRIPTING>"do" {
1444 	RETURN_TOKEN_WITH_IDENT(T_DO);
1445 }
1446 
1447 <ST_IN_SCRIPTING>"for" {
1448 	RETURN_TOKEN_WITH_IDENT(T_FOR);
1449 }
1450 
1451 <ST_IN_SCRIPTING>"endfor" {
1452 	RETURN_TOKEN_WITH_IDENT(T_ENDFOR);
1453 }
1454 
1455 <ST_IN_SCRIPTING>"foreach" {
1456 	RETURN_TOKEN_WITH_IDENT(T_FOREACH);
1457 }
1458 
1459 <ST_IN_SCRIPTING>"endforeach" {
1460 	RETURN_TOKEN_WITH_IDENT(T_ENDFOREACH);
1461 }
1462 
1463 <ST_IN_SCRIPTING>"declare" {
1464 	RETURN_TOKEN_WITH_IDENT(T_DECLARE);
1465 }
1466 
1467 <ST_IN_SCRIPTING>"enddeclare" {
1468 	RETURN_TOKEN_WITH_IDENT(T_ENDDECLARE);
1469 }
1470 
1471 <ST_IN_SCRIPTING>"instanceof" {
1472 	RETURN_TOKEN_WITH_IDENT(T_INSTANCEOF);
1473 }
1474 
1475 <ST_IN_SCRIPTING>"as" {
1476 	RETURN_TOKEN_WITH_IDENT(T_AS);
1477 }
1478 
1479 <ST_IN_SCRIPTING>"switch" {
1480 	RETURN_TOKEN_WITH_IDENT(T_SWITCH);
1481 }
1482 
1483 <ST_IN_SCRIPTING>"match" {
1484 	RETURN_TOKEN_WITH_IDENT(T_MATCH);
1485 }
1486 
1487 <ST_IN_SCRIPTING>"endswitch" {
1488 	RETURN_TOKEN_WITH_IDENT(T_ENDSWITCH);
1489 }
1490 
1491 <ST_IN_SCRIPTING>"case" {
1492 	RETURN_TOKEN_WITH_IDENT(T_CASE);
1493 }
1494 
1495 <ST_IN_SCRIPTING>"default" {
1496 	RETURN_TOKEN_WITH_IDENT(T_DEFAULT);
1497 }
1498 
1499 <ST_IN_SCRIPTING>"break" {
1500 	RETURN_TOKEN_WITH_IDENT(T_BREAK);
1501 }
1502 
1503 <ST_IN_SCRIPTING>"continue" {
1504 	RETURN_TOKEN_WITH_IDENT(T_CONTINUE);
1505 }
1506 
1507 <ST_IN_SCRIPTING>"goto" {
1508 	RETURN_TOKEN_WITH_IDENT(T_GOTO);
1509 }
1510 
1511 <ST_IN_SCRIPTING>"echo" {
1512 	RETURN_TOKEN_WITH_IDENT(T_ECHO);
1513 }
1514 
1515 <ST_IN_SCRIPTING>"print" {
1516 	RETURN_TOKEN_WITH_IDENT(T_PRINT);
1517 }
1518 
1519 <ST_IN_SCRIPTING>"class" {
1520 	RETURN_TOKEN_WITH_IDENT(T_CLASS);
1521 }
1522 
1523 <ST_IN_SCRIPTING>"interface" {
1524 	RETURN_TOKEN_WITH_IDENT(T_INTERFACE);
1525 }
1526 
1527 <ST_IN_SCRIPTING>"trait" {
1528 	RETURN_TOKEN_WITH_IDENT(T_TRAIT);
1529 }
1530 
1531 /*
1532  * The enum keyword must be followed by whitespace and another identifier.
1533  * This avoids the BC break of using enum in classes, namespaces, functions and constants.
1534  */
1535 <ST_IN_SCRIPTING>"enum"{WHITESPACE}("extends"|"implements") {
1536 	yyless(4);
1537 	RETURN_TOKEN_WITH_STR(T_STRING, 0);
1538 }
1539 <ST_IN_SCRIPTING>"enum"{WHITESPACE}[a-zA-Z_\x80-\xff] {
1540 	yyless(4);
1541 	RETURN_TOKEN_WITH_IDENT(T_ENUM);
1542 }
1543 
1544 <ST_IN_SCRIPTING>"extends" {
1545 	RETURN_TOKEN_WITH_IDENT(T_EXTENDS);
1546 }
1547 
1548 <ST_IN_SCRIPTING>"implements" {
1549 	RETURN_TOKEN_WITH_IDENT(T_IMPLEMENTS);
1550 }
1551 
1552 <ST_IN_SCRIPTING>"->" {
1553 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
1554 	RETURN_TOKEN(T_OBJECT_OPERATOR);
1555 }
1556 
1557 <ST_IN_SCRIPTING>"?->" {
1558 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
1559 	RETURN_TOKEN(T_NULLSAFE_OBJECT_OPERATOR);
1560 }
1561 
1562 <ST_IN_SCRIPTING,ST_LOOKING_FOR_PROPERTY>{WHITESPACE}+ {
1563 	goto return_whitespace;
1564 }
1565 
1566 <ST_LOOKING_FOR_PROPERTY>"->" {
1567 	RETURN_TOKEN(T_OBJECT_OPERATOR);
1568 }
1569 
1570 <ST_LOOKING_FOR_PROPERTY>"?->" {
1571 	RETURN_TOKEN(T_NULLSAFE_OBJECT_OPERATOR);
1572 }
1573 
1574 <ST_LOOKING_FOR_PROPERTY>{LABEL} {
1575 	yy_pop_state();
1576 	RETURN_TOKEN_WITH_STR(T_STRING, 0);
1577 }
1578 
1579 <ST_LOOKING_FOR_PROPERTY>{ANY_CHAR} {
1580 	yyless(0);
1581 	yy_pop_state();
1582 	goto restart;
1583 }
1584 
1585 <ST_IN_SCRIPTING>"::" {
1586 	RETURN_TOKEN(T_PAAMAYIM_NEKUDOTAYIM);
1587 }
1588 
1589 <ST_IN_SCRIPTING>"..." {
1590 	RETURN_TOKEN(T_ELLIPSIS);
1591 }
1592 
1593 <ST_IN_SCRIPTING>"??" {
1594 	RETURN_TOKEN(T_COALESCE);
1595 }
1596 
1597 <ST_IN_SCRIPTING>"new" {
1598 	RETURN_TOKEN_WITH_IDENT(T_NEW);
1599 }
1600 
1601 <ST_IN_SCRIPTING>"clone" {
1602 	RETURN_TOKEN_WITH_IDENT(T_CLONE);
1603 }
1604 
1605 <ST_IN_SCRIPTING>"var" {
1606 	RETURN_TOKEN_WITH_IDENT(T_VAR);
1607 }
1608 
1609 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"|"integer"){TABS_AND_SPACES}")" {
1610 	RETURN_TOKEN(T_INT_CAST);
1611 }
1612 
1613 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("double"|"float"){TABS_AND_SPACES}")" {
1614 	RETURN_TOKEN(T_DOUBLE_CAST);
1615 }
1616 
1617 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"real"{TABS_AND_SPACES}")" {
1618 	if (PARSER_MODE()) {
1619 		zend_throw_exception(zend_ce_parse_error, "The (real) cast has been removed, use (float) instead", 0);
1620 		RETURN_TOKEN(T_ERROR);
1621 	}
1622 	RETURN_TOKEN(T_DOUBLE_CAST);
1623 }
1624 
1625 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"|"binary"){TABS_AND_SPACES}")" {
1626 	RETURN_TOKEN(T_STRING_CAST);
1627 }
1628 
1629 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"array"{TABS_AND_SPACES}")" {
1630 	RETURN_TOKEN(T_ARRAY_CAST);
1631 }
1632 
1633 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"object"{TABS_AND_SPACES}")" {
1634 	RETURN_TOKEN(T_OBJECT_CAST);
1635 }
1636 
1637 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"|"boolean"){TABS_AND_SPACES}")" {
1638 	RETURN_TOKEN(T_BOOL_CAST);
1639 }
1640 
1641 <ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("unset"){TABS_AND_SPACES}")" {
1642 	RETURN_TOKEN(T_UNSET_CAST);
1643 }
1644 
1645 <ST_IN_SCRIPTING>"eval" {
1646 	RETURN_TOKEN_WITH_IDENT(T_EVAL);
1647 }
1648 
1649 <ST_IN_SCRIPTING>"include" {
1650 	RETURN_TOKEN_WITH_IDENT(T_INCLUDE);
1651 }
1652 
1653 <ST_IN_SCRIPTING>"include_once" {
1654 	RETURN_TOKEN_WITH_IDENT(T_INCLUDE_ONCE);
1655 }
1656 
1657 <ST_IN_SCRIPTING>"require" {
1658 	RETURN_TOKEN_WITH_IDENT(T_REQUIRE);
1659 }
1660 
1661 <ST_IN_SCRIPTING>"require_once" {
1662 	RETURN_TOKEN_WITH_IDENT(T_REQUIRE_ONCE);
1663 }
1664 
1665 <ST_IN_SCRIPTING>"namespace" {
1666 	RETURN_TOKEN_WITH_IDENT(T_NAMESPACE);
1667 }
1668 
1669 <ST_IN_SCRIPTING>"use" {
1670 	RETURN_TOKEN_WITH_IDENT(T_USE);
1671 }
1672 
1673 <ST_IN_SCRIPTING>"insteadof" {
1674     RETURN_TOKEN_WITH_IDENT(T_INSTEADOF);
1675 }
1676 
1677 <ST_IN_SCRIPTING>"global" {
1678 	RETURN_TOKEN_WITH_IDENT(T_GLOBAL);
1679 }
1680 
1681 <ST_IN_SCRIPTING>"isset" {
1682 	RETURN_TOKEN_WITH_IDENT(T_ISSET);
1683 }
1684 
1685 <ST_IN_SCRIPTING>"empty" {
1686 	RETURN_TOKEN_WITH_IDENT(T_EMPTY);
1687 }
1688 
1689 <ST_IN_SCRIPTING>"__halt_compiler" {
1690 	RETURN_TOKEN_WITH_IDENT(T_HALT_COMPILER);
1691 }
1692 
1693 <ST_IN_SCRIPTING>"static" {
1694 	RETURN_TOKEN_WITH_IDENT(T_STATIC);
1695 }
1696 
1697 <ST_IN_SCRIPTING>"abstract" {
1698 	RETURN_TOKEN_WITH_IDENT(T_ABSTRACT);
1699 }
1700 
1701 <ST_IN_SCRIPTING>"final" {
1702 	RETURN_TOKEN_WITH_IDENT(T_FINAL);
1703 }
1704 
1705 <ST_IN_SCRIPTING>"private" {
1706 	RETURN_TOKEN_WITH_IDENT(T_PRIVATE);
1707 }
1708 
1709 <ST_IN_SCRIPTING>"protected" {
1710 	RETURN_TOKEN_WITH_IDENT(T_PROTECTED);
1711 }
1712 
1713 <ST_IN_SCRIPTING>"public" {
1714 	RETURN_TOKEN_WITH_IDENT(T_PUBLIC);
1715 }
1716 
1717 <ST_IN_SCRIPTING>"readonly" {
1718 	RETURN_TOKEN_WITH_IDENT(T_READONLY);
1719 }
1720 
1721 /* Don't treat "readonly(" as a keyword, to allow using it as a function name. */
1722 <ST_IN_SCRIPTING>"readonly"[ \n\r\t]*"(" {
1723 	yyless(strlen("readonly"));
1724 	RETURN_TOKEN_WITH_STR(T_STRING, 0);
1725 }
1726 
1727 <ST_IN_SCRIPTING>"unset" {
1728 	RETURN_TOKEN_WITH_IDENT(T_UNSET);
1729 }
1730 
1731 <ST_IN_SCRIPTING>"=>" {
1732 	RETURN_TOKEN(T_DOUBLE_ARROW);
1733 }
1734 
1735 <ST_IN_SCRIPTING>"list" {
1736 	RETURN_TOKEN_WITH_IDENT(T_LIST);
1737 }
1738 
1739 <ST_IN_SCRIPTING>"array" {
1740 	RETURN_TOKEN_WITH_IDENT(T_ARRAY);
1741 }
1742 
1743 <ST_IN_SCRIPTING>"callable" {
1744 	RETURN_TOKEN_WITH_IDENT(T_CALLABLE);
1745 }
1746 
1747 <ST_IN_SCRIPTING>"++" {
1748 	RETURN_TOKEN(T_INC);
1749 }
1750 
1751 <ST_IN_SCRIPTING>"--" {
1752 	RETURN_TOKEN(T_DEC);
1753 }
1754 
1755 <ST_IN_SCRIPTING>"===" {
1756 	RETURN_TOKEN(T_IS_IDENTICAL);
1757 }
1758 
1759 <ST_IN_SCRIPTING>"!==" {
1760 	RETURN_TOKEN(T_IS_NOT_IDENTICAL);
1761 }
1762 
1763 <ST_IN_SCRIPTING>"==" {
1764 	RETURN_TOKEN(T_IS_EQUAL);
1765 }
1766 
1767 <ST_IN_SCRIPTING>"!="|"<>" {
1768 	RETURN_TOKEN(T_IS_NOT_EQUAL);
1769 }
1770 
1771 <ST_IN_SCRIPTING>"<=>" {
1772 	RETURN_TOKEN(T_SPACESHIP);
1773 }
1774 
1775 <ST_IN_SCRIPTING>"<=" {
1776 	RETURN_TOKEN(T_IS_SMALLER_OR_EQUAL);
1777 }
1778 
1779 <ST_IN_SCRIPTING>">=" {
1780 	RETURN_TOKEN(T_IS_GREATER_OR_EQUAL);
1781 }
1782 
1783 <ST_IN_SCRIPTING>"+=" {
1784 	RETURN_TOKEN(T_PLUS_EQUAL);
1785 }
1786 
1787 <ST_IN_SCRIPTING>"-=" {
1788 	RETURN_TOKEN(T_MINUS_EQUAL);
1789 }
1790 
1791 <ST_IN_SCRIPTING>"*=" {
1792 	RETURN_TOKEN(T_MUL_EQUAL);
1793 }
1794 
1795 <ST_IN_SCRIPTING>"*\*" {
1796 	RETURN_TOKEN(T_POW);
1797 }
1798 
1799 <ST_IN_SCRIPTING>"*\*=" {
1800 	RETURN_TOKEN(T_POW_EQUAL);
1801 }
1802 
1803 <ST_IN_SCRIPTING>"/=" {
1804 	RETURN_TOKEN(T_DIV_EQUAL);
1805 }
1806 
1807 <ST_IN_SCRIPTING>".=" {
1808 	RETURN_TOKEN(T_CONCAT_EQUAL);
1809 }
1810 
1811 <ST_IN_SCRIPTING>"%=" {
1812 	RETURN_TOKEN(T_MOD_EQUAL);
1813 }
1814 
1815 <ST_IN_SCRIPTING>"<<=" {
1816 	RETURN_TOKEN(T_SL_EQUAL);
1817 }
1818 
1819 <ST_IN_SCRIPTING>">>=" {
1820 	RETURN_TOKEN(T_SR_EQUAL);
1821 }
1822 
1823 <ST_IN_SCRIPTING>"&=" {
1824 	RETURN_TOKEN(T_AND_EQUAL);
1825 }
1826 
1827 <ST_IN_SCRIPTING>"|=" {
1828 	RETURN_TOKEN(T_OR_EQUAL);
1829 }
1830 
1831 <ST_IN_SCRIPTING>"^=" {
1832 	RETURN_TOKEN(T_XOR_EQUAL);
1833 }
1834 
1835 <ST_IN_SCRIPTING>"??=" {
1836 	RETURN_TOKEN(T_COALESCE_EQUAL);
1837 }
1838 
1839 <ST_IN_SCRIPTING>"||" {
1840 	RETURN_TOKEN(T_BOOLEAN_OR);
1841 }
1842 
1843 <ST_IN_SCRIPTING>"&&" {
1844 	RETURN_TOKEN(T_BOOLEAN_AND);
1845 }
1846 
1847 <ST_IN_SCRIPTING>"OR" {
1848 	RETURN_TOKEN_WITH_IDENT(T_LOGICAL_OR);
1849 }
1850 
1851 <ST_IN_SCRIPTING>"AND" {
1852 	RETURN_TOKEN_WITH_IDENT(T_LOGICAL_AND);
1853 }
1854 
1855 <ST_IN_SCRIPTING>"XOR" {
1856 	RETURN_TOKEN_WITH_IDENT(T_LOGICAL_XOR);
1857 }
1858 
1859 <ST_IN_SCRIPTING>"<<" {
1860 	RETURN_TOKEN(T_SL);
1861 }
1862 
1863 <ST_IN_SCRIPTING>">>" {
1864 	RETURN_TOKEN(T_SR);
1865 }
1866 
1867 <ST_IN_SCRIPTING>"&"[ \t\r\n]*("$"|"...") {
1868 	yyless(1);
1869 	RETURN_TOKEN(T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG);
1870 }
1871 
1872 <ST_IN_SCRIPTING>"&" {
1873 	RETURN_TOKEN(T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG);
1874 }
1875 
1876 <ST_IN_SCRIPTING>"]"|")" {
1877 	/* Check that ] and ) match up properly with a preceding [ or ( */
1878 	RETURN_EXIT_NESTING_TOKEN(yytext[0]);
1879 }
1880 
1881 <ST_IN_SCRIPTING>"["|"(" {
1882 	enter_nesting(yytext[0]);
1883 	RETURN_TOKEN(yytext[0]);
1884 }
1885 
1886 <ST_IN_SCRIPTING>{TOKENS} {
1887 	RETURN_TOKEN(yytext[0]);
1888 }
1889 
1890 
1891 <ST_IN_SCRIPTING>"{" {
1892 	yy_push_state(ST_IN_SCRIPTING);
1893 	enter_nesting('{');
1894 	RETURN_TOKEN('{');
1895 }
1896 
1897 
1898 <ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"${" {
1899 	yy_push_state(ST_LOOKING_FOR_VARNAME);
1900 	enter_nesting('{');
1901 	RETURN_TOKEN(T_DOLLAR_OPEN_CURLY_BRACES);
1902 }
1903 
1904 <ST_IN_SCRIPTING>"}" {
1905 	RESET_DOC_COMMENT();
1906 	if (!zend_stack_is_empty(&SCNG(state_stack))) {
1907 		yy_pop_state();
1908 	}
1909 	RETURN_EXIT_NESTING_TOKEN('}');
1910 }
1911 
1912 
1913 <ST_LOOKING_FOR_VARNAME>{LABEL}[[}] {
1914 	yyless(yyleng - 1);
1915 	yy_pop_state();
1916 	yy_push_state(ST_IN_SCRIPTING);
1917 	RETURN_TOKEN_WITH_STR(T_STRING_VARNAME, 0);
1918 }
1919 
1920 
1921 <ST_LOOKING_FOR_VARNAME>{ANY_CHAR} {
1922 	yyless(0);
1923 	yy_pop_state();
1924 	yy_push_state(ST_IN_SCRIPTING);
1925 	goto restart;
1926 }
1927 
1928 <ST_IN_SCRIPTING>{BNUM} {
1929 	/* The +/- 2 skips "0b" */
1930 	size_t len = yyleng - 2;
1931 	char *end, *bin = yytext + 2;
1932 	bool contains_underscores;
1933 
1934 	/* Skip any leading 0s */
1935 	while (len > 0 && (*bin == '0' || *bin == '_')) {
1936 		++bin;
1937 		--len;
1938 	}
1939 
1940 	contains_underscores = (memchr(bin, '_', len) != NULL);
1941 
1942 	if (contains_underscores) {
1943 		bin = estrndup(bin, len);
1944 		strip_underscores(bin, &len);
1945 	}
1946 
1947 	if (len < SIZEOF_ZEND_LONG * 8) {
1948 		if (len == 0) {
1949 			ZVAL_LONG(zendlval, 0);
1950 		} else {
1951 			errno = 0;
1952 			ZVAL_LONG(zendlval, ZEND_STRTOL(bin, &end, 2));
1953 			ZEND_ASSERT(!errno && end == bin + len);
1954 		}
1955 		if (contains_underscores) {
1956 			efree(bin);
1957 		}
1958 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
1959 	} else {
1960 		ZVAL_DOUBLE(zendlval, zend_bin_strtod(bin, (const char **)&end));
1961 		/* errno isn't checked since we allow HUGE_VAL/INF overflow */
1962 		ZEND_ASSERT(end == bin + len);
1963 		if (contains_underscores) {
1964 			efree(bin);
1965 		}
1966 		RETURN_TOKEN_WITH_VAL(T_DNUMBER);
1967 	}
1968 }
1969 
1970 <ST_IN_SCRIPTING>{ONUM} {
1971 	/* The +/- 2 skips "0o" */
1972 	size_t len = yyleng - 2;
1973 	char *end, *octal = yytext + 2;
1974 	bool contains_underscores = (memchr(octal, '_', len) != NULL);
1975 
1976 	/* Skip any leading 0s */
1977 	while (len > 0 && (*octal == '0' || *octal == '_')) {
1978 		++octal;
1979 		--len;
1980 	}
1981 
1982 	if (len == 0) {
1983 		ZVAL_LONG(zendlval, 0);
1984 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
1985 	}
1986 
1987 	if (contains_underscores) {
1988 		octal = estrndup(octal, len);
1989 		strip_underscores(octal, &len);
1990 	}
1991 
1992 	errno = 0;
1993 
1994 	ZVAL_LONG(zendlval, ZEND_STRTOL(octal, &end, 8));
1995 
1996 	ZEND_ASSERT(end == octal + len);
1997 
1998 	if (!errno) {
1999 		if (contains_underscores) {
2000 			efree(octal);
2001 		}
2002 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2003 	}
2004 
2005 	/* Overflow */
2006 	ZEND_ASSERT(errno == ERANGE);
2007 	/* Reset errno */
2008 	errno = 0;
2009 
2010 	/* zend_oct_strtod skips leading '0' */
2011 	ZVAL_DOUBLE(zendlval, zend_oct_strtod(octal, (const char **)&end));
2012 	ZEND_ASSERT(!errno);
2013 	ZEND_ASSERT(end == octal + len);
2014 	if (contains_underscores) {
2015 		efree(octal);
2016 	}
2017 	RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2018 }
2019 
2020 <ST_IN_SCRIPTING>{LNUM} {
2021 	size_t len = yyleng;
2022 	char *end, *lnum = yytext;
2023 	bool is_octal = lnum[0] == '0';
2024 	bool contains_underscores = (memchr(lnum, '_', len) != NULL);
2025 
2026 	if (contains_underscores) {
2027 		lnum = estrndup(lnum, len);
2028 		strip_underscores(lnum, &len);
2029 	}
2030 
2031 	/* Digits 8 and 9 are illegal in octal literals. */
2032 	if (is_octal) {
2033 		size_t i;
2034 		for (i = 0; i < len; i++) {
2035 			if (lnum[i] == '8' || lnum[i] == '9') {
2036 				zend_throw_exception(zend_ce_parse_error, "Invalid numeric literal", 0);
2037 				if (PARSER_MODE()) {
2038 					if (contains_underscores) {
2039 						efree(lnum);
2040 					}
2041 					ZVAL_UNDEF(zendlval);
2042 					RETURN_TOKEN(T_ERROR);
2043 				}
2044 
2045 				/* Continue in order to determine if this is T_LNUMBER or T_DNUMBER. */
2046 				len = i;
2047 				break;
2048 			}
2049 		}
2050 	}
2051 
2052 
2053 	if (len < MAX_LENGTH_OF_LONG - 1) { /* Won't overflow */
2054 		errno = 0;
2055 		/* base must be passed explicitly for correct parse error on Windows */
2056 		ZVAL_LONG(zendlval, ZEND_STRTOL(lnum, &end, is_octal ? 8 : 10));
2057 		ZEND_ASSERT(end == lnum + len);
2058 	} else {
2059 		errno = 0;
2060 		ZVAL_LONG(zendlval, ZEND_STRTOL(lnum, &end, 0));
2061 		if (errno == ERANGE) { /* Overflow */
2062 			errno = 0;
2063 			if (is_octal) { /* octal overflow */
2064 				ZVAL_DOUBLE(zendlval, zend_oct_strtod(lnum, (const char **)&end));
2065 			} else {
2066 				ZVAL_DOUBLE(zendlval, zend_strtod(lnum, (const char **)&end));
2067 			}
2068 			ZEND_ASSERT(end == lnum + len);
2069 			if (contains_underscores) {
2070 				efree(lnum);
2071 			}
2072 			RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2073 		}
2074 		ZEND_ASSERT(end == lnum + len);
2075 	}
2076 	ZEND_ASSERT(!errno);
2077 	if (contains_underscores) {
2078 		efree(lnum);
2079 	}
2080 	RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2081 }
2082 
2083 <ST_IN_SCRIPTING>{HNUM} {
2084 	/* The +/- 2 skips "0x" */
2085 	size_t len = yyleng - 2;
2086 	char *end, *hex = yytext + 2;
2087 	bool contains_underscores;
2088 
2089 	/* Skip any leading 0s */
2090 	while (len > 0 && (*hex == '0' || *hex == '_')) {
2091 		++hex;
2092 		--len;
2093 	}
2094 
2095 	contains_underscores = (memchr(hex, '_', len) != NULL);
2096 
2097 	if (contains_underscores) {
2098 		hex = estrndup(hex, len);
2099 		strip_underscores(hex, &len);
2100 	}
2101 
2102 	if (len < SIZEOF_ZEND_LONG * 2 || (len == SIZEOF_ZEND_LONG * 2 && *hex <= '7')) {
2103 		if (len == 0) {
2104 			ZVAL_LONG(zendlval, 0);
2105 		} else {
2106 			errno = 0;
2107 			ZVAL_LONG(zendlval, ZEND_STRTOL(hex, &end, 16));
2108 			ZEND_ASSERT(!errno && end == hex + len);
2109 		}
2110 		if (contains_underscores) {
2111 			efree(hex);
2112 		}
2113 		RETURN_TOKEN_WITH_VAL(T_LNUMBER);
2114 	} else {
2115 		ZVAL_DOUBLE(zendlval, zend_hex_strtod(hex, (const char **)&end));
2116 		/* errno isn't checked since we allow HUGE_VAL/INF overflow */
2117 		ZEND_ASSERT(end == hex + len);
2118 		if (contains_underscores) {
2119 			efree(hex);
2120 		}
2121 		RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2122 	}
2123 }
2124 
2125 <ST_VAR_OFFSET>[0]|([1-9][0-9]*) { /* Offset could be treated as a long */
2126 	if (yyleng < MAX_LENGTH_OF_LONG - 1 || (yyleng == MAX_LENGTH_OF_LONG - 1 && strcmp(yytext, long_min_digits) < 0)) {
2127 		char *end;
2128 		errno = 0;
2129 		ZVAL_LONG(zendlval, ZEND_STRTOL(yytext, &end, 10));
2130 		if (errno == ERANGE) {
2131 			goto string;
2132 		}
2133 		ZEND_ASSERT(end == yytext + yyleng);
2134 	} else {
2135 string:
2136 		ZVAL_STRINGL(zendlval, yytext, yyleng);
2137 	}
2138 	RETURN_TOKEN_WITH_VAL(T_NUM_STRING);
2139 }
2140 
2141 <ST_VAR_OFFSET>{LNUM}|{HNUM}|{BNUM}|{ONUM} { /* Offset must be treated as a string */
2142 	if (yyleng == 1) {
2143 		ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR((zend_uchar)*(yytext)));
2144 	} else {
2145 		ZVAL_STRINGL(zendlval, yytext, yyleng);
2146 	}
2147 	RETURN_TOKEN_WITH_VAL(T_NUM_STRING);
2148 }
2149 
2150 <ST_IN_SCRIPTING>{DNUM}|{EXPONENT_DNUM} {
2151 	const char *end;
2152 	size_t len = yyleng;
2153 	char *dnum = yytext;
2154 	bool contains_underscores = (memchr(dnum, '_', len) != NULL);
2155 
2156 	if (contains_underscores) {
2157 		dnum = estrndup(dnum, len);
2158 		strip_underscores(dnum, &len);
2159 	}
2160 
2161 	ZVAL_DOUBLE(zendlval, zend_strtod(dnum, &end));
2162 	/* errno isn't checked since we allow HUGE_VAL/INF overflow */
2163 	ZEND_ASSERT(end == dnum + len);
2164 	if (contains_underscores) {
2165 		efree(dnum);
2166 	}
2167 	RETURN_TOKEN_WITH_VAL(T_DNUMBER);
2168 }
2169 
2170 <ST_IN_SCRIPTING>"__CLASS__" {
2171 	RETURN_TOKEN_WITH_IDENT(T_CLASS_C);
2172 }
2173 
2174 <ST_IN_SCRIPTING>"__TRAIT__" {
2175 	RETURN_TOKEN_WITH_IDENT(T_TRAIT_C);
2176 }
2177 
2178 <ST_IN_SCRIPTING>"__FUNCTION__" {
2179 	RETURN_TOKEN_WITH_IDENT(T_FUNC_C);
2180 }
2181 
2182 <ST_IN_SCRIPTING>"__METHOD__" {
2183 	RETURN_TOKEN_WITH_IDENT(T_METHOD_C);
2184 }
2185 
2186 <ST_IN_SCRIPTING>"__LINE__" {
2187 	RETURN_TOKEN_WITH_IDENT(T_LINE);
2188 }
2189 
2190 <ST_IN_SCRIPTING>"__FILE__" {
2191 	RETURN_TOKEN_WITH_IDENT(T_FILE);
2192 }
2193 
2194 <ST_IN_SCRIPTING>"__DIR__" {
2195 	RETURN_TOKEN_WITH_IDENT(T_DIR);
2196 }
2197 
2198 <ST_IN_SCRIPTING>"__NAMESPACE__" {
2199 	RETURN_TOKEN_WITH_IDENT(T_NS_C);
2200 }
2201 
2202 <SHEBANG>"#!" .* {NEWLINE} {
2203 	CG(zend_lineno)++;
2204 	BEGIN(INITIAL);
2205 	goto restart;
2206 }
2207 
2208 <SHEBANG>{ANY_CHAR} {
2209 	yyless(0);
2210 	BEGIN(INITIAL);
2211 	goto restart;
2212 }
2213 
2214 <INITIAL>"<?=" {
2215 	BEGIN(ST_IN_SCRIPTING);
2216 	if (PARSER_MODE()) {
2217 		/* We'll reject this as an identifier in zend_lex_tstring. */
2218 		RETURN_TOKEN_WITH_IDENT(T_ECHO);
2219 	}
2220 	RETURN_TOKEN(T_OPEN_TAG_WITH_ECHO);
2221 }
2222 
2223 
2224 <INITIAL>"<?php"([ \t]|{NEWLINE}) {
2225 	HANDLE_NEWLINE(yytext[yyleng-1]);
2226 	BEGIN(ST_IN_SCRIPTING);
2227 	RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2228 }
2229 
2230 <INITIAL>"<?php" {
2231 	/* Allow <?php followed by end of file. */
2232 	if (YYCURSOR == YYLIMIT) {
2233 		BEGIN(ST_IN_SCRIPTING);
2234 		RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2235 	}
2236 	/* Degenerate case: <?phpX is interpreted as <? phpX with short tags. */
2237 	if (CG(short_tags)) {
2238 		yyless(2);
2239 		BEGIN(ST_IN_SCRIPTING);
2240 		RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2241 	}
2242 	goto inline_char_handler;
2243 }
2244 
2245 <INITIAL>"<?" {
2246 	if (CG(short_tags)) {
2247 		BEGIN(ST_IN_SCRIPTING);
2248 		RETURN_OR_SKIP_TOKEN(T_OPEN_TAG);
2249 	} else {
2250 		goto inline_char_handler;
2251 	}
2252 }
2253 
2254 <INITIAL>{ANY_CHAR} {
2255 	if (YYCURSOR > YYLIMIT) {
2256 		RETURN_END_TOKEN;
2257 	}
2258 
2259 inline_char_handler:
2260 
2261 	while (1) {
2262 		YYCTYPE *ptr = memchr(YYCURSOR, '<', YYLIMIT - YYCURSOR);
2263 
2264 		YYCURSOR = ptr ? ptr + 1 : YYLIMIT;
2265 
2266 		if (YYCURSOR >= YYLIMIT) {
2267 			break;
2268 		}
2269 
2270 		if (*YYCURSOR == '?') {
2271 			if (CG(short_tags) /* <? */
2272 				|| (*(YYCURSOR + 1) == '=') /* <?= */
2273 				|| (!strncasecmp((char*)YYCURSOR + 1, "php", 3) && /* <?php[ \t\r\n] */
2274 					(YYCURSOR + 4 == YYLIMIT ||
2275 					YYCURSOR[4] == ' ' || YYCURSOR[4] == '\t' ||
2276 					YYCURSOR[4] == '\n' || YYCURSOR[4] == '\r'))
2277 			) {
2278 				YYCURSOR--;
2279 				break;
2280 			}
2281 		}
2282 	}
2283 
2284 	yyleng = YYCURSOR - SCNG(yy_text);
2285 
2286 	if (SCNG(output_filter)) {
2287 		size_t readsize;
2288 		char *s = NULL;
2289 		size_t sz = 0;
2290 		// TODO: avoid reallocation ???
2291 		readsize = SCNG(output_filter)((unsigned char **)&s, &sz, (unsigned char *)yytext, (size_t)yyleng);
2292 		ZVAL_STRINGL(zendlval, s, sz);
2293 		efree(s);
2294 		if (readsize < yyleng) {
2295 			yyless(readsize);
2296 		}
2297 	} else if (yyleng == 1) {
2298 		ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR((zend_uchar)*yytext));
2299 	} else {
2300 		ZVAL_STRINGL(zendlval, yytext, yyleng);
2301 	}
2302 	HANDLE_NEWLINES(yytext, yyleng);
2303 	RETURN_TOKEN_WITH_VAL(T_INLINE_HTML);
2304 }
2305 
2306 
2307 /* Make sure a label character follows "->" or "?->", otherwise there is no property
2308  * and "->"/"?->" will be taken literally
2309  */
2310 <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"->"[a-zA-Z_\x80-\xff] {
2311 	yyless(yyleng - 3);
2312 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
2313 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2314 }
2315 
2316 <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"?->"[a-zA-Z_\x80-\xff] {
2317 	yyless(yyleng - 4);
2318 	yy_push_state(ST_LOOKING_FOR_PROPERTY);
2319 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2320 }
2321 
2322 /* A [ always designates a variable offset, regardless of what follows
2323  */
2324 <ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE>"$"{LABEL}"[" {
2325 	yyless(yyleng - 1);
2326 	yy_push_state(ST_VAR_OFFSET);
2327 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2328 }
2329 
2330 <ST_IN_SCRIPTING,ST_DOUBLE_QUOTES,ST_HEREDOC,ST_BACKQUOTE,ST_VAR_OFFSET>"$"{LABEL} {
2331 	RETURN_TOKEN_WITH_STR(T_VARIABLE, 1);
2332 }
2333 
2334 <ST_VAR_OFFSET>"]" {
2335 	yy_pop_state();
2336 	RETURN_TOKEN(']');
2337 }
2338 
2339 <ST_VAR_OFFSET>{TOKENS}|[[(){}"`] {
2340 	/* Only '[' or '-' can be valid, but returning other tokens will allow a more explicit parse error */
2341 	RETURN_TOKEN(yytext[0]);
2342 }
2343 
2344 <ST_VAR_OFFSET>[ \n\r\t\\'#] {
2345 	/* Invalid rule to return a more explicit parse error with proper line number */
2346 	yyless(0);
2347 	yy_pop_state();
2348 	ZVAL_NULL(zendlval);
2349 	RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2350 }
2351 
2352 <ST_IN_SCRIPTING>"namespace"("\\"{LABEL})+ {
2353 	RETURN_TOKEN_WITH_STR(T_NAME_RELATIVE, sizeof("namespace\\") - 1);
2354 }
2355 
2356 <ST_IN_SCRIPTING>{LABEL}("\\"{LABEL})+ {
2357 	RETURN_TOKEN_WITH_STR(T_NAME_QUALIFIED, 0);
2358 }
2359 
2360 <ST_IN_SCRIPTING>"\\"{LABEL}("\\"{LABEL})* {
2361 	RETURN_TOKEN_WITH_STR(T_NAME_FULLY_QUALIFIED, 1);
2362 }
2363 
2364 <ST_IN_SCRIPTING>"\\" {
2365 	RETURN_TOKEN(T_NS_SEPARATOR);
2366 }
2367 
2368 <ST_IN_SCRIPTING,ST_VAR_OFFSET>{LABEL} {
2369 	RETURN_TOKEN_WITH_STR(T_STRING, 0);
2370 }
2371 
2372 
2373 <ST_IN_SCRIPTING>"#"|"//" {
2374 	while (YYCURSOR < YYLIMIT) {
2375 		switch (*YYCURSOR++) {
2376 			case '\r':
2377 			case '\n':
2378 				YYCURSOR--;
2379 				break;
2380 			case '?':
2381 				if (*YYCURSOR == '>') {
2382 					YYCURSOR--;
2383 					break;
2384 				}
2385 				ZEND_FALLTHROUGH;
2386 			default:
2387 				continue;
2388 		}
2389 
2390 		break;
2391 	}
2392 
2393 	yyleng = YYCURSOR - SCNG(yy_text);
2394 	RETURN_OR_SKIP_TOKEN(T_COMMENT);
2395 }
2396 
2397 <ST_IN_SCRIPTING>"/*"|"/**"{WHITESPACE} {
2398 	int doc_com;
2399 
2400 	if (yyleng > 2) {
2401 		doc_com = 1;
2402 		RESET_DOC_COMMENT();
2403 	} else {
2404 		doc_com = 0;
2405 	}
2406 
2407 	while (YYCURSOR < YYLIMIT) {
2408 		if (*YYCURSOR++ == '*' && *YYCURSOR == '/') {
2409 			break;
2410 		}
2411 	}
2412 
2413 	if (YYCURSOR < YYLIMIT) {
2414 		YYCURSOR++;
2415 	} else {
2416 		zend_throw_exception_ex(zend_ce_parse_error, 0, "Unterminated comment starting line %d", CG(zend_lineno));
2417 		if (PARSER_MODE()) {
2418 			RETURN_TOKEN(T_ERROR);
2419 		}
2420 	}
2421 
2422 	yyleng = YYCURSOR - SCNG(yy_text);
2423 	HANDLE_NEWLINES(yytext, yyleng);
2424 
2425 	if (doc_com) {
2426 		CG(doc_comment) = zend_string_init(yytext, yyleng, 0);
2427 		RETURN_OR_SKIP_TOKEN(T_DOC_COMMENT);
2428 	}
2429 
2430 	RETURN_OR_SKIP_TOKEN(T_COMMENT);
2431 }
2432 
2433 <ST_IN_SCRIPTING>"?>"{NEWLINE}? {
2434 	BEGIN(INITIAL);
2435 	if (yytext[yyleng-1] != '>') {
2436 		CG(increment_lineno) = 1;
2437 	}
2438 	if (PARSER_MODE()) {
2439 		RETURN_TOKEN(';');  /* implicit ';' at php-end tag */
2440 	}
2441 	RETURN_TOKEN(T_CLOSE_TAG);
2442 }
2443 
2444 
2445 <ST_IN_SCRIPTING>b?['] {
2446 	char *s, *t;
2447 	char *end;
2448 	int bprefix = (yytext[0] != '\'') ? 1 : 0;
2449 
2450 	while (1) {
2451 		if (YYCURSOR < YYLIMIT) {
2452 			if (*YYCURSOR == '\'') {
2453 				YYCURSOR++;
2454 				yyleng = YYCURSOR - SCNG(yy_text);
2455 
2456 				break;
2457 			} else if (*YYCURSOR++ == '\\' && YYCURSOR < YYLIMIT) {
2458 				YYCURSOR++;
2459 			}
2460 		} else {
2461 			yyleng = YYLIMIT - SCNG(yy_text);
2462 
2463 			/* Unclosed single quotes; treat similar to double quotes, but without a separate token
2464 			 * for ' (unrecognized by parser), instead of old flex fallback to "Unexpected character..."
2465 			 * rule, which continued in ST_IN_SCRIPTING state after the quote */
2466 			ZVAL_NULL(zendlval);
2467 			RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2468 		}
2469 	}
2470 
2471 	if (yyleng-bprefix-2 <= 1) {
2472 		if (yyleng-bprefix-2 < 1) {
2473 			ZVAL_EMPTY_STRING(zendlval);
2474 		} else {
2475 			zend_uchar c = (zend_uchar)*(yytext+bprefix+1);
2476 			if (c == '\n' || c == '\r') {
2477 				CG(zend_lineno)++;
2478 			}
2479 			ZVAL_INTERNED_STR(zendlval, ZSTR_CHAR(c));
2480 		}
2481 		goto skip_escape_conversion;
2482 	}
2483 	ZVAL_STRINGL(zendlval, yytext+bprefix+1, yyleng-bprefix-2);
2484 
2485 	/* convert escape sequences */
2486 	s = Z_STRVAL_P(zendlval);
2487 	end = s+Z_STRLEN_P(zendlval);
2488 	while (1) {
2489 		if (UNEXPECTED(*s=='\\')) {
2490 			break;
2491 		}
2492 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
2493 			CG(zend_lineno)++;
2494 		}
2495 		s++;
2496 		if (s == end) {
2497 			goto skip_escape_conversion;
2498 		}
2499 	}
2500 
2501 	t = s;
2502 	while (s<end) {
2503 		if (*s=='\\') {
2504 			s++;
2505 			if (*s == '\\' || *s == '\'') {
2506 				*t++ = *s;
2507 			} else {
2508 				*t++ = '\\';
2509 				*t++ = *s;
2510 			}
2511 		} else {
2512 			*t++ = *s;
2513 		}
2514 		if (*s == '\n' || (*s == '\r' && (*(s+1) != '\n'))) {
2515 			CG(zend_lineno)++;
2516 		}
2517 		s++;
2518 	}
2519 	*t = 0;
2520 	Z_STRLEN_P(zendlval) = t - Z_STRVAL_P(zendlval);
2521 
2522 skip_escape_conversion:
2523 	if (SCNG(output_filter)) {
2524 		size_t sz = 0;
2525 		char *str = NULL;
2526 		zend_string *new_str;
2527 		s = Z_STRVAL_P(zendlval);
2528 		// TODO: avoid reallocation ???
2529 		SCNG(output_filter)((unsigned char **)&str, &sz, (unsigned char *)s, (size_t)Z_STRLEN_P(zendlval));
2530 		new_str = zend_string_init(str, sz, 0);
2531 		if (str != s) {
2532 			efree(str);
2533 		}
2534 		zend_string_release_ex(Z_STR_P(zendlval), 0);
2535 		ZVAL_STR(zendlval, new_str);
2536 	}
2537 	RETURN_TOKEN_WITH_VAL(T_CONSTANT_ENCAPSED_STRING);
2538 }
2539 
2540 
2541 <ST_IN_SCRIPTING>b?["] {
2542 	int bprefix = (yytext[0] != '"') ? 1 : 0;
2543 
2544 	while (YYCURSOR < YYLIMIT) {
2545 		switch (*YYCURSOR++) {
2546 			case '"':
2547 				yyleng = YYCURSOR - SCNG(yy_text);
2548 				if (EXPECTED(zend_scan_escape_string(zendlval, yytext+bprefix+1, yyleng-bprefix-2, '"') == SUCCESS)
2549 				 || !PARSER_MODE()) {
2550 					RETURN_TOKEN_WITH_VAL(T_CONSTANT_ENCAPSED_STRING);
2551 				} else {
2552 					RETURN_TOKEN(T_ERROR);
2553 				}
2554 			case '$':
2555 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2556 					break;
2557 				}
2558 				continue;
2559 			case '{':
2560 				if (*YYCURSOR == '$') {
2561 					break;
2562 				}
2563 				continue;
2564 			case '\\':
2565 				if (YYCURSOR < YYLIMIT) {
2566 					YYCURSOR++;
2567 				}
2568 				ZEND_FALLTHROUGH;
2569 			default:
2570 				continue;
2571 		}
2572 
2573 		YYCURSOR--;
2574 		break;
2575 	}
2576 
2577 	/* Remember how much was scanned to save rescanning */
2578 	SET_DOUBLE_QUOTES_SCANNED_LENGTH(YYCURSOR - SCNG(yy_text) - yyleng);
2579 
2580 	YYCURSOR = SCNG(yy_text) + yyleng;
2581 
2582 	BEGIN(ST_DOUBLE_QUOTES);
2583 	RETURN_TOKEN('"');
2584 }
2585 
2586 
2587 <ST_IN_SCRIPTING>b?"<<<"{TABS_AND_SPACES}({LABEL}|([']{LABEL}['])|(["]{LABEL}["])){NEWLINE} {
2588 	char *s;
2589 	unsigned char *saved_cursor;
2590 	int bprefix = (yytext[0] != '<') ? 1 : 0, spacing = 0, indentation = 0;
2591 	zend_heredoc_label *heredoc_label = emalloc(sizeof(zend_heredoc_label));
2592 	bool is_heredoc = 1;
2593 
2594 	CG(zend_lineno)++;
2595 	heredoc_label->length = yyleng-bprefix-3-1-(yytext[yyleng-2]=='\r'?1:0);
2596 	s = yytext+bprefix+3;
2597 	while ((*s == ' ') || (*s == '\t')) {
2598 		s++;
2599 		heredoc_label->length--;
2600 	}
2601 
2602 	if (*s == '\'') {
2603 		s++;
2604 		heredoc_label->length -= 2;
2605 		is_heredoc = 0;
2606 
2607 		BEGIN(ST_NOWDOC);
2608 	} else {
2609 		if (*s == '"') {
2610 			s++;
2611 			heredoc_label->length -= 2;
2612 		}
2613 
2614 		BEGIN(ST_HEREDOC);
2615 	}
2616 
2617 	heredoc_label->label = estrndup(s, heredoc_label->length);
2618 	heredoc_label->indentation_uses_spaces = 0;
2619 	heredoc_label->indentation = 0;
2620 	saved_cursor = YYCURSOR;
2621 
2622 	zend_ptr_stack_push(&SCNG(heredoc_label_stack), (void *) heredoc_label);
2623 
2624 	while (YYCURSOR < YYLIMIT && (*YYCURSOR == ' ' || *YYCURSOR == '\t')) {
2625 		if (*YYCURSOR == '\t') {
2626 			spacing |= HEREDOC_USING_TABS;
2627 		} else {
2628 			spacing |= HEREDOC_USING_SPACES;
2629 		}
2630 		++YYCURSOR;
2631 		++indentation;
2632 	}
2633 
2634 	if (YYCURSOR == YYLIMIT) {
2635 		YYCURSOR = saved_cursor;
2636 		RETURN_TOKEN(T_START_HEREDOC);
2637 	}
2638 
2639 	/* Check for ending label on the next line */
2640 	if (heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, s, heredoc_label->length)) {
2641 		if (!IS_LABEL_SUCCESSOR(YYCURSOR[heredoc_label->length])) {
2642 			if (spacing == (HEREDOC_USING_SPACES | HEREDOC_USING_TABS)) {
2643 				zend_throw_exception(zend_ce_parse_error, "Invalid indentation - tabs and spaces cannot be mixed", 0);
2644 				if (PARSER_MODE()) {
2645 					RETURN_TOKEN(T_ERROR);
2646 				}
2647 			}
2648 
2649 			YYCURSOR = saved_cursor;
2650 			heredoc_label->indentation = indentation;
2651 
2652 			BEGIN(ST_END_HEREDOC);
2653 			RETURN_TOKEN(T_START_HEREDOC);
2654 		}
2655 	}
2656 
2657 	YYCURSOR = saved_cursor;
2658 
2659 	if (is_heredoc && !SCNG(heredoc_scan_ahead)) {
2660 		zend_lex_state current_state;
2661 		zend_string *saved_doc_comment = CG(doc_comment);
2662 		int heredoc_nesting_level = 1;
2663 		int first_token = 0;
2664 		int error = 0;
2665 
2666 		zend_save_lexical_state(&current_state);
2667 
2668 		SCNG(heredoc_scan_ahead) = 1;
2669 		SCNG(heredoc_indentation) = 0;
2670 		SCNG(heredoc_indentation_uses_spaces) = 0;
2671 		LANG_SCNG(on_event) = NULL;
2672 		CG(doc_comment) = NULL;
2673 
2674 		zend_ptr_stack_reverse_apply(&current_state.heredoc_label_stack, copy_heredoc_label_stack);
2675 
2676 		zend_exception_save();
2677 		while (heredoc_nesting_level) {
2678 			zval zv;
2679 			int retval;
2680 
2681 			ZVAL_UNDEF(&zv);
2682 			retval = lex_scan(&zv, NULL);
2683 			zval_ptr_dtor_nogc(&zv);
2684 
2685 			if (EG(exception)) {
2686 				zend_clear_exception();
2687 				break;
2688 			}
2689 
2690 			if (!first_token) {
2691 				first_token = retval;
2692 			}
2693 
2694 			switch (retval) {
2695 				case T_START_HEREDOC:
2696 					++heredoc_nesting_level;
2697 					break;
2698 				case T_END_HEREDOC:
2699 					--heredoc_nesting_level;
2700 					break;
2701 				case END:
2702 					heredoc_nesting_level = 0;
2703 			}
2704 		}
2705 		zend_exception_restore();
2706 
2707 		if (
2708 		    (first_token == T_VARIABLE
2709 		     || first_token == T_DOLLAR_OPEN_CURLY_BRACES
2710 		     || first_token == T_CURLY_OPEN
2711 		    ) && SCNG(heredoc_indentation)) {
2712 			zend_throw_exception_ex(zend_ce_parse_error, 0, "Invalid body indentation level (expecting an indentation level of at least %d)", SCNG(heredoc_indentation));
2713 			error = 1;
2714 		}
2715 
2716 		heredoc_label->indentation = SCNG(heredoc_indentation);
2717 		heredoc_label->indentation_uses_spaces = SCNG(heredoc_indentation_uses_spaces);
2718 
2719 		zend_restore_lexical_state(&current_state);
2720 		SCNG(heredoc_scan_ahead) = 0;
2721 		CG(increment_lineno) = 0;
2722 		CG(doc_comment) = saved_doc_comment;
2723 
2724 		if (PARSER_MODE() && error) {
2725 			RETURN_TOKEN(T_ERROR);
2726 		}
2727 	}
2728 
2729 	RETURN_TOKEN(T_START_HEREDOC);
2730 }
2731 
2732 
2733 <ST_IN_SCRIPTING>[`] {
2734 	BEGIN(ST_BACKQUOTE);
2735 	RETURN_TOKEN('`');
2736 }
2737 
2738 
2739 <ST_END_HEREDOC>{ANY_CHAR} {
2740 	zend_heredoc_label *heredoc_label = zend_ptr_stack_pop(&SCNG(heredoc_label_stack));
2741 
2742 	yyleng = heredoc_label->indentation + heredoc_label->length;
2743 	YYCURSOR += yyleng - 1;
2744 
2745 	heredoc_label_dtor(heredoc_label);
2746 	efree(heredoc_label);
2747 
2748 	BEGIN(ST_IN_SCRIPTING);
2749 	RETURN_TOKEN(T_END_HEREDOC);
2750 }
2751 
2752 
2753 <ST_DOUBLE_QUOTES,ST_BACKQUOTE,ST_HEREDOC>"{$" {
2754 	yy_push_state(ST_IN_SCRIPTING);
2755 	yyless(1);
2756 	enter_nesting('{');
2757 	RETURN_TOKEN(T_CURLY_OPEN);
2758 }
2759 
2760 
2761 <ST_DOUBLE_QUOTES>["] {
2762 	BEGIN(ST_IN_SCRIPTING);
2763 	RETURN_TOKEN('"');
2764 }
2765 
2766 <ST_BACKQUOTE>[`] {
2767 	BEGIN(ST_IN_SCRIPTING);
2768 	RETURN_TOKEN('`');
2769 }
2770 
2771 
2772 <ST_DOUBLE_QUOTES>{ANY_CHAR} {
2773 	if (GET_DOUBLE_QUOTES_SCANNED_LENGTH()) {
2774 		YYCURSOR += GET_DOUBLE_QUOTES_SCANNED_LENGTH() - 1;
2775 		SET_DOUBLE_QUOTES_SCANNED_LENGTH(0);
2776 
2777 		goto double_quotes_scan_done;
2778 	}
2779 
2780 	if (YYCURSOR > YYLIMIT) {
2781 		RETURN_END_TOKEN;
2782 	}
2783 	if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2784 		YYCURSOR++;
2785 	}
2786 
2787 	while (YYCURSOR < YYLIMIT) {
2788 		switch (*YYCURSOR++) {
2789 			case '"':
2790 				break;
2791 			case '$':
2792 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2793 					break;
2794 				}
2795 				continue;
2796 			case '{':
2797 				if (*YYCURSOR == '$') {
2798 					break;
2799 				}
2800 				continue;
2801 			case '\\':
2802 				if (YYCURSOR < YYLIMIT) {
2803 					YYCURSOR++;
2804 				}
2805 				ZEND_FALLTHROUGH;
2806 			default:
2807 				continue;
2808 		}
2809 
2810 		YYCURSOR--;
2811 		break;
2812 	}
2813 
2814 double_quotes_scan_done:
2815 	yyleng = YYCURSOR - SCNG(yy_text);
2816 
2817 	if (EXPECTED(zend_scan_escape_string(zendlval, yytext, yyleng, '"') == SUCCESS)
2818 	 || !PARSER_MODE()) {
2819 		RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2820 	} else {
2821 		RETURN_TOKEN(T_ERROR);
2822 	}
2823 }
2824 
2825 
2826 <ST_BACKQUOTE>{ANY_CHAR} {
2827 	if (YYCURSOR > YYLIMIT) {
2828 		RETURN_END_TOKEN;
2829 	}
2830 	if (yytext[0] == '\\' && YYCURSOR < YYLIMIT) {
2831 		YYCURSOR++;
2832 	}
2833 
2834 	while (YYCURSOR < YYLIMIT) {
2835 		switch (*YYCURSOR++) {
2836 			case '`':
2837 				break;
2838 			case '$':
2839 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2840 					break;
2841 				}
2842 				continue;
2843 			case '{':
2844 				if (*YYCURSOR == '$') {
2845 					break;
2846 				}
2847 				continue;
2848 			case '\\':
2849 				if (YYCURSOR < YYLIMIT) {
2850 					YYCURSOR++;
2851 				}
2852 				ZEND_FALLTHROUGH;
2853 			default:
2854 				continue;
2855 		}
2856 
2857 		YYCURSOR--;
2858 		break;
2859 	}
2860 
2861 	yyleng = YYCURSOR - SCNG(yy_text);
2862 
2863 	if (EXPECTED(zend_scan_escape_string(zendlval, yytext, yyleng, '`') == SUCCESS)
2864 	 || !PARSER_MODE()) {
2865 		RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2866 	} else {
2867 		RETURN_TOKEN(T_ERROR);
2868 	}
2869 }
2870 
2871 
2872 <ST_HEREDOC>{ANY_CHAR} {
2873 	zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2874 	int newline = 0, indentation = 0, spacing = 0;
2875 
2876 	if (YYCURSOR > YYLIMIT) {
2877 		RETURN_END_TOKEN;
2878 	}
2879 
2880 	YYCURSOR--;
2881 
2882 	while (YYCURSOR < YYLIMIT) {
2883 		switch (*YYCURSOR++) {
2884 			case '\r':
2885 				if (*YYCURSOR == '\n') {
2886 					YYCURSOR++;
2887 				}
2888 				ZEND_FALLTHROUGH;
2889 			case '\n':
2890 				indentation = spacing = 0;
2891 
2892 				while (YYCURSOR < YYLIMIT && (*YYCURSOR == ' ' || *YYCURSOR == '\t')) {
2893 					if (*YYCURSOR == '\t') {
2894 						spacing |= HEREDOC_USING_TABS;
2895 					} else {
2896 						spacing |= HEREDOC_USING_SPACES;
2897 					}
2898 					++YYCURSOR;
2899 					++indentation;
2900 				}
2901 
2902 				if (YYCURSOR == YYLIMIT) {
2903 					yyleng = YYCURSOR - SCNG(yy_text);
2904 					HANDLE_NEWLINES(yytext, yyleng);
2905 					ZVAL_NULL(zendlval);
2906 					RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2907 				}
2908 
2909 				/* Check for ending label on the next line */
2910 				if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
2911 					if (IS_LABEL_SUCCESSOR(YYCURSOR[heredoc_label->length])) {
2912 						continue;
2913 					}
2914 
2915 					if (spacing == (HEREDOC_USING_SPACES | HEREDOC_USING_TABS)) {
2916 						zend_throw_exception(zend_ce_parse_error, "Invalid indentation - tabs and spaces cannot be mixed", 0);
2917 						if (PARSER_MODE()) {
2918 							RETURN_TOKEN(T_ERROR);
2919 						}
2920 					}
2921 
2922 					/* newline before label will be subtracted from returned text, but
2923 					 * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
2924 					if (YYCURSOR[-indentation - 2] == '\r' && YYCURSOR[-indentation - 1] == '\n') {
2925 						newline = 2; /* Windows newline */
2926 					} else {
2927 						newline = 1;
2928 					}
2929 
2930 					CG(increment_lineno) = 1; /* For newline before label */
2931 
2932 					if (SCNG(heredoc_scan_ahead)) {
2933 						SCNG(heredoc_indentation) = indentation;
2934 						SCNG(heredoc_indentation_uses_spaces) = (spacing == HEREDOC_USING_SPACES);
2935 					} else {
2936 						YYCURSOR -= indentation;
2937 					}
2938 
2939 					BEGIN(ST_END_HEREDOC);
2940 
2941 					goto heredoc_scan_done;
2942 				}
2943 				continue;
2944 			case '$':
2945 				if (IS_LABEL_START(*YYCURSOR) || *YYCURSOR == '{') {
2946 					break;
2947 				}
2948 				continue;
2949 			case '{':
2950 				if (*YYCURSOR == '$') {
2951 					break;
2952 				}
2953 				continue;
2954 			case '\\':
2955 				if (YYCURSOR < YYLIMIT && *YYCURSOR != '\n' && *YYCURSOR != '\r') {
2956 					YYCURSOR++;
2957 				}
2958 				ZEND_FALLTHROUGH;
2959 			default:
2960 				continue;
2961 		}
2962 
2963 		YYCURSOR--;
2964 		break;
2965 	}
2966 
2967 heredoc_scan_done:
2968 
2969 	yyleng = YYCURSOR - SCNG(yy_text);
2970 	ZVAL_STRINGL(zendlval, yytext, yyleng - newline);
2971 
2972 	if (!SCNG(heredoc_scan_ahead) && !EG(exception) && PARSER_MODE()) {
2973 		bool newline_at_start = *(yytext - 1) == '\n' || *(yytext - 1) == '\r';
2974 		zend_string *copy = Z_STR_P(zendlval);
2975 
2976 		if (!strip_multiline_string_indentation(
2977 				zendlval, heredoc_label->indentation, heredoc_label->indentation_uses_spaces,
2978 				newline_at_start, newline != 0)) {
2979 			RETURN_TOKEN(T_ERROR);
2980 		}
2981 
2982 		if (UNEXPECTED(zend_scan_escape_string(zendlval, ZSTR_VAL(copy), ZSTR_LEN(copy), 0) != SUCCESS)) {
2983 			zend_string_efree(copy);
2984 			RETURN_TOKEN(T_ERROR);
2985 		}
2986 
2987 		zend_string_efree(copy);
2988 	} else {
2989 		HANDLE_NEWLINES(yytext, yyleng - newline);
2990 	}
2991 
2992 	RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
2993 }
2994 
2995 
2996 <ST_NOWDOC>{ANY_CHAR} {
2997 	zend_heredoc_label *heredoc_label = zend_ptr_stack_top(&SCNG(heredoc_label_stack));
2998 	int newline = 0, indentation = 0, spacing = -1;
2999 
3000 	if (YYCURSOR > YYLIMIT) {
3001 		RETURN_END_TOKEN;
3002 	}
3003 
3004 	YYCURSOR--;
3005 
3006 	while (YYCURSOR < YYLIMIT) {
3007 		switch (*YYCURSOR++) {
3008 			case '\r':
3009 				if (*YYCURSOR == '\n') {
3010 					YYCURSOR++;
3011 				}
3012 				ZEND_FALLTHROUGH;
3013 			case '\n':
3014 				indentation = spacing = 0;
3015 
3016 				while (YYCURSOR < YYLIMIT && (*YYCURSOR == ' ' || *YYCURSOR == '\t')) {
3017 					if (*YYCURSOR == '\t') {
3018 						spacing |= HEREDOC_USING_TABS;
3019 					} else {
3020 						spacing |= HEREDOC_USING_SPACES;
3021 					}
3022 					++YYCURSOR;
3023 					++indentation;
3024 				}
3025 
3026 				if (YYCURSOR == YYLIMIT) {
3027 					yyleng = YYCURSOR - SCNG(yy_text);
3028 					HANDLE_NEWLINES(yytext, yyleng);
3029 					ZVAL_NULL(zendlval);
3030 					RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
3031 				}
3032 
3033 				/* Check for ending label on the next line */
3034 				if (IS_LABEL_START(*YYCURSOR) && heredoc_label->length < YYLIMIT - YYCURSOR && !memcmp(YYCURSOR, heredoc_label->label, heredoc_label->length)) {
3035 					if (IS_LABEL_SUCCESSOR(YYCURSOR[heredoc_label->length])) {
3036 						continue;
3037 					}
3038 
3039 					if (spacing == (HEREDOC_USING_SPACES | HEREDOC_USING_TABS)) {
3040 						zend_throw_exception(zend_ce_parse_error, "Invalid indentation - tabs and spaces cannot be mixed", 0);
3041 						if (PARSER_MODE()) {
3042 							RETURN_TOKEN(T_ERROR);
3043 						}
3044 					}
3045 
3046 					/* newline before label will be subtracted from returned text, but
3047 					 * yyleng/yytext will include it, for zend_highlight/strip, tokenizer, etc. */
3048 					if (YYCURSOR[-indentation - 2] == '\r' && YYCURSOR[-indentation - 1] == '\n') {
3049 						newline = 2; /* Windows newline */
3050 					} else {
3051 						newline = 1;
3052 					}
3053 
3054 					CG(increment_lineno) = 1; /* For newline before label */
3055 
3056 					YYCURSOR -= indentation;
3057 					heredoc_label->indentation = indentation;
3058 
3059 					BEGIN(ST_END_HEREDOC);
3060 
3061 					goto nowdoc_scan_done;
3062 				}
3063 				ZEND_FALLTHROUGH;
3064 			default:
3065 				continue;
3066 		}
3067 	}
3068 
3069 nowdoc_scan_done:
3070 	yyleng = YYCURSOR - SCNG(yy_text);
3071 	ZVAL_STRINGL(zendlval, yytext, yyleng - newline);
3072 
3073 	if (!EG(exception) && spacing != -1 && PARSER_MODE()) {
3074 		bool newline_at_start = *(yytext - 1) == '\n' || *(yytext - 1) == '\r';
3075 		if (!strip_multiline_string_indentation(
3076 				zendlval, indentation, spacing == HEREDOC_USING_SPACES,
3077 				newline_at_start, newline != 0)) {
3078 			RETURN_TOKEN(T_ERROR);
3079 		}
3080 	}
3081 
3082 	HANDLE_NEWLINES(yytext, yyleng - newline);
3083 	RETURN_TOKEN_WITH_VAL(T_ENCAPSED_AND_WHITESPACE);
3084 }
3085 
3086 
3087 <ST_IN_SCRIPTING,ST_VAR_OFFSET>{ANY_CHAR} {
3088 	if (YYCURSOR > YYLIMIT) {
3089 		RETURN_END_TOKEN;
3090 	}
3091 
3092 	RETURN_TOKEN(T_BAD_CHARACTER);
3093 }
3094 
3095 */
3096 
3097 emit_token_with_str:
3098 	zend_copy_value(zendlval, (yytext + offset), (yyleng - offset));
3099 
3100 emit_token_with_val:
3101 	if (PARSER_MODE()) {
3102 		ZEND_ASSERT(Z_TYPE_P(zendlval) != IS_UNDEF);
3103 		elem->ast = zend_ast_create_zval_with_lineno(zendlval, start_line);
3104 	}
3105 
3106 emit_token:
3107 	if (SCNG(on_event)) {
3108 		SCNG(on_event)(ON_TOKEN, token, start_line, yytext, yyleng, SCNG(on_event_context));
3109 	}
3110 	return token;
3111 
3112 emit_token_with_ident:
3113 	if (PARSER_MODE()) {
3114 		elem->ident = SCNG(yy_text);
3115 	}
3116 	if (SCNG(on_event)) {
3117 		SCNG(on_event)(ON_TOKEN, token, start_line, yytext, yyleng, SCNG(on_event_context));
3118 	}
3119 	return token;
3120 
3121 return_whitespace:
3122 	HANDLE_NEWLINES(yytext, yyleng);
3123 	if (SCNG(on_event)) {
3124 		SCNG(on_event)(ON_TOKEN, T_WHITESPACE, start_line, yytext, yyleng, SCNG(on_event_context));
3125 	}
3126 	if (PARSER_MODE()) {
3127 		start_line = CG(zend_lineno);
3128 		goto restart;
3129 	} else {
3130 		return T_WHITESPACE;
3131 	}
3132 
3133 skip_token:
3134 	if (SCNG(on_event)) {
3135 		SCNG(on_event)(ON_TOKEN, token, start_line, yytext, yyleng, SCNG(on_event_context));
3136 	}
3137 	start_line = CG(zend_lineno);
3138 	goto restart;
3139 }
3140