From 1ad5bfd723e8199435c1cf79e01f2e6bad00fea9 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 14 Jun 2011 14:12:03 +0200 Subject: [PATCH] [CssSelector] renamed SyntaxError --- .../CssSelector/Exception/ParseException.php | 4 +-- .../Component/CssSelector/Node/AttribNode.php | 4 +-- .../CssSelector/Node/CombinedSelectorNode.php | 6 ++-- .../CssSelector/Node/FunctionNode.php | 10 +++---- .../CssSelector/Node/NodeInterface.php | 2 +- .../Component/CssSelector/Node/PseudoNode.php | 26 ++++++++--------- src/Symfony/Component/CssSelector/Parser.php | 28 ++++++++++--------- .../Component/CssSelector/Tokenizer.php | 12 ++++---- .../Component/CssSelector/ParserTest.php | 2 +- 9 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/Symfony/Component/CssSelector/Exception/ParseException.php b/src/Symfony/Component/CssSelector/Exception/ParseException.php index 73b7b450b54a..38206c241163 100644 --- a/src/Symfony/Component/CssSelector/Exception/ParseException.php +++ b/src/Symfony/Component/CssSelector/Exception/ParseException.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace Symfony\Component\CssSelector; +namespace Symfony\Component\CssSelector\Exception; /** * ParseException is thrown when a CSS selector syntax is not valid. @@ -19,6 +19,6 @@ * * @author Fabien Potencier */ -class ParseException extends \LogicException +class ParseException extends \Exception { } diff --git a/src/Symfony/Component/CssSelector/Node/AttribNode.php b/src/Symfony/Component/CssSelector/Node/AttribNode.php index 862147ca6d33..958a413c94ce 100644 --- a/src/Symfony/Component/CssSelector/Node/AttribNode.php +++ b/src/Symfony/Component/CssSelector/Node/AttribNode.php @@ -12,7 +12,7 @@ namespace Symfony\Component\CssSelector\Node; use Symfony\Component\CssSelector\XPathExpr; -use Symfony\Component\CssSelector\SyntaxError; +use Symfony\Component\CssSelector\Exception\ParseException; /** * AttribNode represents a "selector[namespace|attrib operator value]" node. @@ -94,7 +94,7 @@ public function toXpath() // FIXME: case sensitive? $path->addCondition(sprintf('contains(%s, %s)', $attrib, XPathExpr::xpathLiteral($value))); } else { - throw new SyntaxError(sprintf('Unknown operator: %s', $this->operator)); + throw new ParseException(sprintf('Unknown operator: %s', $this->operator)); } return $path; diff --git a/src/Symfony/Component/CssSelector/Node/CombinedSelectorNode.php b/src/Symfony/Component/CssSelector/Node/CombinedSelectorNode.php index 271044c28b7b..e435a87d0f5c 100644 --- a/src/Symfony/Component/CssSelector/Node/CombinedSelectorNode.php +++ b/src/Symfony/Component/CssSelector/Node/CombinedSelectorNode.php @@ -11,7 +11,7 @@ namespace Symfony\Component\CssSelector\Node; -use Symfony\Component\CssSelector\SyntaxError; +use Symfony\Component\CssSelector\Exception\ParseException; /** * CombinedSelectorNode represents a combinator node. @@ -60,12 +60,12 @@ public function __toString() /** * {@inheritDoc} - * @throws SyntaxError When unknown combinator is found + * @throws ParseException When unknown combinator is found */ public function toXpath() { if (!isset(self::$methodMapping[$this->combinator])) { - throw new SyntaxError(sprintf('Unknown combinator: %s', $this->combinator)); + throw new ParseException(sprintf('Unknown combinator: %s', $this->combinator)); } $method = '_xpath_'.self::$methodMapping[$this->combinator]; diff --git a/src/Symfony/Component/CssSelector/Node/FunctionNode.php b/src/Symfony/Component/CssSelector/Node/FunctionNode.php index 1d285c9460ea..c16cb499a7ee 100644 --- a/src/Symfony/Component/CssSelector/Node/FunctionNode.php +++ b/src/Symfony/Component/CssSelector/Node/FunctionNode.php @@ -11,7 +11,7 @@ namespace Symfony\Component\CssSelector\Node; -use Symfony\Component\CssSelector\SyntaxError; +use Symfony\Component\CssSelector\Exception\ParseException; use Symfony\Component\CssSelector\XPathExpr; /** @@ -57,17 +57,17 @@ public function __toString() /** * {@inheritDoc} - * @throws SyntaxError When unsupported or unknown pseudo-class is found + * @throws ParseException When unsupported or unknown pseudo-class is found */ public function toXpath() { $selPath = $this->selector->toXpath(); if (in_array($this->name, self::$unsupported)) { - throw new SyntaxError(sprintf('The pseudo-class %s is not supported', $this->name)); + throw new ParseException(sprintf('The pseudo-class %s is not supported', $this->name)); } $method = '_xpath_'.str_replace('-', '_', $this->name); if (!method_exists($this, $method)) { - throw new SyntaxError(sprintf('The pseudo-class %s is unknown', $this->name)); + throw new ParseException(sprintf('The pseudo-class %s is unknown', $this->name)); } return $this->$method($selPath, $this->expr); @@ -167,7 +167,7 @@ protected function _xpath_nth_last_child($xpath, $expr) protected function _xpath_nth_of_type($xpath, $expr) { if ($xpath->getElement() == '*') { - throw new SyntaxError('*:nth-of-type() is not implemented'); + throw new ParseException('*:nth-of-type() is not implemented'); } return $this->_xpath_nth_child($xpath, $expr, false, false); diff --git a/src/Symfony/Component/CssSelector/Node/NodeInterface.php b/src/Symfony/Component/CssSelector/Node/NodeInterface.php index a3d8670b6fae..534f0dd7b1dc 100644 --- a/src/Symfony/Component/CssSelector/Node/NodeInterface.php +++ b/src/Symfony/Component/CssSelector/Node/NodeInterface.php @@ -31,7 +31,7 @@ function __toString(); /** * @return XPathExpr The XPath expression * - * @throws SyntaxError When unknown operator is found + * @throws ParseException When unknown operator is found */ function toXpath(); } diff --git a/src/Symfony/Component/CssSelector/Node/PseudoNode.php b/src/Symfony/Component/CssSelector/Node/PseudoNode.php index d15f0e5289a8..3ef3fea9ccdb 100644 --- a/src/Symfony/Component/CssSelector/Node/PseudoNode.php +++ b/src/Symfony/Component/CssSelector/Node/PseudoNode.php @@ -11,7 +11,7 @@ namespace Symfony\Component\CssSelector\Node; -use Symfony\Component\CssSelector\SyntaxError; +use Symfony\Component\CssSelector\Exception\ParseException; /** * PseudoNode represents a "selector:ident" node. @@ -39,14 +39,14 @@ class PseudoNode implements NodeInterface * @param NodeInterface $element The NodeInterface element * @param string $type Node type * @param string $ident The ident - * @throws SyntaxError When incorrect PseudoNode type is given + * @throws ParseException When incorrect PseudoNode type is given */ public function __construct($element, $type, $ident) { $this->element = $element; if (!in_array($type, array(':', '::'))) { - throw new SyntaxError(sprintf('The PseudoNode type can only be : or :: (%s given).', $type)); + throw new ParseException(sprintf('The PseudoNode type can only be : or :: (%s given).', $type)); } $this->type = $type; @@ -63,18 +63,18 @@ public function __toString() /** * {@inheritDoc} - * @throws SyntaxError When unsupported or unknown pseudo-class is found + * @throws ParseException When unsupported or unknown pseudo-class is found */ public function toXpath() { $elXpath = $this->element->toXpath(); if (in_array($this->ident, self::$unsupported)) { - throw new SyntaxError(sprintf('The pseudo-class %s is unsupported', $this->ident)); + throw new ParseException(sprintf('The pseudo-class %s is unsupported', $this->ident)); } $method = 'xpath_'.str_replace('-', '_', $this->ident); if (!method_exists($this, $method)) { - throw new SyntaxError(sprintf('The pseudo-class %s is unknown', $this->ident)); + throw new ParseException(sprintf('The pseudo-class %s is unknown', $this->ident)); } return $this->$method($elXpath); @@ -96,12 +96,12 @@ protected function xpath_checked($xpath) /** * @param XPathExpr $xpath The XPath expression * @return XPathExpr The modified XPath expression - * @throws SyntaxError If this element is the root element + * @throws ParseException If this element is the root element */ protected function xpath_root($xpath) { // if this element is the root element - throw new SyntaxError(); + throw new ParseException(); } /** @@ -143,7 +143,7 @@ protected function xpath_last_child($xpath) protected function xpath_first_of_type($xpath) { if ($xpath->getElement() == '*') { - throw new SyntaxError('*:first-of-type is not implemented'); + throw new ParseException('*:first-of-type is not implemented'); } $xpath->addStarPrefix(); $xpath->addCondition('position() = 1'); @@ -156,12 +156,12 @@ protected function xpath_first_of_type($xpath) * * @param XPathExpr $xpath The XPath expression * @return XPathExpr The modified expression - * @throws SyntaxError Because *:last-of-type is not implemented + * @throws ParseException Because *:last-of-type is not implemented */ protected function xpath_last_of_type($xpath) { if ($xpath->getElement() == '*') { - throw new SyntaxError('*:last-of-type is not implemented'); + throw new ParseException('*:last-of-type is not implemented'); } $xpath->addStarPrefix(); $xpath->addCondition('position() = last()'); @@ -189,12 +189,12 @@ protected function xpath_only_child($xpath) * * @param XPathExpr $xpath The XPath expression * @return XPathExpr The modified expression - * @throws SyntaxError Because *:only-of-type is not implemented + * @throws ParseException Because *:only-of-type is not implemented */ protected function xpath_only_of_type($xpath) { if ($xpath->getElement() == '*') { - throw new SyntaxError('*:only-of-type is not implemented'); + throw new ParseException('*:only-of-type is not implemented'); } $xpath->addCondition('last() = 1'); diff --git a/src/Symfony/Component/CssSelector/Parser.php b/src/Symfony/Component/CssSelector/Parser.php index 66033c5043b3..851c919ddaa3 100644 --- a/src/Symfony/Component/CssSelector/Parser.php +++ b/src/Symfony/Component/CssSelector/Parser.php @@ -11,6 +11,8 @@ namespace Symfony\Component\CssSelector; +use Symfony\Component\CssSelector\Exception\ParseException; + /** * Parser is the main entry point of the component and can convert CSS * selectors to XPath expressions. @@ -36,7 +38,7 @@ class Parser * * @return string * - * @throws SyntaxError When got None for xpath expression + * @throws ParseException When got None for xpath expression * * @api */ @@ -63,7 +65,7 @@ static public function cssToXpath($cssExpr, $prefix = 'descendant-or-self::') // @codeCoverageIgnoreStart if (!$expr) { - throw new SyntaxError(sprintf('Got None for xpath expression from %s.', $cssExpr)); + throw new ParseException(sprintf('Got None for xpath expression from %s.', $cssExpr)); } // @codeCoverageIgnoreEnd @@ -130,7 +132,7 @@ private function parseSelectorGroup($stream) * Parses a selector contained in $stream and returns the Node * object that represents it. * - * @throws SyntaxError When expected selector but got something else + * @throws ParseException When expected selector but got something else * * @param TokenStream $stream The stream containing the selector. * @@ -153,7 +155,7 @@ private function parseSelector($stream) $consumed = count($stream->getUsed()); $nextSelector = $this->parseSimpleSelector($stream); if ($consumed == count($stream->getUsed())) { - throw new SyntaxError(sprintf("Expected selector, got '%s'", $stream->peek())); + throw new ParseException(sprintf("Expected selector, got '%s'", $stream->peek())); } $result = new Node\CombinedSelectorNode($result, $combinator, $nextSelector); @@ -166,7 +168,7 @@ private function parseSelector($stream) * Parses a simple selector (the current token) from $stream and returns * the resulting Node object. * - * @throws SyntaxError When expected symbol but got something else + * @throws ParseException When expected symbol but got something else * * @param TokenStream $stream The stream containing the selector. * @@ -180,7 +182,7 @@ private function parseSimpleSelector($stream) } else { $next = $stream->next(); if ('*' != $next && !$next->isType('Symbol')) { - throw new SyntaxError(sprintf("Expected symbol, got '%s'", $next)); + throw new ParseException(sprintf("Expected symbol, got '%s'", $next)); } if ($stream->peek() == '|') { @@ -188,7 +190,7 @@ private function parseSimpleSelector($stream) $stream->next(); $element = $stream->next(); if ('*' != $element && !$next->isType('Symbol')) { - throw new SyntaxError(sprintf("Expected symbol, got '%s'", $next)); + throw new ParseException(sprintf("Expected symbol, got '%s'", $next)); } } else { $namespace = '*'; @@ -223,7 +225,7 @@ private function parseSimpleSelector($stream) $result = $this->parseAttrib($result, $stream); $next = $stream->next(); if (']' != $next) { - throw new SyntaxError(sprintf("] expected, got '%s'", $next)); + throw new ParseException(sprintf("] expected, got '%s'", $next)); } continue; @@ -231,7 +233,7 @@ private function parseSimpleSelector($stream) $type = $stream->next(); $ident = $stream->next(); if (!$ident || !$ident->isType('Symbol')) { - throw new SyntaxError(sprintf("Expected symbol, got '%s'", $ident)); + throw new ParseException(sprintf("Expected symbol, got '%s'", $ident)); } if ($stream->peek() == '(') { @@ -247,7 +249,7 @@ private function parseSimpleSelector($stream) } $next = $stream->next(); if (')' != $next) { - throw new SyntaxError(sprintf("Expected ')', got '%s' and '%s'", $next, $selector)); + throw new ParseException(sprintf("Expected ')', got '%s' and '%s'", $next, $selector)); } $result = new Node\FunctionNode($result, $type, $ident, $selector); @@ -273,7 +275,7 @@ private function parseSimpleSelector($stream) * Parses an attribute from a selector contained in $stream and returns * the resulting AttribNode object. * - * @throws SyntaxError When encountered unexpected selector + * @throws ParseException When encountered unexpected selector * * @param Node\NodeInterface $selector The selector object whose attribute * is to be parsed. @@ -298,12 +300,12 @@ private function parseAttrib($selector, $stream) $op = $stream->next(); if (!in_array($op, array('^=', '$=', '*=', '=', '~=', '|=', '!='))) { - throw new SyntaxError(sprintf("Operator expected, got '%s'", $op)); + throw new ParseException(sprintf("Operator expected, got '%s'", $op)); } $value = $stream->next(); if (!$value->isType('Symbol') && !$value->isType('String')) { - throw new SyntaxError(sprintf("Expected string or symbol, got '%s'", $value)); + throw new ParseException(sprintf("Expected string or symbol, got '%s'", $value)); } return new Node\AttribNode($selector, $namespace, $attrib, $op, $value); diff --git a/src/Symfony/Component/CssSelector/Tokenizer.php b/src/Symfony/Component/CssSelector/Tokenizer.php index 95860b6165b2..e8ad755bd5be 100644 --- a/src/Symfony/Component/CssSelector/Tokenizer.php +++ b/src/Symfony/Component/CssSelector/Tokenizer.php @@ -107,7 +107,7 @@ public function tokenize($s) * and returns an array holding the unquoted string contained by $s and * the new position from which tokenizing should take over. * - * @throws SyntaxError When expected closing is not found + * @throws ParseException When expected closing is not found * * @param string $s The selector string containing the quoted string. * @param integer $pos The starting position for the quoted string. @@ -123,7 +123,7 @@ private function tokenizeEscapedString($s, $pos) while (true) { $next = strpos($s, $quote, $pos); if (false === $next) { - throw new SyntaxError(sprintf('Expected closing %s for string in: %s', $quote, substr($s, $start))); + throw new ParseException(sprintf('Expected closing %s for string in: %s', $quote, substr($s, $start))); } $result = substr($s, $start, $next - $start); @@ -144,7 +144,7 @@ private function tokenizeEscapedString($s, $pos) /** * Unescapes a string literal and returns the unescaped string. * - * @throws SyntaxError When invalid escape sequence is found + * @throws ParseException When invalid escape sequence is found * * @param string $literal The string literal to unescape. * @@ -160,7 +160,7 @@ private function unescapeStringLiteral($literal) return chr(trim($matches[0])); } } else { - throw new SyntaxError(sprintf('Invalid escape sequence %s in string %s', $matches[0], $literal)); + throw new ParseException(sprintf('Invalid escape sequence %s in string %s', $matches[0], $literal)); } }, $literal); } @@ -170,7 +170,7 @@ private function unescapeStringLiteral($literal) * contained in it and the new position from which tokenizing should take * over. * - * @throws SyntaxError When Unexpected symbol is found + * @throws ParseException When Unexpected symbol is found * * @param string $s The selector string. * @param integer $pos The position in $s at which the symbol starts. @@ -189,7 +189,7 @@ private function tokenizeSymbol($s, $pos) $matchStart = $match[0][1]; if ($matchStart == $pos) { - throw new SyntaxError(sprintf('Unexpected symbol: %s at %s', $s[$pos], $pos)); + throw new ParseException(sprintf('Unexpected symbol: %s at %s', $s[$pos], $pos)); } $result = substr($s, $start, $matchStart - $start); diff --git a/tests/Symfony/Tests/Component/CssSelector/ParserTest.php b/tests/Symfony/Tests/Component/CssSelector/ParserTest.php index 8261a1203eac..9a036f99f6b7 100644 --- a/tests/Symfony/Tests/Component/CssSelector/ParserTest.php +++ b/tests/Symfony/Tests/Component/CssSelector/ParserTest.php @@ -42,7 +42,7 @@ public function testParseExceptions() $parser->parse('h1:'); $this->fail('->parse() throws an Exception if the css selector is not valid'); } catch (\Exception $e) { - $this->assertInstanceOf('\Symfony\Component\CssSelector\SyntaxError', $e, '->parse() throws an Exception if the css selector is not valid'); + $this->assertInstanceOf('\Symfony\Component\CssSelector\Exception\ParseException', $e, '->parse() throws an Exception if the css selector is not valid'); $this->assertEquals("Expected symbol, got '' at h1: -> ", $e->getMessage(), '->parse() throws an Exception if the css selector is not valid'); } }