summaryrefslogtreecommitdiff
path: root/src/Parser/Parser.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Parser/Parser.php')
-rw-r--r--src/Parser/Parser.php63
1 files changed, 52 insertions, 11 deletions
diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php
index 05d3726..1ce092c 100644
--- a/src/Parser/Parser.php
+++ b/src/Parser/Parser.php
@@ -44,8 +44,11 @@ class Parser
$this->advance(1);
}
+ else if ($currentToken->type == TokenType::Identifier) {
+ $currentStatement = $this->parseFunctionCall();
+ }
+
else {
- #$currentStatement = $this->parseFunctionCall();
$error = sprintf("Unexpected %s at %d:%d" . PHP_EOL, $currentToken->value, $currentToken->line, $currentToken->column);
$this->addError($error);
@@ -182,7 +185,7 @@ class Parser
// skip const
$this->anticipateTokenAndSkip("const");
- $identifier = $this->getCurrentToken();
+ $identifier = new IdentifierNode($this->getCurrentToken());
$this->advance(1);
// skip :
@@ -324,6 +327,10 @@ class Parser
return new Condition($currentExpression, $nextToken, $this->parseExpression(shouldBeMap: $shouldBeMap));
}
+ else if ($nextToken->literal == "[") {
+ return $this->parseArrayOrMapAccess($currentExpression);
+ }
+
else if ($nextToken->literal == "=>") {
$this->advance(1);
@@ -419,6 +426,28 @@ class Parser
}
}
+ private function parseArrayOrMapAccess(Node $arrayOrMap): Node
+ {
+ $items = [];
+
+ while ($this->getCurrentToken()->literal == "[") {
+ // skip current [
+ $this->anticipateTokenAndSkip("[");
+
+ $item = $this->parseExpression();
+
+ // skip last ]
+ $this->anticipateTokenAndSkip("]");
+
+ $items[] = $item;
+ }
+
+ return new ArrayMapAccessNode(
+ $arrayOrMap,
+ $items,
+ );
+ }
+
private function parseNumber(): Node
{
$currentToken = $this->getCurrentToken();
@@ -470,7 +499,7 @@ class Parser
continue;
}
- $identifier = $this->getCurrentToken();
+ $identifier = new IdentifierNode($this->getCurrentToken());
$this->advance(1);
// skip :
@@ -558,7 +587,7 @@ class Parser
private function parseFunctionCall(): Node
{
- $identifier = $this->getCurrentToken();
+ $identifier = new IdentifierNode($this->getCurrentToken());
$this->advance(1);
// skip first (
@@ -590,7 +619,7 @@ class Parser
// if "=" then identifier is name
if ($this->getNextToken()->literal == "=") {
- $identifier = $this->getCurrentToken();
+ $identifier = new IdentifierNode($this->getCurrentToken());
$this->advance(2);
$value = $this->parseExpression();
@@ -696,7 +725,7 @@ class Tree extends Node
class ConstVariableDeclaration extends Node
{
public function __construct(
- public Token $identifier,
+ public Node $identifier,
public Node|Token $type,
public Node|Token $expression,
) {}
@@ -744,6 +773,7 @@ class MapNode extends Node
public array $values,
) {}
}
+
class MapItemNode extends Node
{
public function __construct(
@@ -752,6 +782,17 @@ class MapItemNode extends Node
) {}
}
+class ArrayMapAccessNode extends Node
+{
+ public function __construct(
+ public ArrayNode|IdentifierNode $arrayOrMap,
+ /**
+ * @param Node[] $items
+ */
+ public array $items,
+ ) {}
+}
+
class OperatorExpression extends Node
{
public function __construct(
@@ -822,7 +863,7 @@ class FunctionDefinition extends Node
* @param FunctionDefinitionParameter[] $parameters
*/
public array $parameters,
- public TypeDeclaration $returnType,
+ public TypeDeclaration|ArrayTypeDeclaration|MapTypeDeclaration $returnType,
/**
* @param Node[] $body
*/
@@ -833,8 +874,8 @@ class FunctionDefinition extends Node
class FunctionDefinitionParameter extends Node
{
public function __construct(
- public Token $identifier,
- public TypeDeclaration $type,
+ public Node $identifier,
+ public TypeDeclaration|ArrayTypeDeclaration|MapTypeDeclaration $type,
public ?Node $defaultValue = null,
) {}
}
@@ -849,7 +890,7 @@ class FunctionReturn extends Node
class FunctionCall extends Node
{
public function __construct(
- public Token $identifier,
+ public Node $identifier,
/**
* @param FunctionCallParameter[] $parameters
*/
@@ -860,7 +901,7 @@ class FunctionCall extends Node
class FunctionCallParameter extends Node
{
public function __construct(
- public ?Token $identifier,
+ public ?Node $identifier,
public Node|Token $value,
) {}
}