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