xref: /php-src/tests/lang/syntax_errors.phpt (revision 8e5df609)
1--TEST--
2Detailed reporting on specific types of syntax errors
3--FILE--
4<?php
5$badCode = [
6  "if(1 > 2",                   /* unclosed ( */
7  "[1, 2,",                     /* unclosed [ */
8  "if(1) { echo 'hello'; ",     /* unclosed { */
9  "(1 + 2));",                  /* too many ) */
10  "[1, 2]]",                    /* too many ] */
11  "if (1) {  }  }",             /* too many } */
12  "(1 + 2];",                   /* ] doesn't match ( */
13  "[1, 2)];",                   /* ) doesn't match [ */
14  "if(1) { echo 'a'; )}",       /* ) doesn't match { */
15  /* separately test cases where the faulty construct spans multiple lines,
16     since the error message should refer to the starting line in those cases */
17  "if(1 > 2) {\n  echo '1';",   /* unclosed (, spans multiple lines */
18  "[1,\n2,\n3,",                /* unclosed [, spans multiple lines */
19  "{\n  echo '1';\n echo '2';", /* unclosed {, spans multiple lines */
20  "(1 +\n 2 +\n 3))",           /* too many ), spans multiple lines */
21  "[1,\n2,\n3]];",              /* too many ], spans multiple lines */
22  "if (1)\n  {\n    }}",        /* too many }, spans multiple lines */
23  "(1 +\n\n  2])",              /* ] doesn't match (, spans multiple lines */
24  "[1,\n2,\n3)]",               /* ) doesn't match [, spans multiple lines */
25  "if(1) {\n  echo 'a';\n)}",   /* ) doesn't match {, spans multiple lines */
26  ];
27
28foreach ($badCode as $code) {
29  try {
30    eval($code);
31  } catch (ParseError $e) {
32    echo $e->getMessage(), "\n";
33  }
34}
35
36echo "==DONE==\n";
37?>
38--EXPECT--
39Unclosed '('
40Unclosed '['
41Unclosed '{'
42Unmatched ')'
43Unmatched ']'
44Unmatched '}'
45Unclosed '(' does not match ']'
46Unclosed '[' does not match ')'
47Unclosed '{' does not match ')'
48Unclosed '{' on line 1
49Unclosed '[' on line 1
50Unclosed '{' on line 1
51Unmatched ')'
52Unmatched ']'
53Unmatched '}'
54Unclosed '(' on line 1 does not match ']'
55Unclosed '[' on line 1 does not match ')'
56Unclosed '{' on line 1 does not match ')'
57==DONE==
58