Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 150 additions & 1 deletion src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,50 @@

class ParserState
{
/**
* @var null
*/
const EOF = null;

/**
* @var Settings
*/
private $oParserSettings;

/**
* @var string
*/
private $sText;

/**
* @var array<int, string>
*/
private $aText;

/**
* @var int
*/
private $iCurrentPosition;

/**
* @var string
*/
private $sCharset;

/**
* @var int
*/
private $iLength;

/**
* @var int
*/
private $iLineNo;

/**
* @param string $sText
* @param int $iLineNo
*/
public function __construct($sText, Settings $oParserSettings, $iLineNo = 1)
{
$this->oParserSettings = $oParserSettings;
Expand All @@ -32,6 +60,11 @@ public function __construct($sText, Settings $oParserSettings, $iLineNo = 1)
$this->setCharset($this->oParserSettings->sDefaultCharset);
}

/**
* @param string $sCharset
*
* @return void
*/
public function setCharset($sCharset)
{
$this->sCharset = $sCharset;
Expand All @@ -41,26 +74,45 @@ public function setCharset($sCharset)
}
}

/**
* @return string
*/
public function getCharset()
{
return $this->sCharset;
}

/**
* @return int
*/
public function currentLine()
{
return $this->iLineNo;
}

/**
* @return int
*/
public function currentColumn()
{
return $this->iCurrentPosition;
}

/**
* @return Settings
*/
public function getSettings()
{
return $this->oParserSettings;
}

/**
* @param bool $bIgnoreCase
*
* @return string
*
* @throws UnexpectedTokenException
*/
public function parseIdentifier($bIgnoreCase = true)
{
$sResult = $this->parseCharacter(true);
Expand All @@ -81,6 +133,14 @@ public function parseIdentifier($bIgnoreCase = true)
return $sResult;
}

/**
* @param bool $bIsForIdentifier
*
* @return string|null
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function parseCharacter($bIsForIdentifier)
{
if ($this->peek() === '\\') {
Expand Down Expand Up @@ -136,6 +196,12 @@ public function parseCharacter($bIsForIdentifier)
return null;
}

/**
* @return array<int, Comment>|void
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consumeWhiteSpace()
{
$comments = [];
Expand All @@ -160,6 +226,12 @@ public function consumeWhiteSpace()
return $comments;
}

/**
* @param string $sString
* @param bool $bCaseInsensitive
*
* @return bool
*/
public function comes($sString, $bCaseInsensitive = false)
{
$sPeek = $this->peek(strlen($sString));
Expand All @@ -168,6 +240,12 @@ public function comes($sString, $bCaseInsensitive = false)
: $this->streql($sPeek, $sString, $bCaseInsensitive);
}

/**
* @param int $iLength
* @param int $iOffset
*
* @return string
*/
public function peek($iLength = 1, $iOffset = 0)
{
$iOffset += $this->iCurrentPosition;
Expand All @@ -177,6 +255,14 @@ public function peek($iLength = 1, $iOffset = 0)
return $this->substr($iOffset, $iLength);
}

/**
* @param int $mValue
*
* @return string
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consume($mValue = 1)
{
if (is_string($mValue)) {
Expand All @@ -200,6 +286,15 @@ public function consume($mValue = 1)
}
}

/**
* @param string $mExpression
* @param int|null $iMaxLength
*
* @return string
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consumeExpression($mExpression, $iMaxLength = null)
{
$aMatches = null;
Expand All @@ -211,7 +306,7 @@ public function consumeExpression($mExpression, $iMaxLength = null)
}

/**
* @return false|Comment
* @return Comment|false
*/
public function consumeComment()
{
Expand All @@ -237,13 +332,24 @@ public function consumeComment()
return $mComment;
}

/**
* @return bool
*/
public function isEnd()
{
return $this->iCurrentPosition >= $this->iLength;
}

/**
* @param array<array-key, string>|string $aEnd
* @param string $bIncludeEnd
* @param string $consumeEnd
* @param array<int, Comment> $comments
*
* @return string
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = [])
{
Expand Down Expand Up @@ -280,11 +386,21 @@ public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, a
);
}

/**
* @return string
*/
private function inputLeft()
{
return $this->substr($this->iCurrentPosition, -1);
}

/**
* @param string $sString1
* @param string $sString2
* @param bool $bCaseInsensitive
*
* @return bool
*/
public function streql($sString1, $sString2, $bCaseInsensitive = true)
{
if ($bCaseInsensitive) {
Expand All @@ -294,11 +410,21 @@ public function streql($sString1, $sString2, $bCaseInsensitive = true)
}
}

/**
* @param int $iAmount
*
* @return void
*/
public function backtrack($iAmount)
{
$this->iCurrentPosition -= $iAmount;
}

/**
* @param string $sString
*
* @return int
*/
public function strlen($sString)
{
if ($this->oParserSettings->bMultibyteSupport) {
Expand All @@ -308,6 +434,12 @@ public function strlen($sString)
}
}

/**
* @param int $iStart
* @param int $iLength
*
* @return string
*/
private function substr($iStart, $iLength)
{
if ($iLength < 0) {
Expand All @@ -325,6 +457,11 @@ private function substr($iStart, $iLength)
return $sResult;
}

/**
* @param string $sString
*
* @return string
*/
private function strtolower($sString)
{
if ($this->oParserSettings->bMultibyteSupport) {
Expand All @@ -334,6 +471,11 @@ private function strtolower($sString)
}
}

/**
* @param string $sString
*
* @return array<int, string>
*/
private function strsplit($sString)
{
if ($this->oParserSettings->bMultibyteSupport) {
Expand All @@ -356,6 +498,13 @@ private function strsplit($sString)
}
}

/**
* @param string $sString
* @param string $sNeedle
* @param int $iOffset
*
* @return int|false
*/
private function strpos($sString, $sNeedle, $iOffset)
{
if ($this->oParserSettings->bMultibyteSupport) {
Expand Down