Skip to content

Commit

Permalink
Token: renamed props
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 28, 2024
1 parent e70439a commit 09afb6e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Neon/Lexer.php
Expand Up @@ -61,7 +61,7 @@ public function tokenize(string $input): TokenStream
$tokens = [];
foreach ($matches as $match) {
$type = $types[count($match) - 2];
$tokens[] = new Token($match[0], $type === Token::Char ? $match[0] : $type);
$tokens[] = new Token($type === Token::Char ? $match[0] : $type, $match[0]);
$offset += strlen($match[0]);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Neon/Parser.php
Expand Up @@ -132,14 +132,14 @@ private function parseValue(): Node
{
if ($token = $this->tokens->tryConsume(Token::String)) {
try {
$node = new Node\StringNode(Node\StringNode::parse($token->value));
$node = new Node\StringNode(Node\StringNode::parse($token->text));
$this->injectPos($node, $this->tokens->getIndex() - 1);
} catch (Exception $e) {
$this->tokens->error($e->getMessage(), $this->tokens->getIndex() - 1);
}
} elseif ($token = $this->tokens->tryConsume(Token::Literal)) {
$pos = $this->tokens->getIndex() - 1;
$node = new Node\LiteralNode(Node\LiteralNode::parse($token->value, $this->tokens->is(':', '=')));
$node = new Node\LiteralNode(Node\LiteralNode::parse($token->text, $this->tokens->is(':', '=')));
$this->injectPos($node, $pos);

} elseif ($this->tokens->is('[', '(', '{')) {
Expand All @@ -163,7 +163,7 @@ private function parseEntity(Node $node): Node
$entities[] = $this->injectPos(new Node\EntityNode($node, $attributes->items), $node->startTokenPos, $attributes->endTokenPos);

while ($token = $this->tokens->tryConsume(Token::Literal)) {
$valueNode = new Node\LiteralNode(Node\LiteralNode::parse($token->value));
$valueNode = new Node\LiteralNode(Node\LiteralNode::parse($token->text));
$this->injectPos($valueNode, $this->tokens->getIndex() - 1);
if ($this->tokens->is('(')) {
$attributes = $this->parseBraces();
Expand All @@ -183,8 +183,8 @@ private function parseEntity(Node $node): Node
private function parseBraces(): Node\InlineArrayNode
{
$token = $this->tokens->tryConsume();
$endBrace = ['[' => ']', '{' => '}', '(' => ')'][$token->value];
$res = new Node\InlineArrayNode($token->value);
$endBrace = ['[' => ']', '{' => '}', '(' => ')'][$token->text];
$res = new Node\InlineArrayNode($token->text);
$this->injectPos($res, $this->tokens->getIndex() - 1);
$keyCheck = [];

Expand Down Expand Up @@ -256,7 +256,7 @@ private function initLines(): void
$line = 1;
foreach ($this->tokens->tokens as $token) {
$this->posToLine[] = $line;
$line += substr_count($token->value, "\n");
$line += substr_count($token->text, "\n");
}

$this->posToLine[] = $line;
Expand Down
2 changes: 1 addition & 1 deletion src/Neon/Token.php
Expand Up @@ -22,8 +22,8 @@ final class Token


public function __construct(
public string $value,
public int|string $type,
public string $text,
) {
}
}
6 changes: 3 additions & 3 deletions src/Neon/TokenStream.php
Expand Up @@ -59,7 +59,7 @@ public function getIndentation(): string
{
return in_array($this->tokens[$this->index - 2]->type ?? null, [Token::Newline, null], strict: true)
&& ($this->tokens[$this->index - 1]->type ?? null) === Token::Whitespace
? $this->tokens[$this->index - 1]->value
? $this->tokens[$this->index - 1]->text
: '';
}

Expand All @@ -74,15 +74,15 @@ public function error(?string $message = null, ?int $pos = null): void
break;
}

$input .= $token->value;
$input .= $token->text;
}

$line = substr_count($input, "\n") + 1;
$col = strlen($input) - strrpos("\n" . $input, "\n") + 1;
$token = $this->tokens[$pos] ?? null;
$message ??= 'Unexpected ' . ($token === null
? 'end'
: "'" . str_replace("\n", '<new line>', substr($this->tokens[$pos]->value, 0, 40)) . "'");
: "'" . str_replace("\n", '<new line>', substr($this->tokens[$pos]->text, 0, 40)) . "'");
throw new Exception("$message on line $line at column $col");
}
}
2 changes: 1 addition & 1 deletion tests/Neon/Parser.nodes.phpt
Expand Up @@ -58,7 +58,7 @@ $traverser = new Traverser;
$traverser->traverse($node, function (Node $node) use ($stream) {
@$node->code = ''; // dynamic property is deprecated
foreach (array_slice($stream->tokens, $node->startTokenPos, $node->endTokenPos - $node->startTokenPos + 1) as $token) {
$node->code .= $token->value;
$node->code .= $token->text;
}

unset($node->startTokenPos, $node->endTokenPos);
Expand Down

0 comments on commit 09afb6e

Please sign in to comment.