From a35c2a20671199fffcb424d9183f0a959b105065 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 27 Apr 2015 15:33:51 +0200 Subject: [PATCH] Add column info to EOF errors EOF errors are now located one past the end of the file. --- lib/PhpParser/Error.php | 2 +- lib/PhpParser/Lexer.php | 14 ++++++++------ test/code/parser/error_pos.test-fail | 9 +++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 test/code/parser/error_pos.test-fail diff --git a/lib/PhpParser/Error.php b/lib/PhpParser/Error.php index 21d67d5..58d1dd6 100644 --- a/lib/PhpParser/Error.php +++ b/lib/PhpParser/Error.php @@ -121,7 +121,7 @@ class Error extends \RuntimeException } private function toColumn($code, $pos) { - if ($pos >= strlen($code)) { + if ($pos > strlen($code)) { throw new \RuntimeException('Invalid position information'); } diff --git a/lib/PhpParser/Lexer.php b/lib/PhpParser/Lexer.php index 4c66562..25cb465 100644 --- a/lib/PhpParser/Lexer.php +++ b/lib/PhpParser/Lexer.php @@ -123,8 +123,13 @@ class Lexer $startAttributes = array(); $endAttributes = array(); - while (isset($this->tokens[++$this->pos])) { - $token = $this->tokens[$this->pos]; + while (1) { + if (isset($this->tokens[++$this->pos])) { + $token = $this->tokens[$this->pos]; + } else { + // EOF token with ID 0 + $token = "\0"; + } if (isset($this->usedAttributes['startTokenPos'])) { $startAttributes['startTokenPos'] = $this->pos; @@ -192,10 +197,7 @@ class Lexer } } - $startAttributes['startLine'] = $this->line; - - // 0 is the EOF token - return 0; + throw new \RuntimeException('Reached end of lexer loop'); } /** diff --git a/test/code/parser/error_pos.test-fail b/test/code/parser/error_pos.test-fail new file mode 100644 index 0000000..9e2bfb5 --- /dev/null +++ b/test/code/parser/error_pos.test-fail @@ -0,0 +1,9 @@ +Error positions +----- +