WooCommerce Code Reference

Parser
in package

Parses string containing Automattic\WooCommerce\Vendor\GraphQL query language or [schema definition language](schema-definition-language.md) to Abstract Syntax Tree.

Tags
phpstan-type

ParserOptions array{ noLocation?: bool, allowLegacySDLEmptyFields?: bool, allowLegacySDLImplementsInterfaces?: bool, experimentalFragmentVariables?: bool }

  • noLocation: By default, the parser creates AST nodes that know the location in the source. This configuration flag disables that behavior for performance or testing.

  • allowLegacySDLEmptyFields: If enabled, the parser will parse empty fields sets in the Schema Definition Language. Otherwise, the parser will follow the current specification. This option is provided to ease adoption of the final SDL specification and will be removed in a future major release.

  • allowLegacySDLImplementsInterfaces: If enabled, the parser will parse implemented interfaces with no & character between each interface. Otherwise, the parser will follow the current specification. This option is provided to ease adoption of the final SDL specification and will be removed in a future major release.

  • experimentalFragmentVariables: If enabled, the parser will understand and parse variable definitions contained in a fragment definition. They'll be represented in the variableDefinitions field of the FragmentDefinitionNode. The syntax is identical to normal, query-defined variables. For example:

    fragment A($var: Boolean = false) on T {
      ...
    }
    

    Note: this feature is experimental and may change or be removed in the future.

Those magic functions allow partial parsing:

see
ParserTest

Table of Contents

$lexer  : Lexer
__callStatic()  : Node|NodeList<string|int, Node>
Parse partial source by delegating calls to the internal parseX methods.
__construct()  : mixed
argument()  :
array()  :
constArgument()  :
constArray()  :
constDirective()  :
constObject()  :
constObjectField()  :
constValue()  :
constValueLiteral()  :
definition()  :
description()  :
directive()  :
directiveDefinition()  :
directiveLocation()  :
enumTypeDefinition()  :
enumTypeExtension()  :
enumValueDefinition()  :
executableDefinition()  :
field()  :
fieldDefinition()  :
fragment()  :
fragmentDefinition()  :
fragmentName()  :
inputObjectTypeDefinition()  :
inputObjectTypeExtension()  :
inputValueDefinition()  :
interfaceTypeDefinition()  :
interfaceTypeExtension()  :
name()  :
namedType()  :
NodeList()  :
NodeList()  :
NodeList()  :
NodeList()  :
NodeList()  :
NodeList()  :
NodeList()  :
NodeList()  :
NodeList()  :
NodeList()  :
NodeList()  :
NodeList()  :
object()  :
objectField()  :
objectTypeDefinition()  :
objectTypeExtension()  :
operationDefinition()  :
operationType()  :
operationTypeDefinition()  :
parse()  : DocumentNode
Given a Automattic\WooCommerce\Vendor\GraphQL source, parses it into a `Automattic\WooCommerce\Vendor\GraphQL\Language\AST\DocumentNode`.
parseType()  : ListTypeNode|NamedTypeNode|NonNullTypeNode
Given a string containing a Automattic\WooCommerce\Vendor\GraphQL Type (ex. `[Int!]`), parse the AST for that type.
parseValue()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|NullValueNode|ObjectValueNode|StringValueNode|VariableNode
Given a string containing a Automattic\WooCommerce\Vendor\GraphQL value (ex. `[42]`), parse the AST for that value.
scalarTypeDefinition()  :
scalarTypeExtension()  :
schemaDefinition()  :
schemaTypeExtension()  :
selection()  :
selectionSet()  :
stringLiteral()  :
typeExtension()  :
typeReference()  :
typeSystemDefinition()  :
unionTypeDefinition()  :
unionTypeExtension()  :
valueLiteral()  :
variable()  :
variableDefinition()  :
variableValue()  :
any()  : NodeList<string|int, Node>
Returns a possibly empty list of parse nodes, determined by the parseFn. This list begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.
expect()  : Token
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.
expectKeyword()  : void
If the next token is a keyword with the given value, advance the lexer.
expectOptionalKeyword()  : bool
If the next token is a given keyword, return "true" after advancing the lexer. Otherwise, do not change the parser state and return "false".
loc()  : Location|null
Returns a location object, used to identify the place in the source that created a given parsed object.
many()  : NodeList<string|int, TNode>
Returns a non-empty list of parse nodes, determined by the parseFn. This list begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.
parseArgument()  : ArgumentNode
parseArguments()  : NodeList<string|int, ArgumentNode>
parseArgumentsDefinition()  : NodeList<string|int, InputValueDefinitionNode>
parseArray()  : ListValueNode
parseConstArgument()  : ArgumentNode
parseConstValue()  : ValueNode
parseDefinition()  : DefinitionNode|Node
parseDescription()  : StringValueNode|null
parseDirective()  : DirectiveNode
parseDirectiveDefinition()  : DirectiveDefinitionNode
DirectiveDefinition : - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations.
parseDirectiveLocation()  : NameNode
parseDirectiveLocations()  : NodeList<string|int, NameNode>
parseDirectives()  : NodeList<string|int, DirectiveNode>
parseDocument()  : DocumentNode
Implements the parsing rules in the Document section.
parseEnumTypeDefinition()  : EnumTypeDefinitionNode
parseEnumTypeExtension()  : EnumTypeExtensionNode
parseEnumValueDefinition()  : EnumValueDefinitionNode
parseEnumValuesDefinition()  : NodeList<string|int, EnumValueDefinitionNode>
parseExecutableDefinition()  : ExecutableDefinitionNode|Node
parseField()  : FieldNode
parseFieldDefinition()  : FieldDefinitionNode
parseFieldsDefinition()  : NodeList<string|int, FieldDefinitionNode>
parseFragment()  : FragmentSpreadNode|InlineFragmentNode
parseFragmentDefinition()  : FragmentDefinitionNode
parseFragmentName()  : NameNode
parseImplementsInterfaces()  : NodeList<string|int, NamedTypeNode>
parseInputFieldsDefinition()  : NodeList<string|int, InputValueDefinitionNode>
parseInputObjectTypeDefinition()  : InputObjectTypeDefinitionNode
parseInputObjectTypeExtension()  : InputObjectTypeExtensionNode
parseInputValueDefinition()  : InputValueDefinitionNode
parseInterfaceTypeDefinition()  : InterfaceTypeDefinitionNode
parseInterfaceTypeExtension()  : InterfaceTypeExtensionNode
parseName()  : NameNode
Converts a name lex token into a name parse node.
parseNamedType()  : NamedTypeNode
parseObject()  : ObjectValueNode
parseObjectField()  : ObjectFieldNode
parseObjectTypeDefinition()  : ObjectTypeDefinitionNode
parseObjectTypeExtension()  : ObjectTypeExtensionNode
parseOperationDefinition()  : OperationDefinitionNode
parseOperationType()  : string
parseOperationTypeDefinition()  : OperationTypeDefinitionNode
parseScalarTypeDefinition()  : ScalarTypeDefinitionNode
parseScalarTypeExtension()  : ScalarTypeExtensionNode
parseSchemaDefinition()  : SchemaDefinitionNode
parseSchemaTypeExtension()  : SchemaExtensionNode
parseSelection()  : SelectionNode|Node
parseSelectionSet()  : SelectionSetNode
parseStringLiteral()  : StringValueNode
parseTypeReference()  : ListTypeNode|NamedTypeNode|NonNullTypeNode
Handles the Type: TypeName, ListType, and NonNullType parsing rules.
parseTypeSystemDefinition()  : TypeSystemDefinitionNode|Node
parseTypeSystemExtension()  : TypeSystemExtensionNode|Node
parseUnionMemberTypes()  : NodeList<string|int, NamedTypeNode>
parseUnionTypeDefinition()  : UnionTypeDefinitionNode
UnionTypeDefinition : - Description? union Name Directives[Const]? UnionMemberTypes?
parseUnionTypeExtension()  : UnionTypeExtensionNode
UnionTypeExtension : - extend union Name Directives[Const]? UnionMemberTypes - extend union Name Directives[Const].
parseValueLiteral()  : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|StringValueNode|VariableNode|ListValueNode|ObjectValueNode|NullValueNode
Value[Const] : - [~Const] Variable - IntValue - FloatValue - StringValue - BooleanValue - NullValue - EnumValue - ListValue[?Const] - ObjectValue[?Const].
parseVariable()  : VariableNode
parseVariableDefinition()  : VariableDefinitionNode
parseVariableDefinitions()  : NodeList<string|int, VariableDefinitionNode>
parseVariableValue()  : ValueNode
peek()  : bool
Determines if the next token is of a given kind.
peekDescription()  : bool
skip()  : bool
If the next token is of the given kind, return true after advancing the parser. Otherwise, do not change the parser state and return false.
unexpected()  : SyntaxError

Properties

Methods

__callStatic()

Parse partial source by delegating calls to the internal parseX methods.

public static __callStatic(string $name, array<string|int, mixed> $arguments) : Node|NodeList<string|int, Node>
Parameters
$name : string
$arguments : array<string|int, mixed>
Tags
phpstan-param

array{string, ParserOptions} $arguments

throws
JsonException
throws
SyntaxError
Return values
Node|NodeList<string|int, Node>

__construct()

public __construct(Source|string $source[, array<string|int, mixed> $options = [] ]) : mixed
Parameters
$source : Source|string
$options : array<string|int, mixed> = []
Tags
phpstan-param

ParserOptions $options

Return values
mixed

parse()

Given a Automattic\WooCommerce\Vendor\GraphQL source, parses it into a `Automattic\WooCommerce\Vendor\GraphQL\Language\AST\DocumentNode`.

public static parse(Source|string $source[, array<string|int, mixed> $options = [] ]) : DocumentNode

Throws Automattic\WooCommerce\Vendor\GraphQL\Error\SyntaxError if a syntax error is encountered.

Parameters
$source : Source|string
$options : array<string|int, mixed> = []
Tags
phpstan-param

ParserOptions $options

throws
JsonException
throws
SyntaxError
Return values
DocumentNode

parseType()

Given a string containing a Automattic\WooCommerce\Vendor\GraphQL Type (ex. `[Int!]`), parse the AST for that type.

public static parseType(Source|string $source[, array<string|int, mixed> $options = [] ]) : ListTypeNode|NamedTypeNode|NonNullTypeNode

Throws Automattic\WooCommerce\Vendor\GraphQL\Error\SyntaxError if a syntax error is encountered.

This is useful within tools that operate upon Automattic\WooCommerce\Vendor\GraphQL Types directly and in isolation of complete Automattic\WooCommerce\Vendor\GraphQL documents.

Consider providing the results to the utility function: Automattic\WooCommerce\Vendor\GraphQL\Utils\AST::typeFromAST().

Parameters
$source : Source|string
$options : array<string|int, mixed> = []
Tags
phpstan-param

ParserOptions $options

throws
JsonException
throws
SyntaxError
Return values
ListTypeNode|NamedTypeNode|NonNullTypeNode

parseValue()

Given a string containing a Automattic\WooCommerce\Vendor\GraphQL value (ex. `[42]`), parse the AST for that value.

public static parseValue(Source|string $source[, array<string|int, mixed> $options = [] ]) : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|NullValueNode|ObjectValueNode|StringValueNode|VariableNode

Throws Automattic\WooCommerce\Vendor\GraphQL\Error\SyntaxError if a syntax error is encountered.

This is useful within tools that operate upon Automattic\WooCommerce\Vendor\GraphQL Values directly and in isolation of complete Automattic\WooCommerce\Vendor\GraphQL documents.

Consider providing the results to the utility function: Automattic\WooCommerce\Vendor\GraphQL\Utils\AST::valueFromAST().

Parameters
$source : Source|string
$options : array<string|int, mixed> = []
Tags
phpstan-param

ParserOptions $options

throws
JsonException
throws
SyntaxError
Return values
BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|ListValueNode|NullValueNode|ObjectValueNode|StringValueNode|VariableNode

any()

Returns a possibly empty list of parse nodes, determined by the parseFn. This list begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.

private any(string $openKind, callable $parseFn, string $closeKind) : NodeList<string|int, Node>
Parameters
$openKind : string
$parseFn : callable
$closeKind : string
Tags
throws
JsonException
throws
SyntaxError
Return values
NodeList<string|int, Node>

expect()

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.

private expect(string $kind) : Token
Parameters
$kind : string
Tags
throws
JsonException
throws
SyntaxError
Return values
Token

expectKeyword()

If the next token is a keyword with the given value, advance the lexer.

private expectKeyword(string $value) : void

Otherwise, throw an error.

Parameters
$value : string
Tags
throws
JsonException
throws
SyntaxError
Return values
void

expectOptionalKeyword()

If the next token is a given keyword, return "true" after advancing the lexer. Otherwise, do not change the parser state and return "false".

private expectOptionalKeyword(string $value) : bool
Parameters
$value : string
Tags
throws
JsonException
throws
SyntaxError
Return values
bool

many()

Returns a non-empty list of parse nodes, determined by the parseFn. This list begins with a lex token of openKind and ends with a lex token of closeKind. Advances the parser to the next lex token after the closing token.

private many(string $openKind, callable $parseFn, string $closeKind) : NodeList<string|int, TNode>
Parameters
$openKind : string
$parseFn : callable
$closeKind : string
Tags
template

TNode of Node

throws
JsonException
throws
SyntaxError
Return values
NodeList<string|int, TNode>

parseValueLiteral()

Value[Const] : - [~Const] Variable - IntValue - FloatValue - StringValue - BooleanValue - NullValue - EnumValue - ListValue[?Const] - ObjectValue[?Const].

private parseValueLiteral(bool $isConst) : BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|StringValueNode|VariableNode|ListValueNode|ObjectValueNode|NullValueNode

BooleanValue : one of true false

NullValue : null

EnumValue : Name but not true, false or null

Parameters
$isConst : bool
Tags
throws
JsonException
throws
SyntaxError
Return values
BooleanValueNode|EnumValueNode|FloatValueNode|IntValueNode|StringValueNode|VariableNode|ListValueNode|ObjectValueNode|NullValueNode

skip()

If the next token is of the given kind, return true after advancing the parser. Otherwise, do not change the parser state and return false.

private skip(string $kind) : bool
Parameters
$kind : string
Tags
throws
JsonException
throws
SyntaxError
Return values
bool