UnexpectedTokenException.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Sabberworm\CSS\Parsing;
  3. /**
  4. * Thrown if the CSS parser encounters a token it did not expect.
  5. */
  6. class UnexpectedTokenException extends SourceException
  7. {
  8. /**
  9. * @var string
  10. */
  11. private $sExpected;
  12. /**
  13. * @var string
  14. */
  15. private $sFound;
  16. /**
  17. * Possible values: literal, identifier, count, expression, search
  18. *
  19. * @var string
  20. */
  21. private $sMatchType;
  22. /**
  23. * @param string $sExpected
  24. * @param string $sFound
  25. * @param string $sMatchType
  26. * @param int $iLineNo
  27. */
  28. public function __construct($sExpected, $sFound, $sMatchType = 'literal', $iLineNo = 0)
  29. {
  30. $this->sExpected = $sExpected;
  31. $this->sFound = $sFound;
  32. $this->sMatchType = $sMatchType;
  33. $sMessage = "Token “{$sExpected}” ({$sMatchType}) not found. Got “{$sFound}”.";
  34. if ($this->sMatchType === 'search') {
  35. $sMessage = "Search for “{$sExpected}” returned no results. Context: “{$sFound}”.";
  36. } elseif ($this->sMatchType === 'count') {
  37. $sMessage = "Next token was expected to have {$sExpected} chars. Context: “{$sFound}”.";
  38. } elseif ($this->sMatchType === 'identifier') {
  39. $sMessage = "Identifier expected. Got “{$sFound}”";
  40. } elseif ($this->sMatchType === 'custom') {
  41. $sMessage = trim("$sExpected $sFound");
  42. }
  43. parent::__construct($sMessage, $iLineNo);
  44. }
  45. }