Debian Codegraph @

vendor/sabberworm/php-css-parser/src/Value/CalcFunction.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Sabberworm\CSS\Value;
6
7 use Sabberworm\CSS\Parsing\ParserState;
8 use Sabberworm\CSS\Parsing\UnexpectedEOFException;
9 use Sabberworm\CSS\Parsing\UnexpectedTokenException;
10
11 use function Safe\preg_match;
12
13 class CalcFunction extends CSSFunction
14 {
15 private const T_OPERAND = 1;
16 private const T_OPERATOR = 2;
17
18 /**
19 * @throws UnexpectedTokenException
20 * @throws UnexpectedEOFException
21 *
22 * @internal since V8.8.0
23 */
24 public static function parse(ParserState $parserState, bool $ignoreCase = false): CSSFunction
25 {
26 $operators = ['+', '-', '*', '/'];
27 $function = $parserState->parseIdentifier();
28 if ($parserState->peek() !== '(') {
29 // Found ; or end of line before an opening bracket
30 throw new UnexpectedTokenException('(', $parserState->peek(), 'literal', $parserState->currentLine());
31 } elseif ($function !== 'calc') {
32 // Found invalid calc definition. Example calc (...
33 throw new UnexpectedTokenException('calc', $function, 'literal', $parserState->currentLine());
34 }
35 $parserState->consume('(');
36 $calcRuleValueList = new CalcRuleValueList($parserState->currentLine());
37 $list = new RuleValueList(',', $parserState->currentLine());
38 $nestingLevel = 0;
39 $lastComponentType = null;
40 while (!$parserState->comes(')') || $nestingLevel > 0) {
41 if ($parserState->isEnd() && $nestingLevel === 0) {
42 break;
43 }
44
45 $parserState->consumeWhiteSpace();
46 if ($parserState->comes('(')) {
47 $nestingLevel++;
48 $calcRuleValueList->addListComponent($parserState->consume(1));
49 $parserState->consumeWhiteSpace();
50 continue;
51 } elseif ($parserState->comes(')')) {
52 $nestingLevel--;
53 $calcRuleValueList->addListComponent($parserState->consume(1));
54 $parserState->consumeWhiteSpace();
55 continue;
56 }
57 if ($lastComponentType !== CalcFunction::T_OPERAND) {
58 $value = Value::parsePrimitiveValue($parserState);
59 $calcRuleValueList->addListComponent($value);
60 $lastComponentType = CalcFunction::T_OPERAND;
61 } else {
62 if (\in_array($parserState->peek(), $operators, true)) {
63 if (($parserState->comes('-') || $parserState->comes('+'))) {
64 if (
65 preg_match('/\\s/', $parserState->peek(1, -1)) !== 1
66 || preg_match('/\\s/', $parserState->peek(1, 1)) !== 1
67 ) {
68 throw new UnexpectedTokenException(
69 " {$parserState->peek()} ",
70 $parserState->peek(1, -1) . $parserState->peek(2),
71 'literal',
72 $parserState->currentLine()
73 );
74 }
75 }
76 $calcRuleValueList->addListComponent($parserState->consume(1));
77 $lastComponentType = CalcFunction::T_OPERATOR;
78 } else {
79 throw new UnexpectedTokenException(
80 \sprintf(
81 'Next token was expected to be an operand of type %s. Instead "%s" was found.',
82 \implode(', ', $operators),
83 $parserState->peek()
84 ),
85 '',
86 'custom',
87 $parserState->currentLine()
88 );
89 }
90 }
91 $parserState->consumeWhiteSpace();
92 }
93 $list->addListComponent($calcRuleValueList);
94 if (!$parserState->isEnd()) {
95 $parserState->consume(')');
96 }
97 return new CalcFunction($function, $list, ',', $parserState->currentLine());
98 }
99
100 /**
101 * @return array<string, bool|int|float|string|array<mixed>|null>
102 *
103 * @internal
104 */
105 public function getArrayRepresentation(): array
106 {
107 throw new \BadMethodCallException('`getArrayRepresentation` is not yet implemented for `' . self::class . '`');
108 }
109 }