PhpPeg\Token::compile PHP Method

compile() public method

abstract protected function match_code() ;
public compile ( )
    function compile()
    {
        $code = $this->match_code();
        $id = $this->varid();
        if ($this->optional) {
            $code = PHPBuilder::build()->l($this->save($id), $code->replace(['FAIL' => $this->restore($id, true)]));
        }
        if ($this->zero_or_more) {
            $code = PHPBuilder::build()->b('while (true)', $this->save($id), $code->replace(['MATCH' => NULL, 'FAIL' => $this->restore($id, true)->l('break;')]))->l('MATCH');
        }
        if ($this->one_or_more) {
            $code = PHPBuilder::build()->l('$count = 0;')->b('while (true)', $this->save($id), $code->replace(['MATCH' => NULL, 'FAIL' => $this->restore($id, true)->l('break;')]), '$count += 1;')->b('if ($count > 0)', 'MATCH')->b('else', 'FAIL');
        }
        if ($this->positive_lookahead) {
            $code = PHPBuilder::build()->l($this->save($id), $code->replace(['MATCH' => $this->restore($id)->l('MATCH'), 'FAIL' => $this->restore($id)->l('FAIL')]));
        }
        if ($this->negative_lookahead) {
            $code = PHPBuilder::build()->l($this->save($id), $code->replace(['MATCH' => $this->restore($id)->l('FAIL'), 'FAIL' => $this->restore($id)->l('MATCH')]));
        }
        if ($this->tag && !$this instanceof TokenRecurse) {
            $code = PHPBuilder::build()->l('$stack[] = $result; $result = $this->construct( $matchrule, "' . $this->tag . '" );', $code->replace(['MATCH' => PHPBuilder::build()->l('$subres = $result; $result = array_pop($stack);', '$this->store( $result, $subres, \'' . $this->tag . '\' );', 'MATCH'), 'FAIL' => PHPBuilder::build()->l('$result = array_pop($stack);', 'FAIL')]));
        }
        return $code;
    }