Skip to content

Commit

Permalink
Token: renamed props
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 27, 2023
1 parent 354547d commit ca19eec
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ private function parseValue(): Node
{
if ($token = $this->tokens->consume(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->getPosition() - 1);
} catch (Exception $e) {
$this->tokens->error($e->getMessage(), $this->tokens->getPosition() - 1);
}
} elseif ($token = $this->tokens->consume(Token::Literal)) {
$pos = $this->tokens->getPosition() - 1;
$node = new Node\LiteralNode(Node\LiteralNode::parse($token->value, $this->tokens->isNext(':', '=')));
$node = new Node\LiteralNode(Node\LiteralNode::parse($token->text, $this->tokens->isNext(':', '=')));
$this->injectPos($node, $pos);

} elseif ($this->tokens->isNext('[', '(', '{')) {
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->consume(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->getPosition() - 1);
if ($this->tokens->isNext('(')) {
$attributes = $this->parseBraces();
Expand All @@ -183,8 +183,8 @@ private function parseEntity(Node $node): Node
private function parseBraces(): Node\InlineArrayNode
{
$token = $this->tokens->consume();
$endBrace = ['[' => ']', '{' => '}', '(' => ')'][$token->value];
$res = new Node\InlineArrayNode($token->value);
$endBrace = ['[' => ']', '{' => '}', '(' => ')'][$token->text];
$res = new Node\InlineArrayNode($token->text);
$this->injectPos($res, $this->tokens->getPosition() - 1);
$keyCheck = [];

Expand Down Expand Up @@ -256,7 +256,7 @@ private function initLines(): void
$line = 1;
foreach ($this->tokens->getTokens() 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getIndentation(): string
{
return in_array($this->tokens[$this->pos - 2]->type ?? null, [Token::Newline, null], strict: true)
&& ($this->tokens[$this->pos - 1]->type ?? null) === Token::Whitespace
? $this->tokens[$this->pos - 1]->value
? $this->tokens[$this->pos - 1]->text
: '';
}

Expand All @@ -81,15 +81,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, column $col.");
}
}
2 changes: 1 addition & 1 deletion tests/Neon/Parser.nodes.phpt
Original file line number Diff line number Diff line change
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->getTokens(), $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 ca19eec

Please sign in to comment.