blob: 53a6cafc350bce75c2cb5e0be5cc7d274860b73a [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 * (c) Armin Ronacher
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13namespace Twig;
14
15/**
16 * @author Fabien Potencier <fabien@symfony.com>
17 */
18final class Token
19{
20 private $value;
21 private $type;
22 private $lineno;
23
24 public const EOF_TYPE = -1;
25 public const TEXT_TYPE = 0;
26 public const BLOCK_START_TYPE = 1;
27 public const VAR_START_TYPE = 2;
28 public const BLOCK_END_TYPE = 3;
29 public const VAR_END_TYPE = 4;
30 public const NAME_TYPE = 5;
31 public const NUMBER_TYPE = 6;
32 public const STRING_TYPE = 7;
33 public const OPERATOR_TYPE = 8;
34 public const PUNCTUATION_TYPE = 9;
35 public const INTERPOLATION_START_TYPE = 10;
36 public const INTERPOLATION_END_TYPE = 11;
37 public const ARROW_TYPE = 12;
38
39 public function __construct(int $type, $value, int $lineno)
40 {
41 $this->type = $type;
42 $this->value = $value;
43 $this->lineno = $lineno;
44 }
45
46 public function __toString()
47 {
48 return sprintf('%s(%s)', self::typeToString($this->type, true), $this->value);
49 }
50
51 /**
52 * Tests the current token for a type and/or a value.
53 *
54 * Parameters may be:
55 * * just type
56 * * type and value (or array of possible values)
57 * * just value (or array of possible values) (NAME_TYPE is used as type)
58 *
59 * @param array|string|int $type The type to test
60 * @param array|string|null $values The token value
61 */
62 public function test($type, $values = null): bool
63 {
64 if (null === $values && !\is_int($type)) {
65 $values = $type;
66 $type = self::NAME_TYPE;
67 }
68
69 return ($this->type === $type) && (
70 null === $values ||
71 (\is_array($values) && \in_array($this->value, $values)) ||
72 $this->value == $values
73 );
74 }
75
76 public function getLine(): int
77 {
78 return $this->lineno;
79 }
80
81 public function getType(): int
82 {
83 return $this->type;
84 }
85
86 public function getValue()
87 {
88 return $this->value;
89 }
90
91 public static function typeToString(int $type, bool $short = false): string
92 {
93 switch ($type) {
94 case self::EOF_TYPE:
95 $name = 'EOF_TYPE';
96 break;
97 case self::TEXT_TYPE:
98 $name = 'TEXT_TYPE';
99 break;
100 case self::BLOCK_START_TYPE:
101 $name = 'BLOCK_START_TYPE';
102 break;
103 case self::VAR_START_TYPE:
104 $name = 'VAR_START_TYPE';
105 break;
106 case self::BLOCK_END_TYPE:
107 $name = 'BLOCK_END_TYPE';
108 break;
109 case self::VAR_END_TYPE:
110 $name = 'VAR_END_TYPE';
111 break;
112 case self::NAME_TYPE:
113 $name = 'NAME_TYPE';
114 break;
115 case self::NUMBER_TYPE:
116 $name = 'NUMBER_TYPE';
117 break;
118 case self::STRING_TYPE:
119 $name = 'STRING_TYPE';
120 break;
121 case self::OPERATOR_TYPE:
122 $name = 'OPERATOR_TYPE';
123 break;
124 case self::PUNCTUATION_TYPE:
125 $name = 'PUNCTUATION_TYPE';
126 break;
127 case self::INTERPOLATION_START_TYPE:
128 $name = 'INTERPOLATION_START_TYPE';
129 break;
130 case self::INTERPOLATION_END_TYPE:
131 $name = 'INTERPOLATION_END_TYPE';
132 break;
133 case self::ARROW_TYPE:
134 $name = 'ARROW_TYPE';
135 break;
136 default:
137 throw new \LogicException(sprintf('Token of type "%s" does not exist.', $type));
138 }
139
140 return $short ? $name : 'Twig\Token::'.$name;
141 }
142
143 public static function typeToEnglish(int $type): string
144 {
145 switch ($type) {
146 case self::EOF_TYPE:
147 return 'end of template';
148 case self::TEXT_TYPE:
149 return 'text';
150 case self::BLOCK_START_TYPE:
151 return 'begin of statement block';
152 case self::VAR_START_TYPE:
153 return 'begin of print statement';
154 case self::BLOCK_END_TYPE:
155 return 'end of statement block';
156 case self::VAR_END_TYPE:
157 return 'end of print statement';
158 case self::NAME_TYPE:
159 return 'name';
160 case self::NUMBER_TYPE:
161 return 'number';
162 case self::STRING_TYPE:
163 return 'string';
164 case self::OPERATOR_TYPE:
165 return 'operator';
166 case self::PUNCTUATION_TYPE:
167 return 'punctuation';
168 case self::INTERPOLATION_START_TYPE:
169 return 'begin of string interpolation';
170 case self::INTERPOLATION_END_TYPE:
171 return 'end of string interpolation';
172 case self::ARROW_TYPE:
173 return 'arrow function';
174 default:
175 throw new \LogicException(sprintf('Token of type "%s" does not exist.', $type));
176 }
177 }
178}