Skip to content

Commit

Permalink
[CssSelector] renamed SyntaxError
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 14, 2011
1 parent d87c96b commit 1ad5bfd
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 46 deletions.
Expand Up @@ -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.
Expand All @@ -19,6 +19,6 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ParseException extends \LogicException
class ParseException extends \Exception
{
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/CssSelector/Node/AttribNode.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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.
Expand Down Expand Up @@ -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];
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/CssSelector/Node/FunctionNode.php
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/CssSelector/Node/NodeInterface.php
Expand Up @@ -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();
}
26 changes: 13 additions & 13 deletions src/Symfony/Component/CssSelector/Node/PseudoNode.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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');
Expand All @@ -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()');
Expand Down Expand Up @@ -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');

Expand Down
28 changes: 15 additions & 13 deletions src/Symfony/Component/CssSelector/Parser.php
Expand Up @@ -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.
Expand All @@ -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
*/
Expand All @@ -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

Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
Expand All @@ -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.
*
Expand All @@ -180,15 +182,15 @@ 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() == '|') {
$namespace = $next;
$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 = '*';
Expand Down Expand Up @@ -223,15 +225,15 @@ 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;
} elseif (':' == $peek || '::' == $peek) {
$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() == '(') {
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/CssSelector/Tokenizer.php
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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.
*
Expand All @@ -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);
}
Expand All @@ -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.
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Symfony/Tests/Component/CssSelector/ParserTest.php
Expand Up @@ -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');
}
}
Expand Down

0 comments on commit 1ad5bfd

Please sign in to comment.