|
1 | 1 | <?php
|
2 |
| -declare(strict_types = 1); |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace TimoLehnertz\formula\parsing;
|
4 | 6 |
|
5 | 7 | use TimoLehnertz\formula\expression\FunctionExpression;
|
|
12 | 14 |
|
13 | 15 | /**
|
14 | 16 | * @author Timo Lehnertz
|
| 17 | + * <Function value> ::= <Type>? <FunctionArguments> "->" <Expression> | ("{" <CodeBlock> "}") |
15 | 18 | */
|
16 | 19 | class FunctionParser extends Parser {
|
17 | 20 |
|
18 | 21 | private readonly bool $parseStatement;
|
19 | 22 |
|
20 | 23 | public function __construct(bool $parseStatement) {
|
21 |
| - if($parseStatement) { |
22 |
| - parent::__construct('function statement'); |
23 |
| - } else { |
24 |
| - parent::__construct('function value'); |
25 |
| - } |
| 24 | + parent::__construct($parseStatement ? 'function statement' : 'function value'); |
26 | 25 | $this->parseStatement = $parseStatement;
|
27 | 26 | }
|
28 | 27 |
|
29 | 28 | protected function parsePart(Token $firstToken): ParserReturn {
|
30 |
| - // var_dump('Moin'); |
31 |
| - if(!$this->parseStatement) { |
| 29 | + if (!$this->parseStatement) { |
32 | 30 | try {
|
33 | 31 | $parsedReturnType = (new TypeParser(false))->parse($firstToken);
|
34 | 32 | $token = $parsedReturnType->nextToken;
|
35 |
| - } catch(ParsingException | ParsingSkippedException $e) { |
| 33 | + } catch (ParsingException | ParsingSkippedException) { |
36 | 34 | $parsedReturnType = null;
|
37 | 35 | $token = $firstToken;
|
38 | 36 | }
|
39 | 37 | } else {
|
40 |
| - if($firstToken->id === Token::KEYWORD_VOID) { |
| 38 | + if ($firstToken->id === Token::KEYWORD_VOID) { |
41 | 39 | $parsedReturnType = new ParserReturn(new VoidType(), $firstToken->next());
|
42 | 40 | } else {
|
43 | 41 | $parsedReturnType = (new TypeParser(false))->parse($firstToken);
|
44 | 42 | }
|
45 | 43 | $token = $parsedReturnType->nextToken;
|
46 | 44 | }
|
47 |
| - if($token === null) { |
| 45 | + if ($token === null) { |
48 | 46 | throw new ParsingSkippedException();
|
49 | 47 | }
|
50 |
| - if($this->parseStatement) { |
51 |
| - if($token->id !== Token::IDENTIFIER) { |
| 48 | + if ($this->parseStatement) { |
| 49 | + if ($token->id !== Token::IDENTIFIER) { |
52 | 50 | throw new ParsingSkippedException();
|
53 | 51 | }
|
54 | 52 | $identifier = $token->value;
|
55 | 53 | $token = $token->next();
|
56 |
| - if($token === null) { |
| 54 | + if ($token === null) { |
57 | 55 | throw new ParsingSkippedException();
|
58 | 56 | }
|
59 | 57 | }
|
60 | 58 | $functionArgumentParser = new EnumeratedParser('Function arguments', new InnerFunctionArgumentParser(false), Token::BRACKETS_OPEN, Token::COMMA, Token::BRACKETS_CLOSED, false, true);
|
61 |
| - $parsedArguments = $functionArgumentParser->parse($token); |
| 59 | + try { |
| 60 | + $parsedArguments = $functionArgumentParser->parse($token); |
| 61 | + } catch(ParsingException) { |
| 62 | + throw new ParsingSkippedException(); |
| 63 | + } |
62 | 64 | $token = $parsedArguments->nextToken;
|
63 | 65 | $normalArgs = [];
|
64 | 66 | $vArg = null;
|
65 |
| - for($i = 0;$i < count($parsedArguments->parsed);$i++) { |
| 67 | + for ($i = 0; $i < count($parsedArguments->parsed); $i++) { |
66 | 68 | $arg = $parsedArguments->parsed[$i];
|
67 |
| - if($arg instanceof InnerFunctionArgument) { |
| 69 | + if ($arg instanceof InnerFunctionArgument) { |
68 | 70 | $normalArgs[] = $arg;
|
69 |
| - } else if($arg instanceof InnerVargFunctionArgument) { |
| 71 | + } else if ($arg instanceof InnerVargFunctionArgument) { |
70 | 72 | $vArg = $arg;
|
71 |
| - if($i !== count($parsedArguments->parsed) - 1) { |
| 73 | + if ($i !== count($parsedArguments->parsed) - 1) { |
72 | 74 | throw new ParsingException(ParsingException::ERROR_VARG_NOT_LAST);
|
73 | 75 | }
|
74 | 76 | }
|
75 | 77 | }
|
76 |
| - if(!$this->parseStatement) { |
| 78 | + if (!$this->parseStatement) { |
77 | 79 | try {
|
78 | 80 | $parsedCodeBlock = (new ExpressionFunctionBodyParser())->parse($token);
|
79 |
| - } catch(ParsingSkippedException $e) { |
| 81 | + } catch (ParsingSkippedException $e) { |
80 | 82 | $parsedCodeBlock = (new CodeBlockParser(false, false))->parse($token);
|
81 |
| - if($parsedReturnType === null) { |
| 83 | + if ($parsedReturnType === null) { |
82 | 84 | throw new ParsingException(ParsingException::ERROR_UNEXPECTED_TOKEN, $token, 'Function requires return type');
|
83 | 85 | }
|
84 | 86 | }
|
85 | 87 | } else {
|
86 | 88 | $parsedCodeBlock = (new CodeBlockParser(false, false))->parse($token);
|
87 | 89 | }
|
88 | 90 | $innerArgs = new InnerFunctionArgumentList($normalArgs, $vArg);
|
89 |
| - if($this->parseStatement) { |
| 91 | + if ($this->parseStatement) { |
90 | 92 | $parsed = new FunctionStatement($parsedReturnType->parsed, $identifier, $innerArgs, $parsedCodeBlock->parsed);
|
91 | 93 | } else {
|
92 | 94 | $parsed = new FunctionExpression($parsedReturnType?->parsed ?? null, $innerArgs, $parsedCodeBlock->parsed);
|
|
0 commit comments