Example #1
0
 public function testContextMethods()
 {
     $mock = $this->getMock('Expressive\\Context\\Scope');
     $this->assertNull($this->parser->popContext());
     $this->parser->pushContext($mock);
     $this->assertInstanceOf('Expressive\\Context\\ContextInterface', $this->parser->popContext());
     $this->assertNull($this->parser->popContext());
     $this->parser->pushContext($mock);
     $this->assertInstanceOf('Expressive\\Context\\ContextInterface', $this->parser->getContext());
 }
Example #2
0
 /**
  *
  * @param $token
  *
  * @throws \Expressive\Exception\UnknownTokenException
  * @throws \Expressive\Exception\OutOfScopeException
  */
 public function handleToken($token)
 {
     $type = null;
     if (in_array($token, array('*', '/', '+', '-', '^'))) {
         $type = self::T_OP;
     }
     if ($token === ')') {
         $type = self::T_CLOSE;
     }
     if ($token === '(') {
         $type = self::T_SCOPE;
     }
     if ($token === 'sin(') {
         $type = self::T_SIN;
     }
     if ($token === 'cos(') {
         $type = self::T_COS;
     }
     if ($token === 'tan(') {
         $type = self::T_TAN;
     }
     if ($token === 'sqrt(') {
         $type = self::T_SQRT;
     }
     if ($token === 'exp(') {
         $type = self::T_EXP;
     }
     if (is_null($type)) {
         if (is_numeric($token)) {
             $type = self::T_NUM;
             $token = (double) $token;
         } else {
             throw new \UnexpectedValueException('unacceptable value');
         }
     }
     switch ($type) {
         case self::T_NUM:
         case self::T_OP:
             $this->ops[] = $token;
             break;
         case self::T_SCOPE:
             $this->builder->pushContext(new Scope());
             break;
         case self::T_SIN:
             $this->builder->pushContext(new SinScope());
             break;
         case self::T_COS:
             $this->builder->pushContext(new CosScope());
             break;
         case self::T_TAN:
             $this->builder->pushContext(new TanScope());
             break;
         case self::T_SQRT:
             $this->builder->pushContext(new SqrtScope());
             break;
         case self::T_EXP:
             $this->builder->pushContext(new ExpScope());
             break;
         case self::T_CLOSE:
             $operation = $this->builder->popContext();
             $newContext = $this->builder->getContext();
             if (is_null($operation) || !$newContext) {
                 throw new OutOfScopeException();
             }
             $newContext->addOp($operation);
             break;
         default:
             throw new UnknownTokenException($token);
             break;
     }
 }
Example #3
0
 /**
  * @expectedException \UnexpectedValueException
  */
 public function testOutOfScopeException()
 {
     $parser = new Parser(' This is a test 4-2');
     $parser->setThrowExceptions();
     echo $parser->getResult();
 }