From 62f83a0dc25e191b369b3c77415059c263009a23 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 18 Apr 2015 13:20:39 +0200 Subject: [PATCH] Add column info for non-syntax errors where relatively precise Should it also be added if only rough information is available? E.g. spanning an entire class? --- lib/PhpParser/Node/Param.php | 2 +- lib/PhpParser/Node/Stmt/Class_.php | 10 ++++++++-- lib/PhpParser/Node/Stmt/Interface_.php | 5 ++++- lib/PhpParser/Node/Stmt/Namespace_.php | 7 +++++-- lib/PhpParser/Parser.php | 2 +- test/code/parser/stmt/class/name.test-fail | 12 ++++++------ test/code/parser/stmt/function/variadic.test-fail | 2 +- .../parser/stmt/haltCompilerOutermostScope.test-fail | 2 +- test/code/parser/stmt/namespace/name.test-fail | 4 ++-- test/code/parser/stmt/namespace/nested.test-fail | 2 +- 10 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/PhpParser/Node/Param.php b/lib/PhpParser/Node/Param.php index e96eb03..c65842d 100644 --- a/lib/PhpParser/Node/Param.php +++ b/lib/PhpParser/Node/Param.php @@ -37,7 +37,7 @@ class Param extends NodeAbstract $this->default = $default; if ($variadic && null !== $default) { - throw new Error('Variadic parameter cannot have a default value'); + throw new Error('Variadic parameter cannot have a default value', $default->getAttributes()); } } diff --git a/lib/PhpParser/Node/Stmt/Class_.php b/lib/PhpParser/Node/Stmt/Class_.php index 548aaca..c2f983f 100644 --- a/lib/PhpParser/Node/Stmt/Class_.php +++ b/lib/PhpParser/Node/Stmt/Class_.php @@ -53,12 +53,18 @@ class Class_ extends ClassLike } if (isset(self::$specialNames[(string) $this->extends])) { - throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->extends)); + throw new Error( + sprintf('Cannot use \'%s\' as class name as it is reserved', $this->extends), + $this->extends->getAttributes() + ); } foreach ($this->implements as $interface) { if (isset(self::$specialNames[(string) $interface])) { - throw new Error(sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface)); + throw new Error( + sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), + $interface->getAttributes() + ); } } } diff --git a/lib/PhpParser/Node/Stmt/Interface_.php b/lib/PhpParser/Node/Stmt/Interface_.php index b047e51..0149de6 100644 --- a/lib/PhpParser/Node/Stmt/Interface_.php +++ b/lib/PhpParser/Node/Stmt/Interface_.php @@ -37,7 +37,10 @@ class Interface_ extends ClassLike foreach ($this->extends as $interface) { if (isset(self::$specialNames[(string) $interface])) { - throw new Error(sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface)); + throw new Error( + sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), + $interface->getAttributes() + ); } } } diff --git a/lib/PhpParser/Node/Stmt/Namespace_.php b/lib/PhpParser/Node/Stmt/Namespace_.php index 49b1490..b221ea1 100644 --- a/lib/PhpParser/Node/Stmt/Namespace_.php +++ b/lib/PhpParser/Node/Stmt/Namespace_.php @@ -31,13 +31,16 @@ class Namespace_ extends Node\Stmt $this->stmts = $stmts; if (isset(self::$specialNames[(string) $this->name])) { - throw new Error(sprintf('Cannot use \'%s\' as namespace name', $this->name)); + throw new Error( + sprintf('Cannot use \'%s\' as namespace name', $this->name), + $this->name->getAttributes() + ); } if (null !== $this->stmts) { foreach ($this->stmts as $stmt) { if ($stmt instanceof self) { - throw new Error('Namespace declarations cannot be nested', $stmt->getLine()); + throw new Error('Namespace declarations cannot be nested', $stmt->getAttributes()); } } } diff --git a/lib/PhpParser/Parser.php b/lib/PhpParser/Parser.php index 8825251..7935b30 100644 --- a/lib/PhpParser/Parser.php +++ b/lib/PhpParser/Parser.php @@ -1143,7 +1143,7 @@ class Parser extends ParserAbstract } protected function reduceRule32($attributes) { - throw new Error('__HALT_COMPILER() can only be used from the outermost scope'); + throw new Error('__HALT_COMPILER() can only be used from the outermost scope', $attributes); } protected function reduceRule33($attributes) { diff --git a/test/code/parser/stmt/class/name.test-fail b/test/code/parser/stmt/class/name.test-fail index 5c5dc13..dd698b6 100644 --- a/test/code/parser/stmt/class/name.test-fail +++ b/test/code/parser/stmt/class/name.test-fail @@ -14,11 +14,11 @@ Syntax error, unexpected T_STATIC, expecting T_STRING from 1:12 to 1:17 -----