You've already forked MyDSL
优化下一行移动逻辑
This commit is contained in:
@ -19,9 +19,9 @@ abstract class ReaderInterface
|
||||
{
|
||||
protected $currentLine = 1;
|
||||
protected $currentPosition = 0;
|
||||
protected $currentLinePosition = 0;
|
||||
protected $nextPosition = 0;
|
||||
protected $currentToken = "";
|
||||
protected $currentLineDelta = 0;
|
||||
|
||||
/**
|
||||
* 获取下一个字符
|
||||
@ -73,6 +73,7 @@ abstract class ReaderInterface
|
||||
{
|
||||
$this->currentLine = 1;
|
||||
$this->currentPosition = 0;
|
||||
$this->currentLineDelta = 0;
|
||||
$this->nextPosition = 0;
|
||||
$this->moveToNextToken();
|
||||
}
|
||||
@ -104,7 +105,7 @@ abstract class ReaderInterface
|
||||
|
||||
public function getCurrentLinePosition(): int
|
||||
{
|
||||
return $this->currentLinePosition;
|
||||
return $this->currentPosition - $this->currentLineDelta;
|
||||
}
|
||||
|
||||
public function getCurrentToken(): string
|
||||
@ -115,13 +116,12 @@ abstract class ReaderInterface
|
||||
protected function moveCursorToNextChar(): void
|
||||
{
|
||||
$this->currentPosition++;
|
||||
$this->currentLinePosition++;
|
||||
}
|
||||
|
||||
protected function moveCursorToNextLine(int $chars = 1): void
|
||||
{
|
||||
$this->currentPosition += $chars;
|
||||
$this->currentLinePosition = 0;
|
||||
$this->currentLineDelta = $this->currentPosition;
|
||||
$this->currentLine++;
|
||||
}
|
||||
}
|
@ -25,6 +25,7 @@ class StringReader extends ReaderInterface
|
||||
public function getNextChar(int $startAt = null): string
|
||||
{
|
||||
if ($startAt === null) $startAt = $this->currentPosition;
|
||||
if (mb_strlen($this->string) <= $startAt) return "";
|
||||
return mb_substr($this->string, $startAt, 1);
|
||||
}
|
||||
|
||||
@ -35,7 +36,7 @@ class StringReader extends ReaderInterface
|
||||
{
|
||||
$curToken = "";
|
||||
$curPos = $this->nextPosition;
|
||||
while ($curChar = $this->getNextChar($curPos)) {
|
||||
while (mb_strlen($curChar = $this->getNextChar($curPos)) > 0) {
|
||||
$curPos++;
|
||||
switch ($curChar) {
|
||||
case " ":
|
||||
@ -65,7 +66,7 @@ class StringReader extends ReaderInterface
|
||||
{
|
||||
$curToken = "";
|
||||
$this->currentPosition = $this->nextPosition;
|
||||
while ($curChar = $this->getNextChar($this->nextPosition)) {
|
||||
while (mb_strlen($curChar = $this->getNextChar($this->nextPosition)) > 0) {
|
||||
$this->nextPosition++;
|
||||
switch ($curChar) {
|
||||
// TODO: 注释跳过
|
||||
@ -78,22 +79,24 @@ class StringReader extends ReaderInterface
|
||||
// 否则就结束(已经匹配完成)
|
||||
break 2;
|
||||
case "\r":
|
||||
if ($this->getNextChar($this->nextPosition + 1) === "\n") {
|
||||
if ($this->getNextChar($this->nextPosition) === "\n") {
|
||||
// CRLF换行
|
||||
$this->moveCursorToNextChar();
|
||||
if (empty($curToken)) {
|
||||
$this->moveCursorToNextChar();
|
||||
}
|
||||
$this->nextPosition++;
|
||||
}
|
||||
// CR换行
|
||||
$this->moveCursorToNextLine();
|
||||
if (empty($curToken)) {
|
||||
$this->moveCursorToNextLine();
|
||||
continue 2;
|
||||
} else {
|
||||
break 2;
|
||||
}
|
||||
case "\n":
|
||||
// LF换行
|
||||
$this->moveCursorToNextLine();
|
||||
if (empty($curToken)) {
|
||||
$this->moveCursorToNextLine();
|
||||
continue 2;
|
||||
} else {
|
||||
break 2;
|
||||
@ -103,7 +106,7 @@ class StringReader extends ReaderInterface
|
||||
}
|
||||
}
|
||||
$this->currentToken = $curToken;
|
||||
return true;
|
||||
return $curChar !== "";
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,7 +119,7 @@ class StringReader extends ReaderInterface
|
||||
$curPos++;
|
||||
switch ($curChar) {
|
||||
case "\r":
|
||||
if ($this->getNextChar($this->nextPosition + 1) === "\n") {
|
||||
if ($this->getNextChar($curPos) === "\n") {
|
||||
// CRLF换行
|
||||
$curPos++;
|
||||
}
|
||||
@ -126,8 +129,7 @@ class StringReader extends ReaderInterface
|
||||
}
|
||||
}
|
||||
$this->nextPosition = $curPos;
|
||||
$this->currentLine++;
|
||||
$this->currentLinePosition = 0;
|
||||
$this->moveCursorToNextLine($curPos - $this->currentPosition);
|
||||
return $this->moveToNextToken();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user