You've already forked MyDSL
优化光标移动逻辑
This commit is contained in:
@ -62,7 +62,7 @@ abstract class ReaderInterface
|
||||
* @author Jerry Yan <792602257@qq.com>
|
||||
* @date 2020/12/17 15:43
|
||||
*/
|
||||
abstract public function skipUntil(string $end="*/"): bool;
|
||||
abstract public function skipUntil(string $end = "*/"): bool;
|
||||
|
||||
/**
|
||||
* 重置读取器
|
||||
@ -91,12 +91,37 @@ abstract class ReaderInterface
|
||||
{
|
||||
return $this->currentPosition;
|
||||
}
|
||||
|
||||
public function getNextPosition(): int
|
||||
{
|
||||
return $this->nextPosition;
|
||||
}
|
||||
|
||||
public function getCurrentLine(): int
|
||||
{
|
||||
return $this->currentLine;
|
||||
}
|
||||
|
||||
public function getCurrentLinePosition(): int
|
||||
{
|
||||
return $this->currentLinePosition;
|
||||
}
|
||||
|
||||
public function getCurrentToken(): string
|
||||
{
|
||||
return $this->currentToken;
|
||||
}
|
||||
|
||||
protected function moveCursorToNextChar(): void
|
||||
{
|
||||
$this->currentPosition++;
|
||||
$this->currentLinePosition++;
|
||||
}
|
||||
|
||||
protected function moveCursorToNextLine(int $chars = 1): void
|
||||
{
|
||||
$this->currentPosition += $chars;
|
||||
$this->currentLinePosition = 0;
|
||||
$this->currentLine++;
|
||||
}
|
||||
}
|
@ -47,6 +47,9 @@ class StringReader extends ReaderInterface
|
||||
break 2;
|
||||
case "\r":
|
||||
case "\n":
|
||||
if (empty($curToken)) {
|
||||
continue 2;
|
||||
}
|
||||
break 2;
|
||||
default:
|
||||
$curToken .= $curChar;
|
||||
@ -64,13 +67,12 @@ class StringReader extends ReaderInterface
|
||||
$this->currentPosition = $this->nextPosition;
|
||||
while ($curChar = $this->getNextChar($this->nextPosition)) {
|
||||
$this->nextPosition++;
|
||||
$this->currentLinePosition++;
|
||||
switch ($curChar) {
|
||||
// TODO: 注释跳过
|
||||
case " ":
|
||||
// 如果开始的时候就有空白,跳过它
|
||||
if (empty($curToken)) {
|
||||
$this->currentPosition++;
|
||||
$this->moveCursorToNextChar();
|
||||
continue 2;
|
||||
}
|
||||
// 否则就结束(已经匹配完成)
|
||||
@ -78,17 +80,24 @@ class StringReader extends ReaderInterface
|
||||
case "\r":
|
||||
if ($this->getNextChar($this->nextPosition + 1) === "\n") {
|
||||
// CRLF换行
|
||||
$this->moveCursorToNextChar();
|
||||
$this->nextPosition++;
|
||||
}
|
||||
// CR换行
|
||||
$this->currentLine++;
|
||||
$this->currentLinePosition = 0;
|
||||
break 2;
|
||||
$this->moveCursorToNextLine();
|
||||
if (empty($curToken)) {
|
||||
continue 2;
|
||||
} else {
|
||||
break 2;
|
||||
}
|
||||
case "\n":
|
||||
// LF换行
|
||||
$this->currentLine++;
|
||||
$this->currentLinePosition = 0;
|
||||
break 2;
|
||||
$this->moveCursorToNextLine();
|
||||
if (empty($curToken)) {
|
||||
continue 2;
|
||||
} else {
|
||||
break 2;
|
||||
}
|
||||
default:
|
||||
$curToken .= $curChar;
|
||||
}
|
||||
@ -116,7 +125,7 @@ class StringReader extends ReaderInterface
|
||||
break 2;
|
||||
}
|
||||
}
|
||||
$this->nextPosition = $curPos + 1;
|
||||
$this->nextPosition = $curPos;
|
||||
$this->currentLine++;
|
||||
$this->currentLinePosition = 0;
|
||||
return $this->moveToNextToken();
|
||||
|
Reference in New Issue
Block a user