Skip to content

Commit

Permalink
Fix scanning for new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Oct 25, 2024
1 parent 1600b53 commit bbfb38b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
1 change: 0 additions & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class Compiler
public const PHVOLT_MODE_RAW = 0;
public const PHVOLT_PARSING_FAILED = 0;
public const PHVOLT_PARSING_OK = 1;
public const PHVOLT_RAW_BUFFER_SIZE = 256;
public const PHVOLT_SCANNER_RETCODE_EOF = -1;
public const PHVOLT_SCANNER_RETCODE_ERR = -2;
public const PHVOLT_SCANNER_RETCODE_IMPOSSIBLE = -3;
Expand Down
53 changes: 24 additions & 29 deletions src/Scanner/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,33 @@ public function scanForToken(): int
$status = self::PHVOLT_SCANNER_RETCODE_IMPOSSIBLE;
while (self::PHVOLT_SCANNER_RETCODE_IMPOSSIBLE === $status) {
$cursor = $this->state->getStart();
if ($cursor === null) {
return self::PHVOLT_SCANNER_RETCODE_EOF;
}

$mode = $this->state->getMode();
if ($mode === Compiler::PHVOLT_MODE_RAW || $mode === Compiler::PHVOLT_MODE_COMMENT) {
$next = $this->state->getNext();
$doubleNext = $this->state->getNext(2);

if ($cursor === "\n") {
$this->state->incrementActiveLine();
}

if ($cursor === '{' && ($next === '%' || $next === '{' || $next === '#')) {
if ($cursor === null || ($cursor === '{' && ($next === '%' || $next === '{' || $next === '#'))) {
if ($next !== '#') {
$this->state->setMode(Compiler::PHVOLT_MODE_CODE);

if ($this->state->getRawBufferCursor() > 0) {
$value = substr(
$this->state->getRawBuffer(),
$this->state->getCursor() - $this->state->getRawBufferCursor(),
$this->state->getRawBufferCursor(),
);
$this
->token
->setOpcode(Compiler::PHVOLT_T_RAW_FRAGMENT)
->setValue($value)
;

if (!empty($this->state->rawFragment)) {
if ($this->state->getWhitespaceControl()) {
//ltrim(); // TODO
$this->state->rawFragment = ltrim($this->state->rawFragment);
$this->state->setWhitespaceControl(false);
}

if ($doubleNext === '-') {
// rtrim($token); // TODO
$this->state->rawFragment = rtrim($this->state->rawFragment);
}

$this->state->setRawBufferCursor(0);
$this
->token
->setOpcode(Compiler::PHVOLT_T_RAW_FRAGMENT)
->setValue($this->state->rawFragment)
;

$this->state->rawFragment = '';
} else {
$this->token->setOpcode(Compiler::PHVOLT_T_IGNORE);
}
Expand All @@ -94,18 +82,20 @@ public function scanForToken(): int
}

return 0;
} else {
$this->state->incrementRawBufferCursor();
}

if ($cursor === "\n") {
$this->state->incrementActiveLine();
}

$this->state->rawFragment .= $cursor;
$this->state->incrementStart();
} else {
$vvch = $cursor;
if ($vvch === null) {
return self::PHVOLT_SCANNER_RETCODE_EOF;
}

$start = $this->state->getCursor();
switch ($vvch) {
case null:
goto vv2;
case "\t":
case "\r":
case ' ':
Expand Down Expand Up @@ -243,6 +233,11 @@ public function scanForToken(): int
default:
$this->state->incrementStart();
}

vv2:
$status = self::PHVOLT_SCANNER_RETCODE_EOF;
break;

vv5:
$status = self::PHVOLT_SCANNER_RETCODE_ERR;
break;
Expand Down
16 changes: 1 addition & 15 deletions src/Scanner/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class State
public mixed $marker = null;
public int $oldIfLevel = 0;
public string $rawBuffer;
public string $rawFragment = '';
public int $rawBufferCursor = 0;
public int $rawBufferSize = Compiler::PHVOLT_RAW_BUFFER_SIZE;
public int $startLength;
public int $statementPosition = 0;
public int $switchLevel = 0;
Expand Down Expand Up @@ -240,20 +240,6 @@ public function setRawBuffer(string $rawBuffer): self
return $this;
}

public function setRawBufferCursor(int $rawBufferCursor): self
{
$this->rawBufferCursor = $rawBufferCursor;

return $this;
}

public function setRawBufferSize(int $rawBufferSize): self
{
$this->rawBufferSize = $rawBufferSize;

return $this;
}

public function setStart(?string $start): self
{
$this->start = $start;
Expand Down

0 comments on commit bbfb38b

Please sign in to comment.