QuackCompiler\Parser\Grammar::_expr PHP Method

_expr() public method

public _expr ( $precedence, $opt = false )
    public function _expr($precedence = 0, $opt = false)
    {
        $token = $this->parser->lookahead;
        $prefix = $this->parser->prefixParseletForToken($token);
        if (is_null($prefix)) {
            if (!$opt) {
                throw new SyntaxError(['expected' => 'expression', 'found' => $token, 'parser' => $this->parser]);
            }
            return null;
        }
        // We consume the token only when ensure it has a parselet, thus,
        // avoiding to rollback in the tape
        $this->parser->consume();
        $left = $prefix->parse($this, $token);
        while ($precedence < $this->getPrecedence()) {
            $token = $this->parser->consumeAndFetch();
            $infix = $this->parser->infixParseletForToken($token);
            $left = $infix->parse($this, $left, $token);
        }
        return $left;
    }

Usage Example

Esempio n. 1
0
 public function parse(Grammar $grammar, Token $token)
 {
     $cases = [];
     do {
         $grammar->parser->consume();
         // Default operation
         if ($grammar->parser->is(Tag::T_ELSE)) {
             $grammar->parser->consume();
             $default = new \stdClass();
             $default->condition = null;
             $default->action = $grammar->_expr();
             $cases[] = $default;
             goto fetch_next;
         }
         $case = new \stdClass();
         $case->condition = $grammar->_expr();
         $grammar->parser->match('->');
         $case->action = $grammar->_expr();
         $cases[] = $case;
         fetch_next:
         if (!$grammar->parser->is(Tag::T_END)) {
             $grammar->parser->match(';');
         }
     } while ($grammar->parser->is('|'));
     $grammar->parser->match(Tag::T_END);
     return new WhenExpr($cases);
 }
All Usage Examples Of QuackCompiler\Parser\Grammar::_expr