From 80598f12507c5cbde04163289e4d2575f05d2a0c Mon Sep 17 00:00:00 2001 From: Alex Dowad Date: Wed, 8 Apr 2020 13:19:39 +0200 Subject: Syntax errors caused by unclosed {, [, ( mention specific location Aside from a few very specific syntax errors for which detailed exceptions are thrown, generally PHP just emits the default error messages generated by bison on syntax error. These messages are very uninformative; they just say "Unexpected ... at line ...". This is most problematic with constructs which can span an arbitrary number of lines, such as blocks of code delimited by { }, 'if' conditions delimited by ( ), and so on. If a closing delimiter is missed, the block will run for the entire remainder of the source file (which could be thousands of lines), and then at the end, a parse error will be thrown with the dreaded words: "Unexpected end of file". Therefore, track the positions of opening and closing delimiters and ensure that they match up correctly. If any mismatch or missing delimiter is detected, immediately throw a parse error which points the user to the offending line. This is best done in the *lexer* and not in the parser. Thanks to Nikita Popov and George Peter Banyard for suggesting improvements. Fixes bug #79368. Closes GH-5364. --- Zend/zend_language_scanner.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'Zend/zend_language_scanner.h') diff --git a/Zend/zend_language_scanner.h b/Zend/zend_language_scanner.h index 4d51a064fc..35eccaf7e6 100644 --- a/Zend/zend_language_scanner.h +++ b/Zend/zend_language_scanner.h @@ -30,6 +30,7 @@ typedef struct _zend_lex_state { int yy_state; zend_stack state_stack; zend_ptr_stack heredoc_label_stack; + zend_stack nest_location_stack; /* for syntax error reporting */ zend_file_handle *in; uint32_t lineno; @@ -63,6 +64,12 @@ typedef struct _zend_heredoc_label { zend_bool indentation_uses_spaces; } zend_heredoc_label; +/* Track locations of unclosed {, [, (, etc. for better syntax error reporting */ +typedef struct _zend_nest_location { + char text; + int lineno; +} zend_nest_location; + BEGIN_EXTERN_C() ZEND_API void zend_save_lexical_state(zend_lex_state *lex_state); ZEND_API void zend_restore_lexical_state(zend_lex_state *lex_state); -- cgit v1.2.1