public function testCallable()
 {
     $source = 'abc';
     $expected = Console::open(['color' => 'red']) . 'abc' . Console::close() . Console::reset();
     $token = $this->_factory->create('token', ['pos' => 0, 'length' => 3]);
     $iterator = new Result($source);
     $iterator->merge([$token, $token->getEnd()]);
     $mock = $this->getMock('stdClass', ['call']);
     $mock->expects($this->once())->method('call')->with($token)->willReturn(['color' => 'red']);
     $formatter = new CliFormatter(['token' => [$mock, 'call']]);
     $this->assertEquals($expected, $formatter->format($iterator));
 }
Example #2
0
 protected function processEnd(Context $context, Language $language, Result $result, TokenIterator $tokens)
 {
     foreach (array_filter($context->stack, function ($name) {
         return in_array($name, $this->closes);
     }) as $hash => $name) {
         $end = new Token($name, ['pos' => $this->pos]);
         $tokens[$hash]->setEnd($end);
         $result->append($end);
         unset($context->stack[$hash]);
     }
     return true;
 }
    public function testRender()
    {
        $source = 'abc + test';
        $expected = <<<EXPECTED
<span class="token">abc</span> <span class="operator">+</span> <span class="token second">test</span>
EXPECTED;
        $first = $this->_factory->create('token', ['pos' => 0, 'length' => 3]);
        $operator = $this->_factory->create('operator', ['pos' => 4, 'length' => 1]);
        $second = $this->_factory->create('token.second', ['pos' => 6, 'length' => 4]);
        $iterator = new Result($source);
        $iterator->merge([$first, $first->getEnd(), $operator, $operator->getEnd(), $second, $second->getEnd()]);
        $formatter = new HtmlFormatter();
        $this->assertEquals($expected, $formatter->format($iterator));
    }
Example #4
0
 protected function processEnd(Context $context, Language $language, Result $result, TokenIterator $tokens)
 {
     $this->setStart($result->getStart());
     if ($this->_start->postProcess) {
         $source = substr($tokens->getSource(), $this->_start->pos - $tokens->getOffset(), $this->_start->getLength());
         $tokens = $this->_start->inject->tokenize($source, $result->getTokens(), $this->_start->pos, Language::EMBEDDED_BY_PARENT);
         $result->exchangeArray($this->_start->inject->parse($tokens)->getTokens());
     }
     # closing unclosed tokens
     foreach (array_reverse($context->stack, true) as $id => $name) {
         $end = new Token($name, ['pos' => $this->pos]);
         $tokens[$id]->setEnd($end);
         $result->append($end);
     }
     $result->append($this);
     return false;
 }
Example #5
0
 protected function processEnd(Context $context, Language $language, Result $result, TokenIterator $tokens)
 {
     if ($this->_start) {
         $context->pop($this->_start);
     } else {
         if (($start = $context->find($this->name)) !== false) {
             $this->setStart($tokens[$start]);
             unset($context->stack[$start]);
         }
     }
     if (!$this->_start instanceof MetaToken) {
         $result->append($this);
     }
     return true;
 }