Skip to content

Commit

Permalink
Throw ParseException from lexer
Browse files Browse the repository at this point in the history
Primarily to avoid getting fatal errors from token_get_all().

Implemented using a magic E_ERROR token, which the lexer emits to
force a parser failure.
  • Loading branch information
nikic committed Apr 2, 2015
1 parent 83a1580 commit a8bf1c5
Show file tree
Hide file tree
Showing 15 changed files with 470 additions and 384 deletions.
20 changes: 15 additions & 5 deletions Zend/tests/require_parse_exception.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ allow_url_include=1
--FILE--
<?php


function test_parse_error($code) {
try {
require 'data://text/plain;base64,' . base64_encode($code);
} catch (ParseException $e) {
echo $e->getMessage(), "\n";
echo $e->getMessage(), " on line ", $e->getLine(), "\n";
}
}

Expand All @@ -33,8 +32,19 @@ 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--
syntax error, unexpected end of file
syntax error, unexpected end of file
syntax error, unexpected end of file, expecting '('
syntax error, unexpected end of file on line 2
syntax error, unexpected end of file 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
5 changes: 5 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,11 @@ void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
/* this should be compatible with the standard zenderror */
void zenderror(const char *error) /* {{{ */
{
if (EG(exception)) {
/* An exception was thrown in the lexer, don't throw another in the parser. */
return;
}

zend_throw_exception(zend_get_parse_exception(), error, E_PARSE);
}
/* }}} */
Expand Down
5 changes: 4 additions & 1 deletion Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
%token T_POW "** (T_POW)"
%token T_POW_EQUAL "**= (T_POW_EQUAL)"

/* Token used to force a parse error from the lexer */
%token T_ERROR

%type <ast> top_statement namespace_name name statement function_declaration_statement
%type <ast> class_declaration_statement trait_declaration_statement
%type <ast> interface_declaration_statement interface_extends_list
Expand Down Expand Up @@ -1227,7 +1230,7 @@ static YYSIZE_T zend_yytnamerr(char *yyres, const char *yystr)
return yystrlen(yystr);
}
{
if (CG(parse_error) == 0) {
if (CG(parse_error) == 0) {
char buffer[120];
const unsigned char *end, *str, *tok1 = NULL, *tok2 = NULL;
unsigned int len = 0, toklen = 0, yystr_len;
Expand Down
Loading

0 comments on commit a8bf1c5

Please sign in to comment.