Parses SassScript. SassScript is lexed into {@link http://en.wikipedia.org/wiki/Reverse_Polish_notation Reverse Polish notation} by the SassScriptLexer and the calculated result returned.
 /**
  * Evaluate a SassScript.
  * @param string $expression expression to parse
  * @param SassContext $context the context in which the expression is evaluated
  * @param  integer $environment the environment in which the expression is evaluated
  * @return SassLiteral parsed value
  */
 public function evaluate($expression, $context, $environment = self::DEFAULT_ENV)
 {
     self::$context = $context;
     $operands = array();
     $tokens = $this->parse($expression, $context, $environment);
     while (count($tokens)) {
         $token = array_shift($tokens);
         if ($token instanceof SassScriptFunction) {
             $perform = $token->perform();
             array_push($operands, $perform);
         } elseif ($token instanceof SassLiteral) {
             if ($token instanceof SassString) {
                 $token = new SassString($this->interpolate($token->toString(), self::$context));
             }
             array_push($operands, $token);
         } else {
             $args = array();
             for ($i = 0, $c = $token->operandCount; $i < $c; $i++) {
                 $args[] = array_pop($operands);
             }
             array_push($operands, $token->perform($args));
         }
     }
     return self::makeSingular($operands);
 }
Beispiel #2
0
 public static function colour_stops()
 {
     $args = func_get_args();
     $list = array();
     foreach ($args as $arg) {
         if ($arg instanceof SassColour) {
             $list[] = new CompassColourStop($arg);
         } elseif ($arg instanceof SassString) {
             # We get a string as the result of concatenation
             # So we have to reparse the expression
             $colour = $stop = null;
             if (empty($parser)) {
                 $parser = new SassScriptParser();
             }
             $expr = $parser->parse($arg->value, SassScriptParser::$context);
             $x = array_pop($expr);
             if ($x instanceof SassColour) {
                 $colour = $x;
             } elseif ($x instanceof SassScriptOperation) {
                 if ($x->operator != 'concat') {
                     # This should never happen.
                     throw new SassScriptFunctionException("Couldn't parse a colour stop from: {value}", array('{value}' => $arg->value), SassScriptParser::$context->node);
                 }
                 $colour = $expr[0];
                 $stop = $expr[1];
             } else {
                 throw new SassScriptFunctionException("Couldn't parse a colour stop from: {value}", array('{value}' => $arg->value), SassScriptParser::$context->node);
             }
             $list[] = new CompassColourStop($colour, $stop);
         } else {
             throw new SassScriptFunctionException('Not a valid color stop: {arg}', array('{arg}' => $arg->value), SassScriptParser::$context->node);
         }
     }
     return new CompassList($list);
 }