GraphQL\Language\Parser::expect PHP Method

expect() public method

If the next token is of the given kind, return that token after advancing the parser. Otherwise, do not change the parser state and return false.
public expect ( string $kind ) : Token
$kind string
return Token
    function expect($kind)
    {
        $token = $this->lexer->token;
        if ($token->kind === $kind) {
            $this->lexer->advance();
            return $token;
        }
        throw new SyntaxError($this->lexer->source, $token->start, "Expected {$kind}, found " . $token->getDescription());
    }

Usage Example

Example #1
0
 /**
  * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for
  * that type.
  * Throws GraphQLError if a syntax error is encountered.
  *
  * This is useful within tools that operate upon GraphQL Types directly and
  * in isolation of complete GraphQL documents.
  *
  * Consider providing the results to the utility function: typeFromAST().
  * @param Source|string $source
  * @param array $options
  * @return ListType|Name|NonNullType
  */
 public static function parseType($source, array $options = [])
 {
     $sourceObj = $source instanceof Source ? $source : new Source($source);
     $parser = new Parser($sourceObj, $options);
     $parser->expect(Token::SOF);
     $type = $parser->parseTypeReference();
     $parser->expect(Token::EOF);
     return $type;
 }