8000 Add line number support to UnexpectedTokenException and OutputException · surjit/PHP-CSS-Parser@b55cee9 · GitHub
Skip to content

Commit b55cee9

Browse files
committed
Add line number support to UnexpectedTokenException and OutputException
1 parent ab8c45c commit b55cee9

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ private function parseListItem(CSSList $oList, $bIsRoot = false) {
109109
$oAtRule = $this->parseAtRule();
110110
if($oAtRule instanceof Charset) {
111111
if(!$bIsRoot) {
112-
throw new UnexpectedTokenException('@charset may only occur in root document', '', 'custom');
112+
throw new UnexpectedTokenException('@charset may only occur in root document', '', 'custom', $this->iLineNum);
113113
}
114114
if(count($oList->getContents()) > 0) {
115-
throw new UnexpectedTokenException('@charset must be the first parseable token in a document', '', 'custom');
115+
throw new UnexpectedTokenException('@charset must be the first parseable token in a document', '', 'custom', $this->iLineNum);
116116
}
117117
$this->setCharset($oAtRule->getCharset()->getString());
118118
}
@@ -164,10 +164,10 @@ private function parseAtRule() {
164164
}
165165
$this->consume(';');
166166
if ($sPrefix !== null && !is_string($sPrefix)) {
167-
throw new UnexpectedTokenException('Wrong namespace prefix', $sPrefix, 'custom');
167+
throw new UnexpectedTokenException('Wrong namespace prefix', $sPrefix, 'custom', $iIdentifierLineNum);
168168
}
169169
if (!($mUrl instanceof CSSString || $mUrl instanceof URL)) {
170-
throw new UnexpectedTokenException('Wrong namespace url of invalid type', $mUrl, 'custom');
170+
throw new UnexpectedTokenException('Wrong namespace url of invalid type', $mUrl, 'custom', $iIdentifierLineNum);
171171
}
172172
return new CSSNamespace($mUrl, $sPrefix, $iIdentifierLineNum);
173173
} else {
@@ -195,7 +195,7 @@ private function parseAtRule() {
195195
private function parseIdentifier($bAllowFunctions = true, $bIgnoreCase = true) {
196196
$sResult = $this->parseCharacter(true);
197197
if ($sResult === null) {
198-
throw new UnexpectedTokenException($sResult, $this->peek(5), 'identifier');
198+
throw new UnexpectedTokenException($sResult, $this->peek(5), 'identifier', $this->iLineNum);
199199
}
200200
$sCharacter = null;
201201
while (($sCharacter = $this->parseCharacter(true)) !== null) {
@@ -522,14 +522,14 @@ private function consume($mValue = 1) {
522522
$iLineCount = substr_count($mValue, "\n");
523523
$iLength = $this->strlen($mValue);
524524
if (!$this->streql($this->substr($this->iCurrentPosition, $iLength), $mValue)) {
525-
throw new UnexpectedTokenException($mValue, $this->peek(max($iLength, 5)));
525+
throw new UnexpectedTokenException($mValue, $this->peek(max($iLength, 5)), $this->iLineNum);
526526
}
527527
$this->iLineNum += $iLineCount;
528528
$this->iCurrentPosition += $this->strlen($mValue);
529529
return $mValue;
530530
} else {
531531
if ($this->iCurrentPosition + $mValue > $this->iLength) {
532-
throw new UnexpectedTokenException($mValue, $this->peek(5), 'count');
532+
throw new UnexpectedTokenException($mValue, $this->peek(5), 'count', $this->iLineNum);
533533
}
534534
$sResult = $this->substr($this->iCurrentPosition, $mValue);
535535
$iLineCount = substr_count($sResult, "\n");
@@ -544,7 +544,7 @@ private function consumeExpression($mExpression) {
544544
if (preg_match($mExpression, $this->inputLeft(), $aMatches, PREG_OFFSET_CAPTURE) === 1) {
545545
return $this->consume($aMatches[0][0]);
546546
}
547-
throw new UnexpectedTokenException($mExpression, $this->peek(5), 'expression');
547+
throw new UnexpectedTokenException($mExpression, $this->peek(5), 'expression', $this->iLineNum);
548548
}
549549

550550
private function consumeWhiteSpace() {
@@ -602,7 +602,7 @@ private function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false)
602602
}
603603

604604
$this->iCurrentPosition = $start;
605-
throw new UnexpectedTokenException('One of ("'.implode('","', $aEnd).'")', $this->peek(5), 'search');
605+
throw new UnexpectedTokenException('One of ("'.implode('","', $aEnd).'")', $this->peek(5), 'search', $this->iLineNum);
606606
}
607607

608608
private function inputLeft() {

lib/Sabberworm/CSS/Parsing/OutputException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@
66
* Thrown if the CSS parsers attempts to print something invalid
77
*/
88
class OutputException extends \Exception {
9+
private $iLineNum;
10+
public function __construct($sMessage, $iLineNum = 0)
11+
{
12+
$this->$iLineNum = $iLineNum;
13+
if (!empty($iLineNum)) {
14+
$sMessage .= " [line no: $iLineNum]";
15+
}
16+
parent::__construct($sMessage);
17+
}
918
}

lib/Sabberworm/CSS/Parsing/UnexpectedTokenException.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ class UnexpectedTokenException extends \Exception {
1010
private $sFound;
1111
// Possible values: literal, identifier, count, expression, search
1212
private $sMatchType;
13+
private $iLineNum;
1314

14-
public function __construct($sExpected, $sFound, $sMatchType = 'literal') {
15+
public function __construct($sExpected, $sFound, $sMatchType = 'literal', $iLineNum = 0) {
1516
$this->sExpected = $sExpected;
1617
$this->sFound = $sFound;
1718
$this->sMatchType = $sMatchType;
19+
$this->iLineNum = $iLineNum;
1820
$sMessage = "Token “{$sExpected}” ({$sMatchType}) not found. Got “{$sFound}”.";
1921
if($this->sMatchType === 'search') {
2022
$sMessage = "Search for “{$sExpected}” returned no results. Context: “{$sFound}”.";
@@ -25,6 +27,11 @@ public function __construct($sExpected, $sFound, $sMatchType = 'literal') {
2527
} else if($this->sMatchType === 'custom') {
2628
$sMessage = trim("$sExpected $sFound");
2729
}
30+
31+
if (!empty($iLineNum)) {
32+
$sMessage .= " [line no: $iLineNum]";
33+
}
34+
2835
parent::__construct($sMessage);
2936
}
3037
}

lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ public function __toString() {
597597
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
598598
if(count($this->aSelectors) === 0) {
599599
// If all the selectors have been removed, this declaration block becomes invalid
600-
throw new OutputException("Attempt to print declaration block with missing selector");
600+
throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNum);
601601
}
602602
$sResult = $oOutputFormat->implode($oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(), $this->aSelectors) . $oOutputFormat->spaceBeforeOpeningBrace() . '{';
603603
$sResult .= parent::render($oOutputFormat);

0 commit comments

Comments
 (0)