/** * Lex an expression into Script tokens. * * @param string $string expression to lex * @param Context $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 [$string]; } if (is_array($string)) { return $string; } $tokens = []; // 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 = ScriptFunction::isa($string)) !== FALSE) { preg_match(ScriptFunction::MATCH_FUNC, $match, $matches); $args = []; foreach (ScriptFunction::extractArgs($matches[ScriptFunction::ARGS], FALSE, $context) as $key => $expression) { $args[$key] = $this->parser->evaluate($expression, $context); } $tokens[] = new ScriptFunction($matches[ScriptFunction::NAME], $args); } elseif (($match = Literals\Boolean::isa($string)) !== FALSE) { $tokens[] = new Literals\Boolean($match); } elseif (($match = Literals\Colour::isa($string)) !== FALSE) { $tokens[] = new Literals\Colour($match); } elseif (($match = Literals\Number::isa($string)) !== FALSE) { $tokens[] = new Literals\Number($match); } elseif (($match = Literals\SassString::isa($string)) !== FALSE) { $stringed = new Literals\SassString($match); $tokens[] = !strlen($stringed->quote) && Literals\SassList::isa($string) !== FALSE && !preg_match("/^\\-\\w+\\-\\w+\$/", $stringed->value) ? new Literals\SassList($string) : $stringed; } elseif ($string == '()') { $match = $string; $tokens[] = new Literals\SassList($match); } elseif (($match = Operation::isa($string)) !== FALSE) { $tokens[] = new Operation($match); } elseif (($match = Variable::isa($string)) !== FALSE) { $tokens[] = new Variable($match); } else { $_string = $string; $match = ''; while (strlen($_string) && !$this->isWhitespace($_string)) { foreach (Operation::$inStrOperators as $operator) { if (substr($_string, 0, strlen($operator)) == $operator) { break 2; } } $match .= $_string[0]; $_string = substr($_string, 1); } $tokens[] = new Literals\SassString($match); } $string = substr($string, strlen($match)); } return $tokens; }
/** * @param Literals\SassList $list * @param mixed $value * @return mixed */ public static function index($list, $value) { if (!$list instanceof Literals\SassList) { $list = new Literals\SassList($list->toString()); } return $list->index($value); }