-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
InterpretedEvaluator.php
50 lines (46 loc) · 1.65 KB
/
InterpretedEvaluator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Neos\Eel;
/*
* This file is part of the Neos.Eel package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
/**
* An expression evalutator that interprets expressions
*
* There is no generated PHP code so this evaluator does not perform very
* good in multiple invocations.
*/
class InterpretedEvaluator implements EelEvaluatorInterface
{
/**
* Evaluate an expression under a given context
*
* @param string $expression
* @param Context $context
* @return mixed
* @throws ParserException
*/
public function evaluate($expression, Context $context)
{
$expression = trim($expression);
$parser = new InterpretedEelParser($expression, $context);
$res = $parser->match_Expression();
if ($res === false) {
throw new ParserException(sprintf('Expression "%s" could not be parsed.', $expression), 1344514198);
} elseif ($parser->pos !== strlen($expression)) {
throw new ParserException(sprintf('Expression "%s" could not be parsed. Error starting at character %d: "%s".', $expression, $parser->pos, substr($expression, $parser->pos)), 1344514188);
} elseif (!array_key_exists('val', $res)) {
throw new ParserException(sprintf('Parser error, no val in result %s ', json_encode($res)), 1344514204);
}
if ($res['val'] instanceof Context) {
return $res['val']->unwrap();
} else {
return $res['val'];
}
}
}