Skip to content

Commit

Permalink
coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
rasteiner committed Nov 13, 2024
1 parent f46ab0a commit c68b85f
Show file tree
Hide file tree
Showing 23 changed files with 221 additions and 144 deletions.
6 changes: 4 additions & 2 deletions src/Toolkit/Query/AST/ArgumentListNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Kirby\Toolkit\Query\AST;

class ArgumentListNode extends Node {
class ArgumentListNode extends Node
{
public function __construct(
public array $arguments,
) {}
) {
}
}
6 changes: 4 additions & 2 deletions src/Toolkit/Query/AST/ArrayListNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Kirby\Toolkit\Query\AST;

class ArrayListNode extends Node {
class ArrayListNode extends Node
{
public function __construct(
public array $elements,
) {}
) {
}
}
6 changes: 4 additions & 2 deletions src/Toolkit/Query/AST/CoalesceNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Kirby\Toolkit\Query\AST;

class CoalesceNode extends Node {
class CoalesceNode extends Node
{
public function __construct(
public Node $left,
public Node $right,
) {}
) {
}
}
9 changes: 6 additions & 3 deletions src/Toolkit/Query/AST/GlobalFunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

namespace Kirby\Toolkit\Query\AST;

class GlobalFunctionNode extends IdentifierNode {
class GlobalFunctionNode extends IdentifierNode
{
public function __construct(
public string $name,
public ArgumentListNode $arguments,
) {}
) {
}

/**
* Replace escaped dots with real dots
*/
public function name(): string {
public function name(): string
{
return str_replace('\.', '.', $this->name);
}
}
6 changes: 4 additions & 2 deletions src/Toolkit/Query/AST/IdentifierNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Kirby\Toolkit\Query\AST;

abstract class IdentifierNode extends Node {
abstract class IdentifierNode extends Node
{
/**
* Replaces the escaped identifier with the actual identifier
*/
static public function unescape(string $name): string {
public static function unescape(string $name): string
{
return stripslashes($name);
}
}
6 changes: 4 additions & 2 deletions src/Toolkit/Query/AST/LiteralNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Kirby\Toolkit\Query\AST;

class LiteralNode extends Node {
class LiteralNode extends Node
{
public function __construct(
public mixed $value,
) {}
) {
}
}
15 changes: 9 additions & 6 deletions src/Toolkit/Query/AST/MemberAccessNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

namespace Kirby\Toolkit\Query\AST;

class MemberAccessNode extends IdentifierNode {
class MemberAccessNode extends IdentifierNode
{
public function __construct(
public Node $object,
public string|int $member,
public ?ArgumentListNode $arguments = null,
public ArgumentListNode|null $arguments = null,
public bool $nullSafe = false,
) {}
) {
}

/**
* Returns the member name and replaces escaped dots with real dots if it's a string
*/
public function member(): string|int {
public function member(): string|int
{
if (is_string($this->member)) {
return self::unescape($this->member);
} else {
return $this->member;
}
return $this->member;

}
}
6 changes: 4 additions & 2 deletions src/Toolkit/Query/AST/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Kirby\Toolkit\Query\Visitor;

class Node {
public function accept(Visitor $visitor) {
class Node
{
public function accept(Visitor $visitor)
{
return $visitor->visitNode($this);
}
}
9 changes: 5 additions & 4 deletions src/Toolkit/Query/AST/TernaryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Kirby\Toolkit\Query\AST;

class TernaryNode extends Node {
class TernaryNode extends Node
{
public function __construct(
public Node $condition,
public ?Node $trueBranch,
public Node|null $trueBranch,
public Node $falseBranch,

public bool $trueBranchIsDefault = false,
) {}
) {
}
}
9 changes: 6 additions & 3 deletions src/Toolkit/Query/AST/VariableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

namespace Kirby\Toolkit\Query\AST;

class VariableNode extends IdentifierNode {
class VariableNode extends IdentifierNode
{
public function __construct(
public string $name,
) {}
) {
}

/**
* Replaces escaped dots with real dots
*/
public function name(): string {
public function name(): string
{
return self::unescape($this->name);
}
}
23 changes: 15 additions & 8 deletions src/Toolkit/Query/BaseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Exception;
use Iterator;

abstract class BaseParser {
protected ?Token $previous;
abstract class BaseParser
{
protected Token|null $previous;
protected Token $current;

/**
Expand All @@ -33,23 +34,26 @@ public function __construct(
$this->current = $first;
}

protected function consume(TokenType $type, string $message): Token {
protected function consume(TokenType $type, string $message): Token
{
if ($this->check($type)) {
return $this->advance();
}

throw new Exception($message);
}

protected function check(TokenType $type): bool {
protected function check(TokenType $type): bool
{
if ($this->isAtEnd()) {
return false;
}

return $this->current->type === $type;
}

protected function advance(): ?Token {
protected function advance(): Token|null
{
if (!$this->isAtEnd()) {
$this->previous = $this->current;
$this->tokens->next();
Expand All @@ -59,20 +63,23 @@ protected function advance(): ?Token {
return $this->previous;
}

protected function isAtEnd(): bool {
protected function isAtEnd(): bool
{
return $this->current->type === TokenType::T_EOF;
}


protected function match(TokenType $type): Token|false {
protected function match(TokenType $type): Token|false
{
if ($this->check($type)) {
return $this->advance();
}

return false;
}

protected function matchAny(array $types): Token|false {
protected function matchAny(array $types): Token|false
{
foreach ($types as $type) {
if ($this->check($type)) {
return $this->advance();
Expand Down
50 changes: 30 additions & 20 deletions src/Toolkit/Query/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,33 @@
use Kirby\Toolkit\Query\AST\TernaryNode;
use Kirby\Toolkit\Query\AST\VariableNode;

class Parser extends BaseParser {
class Parser extends BaseParser
{
public function __construct(
Tokenizer|Iterator $source,
) {
parent::__construct($source);
}

public function parse(): Node {
public function parse(): Node
{
$expression = $this->expression();

// ensure that we consumed all tokens
if(!$this->isAtEnd())
if(!$this->isAtEnd()) {
$this->consume(TokenType::T_EOF, 'Expect end of expression.');
}

return $expression;
}

private function expression(): Node {
private function expression(): Node
{
return $this->coalesce();
}

private function coalesce(): Node {
private function coalesce(): Node
{
$left = $this->ternary();

while ($this->match(TokenType::T_COALESCE)) {
Expand All @@ -47,7 +52,8 @@ private function coalesce(): Node {
return $left;
}

private function ternary(): Node {
private function ternary(): Node
{
$left = $this->memberAccess();

if ($tok = $this->matchAny([TokenType::T_QUESTION_MARK, TokenType::T_TERNARY_DEFAULT])) {
Expand All @@ -68,15 +74,16 @@ private function ternary(): Node {
return $left;
}

private function memberAccess(): Node {
private function memberAccess(): Node
{
$left = $this->atomic();

while ($tok = $this->matchAny([TokenType::T_DOT, TokenType::T_NULLSAFE])) {
$nullSafe = $tok->type === TokenType::T_NULLSAFE;

if($right = $this->match(TokenType::T_IDENTIFIER)) {
$right = $right->lexeme;
} else if($right = $this->match(TokenType::T_INTEGER)) {
} elseif($right = $this->match(TokenType::T_INTEGER)) {
$right = $right->literal;
} else {
throw new Exception('Expect property name after ".".');
Expand All @@ -93,7 +100,8 @@ private function memberAccess(): Node {
return $left;
}

private function listUntil(TokenType $until): array {
private function listUntil(TokenType $until): array
{
$elements = [];

while (!$this->isAtEnd() && !$this->check($until)) {
Expand All @@ -110,18 +118,20 @@ private function listUntil(TokenType $until): array {
return $elements;
}

private function argumentList(): Node {
private function argumentList(): Node
{
$list = $this->listUntil(TokenType::T_CLOSE_PAREN);

return new ArgumentListNode($list);
}

private function atomic(): Node {
private function atomic(): Node
{
// float numbers
if ($integer = $this->match(TokenType::T_INTEGER)) {
if($this->match(TokenType::T_DOT)) {
$fractional = $this->match(TokenType::T_INTEGER);
return new LiteralNode(floatval($integer->literal . '.' . $fractional->literal));
return new LiteralNode((float)($integer->literal . '.' . $fractional->literal));
}
return new LiteralNode($integer->literal);
}
Expand Down Expand Up @@ -170,16 +180,16 @@ private function atomic(): Node {
}
}

$arguments = array_map(fn($element) => $element->name, $list);
$arguments = array_map(fn ($element) => $element->name, $list);
return new ClosureNode($arguments, $expression);
} else {
if(count($list) > 1) {
throw new Exception('Expecting \"=>\" after closure argument list.');
} else {
// this is just a grouping
return $list[0];
}
}
if(count($list) > 1) {
throw new Exception('Expecting \"=>\" after closure argument list.');
}
// this is just a grouping
return $list[0];


}

throw new Exception('Expect expression.');
Expand Down
6 changes: 4 additions & 2 deletions src/Toolkit/Query/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Closure;
use Exception;

abstract class Runner {
abstract class Runner
{
/**
* Runner constructor.
*
Expand All @@ -14,7 +15,8 @@ abstract class Runner {
public function __construct(
public array $allowedFunctions = [],
protected Closure|null $interceptor = null,
) {}
) {
}

/**
* Executes a query within a given data context.
Expand Down
Loading

0 comments on commit c68b85f

Please sign in to comment.