/** * Tests ExpParser::parse */ public function testParse() { $this->dataContext['no_prefix_id']->Ee = 1; $actualResult = ExpParser::parse($this->tokenStream, $this->dataContext); $this->assertEquals(new Token(ExpType::$STRING, 'no_prefix_id.Ee > 0'), $actualResult); $this->dataContext['no_prefix_id']->Ee = -1; $actualResult = ExpParser::parse($this->tokenStream, $this->dataContext); $this->assertEquals(new Token(ExpType::$STRING, 'no_prefix_id.Ee <= 0'), $actualResult); }
/** * Evaluates the given $expression using the $dataContext as variable source. * * Internally the ExpressionParser uses a infix to postfix conversion to easily * be able to evaluate mathematical expressions * * @param string $expression * @param array $dataContext * @return string evaluated result or an exception of failure */ public static function evaluate($expression, $dataContext) { $outputTokens = ExpLexer::process($expression); $result = ExpParser::parse($outputTokens, $dataContext); return $result->value; }
private static function findNextCloseSymbol($tokenStream, $startPos) { $stackDepth = 0; for ($i = $startPos; $i < count($tokenStream); $i++) { $token = $tokenStream[$i]; if ($stackDepth == 0 && ExpParser::isCloseSymbol($token, false)) { break; } if (ExpParser::isOpenSymbol($token)) { $stackDepth++; } if (ExpParser::isCloseSymbol($token)) { $stackDepth--; } } return $i; }