Skip to content

Commit

Permalink
added 'end' token
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 28, 2024
1 parent f63c5d3 commit 26a9691
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/Neon/Lexer.php
Expand Up @@ -65,10 +65,12 @@ public function tokenize(string $input): TokenStream
$offset += strlen($match[0]);
}

$tokens[] = new Token(Token::End, '');

$stream = new TokenStream($tokens);
if ($offset !== strlen($input)) {
$s = str_replace("\n", '\n', substr($input, $offset, 40));
$stream->error("Unexpected '$s'", count($tokens));
$stream->error("Unexpected '$s'", count($tokens) - 1);
}

return $stream;
Expand Down
10 changes: 5 additions & 5 deletions src/Neon/Parser.php
Expand Up @@ -28,7 +28,7 @@ public function parse(TokenStream $stream): Node
$node = $this->parseBlock($this->stream->getIndentation());

while ($this->stream->tryConsume(Token::Newline));
if ($this->stream->is()) {
if (!$this->stream->is(Token::End)) {
$this->stream->error();
}

Expand All @@ -47,7 +47,7 @@ private function parseBlock(string $indent, bool $onlyBullets = false): Node
$this->injectPos($item);
if ($this->stream->tryConsume('-')) {
// continue
} elseif (!$this->stream->is() || $onlyBullets) {
} elseif ($this->stream->is(Token::End) || $onlyBullets) {
return $res->items
? $res
: $this->injectPos(new Node\LiteralNode(null));
Expand Down Expand Up @@ -94,9 +94,9 @@ private function parseBlock(string $indent, bool $onlyBullets = false): Node
$this->stream->seek($save);
$item->value = $this->parseBlock($indent . ' ');
}
} elseif ($this->stream->is()) {
} elseif (!$this->stream->is(Token::End)) {
$item->value = $this->parseValue();
if ($this->stream->is() && !$this->stream->is(Token::Newline)) {
if (!$this->stream->is(Token::End, Token::Newline)) {
$this->stream->error();
}
}
Expand All @@ -109,7 +109,7 @@ private function parseBlock(string $indent, bool $onlyBullets = false): Node
$this->injectPos($item, $item->startTokenPos, $item->value->endTokenPos);

while ($this->stream->tryConsume(Token::Newline));
if (!$this->stream->is()) {
if ($this->stream->is(Token::End)) {
return $res;
}

Expand Down
1 change: 1 addition & 0 deletions src/Neon/Token.php
Expand Up @@ -19,6 +19,7 @@ final class Token
public const Comment = 3;
public const Newline = 4;
public const Whitespace = 5;
public const End = -1;


public function __construct(
Expand Down
10 changes: 5 additions & 5 deletions src/Neon/TokenStream.php
Expand Up @@ -37,13 +37,13 @@ public function seek(int $index): void

public function is(int|string ...$types): bool
{
while (in_array($this->tokens[$this->index]->type ?? null, [Token::Comment, Token::Whitespace], strict: true)) {
while (in_array($this->tokens[$this->index]->type, [Token::Comment, Token::Whitespace], strict: true)) {
$this->index++;
}

return $types
? in_array($this->tokens[$this->index]->type ?? null, $types, strict: true)
: isset($this->tokens[$this->index]);
? in_array($this->tokens[$this->index]->type, $types, strict: true)
: $this->tokens[$this->index]->type !== Token::End;
}


Expand Down Expand Up @@ -79,8 +79,8 @@ public function error(?string $message = null, ?int $pos = null): void

$line = substr_count($input, "\n") + 1;
$col = strlen($input) - strrpos("\n" . $input, "\n") + 1;
$token = $this->tokens[$pos] ?? null;
$message ??= 'Unexpected ' . ($token === null
$token = $this->tokens[$pos];
$message ??= 'Unexpected ' . ($token->type === Token::End
? 'end'
: "'" . str_replace("\n", '<new line>', substr($this->tokens[$pos]->text, 0, 40)) . "'");
throw new Exception("$message on line $line at column $col");
Expand Down

0 comments on commit 26a9691

Please sign in to comment.