/** * Lex an expression into SassScript tokens. * @param string $string expression to lex * @param SassContext $context the context in which the expression is lexed * @return array tokens */ public function lex($string, $context) { // if it's already lexed, just return it as-is if (is_object($string)) { return array($string); } if (is_array($string)) { return $string; } $tokens = array(); // whilst the string is not empty, split it into it's tokens. while ($string !== false) { if (($match = $this->isWhitespace($string)) !== false) { $tokens[] = null; } elseif (($match = SassScriptFunction::isa($string)) !== false) { preg_match(SassScriptFunction::MATCH_FUNC, $match, $matches); $args = array(); foreach (SassScriptFunction::extractArgs($matches[SassScriptFunction::ARGS], false, $context) as $key => $expression) { $args[$key] = $this->parser->evaluate($expression, $context); } $tokens[] = new SassScriptFunction($matches[SassScriptFunction::NAME], $args); } elseif (($match = SassBoolean::isa($string)) !== false) { $tokens[] = new SassBoolean($match); } elseif (($match = SassColour::isa($string)) !== false) { $tokens[] = new SassColour($match); } elseif (($match = SassNumber::isa($string)) !== false) { $tokens[] = new SassNumber($match); } elseif (($match = SassString::isa($string)) !== false) { $stringed = new SassString($match); if (strlen($stringed->quote) == 0 && SassList::isa($string) !== false && !preg_match("/^\\-\\w+\\-\\w+\$/", $stringed->value)) { $tokens[] = new SassList($string); } else { $tokens[] = $stringed; } } elseif ($string == '()') { $match = $string; $tokens[] = new SassList($match); } elseif (($match = SassScriptOperation::isa($string)) !== false) { $tokens[] = new SassScriptOperation($match); } elseif (($match = SassScriptVariable::isa($string)) !== false) { $tokens[] = new SassScriptVariable($match); } else { $_string = $string; $match = ''; while (strlen($_string) && !$this->isWhitespace($_string)) { foreach (SassScriptOperation::$inStrOperators as $operator) { if (substr($_string, 0, strlen($operator)) == $operator) { break 2; } } $match .= $_string[0]; $_string = substr($_string, 1); } $tokens[] = new SassString($match); } $string = substr($string, strlen($match)); } return $tokens; }
/** * Evaluate a SassScript. * @param string expression to parse * @param SassContext the context in which the expression is evaluated * @param integer 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) { array_push($operands, $token->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 array_shift($operands); }
/** * Lex an expression into SassScript tokens. * @param string expression to lex * @param SassContext the context in which the expression is lexed * @return array tokens */ public function lex($string, $context) { $tokens = array(); while ($string !== false) { if (($match = $this->isWhitespace($string)) !== false) { $tokens[] = null; } elseif (($match = SassScriptFunction::isa($string)) !== false) { preg_match(SassScriptFunction::MATCH_FUNC, $match, $matches); $args = array(); foreach (SassScriptFunction::extractArgs($matches[SassScriptFunction::ARGS]) as $expression) { $args[] = $this->parser->evaluate($expression, $context); } $tokens[] = new SassScriptFunction($matches[SassScriptFunction::NAME], $args); } elseif (($match = SassString::isa($string)) !== false) { $tokens[] = new SassString($match); } elseif (($match = SassBoolean::isa($string)) !== false) { $tokens[] = new SassBoolean($match); } elseif (($match = SassColour::isa($string)) !== false) { $tokens[] = new SassColour($match); } elseif (($match = SassNumber::isa($string)) !== false) { $tokens[] = new SassNumber($match); } elseif (($match = SassScriptOperation::isa($string)) !== false) { $tokens[] = new SassScriptOperation($match); } elseif (($match = SassScriptVariable::isa($string)) !== false) { $tokens[] = new SassScriptVariable($match); } else { $_string = $string; $match = ''; while (strlen($_string) && !$this->isWhitespace($_string)) { foreach (SassScriptOperation::$inStrOperators as $operator) { if (substr($_string, 0, strlen($operator)) == $operator) { break 2; } } $match .= $_string[0]; $_string = substr($_string, 1); } $tokens[] = new SassString($match); } $string = substr($string, strlen($match)); } return $tokens; }
/** * Returns the next token from the string. * @param string string to tokenise * @return mixed token. Either a SassLiteral, a SassScriptOperation * @throws SassScriptLexerException if unable to tokenise string */ private function nextToken(&$string) { if (($match = $this->isWhitespace($string)) !== false) { $string = substr($string, strlen($match)); return $this->nextToken($string); } elseif (($match = SassNumber::isa($string)) !== false) { $string = substr($string, strlen($match)); return new SassNumber($match); } elseif (($match = SassColour::isa($string)) !== false) { $string = substr($string, strlen($match)); return new SassColour($match); } elseif (($match = SassBoolean::isa($string)) !== false) { $string = substr($string, strlen($match)); return new SassBoolean($match); } elseif (($match = SassString::isa($string)) !== false) { $string = substr($string, strlen($match)); return new SassString($match); } elseif (($match = SassScriptFunction::isa($string)) !== false) { $string = substr($string, strlen($match)); return new SassScriptFunction($match); } elseif (($match = SassScriptOperation::isa($string)) !== false) { $string = substr($string, strlen($match)); return new SassScriptOperation($match); } else { throw new SassScriptLexerException("Unable to tokenise \"{$string}\""); } }