1
0
mirror of https://github.com/php/php-src.git synced 2026-04-15 20:11:02 +02:00
Files
archived-php-src/tests/lang/syntax_errors.phpt
Alex Dowad 80598f1250 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.
2020-04-14 11:22:23 +02:00

58 lines
1.9 KiB
PHP

--TEST--
Detailed reporting on specific types of syntax errors
--FILE--
<?
$badCode = [
"if(1 > 2", /* unclosed ( */
"[1, 2,", /* unclosed [ */
"if(1) { echo 'hello'; ", /* unclosed { */
"(1 + 2));", /* too many ) */
"[1, 2]]", /* too many ] */
"if (1) { } }", /* too many } */
"(1 + 2];", /* ] doesn't match ( */
"[1, 2)];", /* ) doesn't match [ */
"if(1) { echo 'a'; )}", /* ) doesn't match { */
/* separately test cases where the faulty construct spans multiple lines,
since the error message should refer to the starting line in those cases */
"if(1 > 2) {\n echo '1';", /* unclosed (, spans multiple lines */
"[1,\n2,\n3,", /* unclosed [, spans multiple lines */
"{\n echo '1';\n echo '2';", /* unclosed {, spans multiple lines */
"(1 +\n 2 +\n 3))", /* too many ), spans multiple lines */
"[1,\n2,\n3]];", /* too many ], spans multiple lines */
"if (1)\n {\n }}", /* too many }, spans multiple lines */
"(1 +\n\n 2])", /* ] doesn't match (, spans multiple lines */
"[1,\n2,\n3)]", /* ) doesn't match [, spans multiple lines */
"if(1) {\n echo 'a';\n)}", /* ) doesn't match {, spans multiple lines */
];
foreach ($badCode as $code) {
try {
eval($code);
} catch (ParseError $e) {
echo $e->getMessage(), "\n";
}
}
echo "==DONE==\n";
?>
--EXPECT--
Unclosed '('
Unclosed '['
Unclosed '{'
Unmatched ')'
Unmatched ']'
Unmatched '}'
Unclosed '(' does not match ']'
Unclosed '[' does not match ')'
Unclosed '{' does not match ')'
Unclosed '{' on line 1
Unclosed '[' on line 1
Unclosed '{' on line 1
Unmatched ')'
Unmatched ']'
Unmatched '}'
Unclosed '(' on line 1 does not match ']'
Unclosed '[' on line 1 does not match ')'
Unclosed '{' on line 1 does not match ')'
==DONE==