syntax_errors.phpt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --TEST--
  2. Detailed 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. foreach ($badCode as $code) {
  28. try {
  29. eval($code);
  30. } catch (ParseError $e) {
  31. echo $e->getMessage(), "\n";
  32. }
  33. }
  34. echo "==DONE==\n";
  35. ?>
  36. --EXPECT--
  37. Unclosed '('
  38. Unclosed '['
  39. Unclosed '{'
  40. Unmatched ')'
  41. Unmatched ']'
  42. Unmatched '}'
  43. Unclosed '(' does not match ']'
  44. Unclosed '[' does not match ')'
  45. Unclosed '{' does not match ')'
  46. Unclosed '{' on line 1
  47. Unclosed '[' on line 1
  48. Unclosed '{' on line 1
  49. Unmatched ')'
  50. Unmatched ']'
  51. Unmatched '}'
  52. Unclosed '(' on line 1 does not match ']'
  53. Unclosed '[' on line 1 does not match ')'
  54. Unclosed '{' on line 1 does not match ')'
  55. ==DONE==