1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\ExpressionLanguage;
13
14/**
15 * Represents a Token.
16 *
17 * @author Fabien Potencier <fabien@symfony.com>
18 */
19class Token
20{
21    public $value;
22    public $type;
23    public $cursor;
24
25    public const EOF_TYPE = 'end of expression';
26    public const NAME_TYPE = 'name';
27    public const NUMBER_TYPE = 'number';
28    public const STRING_TYPE = 'string';
29    public const OPERATOR_TYPE = 'operator';
30    public const PUNCTUATION_TYPE = 'punctuation';
31
32    /**
33     * @param string                $type   The type of the token (self::*_TYPE)
34     * @param string|int|float|null $value  The token value
35     * @param int                   $cursor The cursor position in the source
36     */
37    public function __construct(string $type, $value, ?int $cursor)
38    {
39        $this->type = $type;
40        $this->value = $value;
41        $this->cursor = $cursor;
42    }
43
44    /**
45     * Returns a string representation of the token.
46     *
47     * @return string A string representation of the token
48     */
49    public function __toString()
50    {
51        return sprintf('%3d %-11s %s', $this->cursor, strtoupper($this->type), $this->value);
52    }
53
54    /**
55     * Tests the current token for a type and/or a value.
56     *
57     * @param string      $type  The type to test
58     * @param string|null $value The token value
59     *
60     * @return bool
61     */
62    public function test($type, $value = null)
63    {
64        return $this->type === $type && (null === $value || $this->value == $value);
65    }
66}
67