php-src/Zend/tests/require_parse_exception.phpt
Rowan Tommins 55a15f32ce Improve output of tokens in Parse Errors
Currently, unexpected tokens in the parser are shown as the text
found, plus the internal token name, including the notorious
"unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)".

This commit replaces that with a more user-friendly format, with
two main types of token:

* Tokens which always represent the same text are shown like
  'unexpected token "::"' and 'expected "::"'
* Tokens which have variable text are given a user-friendly
  name, and show like 'unexpected identifier "foo"', and
  'expected identifer'.

A few tokens have special cases:

* unexpected token """ -> unexpected double-quote mark
* unexpected quoted string "'foo'" -> unexpected single-quoted
  string "foo"
* unexpected quoted string ""foo"" -> unexpected double-quoted
  string "foo"
* unexpected illegal character "_" -> unexpected character 0xNN
  (where _ is almost certainly a control character, and NN is the
   hexadecimal value of the byte)

The \ token has a special case in the implementation just to stop
bison making a mess of escaping it and it coming out as \\
2020-07-13 11:07:40 +02:00

52 lines
975 B
PHP

--TEST--
Parse exceptions when using require
--INI--
allow_url_include=1
--FILE--
<?php
function test_parse_error($code) {
try {
require 'data://text/plain;base64,' . base64_encode($code);
} catch (ParseError $e) {
echo $e->getMessage(), " on line ", $e->getLine(), "\n";
}
}
test_parse_error(<<<'EOC'
<?php
{ { { { { }
EOC
);
test_parse_error(<<<'EOC'
<?php
/** doc comment */
function f() {
EOC
);
test_parse_error(<<<'EOC'
<?php
empty
EOC
);
test_parse_error('<?php
var_dump(078);');
test_parse_error('<?php
var_dump("\u{xyz}");');
test_parse_error('<?php
var_dump("\u{ffffff}");');
?>
--EXPECT--
Deprecated: Directive 'allow_url_include' is deprecated in Unknown on line 0
Unclosed '{' on line 2
Unclosed '{' on line 3
syntax error, unexpected end of file, expecting "(" on line 2
Invalid numeric literal on line 2
Invalid UTF-8 codepoint escape sequence on line 2
Invalid UTF-8 codepoint escape sequence: Codepoint too large on line 2