3 namespace Sabberworm\CSS\Value;
5 use Sabberworm\CSS\Parsing\ParserState;
6 use Sabberworm\CSS\Parsing\UnexpectedTokenException;
8 class CalcFunction extends CSSFunction {
12 public static function parse(ParserState $oParserState) {
13 $aOperators = array('+', '-', '*', '/');
14 $sFunction = trim($oParserState->consumeUntil('(', false, true));
15 $oCalcList = new CalcRuleValueList($oParserState->currentLine());
16 $oList = new RuleValueList(',', $oParserState->currentLine());
18 $iLastComponentType = NULL;
19 while(!$oParserState->comes(')') || $iNestingLevel > 0) {
20 $oParserState->consumeWhiteSpace();
21 if ($oParserState->comes('(')) {
23 $oCalcList->addListComponent($oParserState->consume(1));
25 } else if ($oParserState->comes(')')) {
27 $oCalcList->addListComponent($oParserState->consume(1));
30 if ($iLastComponentType != CalcFunction::T_OPERAND) {
31 $oVal = Value::parsePrimitiveValue($oParserState);
32 $oCalcList->addListComponent($oVal);
33 $iLastComponentType = CalcFunction::T_OPERAND;
35 if (in_array($oParserState->peek(), $aOperators)) {
36 if (($oParserState->comes('-') || $oParserState->comes('+'))) {
37 if ($oParserState->peek(1, -1) != ' ' || !($oParserState->comes('- ') || $oParserState->comes('+ '))) {
38 throw new UnexpectedTokenException(" {$oParserState->peek()} ", $oParserState->peek(1, -1) . $oParserState->peek(2), 'literal', $oParserState->currentLine());
41 $oCalcList->addListComponent($oParserState->consume(1));
42 $iLastComponentType = CalcFunction::T_OPERATOR;
44 throw new UnexpectedTokenException(
46 'Next token was expected to be an operand of type %s. Instead "%s" was found.',
47 implode(', ', $aOperators),
52 $oParserState->currentLine()
57 $oList->addListComponent($oCalcList);
58 $oParserState->consume(')');
59 return new CalcFunction($sFunction, $oList, ',', $oParserState->currentLine());