diff options
author | Alex Dowad <alexinbeijing@gmail.com> | 2020-04-08 13:19:39 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-04-14 11:22:23 +0200 |
commit | 80598f12507c5cbde04163289e4d2575f05d2a0c (patch) | |
tree | f5d3c26400dcb577e6c508469d1de086f5fda695 /Zend/zend_language_scanner.h | |
parent | d4471c6aaecab2f1cc467ea8e92040c73685bd21 (diff) | |
download | php-git-80598f12507c5cbde04163289e4d2575f05d2a0c.tar.gz |
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.
Diffstat (limited to 'Zend/zend_language_scanner.h')
-rw-r--r-- | Zend/zend_language_scanner.h | 7 |
1 files changed, 7 insertions, 0 deletions
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); |