Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: parse as AST #6788

Open
wants to merge 66 commits into
base: v5/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
7f06866
Add new query parsers / compilers / runners
rasteiner Nov 12, 2024
86ab9f8
Fix missing imports of Exception
rasteiner Nov 12, 2024
d0dca25
query parser fixes
rasteiner Nov 12, 2024
bb26050
Implement `intercept`, rename AST classes with a "Node" suffix
rasteiner Nov 12, 2024
8292a57
fix type annotations for arrays in CodeGen and Interpreter classes
rasteiner Nov 12, 2024
130e731
rename parameter in run method from bindings to context to be fully c…
rasteiner Nov 12, 2024
3eeed06
don't use option helper
rasteiner Nov 13, 2024
8f788ae
Update token type constants to include "T_" prefix for safety
rasteiner Nov 13, 2024
d38f916
allow identifiers to start with a number and contain (escaped) dots
rasteiner Nov 13, 2024
8a4cdc2
coding style
rasteiner Nov 13, 2024
e0df40d
Remove unused imports in Query.php
rasteiner Nov 13, 2024
37a4917
First set of CS fixes
distantnative Nov 13, 2024
125b5ba
set "interpreted" as default query runner
rasteiner Nov 13, 2024
59cf417
Add new query parsers / compilers / runners
rasteiner Nov 12, 2024
89d8505
Fix missing imports of Exception
rasteiner Nov 12, 2024
8066379
query parser fixes
rasteiner Nov 12, 2024
4082fe6
Implement `intercept`, rename AST classes with a "Node" suffix
rasteiner Nov 12, 2024
98ba957
fix type annotations for arrays in CodeGen and Interpreter classes
rasteiner Nov 12, 2024
870bfc9
rename parameter in run method from bindings to context to be fully c…
rasteiner Nov 12, 2024
02b1aef
don't use option helper
rasteiner Nov 13, 2024
54b8153
Update token type constants to include "T_" prefix for safety
rasteiner Nov 13, 2024
dcb432d
allow identifiers to start with a number and contain (escaped) dots
rasteiner Nov 13, 2024
1557644
coding style
rasteiner Nov 13, 2024
a68b9d8
Remove unused imports in Query.php
rasteiner Nov 13, 2024
ba85197
First set of CS fixes
distantnative Nov 13, 2024
748c2f0
set "interpreted" as default query runner
rasteiner Nov 13, 2024
a3400a1
fix query: sequential member access was not resetting method arguments
rasteiner Dec 3, 2024
97ea9d2
code style
rasteiner Dec 3, 2024
730033d
only intercept accessed objects
rasteiner Dec 4, 2024
92951c5
query fixes, more tests, inject cache as dependency
rasteiner Dec 4, 2024
aa434e4
Move new Query classes to `Kirby\Query`
distantnative Jan 18, 2025
0fe18c8
Refactor Tokenizer and add tests
distantnative Jan 18, 2025
80639a7
Tokenizer: float support + more tests
distantnative Jan 18, 2025
e529171
Unit tests for Token class
distantnative Jan 18, 2025
c0f28f1
Query: move classes around
distantnative Jan 18, 2025
50853d0
Tokenizer: rename $source to $query
distantnative Jan 18, 2025
560737f
Refactor visit/access/resolve of nodes
distantnative Jan 18, 2025
1ac21e5
Clean up Visitor classes
distantnative Jan 18, 2025
acd674d
Parser: restructure + unit tests
distantnative Jan 18, 2025
08a6057
Runner classes: some tidying up
distantnative Jan 18, 2025
1147d03
AST classes: unit tests
distantnative Jan 18, 2025
6631e71
Rename CodeGen to Transpiler
distantnative Jan 19, 2025
1d76a33
Runners: move Query related code to `::for($query)`
distantnative Jan 19, 2025
37ae2e0
Visitor: Move passing interceptor to constructor
distantnative Jan 19, 2025
4bd4ad1
Transpiled: extract string parts into own methods
distantnative Jan 19, 2025
7f39eef
Move more logic into Runtime class
distantnative Jan 19, 2025
dff6d6c
cs fixes
distantnative Jan 19, 2025
644d614
Tweak namespace to `Kirby\Query\Parser`
distantnative Jan 19, 2025
4d8b42f
Parse: split into smaller methods + unit tests
distantnative Jan 19, 2025
a02e16d
DRY `Interpreter::variable()`
distantnative Jan 19, 2025
aacd01a
Unit tests for visitor classes
distantnative Jan 19, 2025
6c608aa
Query: more and more unit tests
distantnative Jan 19, 2025
b959ef2
Visitor: no abstract methods -> strict type hints
distantnative Jan 19, 2025
24bef34
Queries: subscript member access
distantnative Jan 19, 2025
7396104
Simplify subscript key parsing
distantnative Jan 20, 2025
3f6b4ff
Clean up `StrTest::testTemplate()` with provider
distantnative Jan 24, 2025
3923d05
Add back support for direct data entry matches
distantnative Jan 24, 2025
52827ce
Query package: complete author information
distantnative Jan 24, 2025
f562faa
Leave legacy query as default runner for now
distantnative Jan 24, 2025
0b34f6d
Fix cs
distantnative Jan 24, 2025
7b57a3f
Fix StrTest
distantnative Jan 24, 2025
c4c0188
Add default functions test also for interpreted
distantnative Jan 24, 2025
1cb8fd4
resolve_legacy > resolveLegacy
rasteiner Feb 3, 2025
ecc2678
Move to 5.1 and update unit tests
distantnative Mar 21, 2025
2824df7
Fix cs
distantnative Mar 21, 2025
5c1d634
Fix deprecated tag
distantnative Mar 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Query/AST/ArgumentListNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* Represents a list of (method) arguments
*
* @package Kirby Query
* @author Roman Steiner <[email protected]>,
* Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*/
class ArgumentListNode extends Node
{
public function __construct(
public array $arguments = []
) {
}

public function resolve(Visitor $visitor): array|string
{
// Resolve each argument
$arguments = array_map(
fn ($argument) => $argument->resolve($visitor),
$this->arguments
);

// Keep as array or convert to string
// depending on the visitor type
return $visitor->argumentList($arguments);
}
}
34 changes: 34 additions & 0 deletions src/Query/AST/ArrayListNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* @package Kirby Query
* @author Roman Steiner <[email protected]>,
* Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*/
class ArrayListNode extends Node
{
public function __construct(
public array $elements,
) {
}

public function resolve(Visitor $visitor): array|string
{
// Resolve each array element
$elements = array_map(
fn ($element) => $element->resolve($visitor),
$this->elements
);

// Keep as array or convert to string
// depending on the visitor type
return $visitor->arrayList($elements);
}
}
30 changes: 30 additions & 0 deletions src/Query/AST/ClosureNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* @package Kirby Query
* @author Roman Steiner <[email protected]>,
* Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*/
class ClosureNode extends Node
{
/**
* @param string[] $arguments The arguments names
*/
public function __construct(
public array $arguments,
public Node $body,
) {
}

public function resolve(Visitor $visitor): mixed
{
return $visitor->closure($this);
}
}
30 changes: 30 additions & 0 deletions src/Query/AST/CoalesceNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* @package Kirby Query
* @author Roman Steiner <[email protected]>,
* Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*/
class CoalesceNode extends Node
{
public function __construct(
public Node $left,
public Node $right,
) {
}

public function resolve(Visitor $visitor): mixed
{
return $visitor->coalescence(
left: $this->left->resolve($visitor),
right: $this->right->resolve($visitor)
);
}
}
30 changes: 30 additions & 0 deletions src/Query/AST/GlobalFunctionNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* @package Kirby Query
* @author Roman Steiner <[email protected]>,
* Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*/
class GlobalFunctionNode extends Node
{
public function __construct(
public string $name,
public ArgumentListNode $arguments,
) {
}

public function resolve(Visitor $visitor): mixed
{
return $visitor->function(
name: $this->name,
arguments: $this->arguments->resolve($visitor)
);
}
}
28 changes: 28 additions & 0 deletions src/Query/AST/LiteralNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* Represents literal values (e.g. string, int, bool)
*
* @package Kirby Query
* @author Roman Steiner <[email protected]>,
* Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*/
class LiteralNode extends Node
{
public function __construct(
public mixed $value,
) {
}

public function resolve(Visitor $visitor): mixed
{
return $visitor->literal($this->value);
}
}
36 changes: 36 additions & 0 deletions src/Query/AST/MemberAccessNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* Represents the access (e.g. method call) on a node
*
* @package Kirby Query
* @author Roman Steiner <[email protected]>,
* Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*/
class MemberAccessNode extends Node
{
public function __construct(
public Node $object,
public Node $member,
public ArgumentListNode|null $arguments = null,
public bool $nullSafe = false,
) {
}

public function resolve(Visitor $visitor): mixed
{
return $visitor->memberAccess(
object: $this->object->resolve($visitor),
member: $this->member->resolve($visitor),
arguments: $this->arguments?->resolve($visitor),
nullSafe: $this->nullSafe
);
}
}
22 changes: 22 additions & 0 deletions src/Query/AST/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* Basic query node representation
*
* @package Kirby Query
* @author Roman Steiner <[email protected]>,
* Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*
* @codeCoverageIgnore
*/
abstract class Node
{
abstract public function resolve(Visitor $visitor);
}
35 changes: 35 additions & 0 deletions src/Query/AST/TernaryNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* Represents a ternary condition
* with a value for when the condition is true
* and another value for when the condition is false
*
* @package Kirby Query
* @author Roman Steiner <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*/
class TernaryNode extends Node
{
public function __construct(
public Node $condition,
public Node $false,
public Node|null $true = null
) {
}

public function resolve(Visitor $visitor): mixed
{
return $visitor->ternary(
condition: $this->condition->resolve($visitor),
true: $this->true?->resolve($visitor),
false: $this->false->resolve($visitor)
);
}
}
28 changes: 28 additions & 0 deletions src/Query/AST/VariableNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Kirby\Query\AST;

use Kirby\Query\Visitors\Visitor;

/**
* Represents a variable (e.g. an object)
*
* @package Kirby Query
* @author Roman Steiner <[email protected]>,
* Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @license https://opensource.org/licenses/MIT
* @since 5.1.0
*/
class VariableNode extends Node
{
public function __construct(
public string $name,
) {
}

public function resolve(Visitor $visitor): mixed
{
return $visitor->variable($this->name);
}
}
2 changes: 2 additions & 0 deletions src/Query/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*
* @todo Deprecate in v6
*/
class Argument
{
Expand Down
2 changes: 2 additions & 0 deletions src/Query/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*
* @todo Deprecate in v6
*
* @extends \Kirby\Toolkit\Collection<\Kirby\Query\Argument>
*/
class Arguments extends Collection
Expand Down
2 changes: 2 additions & 0 deletions src/Query/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*
* @todo Deprecate in v6
*/
class Expression
{
Expand Down
Loading
Loading