AST
2 months ago
BlockString.php
2 months ago
DirectiveLocation.php
2 months ago
Lexer.php
2 months ago
Parser.php
2 months ago
Printer.php
2 months ago
Source.php
2 months ago
SourceLocation.php
2 months ago
Token.php
2 months ago
Visitor.php
2 months ago
VisitorOperation.php
2 months ago
VisitorRemoveNode.php
2 months ago
VisitorSkipNode.php
2 months ago
VisitorStop.php
2 months ago
Token.php
100 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Language; |
| 4 | |
| 5 | /** |
| 6 | * Represents a range of characters represented by a lexical token |
| 7 | * within a Source. |
| 8 | * |
| 9 | * @see \Automattic\WooCommerce\Vendor\GraphQL\Tests\Language\TokenTest |
| 10 | */ |
| 11 | class Token |
| 12 | { |
| 13 | // Each kind of token. |
| 14 | public const SOF = '<SOF>'; |
| 15 | public const EOF = '<EOF>'; |
| 16 | public const BANG = '!'; |
| 17 | public const DOLLAR = '$'; |
| 18 | public const AMP = '&'; |
| 19 | public const PAREN_L = '('; |
| 20 | public const PAREN_R = ')'; |
| 21 | public const SPREAD = '...'; |
| 22 | public const COLON = ':'; |
| 23 | public const EQUALS = '='; |
| 24 | public const AT = '@'; |
| 25 | public const BRACKET_L = '['; |
| 26 | public const BRACKET_R = ']'; |
| 27 | public const BRACE_L = '{'; |
| 28 | public const PIPE = '|'; |
| 29 | public const BRACE_R = '}'; |
| 30 | public const NAME = 'Name'; |
| 31 | public const INT = 'Int'; |
| 32 | public const FLOAT = 'Float'; |
| 33 | public const STRING = 'String'; |
| 34 | public const BLOCK_STRING = 'BlockString'; |
| 35 | public const COMMENT = 'Comment'; |
| 36 | |
| 37 | /** The kind of Token (see one of constants above). */ |
| 38 | public string $kind; |
| 39 | |
| 40 | /** The character offset at which this Node begins. */ |
| 41 | public int $start; |
| 42 | |
| 43 | /** The character offset at which this Node ends. */ |
| 44 | public int $end; |
| 45 | |
| 46 | /** The 1-indexed line number on which this Token appears. */ |
| 47 | public int $line; |
| 48 | |
| 49 | /** The 1-indexed column number at which this Token begins. */ |
| 50 | public int $column; |
| 51 | |
| 52 | public ?string $value; |
| 53 | |
| 54 | /** |
| 55 | * Tokens exist as nodes in a double-linked-list amongst all tokens |
| 56 | * including ignored tokens. <SOF> is always the first node and <EOF> |
| 57 | * the last. |
| 58 | */ |
| 59 | public ?Token $prev; |
| 60 | |
| 61 | public ?Token $next = null; |
| 62 | |
| 63 | public function __construct(string $kind, int $start, int $end, int $line, int $column, ?Token $previous = null, ?string $value = null) |
| 64 | { |
| 65 | $this->kind = $kind; |
| 66 | $this->start = $start; |
| 67 | $this->end = $end; |
| 68 | $this->line = $line; |
| 69 | $this->column = $column; |
| 70 | $this->prev = $previous; |
| 71 | $this->value = $value; |
| 72 | } |
| 73 | |
| 74 | public function getDescription(): string |
| 75 | { |
| 76 | return $this->kind |
| 77 | . ($this->value === null |
| 78 | ? '' |
| 79 | : " \"{$this->value}\""); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return array{ |
| 84 | * kind: string, |
| 85 | * value: string|null, |
| 86 | * line: int, |
| 87 | * column: int, |
| 88 | * } |
| 89 | */ |
| 90 | public function toArray(): array |
| 91 | { |
| 92 | return [ |
| 93 | 'kind' => $this->kind, |
| 94 | 'value' => $this->value, |
| 95 | 'line' => $this->line, |
| 96 | 'column' => $this->column, |
| 97 | ]; |
| 98 | } |
| 99 | } |
| 100 |