| Total Complexity | 351 |
| Total Lines | 1394 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like lessc_parser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use lessc_parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 2383 | class lessc_parser { |
||
| 2384 | protected static $nextBlockId = 0; // used to uniquely identify blocks |
||
| 2385 | |||
| 2386 | protected static $precedence = array( |
||
| 2387 | '=<' => 0, |
||
| 2388 | '>=' => 0, |
||
| 2389 | '=' => 0, |
||
| 2390 | '<' => 0, |
||
| 2391 | '>' => 0, |
||
| 2392 | |||
| 2393 | '+' => 1, |
||
| 2394 | '-' => 1, |
||
| 2395 | '*' => 2, |
||
| 2396 | '/' => 2, |
||
| 2397 | '%' => 2, |
||
| 2398 | ); |
||
| 2399 | |||
| 2400 | protected static $whitePattern; |
||
| 2401 | protected static $commentMulti; |
||
| 2402 | |||
| 2403 | protected static $commentSingle = "//"; |
||
| 2404 | protected static $commentMultiLeft = "/*"; |
||
| 2405 | protected static $commentMultiRight = "*/"; |
||
| 2406 | |||
| 2407 | // regex string to match any of the operators |
||
| 2408 | protected static $operatorString; |
||
| 2409 | |||
| 2410 | // these properties will supress division unless it's inside parenthases |
||
| 2411 | protected static $supressDivisionProps = |
||
| 2412 | array('/border-radius$/i', '/^font$/i'); |
||
| 2413 | |||
| 2414 | protected $blockDirectives = array("font-face", "keyframes", "page", "-moz-document", "viewport", "-moz-viewport", "-o-viewport", "-ms-viewport"); |
||
| 2415 | protected $lineDirectives = array("charset"); |
||
| 2416 | |||
| 2417 | /** |
||
| 2418 | * if we are in parens we can be more liberal with whitespace around |
||
| 2419 | * operators because it must evaluate to a single value and thus is less |
||
| 2420 | * ambiguous. |
||
| 2421 | * |
||
| 2422 | * Consider: |
||
| 2423 | * property1: 10 -5; // is two numbers, 10 and -5 |
||
| 2424 | * property2: (10 -5); // should evaluate to 5 |
||
| 2425 | */ |
||
| 2426 | protected $inParens = false; |
||
| 2427 | |||
| 2428 | // caches preg escaped literals |
||
| 2429 | protected static $literalCache = array(); |
||
| 2430 | |||
| 2431 | public function __construct($lessc, $sourceName = null) { |
||
| 2432 | $this->eatWhiteDefault = true; |
||
| 2433 | // reference to less needed for vPrefix, mPrefix, and parentSelector |
||
| 2434 | $this->lessc = $lessc; |
||
| 2435 | |||
| 2436 | $this->sourceName = $sourceName; // name used for error messages |
||
| 2437 | |||
| 2438 | $this->writeComments = false; |
||
| 2439 | |||
| 2440 | if (!self::$operatorString) { |
||
| 2441 | self::$operatorString = |
||
| 2442 | '('.implode('|', array_map(array('lessc', 'preg_quote'), |
||
| 2443 | array_keys(self::$precedence))).')'; |
||
| 2444 | |||
| 2445 | $commentSingle = lessc::preg_quote(self::$commentSingle); |
||
| 2446 | $commentMultiLeft = lessc::preg_quote(self::$commentMultiLeft); |
||
| 2447 | $commentMultiRight = lessc::preg_quote(self::$commentMultiRight); |
||
| 2448 | |||
| 2449 | self::$commentMulti = $commentMultiLeft.'.*?'.$commentMultiRight; |
||
| 2450 | self::$whitePattern = '/'.$commentSingle.'[^\n]*\s*|('.self::$commentMulti.')\s*|\s+/Ais'; |
||
| 2451 | } |
||
| 2452 | } |
||
| 2453 | |||
| 2454 | /** |
||
| 2455 | * Parse a string |
||
| 2456 | * |
||
| 2457 | * @param string $buffer String to parse |
||
| 2458 | * @throws exception |
||
| 2459 | * @return NULL|stdclass |
||
| 2460 | */ |
||
| 2461 | public function parse($buffer) { |
||
| 2462 | $this->count = 0; |
||
| 2463 | $this->line = 1; |
||
| 2464 | |||
| 2465 | $this->env = null; // block stack |
||
| 2466 | $this->buffer = $this->writeComments ? $buffer : $this->removeComments($buffer); |
||
| 2467 | $this->pushSpecialBlock("root"); |
||
| 2468 | $this->eatWhiteDefault = true; |
||
| 2469 | $this->seenComments = array(); |
||
| 2470 | |||
| 2471 | // trim whitespace on head |
||
| 2472 | // if (preg_match('/^\s+/', $this->buffer, $m)) { |
||
| 2473 | // $this->line += substr_count($m[0], "\n"); |
||
| 2474 | // $this->buffer = ltrim($this->buffer); |
||
| 2475 | // } |
||
| 2476 | $this->whitespace(); |
||
| 2477 | |||
| 2478 | // parse the entire file |
||
| 2479 | while (false !== $this->parseChunk()); |
||
| 2480 | |||
| 2481 | if ($this->count != strlen($this->buffer)) |
||
| 2482 | { |
||
| 2483 | $this->throwError('parse error count '.$this->count.' != len buffer '.strlen($this->buffer)); |
||
| 2484 | } |
||
| 2485 | |||
| 2486 | // TODO report where the block was opened |
||
| 2487 | if (!property_exists($this->env, 'parent') || !is_null($this->env->parent)) |
||
| 2488 | { |
||
| 2489 | throw new exception('parse error: unclosed block'); |
||
| 2490 | } |
||
| 2491 | |||
| 2492 | return $this->env; |
||
| 2493 | } |
||
| 2494 | |||
| 2495 | /** |
||
| 2496 | * Parse a single chunk off the head of the buffer and append it to the |
||
| 2497 | * current parse environment. |
||
| 2498 | * Returns false when the buffer is empty, or when there is an error. |
||
| 2499 | * |
||
| 2500 | * This function is called repeatedly until the entire document is |
||
| 2501 | * parsed. |
||
| 2502 | * |
||
| 2503 | * This parser is most similar to a recursive descent parser. Single |
||
| 2504 | * functions represent discrete grammatical rules for the language, and |
||
| 2505 | * they are able to capture the text that represents those rules. |
||
| 2506 | * |
||
| 2507 | * Consider the function lessc::keyword(). (all parse functions are |
||
| 2508 | * structured the same) |
||
| 2509 | * |
||
| 2510 | * The function takes a single reference argument. When calling the |
||
| 2511 | * function it will attempt to match a keyword on the head of the buffer. |
||
| 2512 | * If it is successful, it will place the keyword in the referenced |
||
| 2513 | * argument, advance the position in the buffer, and return true. If it |
||
| 2514 | * fails then it won't advance the buffer and it will return false. |
||
| 2515 | * |
||
| 2516 | * All of these parse functions are powered by lessc::match(), which behaves |
||
| 2517 | * the same way, but takes a literal regular expression. Sometimes it is |
||
| 2518 | * more convenient to use match instead of creating a new function. |
||
| 2519 | * |
||
| 2520 | * Because of the format of the functions, to parse an entire string of |
||
| 2521 | * grammatical rules, you can chain them together using &&. |
||
| 2522 | * |
||
| 2523 | * But, if some of the rules in the chain succeed before one fails, then |
||
| 2524 | * the buffer position will be left at an invalid state. In order to |
||
| 2525 | * avoid this, lessc::seek() is used to remember and set buffer positions. |
||
| 2526 | * |
||
| 2527 | * Before parsing a chain, use $s = $this->seek() to remember the current |
||
| 2528 | * position into $s. Then if a chain fails, use $this->seek($s) to |
||
| 2529 | * go back where we started. |
||
| 2530 | */ |
||
| 2531 | protected function parseChunk() { |
||
| 2532 | if (empty($this->buffer)) return false; |
||
| 2533 | $s = $this->seek(); |
||
| 2534 | |||
| 2535 | if ($this->whitespace()) { |
||
| 2536 | return true; |
||
| 2537 | } |
||
| 2538 | |||
| 2539 | // setting a property |
||
| 2540 | if ($this->keyword($key) && $this->assign() && |
||
| 2541 | $this->propertyValue($value, $key) && $this->end() |
||
| 2542 | ) { |
||
| 2543 | $this->append(array('assign', $key, $value), $s); |
||
| 2544 | return true; |
||
| 2545 | } else { |
||
| 2546 | $this->seek($s); |
||
| 2547 | } |
||
| 2548 | |||
| 2549 | |||
| 2550 | // look for special css blocks |
||
| 2551 | if ($this->literal('@', false)) { |
||
| 2552 | $this->count--; |
||
| 2553 | |||
| 2554 | // media |
||
| 2555 | if ($this->literal('@media')) { |
||
| 2556 | if (($this->mediaQueryList($mediaQueries) || true) |
||
| 2557 | && $this->literal('{') |
||
| 2558 | ) { |
||
| 2559 | $media = $this->pushSpecialBlock("media"); |
||
| 2560 | $media->queries = is_null($mediaQueries) ? array() : $mediaQueries; |
||
| 2561 | return true; |
||
| 2562 | } else { |
||
| 2563 | $this->seek($s); |
||
| 2564 | return false; |
||
| 2565 | } |
||
| 2566 | } |
||
| 2567 | |||
| 2568 | if ($this->literal("@", false) && $this->keyword($dirName)) { |
||
| 2569 | if ($this->isDirective($dirName, $this->blockDirectives)) { |
||
| 2570 | if (($this->openString("{", $dirValue, null, array(";")) || true) && |
||
| 2571 | $this->literal("{") |
||
| 2572 | ) { |
||
| 2573 | $dir = $this->pushSpecialBlock("directive"); |
||
| 2574 | $dir->name = $dirName; |
||
| 2575 | if (isset($dirValue)) $dir->value = $dirValue; |
||
| 2576 | return true; |
||
| 2577 | } |
||
| 2578 | } elseif ($this->isDirective($dirName, $this->lineDirectives)) { |
||
| 2579 | if ($this->propertyValue($dirValue) && $this->end()) { |
||
| 2580 | $this->append(array("directive", $dirName, $dirValue)); |
||
| 2581 | return true; |
||
| 2582 | } |
||
| 2583 | } |
||
| 2584 | } |
||
| 2585 | |||
| 2586 | $this->seek($s); |
||
| 2587 | } |
||
| 2588 | |||
| 2589 | // setting a variable |
||
| 2590 | if ($this->variable($var) && $this->assign() && |
||
| 2591 | $this->propertyValue($value) && $this->end() |
||
| 2592 | ) { |
||
| 2593 | $this->append(array('assign', $var, $value), $s); |
||
| 2594 | return true; |
||
| 2595 | } else { |
||
| 2596 | $this->seek($s); |
||
| 2597 | } |
||
| 2598 | |||
| 2599 | if ($this->import($importValue)) { |
||
| 2600 | $this->append($importValue, $s); |
||
| 2601 | return true; |
||
| 2602 | } |
||
| 2603 | |||
| 2604 | // opening parametric mixin |
||
| 2605 | if ($this->tag($tag, true) && $this->argumentDef($args, $isVararg) && |
||
| 2606 | ($this->guards($guards) || true) && |
||
| 2607 | $this->literal('{') |
||
| 2608 | ) { |
||
| 2609 | $block = $this->pushBlock($this->fixTags(array($tag))); |
||
| 2610 | $block->args = $args; |
||
| 2611 | $block->isVararg = $isVararg; |
||
| 2612 | if (!empty($guards)) $block->guards = $guards; |
||
| 2613 | return true; |
||
| 2614 | } else { |
||
| 2615 | $this->seek($s); |
||
| 2616 | } |
||
| 2617 | |||
| 2618 | // opening a simple block |
||
| 2619 | if ($this->tags($tags) && $this->literal('{', false)) { |
||
| 2620 | $tags = $this->fixTags($tags); |
||
| 2621 | $this->pushBlock($tags); |
||
| 2622 | return true; |
||
| 2623 | } else { |
||
| 2624 | $this->seek($s); |
||
| 2625 | } |
||
| 2626 | |||
| 2627 | // closing a block |
||
| 2628 | if ($this->literal('}', false)) { |
||
| 2629 | try { |
||
| 2630 | $block = $this->pop(); |
||
| 2631 | } catch (exception $e) { |
||
| 2632 | $this->seek($s); |
||
| 2633 | $this->throwError($e->getMessage()); |
||
| 2634 | } |
||
| 2635 | |||
| 2636 | $hidden = false; |
||
| 2637 | if (is_null($block->type)) { |
||
| 2638 | $hidden = true; |
||
| 2639 | if (!isset($block->args)) { |
||
| 2640 | foreach ($block->tags as $tag) { |
||
| 2641 | if (!is_string($tag) || $tag[0] != $this->lessc->mPrefix) { |
||
| 2642 | $hidden = false; |
||
| 2643 | break; |
||
| 2644 | } |
||
| 2645 | } |
||
| 2646 | } |
||
| 2647 | |||
| 2648 | foreach ($block->tags as $tag) { |
||
| 2649 | if (is_string($tag)) { |
||
| 2650 | $this->env->children[$tag][] = $block; |
||
| 2651 | } |
||
| 2652 | } |
||
| 2653 | } |
||
| 2654 | |||
| 2655 | if (!$hidden) { |
||
| 2656 | $this->append(array('block', $block), $s); |
||
| 2657 | } |
||
| 2658 | |||
| 2659 | // this is done here so comments aren't bundled into he block that |
||
| 2660 | // was just closed |
||
| 2661 | $this->whitespace(); |
||
| 2662 | return true; |
||
| 2663 | } |
||
| 2664 | |||
| 2665 | // mixin |
||
| 2666 | if ($this->mixinTags($tags) && |
||
| 2667 | ($this->argumentDef($argv, $isVararg) || true) && |
||
| 2668 | ($this->keyword($suffix) || true) && $this->end() |
||
| 2669 | ) { |
||
| 2670 | $tags = $this->fixTags($tags); |
||
| 2671 | $this->append(array('mixin', $tags, $argv, $suffix), $s); |
||
| 2672 | return true; |
||
| 2673 | } else { |
||
| 2674 | $this->seek($s); |
||
| 2675 | } |
||
| 2676 | |||
| 2677 | // spare ; |
||
| 2678 | if ($this->literal(';')) return true; |
||
| 2679 | |||
| 2680 | return false; // got nothing, throw error |
||
| 2681 | } |
||
| 2682 | |||
| 2683 | protected function isDirective($dirname, $directives) { |
||
| 2684 | // TODO: cache pattern in parser |
||
| 2685 | $pattern = implode("|", |
||
| 2686 | array_map(array("lessc", "preg_quote"), $directives)); |
||
| 2687 | $pattern = '/^(-[a-z-]+-)?('.$pattern.')$/i'; |
||
| 2688 | |||
| 2689 | return preg_match($pattern, $dirname); |
||
| 2690 | } |
||
| 2691 | |||
| 2692 | protected function fixTags($tags) { |
||
| 2693 | // move @ tags out of variable namespace |
||
| 2694 | foreach ($tags as &$tag) { |
||
| 2695 | if ($tag[0] == $this->lessc->vPrefix) |
||
| 2696 | $tag[0] = $this->lessc->mPrefix; |
||
| 2697 | } |
||
| 2698 | return $tags; |
||
| 2699 | } |
||
| 2700 | |||
| 2701 | // a list of expressions |
||
| 2702 | protected function expressionList(&$exps) { |
||
| 2703 | $values = array(); |
||
| 2704 | |||
| 2705 | while ($this->expression($exp)) { |
||
| 2706 | $values[] = $exp; |
||
| 2707 | } |
||
| 2708 | |||
| 2709 | if (count($values) == 0) return false; |
||
| 2710 | |||
| 2711 | $exps = lessc::compressList($values, ' '); |
||
| 2712 | return true; |
||
| 2713 | } |
||
| 2714 | |||
| 2715 | /** |
||
| 2716 | * Attempt to consume an expression. |
||
| 2717 | * @link http://en.wikipedia.org/wiki/Operator-precedence_parser#Pseudo-code |
||
| 2718 | */ |
||
| 2719 | protected function expression(&$out) { |
||
| 2720 | if ($this->value($lhs)) { |
||
| 2721 | $out = $this->expHelper($lhs, 0); |
||
| 2722 | |||
| 2723 | // look for / shorthand |
||
| 2724 | if (!empty($this->env->supressedDivision)) { |
||
| 2725 | unset($this->env->supressedDivision); |
||
| 2726 | $s = $this->seek(); |
||
| 2727 | if ($this->literal("/") && $this->value($rhs)) { |
||
| 2728 | $out = array("list", "", |
||
| 2729 | array($out, array("keyword", "/"), $rhs)); |
||
| 2730 | } else { |
||
| 2731 | $this->seek($s); |
||
| 2732 | } |
||
| 2733 | } |
||
| 2734 | |||
| 2735 | return true; |
||
| 2736 | } |
||
| 2737 | return false; |
||
| 2738 | } |
||
| 2739 | |||
| 2740 | /** |
||
| 2741 | * recursively parse infix equation with $lhs at precedence $minP |
||
| 2742 | */ |
||
| 2743 | protected function expHelper($lhs, $minP) { |
||
| 2744 | $this->inExp = true; |
||
| 2745 | $ss = $this->seek(); |
||
| 2746 | |||
| 2747 | while (true) { |
||
| 2748 | $whiteBefore = isset($this->buffer[$this->count - 1]) && |
||
| 2749 | ctype_space($this->buffer[$this->count - 1]); |
||
| 2750 | |||
| 2751 | // If there is whitespace before the operator, then we require |
||
| 2752 | // whitespace after the operator for it to be an expression |
||
| 2753 | $needWhite = $whiteBefore && !$this->inParens; |
||
| 2754 | |||
| 2755 | if ($this->match(self::$operatorString.($needWhite ? '\s' : ''), $m) && self::$precedence[$m[1]] >= $minP) { |
||
| 2756 | if (!$this->inParens && isset($this->env->currentProperty) && $m[1] == "/" && empty($this->env->supressedDivision)) { |
||
| 2757 | foreach (self::$supressDivisionProps as $pattern) { |
||
| 2758 | if (preg_match($pattern, $this->env->currentProperty)) { |
||
| 2759 | $this->env->supressedDivision = true; |
||
| 2760 | break 2; |
||
| 2761 | } |
||
| 2762 | } |
||
| 2763 | } |
||
| 2764 | |||
| 2765 | |||
| 2766 | $whiteAfter = isset($this->buffer[$this->count - 1]) && |
||
| 2767 | ctype_space($this->buffer[$this->count - 1]); |
||
| 2768 | |||
| 2769 | if (!$this->value($rhs)) break; |
||
| 2770 | |||
| 2771 | // peek for next operator to see what to do with rhs |
||
| 2772 | if ($this->peek(self::$operatorString, $next) && self::$precedence[$next[1]] > self::$precedence[$m[1]]) { |
||
| 2773 | $rhs = $this->expHelper($rhs, self::$precedence[$next[1]]); |
||
| 2774 | } |
||
| 2775 | |||
| 2776 | $lhs = array('expression', $m[1], $lhs, $rhs, $whiteBefore, $whiteAfter); |
||
| 2777 | $ss = $this->seek(); |
||
| 2778 | |||
| 2779 | continue; |
||
| 2780 | } |
||
| 2781 | |||
| 2782 | break; |
||
| 2783 | } |
||
| 2784 | |||
| 2785 | $this->seek($ss); |
||
| 2786 | |||
| 2787 | return $lhs; |
||
| 2788 | } |
||
| 2789 | |||
| 2790 | // consume a list of values for a property |
||
| 2791 | public function propertyValue(&$value, $keyName = null) { |
||
| 2792 | $values = array(); |
||
| 2793 | |||
| 2794 | if ($keyName !== null) $this->env->currentProperty = $keyName; |
||
| 2795 | |||
| 2796 | $s = null; |
||
| 2797 | while ($this->expressionList($v)) { |
||
| 2798 | $values[] = $v; |
||
| 2799 | $s = $this->seek(); |
||
| 2800 | if (!$this->literal(',')) break; |
||
| 2801 | } |
||
| 2802 | |||
| 2803 | if ($s) $this->seek($s); |
||
| 2804 | |||
| 2805 | if ($keyName !== null) unset($this->env->currentProperty); |
||
| 2806 | |||
| 2807 | if (count($values) == 0) return false; |
||
| 2808 | |||
| 2809 | $value = lessc::compressList($values, ', '); |
||
| 2810 | return true; |
||
| 2811 | } |
||
| 2812 | |||
| 2813 | protected function parenValue(&$out) { |
||
| 2814 | $s = $this->seek(); |
||
| 2815 | |||
| 2816 | // speed shortcut |
||
| 2817 | if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] != "(") { |
||
| 2818 | return false; |
||
| 2819 | } |
||
| 2820 | |||
| 2821 | $inParens = $this->inParens; |
||
| 2822 | if ($this->literal("(") && |
||
| 2823 | ($this->inParens = true) && $this->expression($exp) && |
||
| 2824 | $this->literal(")") |
||
| 2825 | ) { |
||
| 2826 | $out = $exp; |
||
| 2827 | $this->inParens = $inParens; |
||
| 2828 | return true; |
||
| 2829 | } else { |
||
| 2830 | $this->inParens = $inParens; |
||
| 2831 | $this->seek($s); |
||
| 2832 | } |
||
| 2833 | |||
| 2834 | return false; |
||
| 2835 | } |
||
| 2836 | |||
| 2837 | // a single value |
||
| 2838 | protected function value(&$value) { |
||
| 2839 | $s = $this->seek(); |
||
| 2840 | |||
| 2841 | // speed shortcut |
||
| 2842 | if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] == "-") { |
||
| 2843 | // negation |
||
| 2844 | if ($this->literal("-", false) && |
||
| 2845 | (($this->variable($inner) && $inner = array("variable", $inner)) || |
||
| 2846 | $this->unit($inner) || |
||
| 2847 | $this->parenValue($inner)) |
||
| 2848 | ) { |
||
| 2849 | $value = array("unary", "-", $inner); |
||
| 2850 | return true; |
||
| 2851 | } else { |
||
| 2852 | $this->seek($s); |
||
| 2853 | } |
||
| 2854 | } |
||
| 2855 | |||
| 2856 | if ($this->parenValue($value)) return true; |
||
| 2857 | if ($this->unit($value)) return true; |
||
| 2858 | if ($this->color($value)) return true; |
||
| 2859 | if ($this->func($value)) return true; |
||
| 2860 | if ($this->string($value)) return true; |
||
| 2861 | |||
| 2862 | if ($this->keyword($word)) { |
||
| 2863 | $value = array('keyword', $word); |
||
| 2864 | return true; |
||
| 2865 | } |
||
| 2866 | |||
| 2867 | // try a variable |
||
| 2868 | if ($this->variable($var)) { |
||
| 2869 | $value = array('variable', $var); |
||
| 2870 | return true; |
||
| 2871 | } |
||
| 2872 | |||
| 2873 | // unquote string (should this work on any type? |
||
| 2874 | if ($this->literal("~") && $this->string($str)) { |
||
| 2875 | $value = array("escape", $str); |
||
| 2876 | return true; |
||
| 2877 | } else { |
||
| 2878 | $this->seek($s); |
||
| 2879 | } |
||
| 2880 | |||
| 2881 | // css hack: \0 |
||
| 2882 | if ($this->literal('\\') && $this->match('([0-9]+)', $m)) { |
||
| 2883 | $value = array('keyword', '\\'.$m[1]); |
||
| 2884 | return true; |
||
| 2885 | } else { |
||
| 2886 | $this->seek($s); |
||
| 2887 | } |
||
| 2888 | |||
| 2889 | return false; |
||
| 2890 | } |
||
| 2891 | |||
| 2892 | // an import statement |
||
| 2893 | protected function import(&$out) { |
||
| 2894 | if (!$this->literal('@import')) return false; |
||
| 2895 | |||
| 2896 | // @import "something.css" media; |
||
| 2897 | // @import url("something.css") media; |
||
| 2898 | // @import url(something.css) media; |
||
| 2899 | |||
| 2900 | if ($this->propertyValue($value)) { |
||
| 2901 | $out = array("import", $value); |
||
| 2902 | return true; |
||
| 2903 | } |
||
| 2904 | } |
||
| 2905 | |||
| 2906 | protected function mediaQueryList(&$out) { |
||
| 2907 | if ($this->genericList($list, "mediaQuery", ",", false)) { |
||
| 2908 | $out = $list[2]; |
||
| 2909 | return true; |
||
| 2910 | } |
||
| 2911 | return false; |
||
| 2912 | } |
||
| 2913 | |||
| 2914 | protected function mediaQuery(&$out) { |
||
| 2915 | $s = $this->seek(); |
||
| 2916 | |||
| 2917 | $expressions = null; |
||
| 2918 | $parts = array(); |
||
| 2919 | |||
| 2920 | if (($this->literal("only") && ($only = true) || $this->literal("not") && ($not = true) || true) && $this->keyword($mediaType)) { |
||
| 2921 | $prop = array("mediaType"); |
||
| 2922 | if (isset($only)) $prop[] = "only"; |
||
| 2923 | if (isset($not)) $prop[] = "not"; |
||
| 2924 | $prop[] = $mediaType; |
||
| 2925 | $parts[] = $prop; |
||
| 2926 | } else { |
||
| 2927 | $this->seek($s); |
||
| 2928 | } |
||
| 2929 | |||
| 2930 | |||
| 2931 | if (!empty($mediaType) && !$this->literal("and")) { |
||
| 2932 | // ~ |
||
| 2933 | } else { |
||
| 2934 | $this->genericList($expressions, "mediaExpression", "and", false); |
||
| 2935 | if (is_array($expressions)) $parts = array_merge($parts, $expressions[2]); |
||
| 2936 | } |
||
| 2937 | |||
| 2938 | if (count($parts) == 0) { |
||
| 2939 | $this->seek($s); |
||
| 2940 | return false; |
||
| 2941 | } |
||
| 2942 | |||
| 2943 | $out = $parts; |
||
| 2944 | return true; |
||
| 2945 | } |
||
| 2946 | |||
| 2947 | protected function mediaExpression(&$out) { |
||
| 2948 | $s = $this->seek(); |
||
| 2949 | $value = null; |
||
| 2950 | if ($this->literal("(") && |
||
| 2951 | $this->keyword($feature) && |
||
| 2952 | ($this->literal(":") && $this->expression($value) || true) && |
||
| 2953 | $this->literal(")") |
||
| 2954 | ) { |
||
| 2955 | $out = array("mediaExp", $feature); |
||
| 2956 | if ($value) $out[] = $value; |
||
| 2957 | return true; |
||
| 2958 | } elseif ($this->variable($variable)) { |
||
| 2959 | $out = array('variable', $variable); |
||
| 2960 | return true; |
||
| 2961 | } |
||
| 2962 | |||
| 2963 | $this->seek($s); |
||
| 2964 | return false; |
||
| 2965 | } |
||
| 2966 | |||
| 2967 | // an unbounded string stopped by $end |
||
| 2968 | protected function openString($end, &$out, $nestingOpen = null, $rejectStrs = null) { |
||
| 2969 | $oldWhite = $this->eatWhiteDefault; |
||
| 2970 | $this->eatWhiteDefault = false; |
||
| 2971 | |||
| 2972 | $stop = array("'", '"', "@{", $end); |
||
| 2973 | $stop = array_map(array("lessc", "preg_quote"), $stop); |
||
| 2974 | // $stop[] = self::$commentMulti; |
||
| 2975 | |||
| 2976 | if (!is_null($rejectStrs)) { |
||
| 2977 | $stop = array_merge($stop, $rejectStrs); |
||
| 2978 | } |
||
| 2979 | |||
| 2980 | $patt = '(.*?)('.implode("|", $stop).')'; |
||
| 2981 | |||
| 2982 | $nestingLevel = 0; |
||
| 2983 | |||
| 2984 | $content = array(); |
||
| 2985 | while ($this->match($patt, $m, false)) { |
||
| 2986 | if (!empty($m[1])) { |
||
| 2987 | $content[] = $m[1]; |
||
| 2988 | if ($nestingOpen) { |
||
| 2989 | $nestingLevel += substr_count($m[1], $nestingOpen); |
||
| 2990 | } |
||
| 2991 | } |
||
| 2992 | |||
| 2993 | $tok = $m[2]; |
||
| 2994 | |||
| 2995 | $this->count -= strlen($tok); |
||
| 2996 | if ($tok == $end) { |
||
| 2997 | if ($nestingLevel == 0) { |
||
| 2998 | break; |
||
| 2999 | } else { |
||
| 3000 | $nestingLevel--; |
||
| 3001 | } |
||
| 3002 | } |
||
| 3003 | |||
| 3004 | if (($tok == "'" || $tok == '"') && $this->string($str)) { |
||
| 3005 | $content[] = $str; |
||
| 3006 | continue; |
||
| 3007 | } |
||
| 3008 | |||
| 3009 | if ($tok == "@{" && $this->interpolation($inter)) { |
||
| 3010 | $content[] = $inter; |
||
| 3011 | continue; |
||
| 3012 | } |
||
| 3013 | |||
| 3014 | if (!empty($rejectStrs) && in_array($tok, $rejectStrs)) { |
||
| 3015 | break; |
||
| 3016 | } |
||
| 3017 | |||
| 3018 | $content[] = $tok; |
||
| 3019 | $this->count += strlen($tok); |
||
| 3020 | } |
||
| 3021 | |||
| 3022 | $this->eatWhiteDefault = $oldWhite; |
||
| 3023 | |||
| 3024 | if (count($content) == 0) return false; |
||
| 3025 | |||
| 3026 | // trim the end |
||
| 3027 | if (is_string(end($content))) { |
||
| 3028 | $content[count($content) - 1] = rtrim(end($content)); |
||
| 3029 | } |
||
| 3030 | |||
| 3031 | $out = array("string", "", $content); |
||
| 3032 | return true; |
||
| 3033 | } |
||
| 3034 | |||
| 3035 | protected function string(&$out) { |
||
| 3036 | $s = $this->seek(); |
||
| 3037 | if ($this->literal('"', false)) { |
||
| 3038 | $delim = '"'; |
||
| 3039 | } elseif ($this->literal("'", false)) { |
||
| 3040 | $delim = "'"; |
||
| 3041 | } else { |
||
| 3042 | return false; |
||
| 3043 | } |
||
| 3044 | |||
| 3045 | $content = array(); |
||
| 3046 | |||
| 3047 | // look for either ending delim , escape, or string interpolation |
||
| 3048 | $patt = '([^\n]*?)(@\{|\\\\|'. |
||
| 3049 | lessc::preg_quote($delim).')'; |
||
| 3050 | |||
| 3051 | $oldWhite = $this->eatWhiteDefault; |
||
| 3052 | $this->eatWhiteDefault = false; |
||
| 3053 | |||
| 3054 | while ($this->match($patt, $m, false)) { |
||
| 3055 | $content[] = $m[1]; |
||
| 3056 | if ($m[2] == "@{") { |
||
| 3057 | $this->count -= strlen($m[2]); |
||
| 3058 | if ($this->interpolation($inter)) { |
||
| 3059 | $content[] = $inter; |
||
| 3060 | } else { |
||
| 3061 | $this->count += strlen($m[2]); |
||
| 3062 | $content[] = "@{"; // ignore it |
||
| 3063 | } |
||
| 3064 | } elseif ($m[2] == '\\') { |
||
| 3065 | $content[] = $m[2]; |
||
| 3066 | if ($this->literal($delim, false)) { |
||
| 3067 | $content[] = $delim; |
||
| 3068 | } |
||
| 3069 | } else { |
||
| 3070 | $this->count -= strlen($delim); |
||
| 3071 | break; // delim |
||
| 3072 | } |
||
| 3073 | } |
||
| 3074 | |||
| 3075 | $this->eatWhiteDefault = $oldWhite; |
||
| 3076 | |||
| 3077 | if ($this->literal($delim)) { |
||
| 3078 | $out = array("string", $delim, $content); |
||
| 3079 | return true; |
||
| 3080 | } |
||
| 3081 | |||
| 3082 | $this->seek($s); |
||
| 3083 | return false; |
||
| 3084 | } |
||
| 3085 | |||
| 3086 | protected function interpolation(&$out) { |
||
| 3087 | $oldWhite = $this->eatWhiteDefault; |
||
| 3088 | $this->eatWhiteDefault = true; |
||
| 3089 | |||
| 3090 | $s = $this->seek(); |
||
| 3091 | if ($this->literal("@{") && |
||
| 3092 | $this->openString("}", $interp, null, array("'", '"', ";")) && |
||
| 3093 | $this->literal("}", false) |
||
| 3094 | ) { |
||
| 3095 | $out = array("interpolate", $interp); |
||
| 3096 | $this->eatWhiteDefault = $oldWhite; |
||
| 3097 | if ($this->eatWhiteDefault) $this->whitespace(); |
||
| 3098 | return true; |
||
| 3099 | } |
||
| 3100 | |||
| 3101 | $this->eatWhiteDefault = $oldWhite; |
||
| 3102 | $this->seek($s); |
||
| 3103 | return false; |
||
| 3104 | } |
||
| 3105 | |||
| 3106 | protected function unit(&$unit) { |
||
| 3107 | // speed shortcut |
||
| 3108 | if (isset($this->buffer[$this->count])) { |
||
| 3109 | $char = $this->buffer[$this->count]; |
||
| 3110 | if (!ctype_digit($char) && $char != ".") return false; |
||
| 3111 | } |
||
| 3112 | |||
| 3113 | if ($this->match('([0-9]+(?:\.[0-9]*)?|\.[0-9]+)([%a-zA-Z]+)?', $m)) { |
||
| 3114 | $unit = array("number", $m[1], empty($m[2]) ? "" : $m[2]); |
||
| 3115 | return true; |
||
| 3116 | } |
||
| 3117 | return false; |
||
| 3118 | } |
||
| 3119 | |||
| 3120 | // a # color |
||
| 3121 | protected function color(&$out) { |
||
| 3122 | if ($this->match('(#(?:[0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{3}))', $m)) { |
||
| 3123 | if (strlen($m[1]) > 7) { |
||
| 3124 | $out = array("string", "", array($m[1])); |
||
| 3125 | } else { |
||
| 3126 | $out = array("raw_color", $m[1]); |
||
| 3127 | } |
||
| 3128 | return true; |
||
| 3129 | } |
||
| 3130 | |||
| 3131 | return false; |
||
| 3132 | } |
||
| 3133 | |||
| 3134 | // consume an argument definition list surrounded by () |
||
| 3135 | // each argument is a variable name with optional value |
||
| 3136 | // or at the end a ... or a variable named followed by ... |
||
| 3137 | // arguments are separated by , unless a ; is in the list, then ; is the |
||
| 3138 | // delimiter. |
||
| 3139 | protected function argumentDef(&$args, &$isVararg) { |
||
| 3140 | $s = $this->seek(); |
||
| 3141 | if (!$this->literal('(')) { |
||
| 3142 | return false; |
||
| 3143 | } |
||
| 3144 | |||
| 3145 | $values = array(); |
||
| 3146 | $delim = ","; |
||
| 3147 | $method = "expressionList"; |
||
| 3148 | |||
| 3149 | $isVararg = false; |
||
| 3150 | while (true) { |
||
| 3151 | if ($this->literal("...")) { |
||
| 3152 | $isVararg = true; |
||
| 3153 | break; |
||
| 3154 | } |
||
| 3155 | |||
| 3156 | if ($this->$method($value)) { |
||
| 3157 | if ($value[0] == "variable") { |
||
| 3158 | $arg = array("arg", $value[1]); |
||
| 3159 | $ss = $this->seek(); |
||
| 3160 | |||
| 3161 | if ($this->assign() && $this->$method($rhs)) { |
||
| 3162 | $arg[] = $rhs; |
||
| 3163 | } else { |
||
| 3164 | $this->seek($ss); |
||
| 3165 | if ($this->literal("...")) { |
||
| 3166 | $arg[0] = "rest"; |
||
| 3167 | $isVararg = true; |
||
| 3168 | } |
||
| 3169 | } |
||
| 3170 | |||
| 3171 | $values[] = $arg; |
||
| 3172 | if ($isVararg) { |
||
| 3173 | break; |
||
| 3174 | } |
||
| 3175 | continue; |
||
| 3176 | } else { |
||
| 3177 | $values[] = array("lit", $value); |
||
| 3178 | } |
||
| 3179 | } |
||
| 3180 | |||
| 3181 | |||
| 3182 | if (!$this->literal($delim)) { |
||
| 3183 | if ($delim == "," && $this->literal(";")) { |
||
| 3184 | // found new delim, convert existing args |
||
| 3185 | $delim = ";"; |
||
| 3186 | $method = "propertyValue"; |
||
| 3187 | |||
| 3188 | // transform arg list |
||
| 3189 | if (isset($values[1])) { // 2 items |
||
| 3190 | $newList = array(); |
||
| 3191 | foreach ($values as $i => $arg) { |
||
| 3192 | switch ($arg[0]) { |
||
| 3193 | case "arg": |
||
| 3194 | if ($i) { |
||
| 3195 | $this->throwError("Cannot mix ; and , as delimiter types"); |
||
| 3196 | } |
||
| 3197 | $newList[] = $arg[2]; |
||
| 3198 | break; |
||
| 3199 | case "lit": |
||
| 3200 | $newList[] = $arg[1]; |
||
| 3201 | break; |
||
| 3202 | case "rest": |
||
| 3203 | $this->throwError("Unexpected rest before semicolon"); |
||
| 3204 | } |
||
| 3205 | } |
||
| 3206 | |||
| 3207 | $newList = array("list", ", ", $newList); |
||
| 3208 | |||
| 3209 | switch ($values[0][0]) { |
||
| 3210 | case "arg": |
||
| 3211 | $newArg = array("arg", $values[0][1], $newList); |
||
| 3212 | break; |
||
| 3213 | case "lit": |
||
| 3214 | $newArg = array("lit", $newList); |
||
| 3215 | break; |
||
| 3216 | } |
||
| 3217 | |||
| 3218 | } elseif ($values) { // 1 item |
||
| 3219 | $newArg = $values[0]; |
||
| 3220 | } |
||
| 3221 | |||
| 3222 | if ($newArg) { |
||
| 3223 | $values = array($newArg); |
||
| 3224 | } |
||
| 3225 | } else { |
||
| 3226 | break; |
||
| 3227 | } |
||
| 3228 | } |
||
| 3229 | } |
||
| 3230 | |||
| 3231 | if (!$this->literal(')')) { |
||
| 3232 | $this->seek($s); |
||
| 3233 | return false; |
||
| 3234 | } |
||
| 3235 | |||
| 3236 | $args = $values; |
||
| 3237 | |||
| 3238 | return true; |
||
| 3239 | } |
||
| 3240 | |||
| 3241 | // consume a list of tags |
||
| 3242 | // this accepts a hanging delimiter |
||
| 3243 | protected function tags(&$tags, $simple = false, $delim = ',') { |
||
| 3244 | $tags = array(); |
||
| 3245 | while ($this->tag($tt, $simple)) { |
||
| 3246 | $tags[] = $tt; |
||
| 3247 | if (!$this->literal($delim)) break; |
||
| 3248 | } |
||
| 3249 | if (count($tags) == 0) return false; |
||
| 3250 | |||
| 3251 | return true; |
||
| 3252 | } |
||
| 3253 | |||
| 3254 | // list of tags of specifying mixin path |
||
| 3255 | // optionally separated by > (lazy, accepts extra >) |
||
| 3256 | protected function mixinTags(&$tags) { |
||
| 3257 | $tags = array(); |
||
| 3258 | while ($this->tag($tt, true)) { |
||
| 3259 | $tags[] = $tt; |
||
| 3260 | $this->literal(">"); |
||
| 3261 | } |
||
| 3262 | |||
| 3263 | if (!$tags) { |
||
| 3264 | return false; |
||
| 3265 | } |
||
| 3266 | |||
| 3267 | return true; |
||
| 3268 | } |
||
| 3269 | |||
| 3270 | // a bracketed value (contained within in a tag definition) |
||
| 3271 | protected function tagBracket(&$parts, &$hasExpression) { |
||
| 3272 | // speed shortcut |
||
| 3273 | if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] != "[") { |
||
| 3274 | return false; |
||
| 3275 | } |
||
| 3276 | |||
| 3277 | $s = $this->seek(); |
||
| 3278 | |||
| 3279 | $hasInterpolation = false; |
||
| 3280 | |||
| 3281 | if ($this->literal("[", false)) { |
||
| 3282 | $attrParts = array("["); |
||
| 3283 | // keyword, string, operator |
||
| 3284 | while (true) { |
||
| 3285 | if ($this->literal("]", false)) { |
||
| 3286 | $this->count--; |
||
| 3287 | break; // get out early |
||
| 3288 | } |
||
| 3289 | |||
| 3290 | if ($this->match('\s+', $m)) { |
||
| 3291 | $attrParts[] = " "; |
||
| 3292 | continue; |
||
| 3293 | } |
||
| 3294 | if ($this->string($str)) { |
||
| 3295 | // escape parent selector, (yuck) |
||
| 3296 | foreach ($str[2] as &$chunk) { |
||
| 3297 | $chunk = str_replace($this->lessc->parentSelector, "$&$", $chunk); |
||
| 3298 | } |
||
| 3299 | |||
| 3300 | $attrParts[] = $str; |
||
| 3301 | $hasInterpolation = true; |
||
| 3302 | continue; |
||
| 3303 | } |
||
| 3304 | |||
| 3305 | if ($this->keyword($word)) { |
||
| 3306 | $attrParts[] = $word; |
||
| 3307 | continue; |
||
| 3308 | } |
||
| 3309 | |||
| 3310 | if ($this->interpolation($inter)) { |
||
| 3311 | $attrParts[] = $inter; |
||
| 3312 | $hasInterpolation = true; |
||
| 3313 | continue; |
||
| 3314 | } |
||
| 3315 | |||
| 3316 | // operator, handles attr namespace too |
||
| 3317 | if ($this->match('[|-~\$\*\^=]+', $m)) { |
||
| 3318 | $attrParts[] = $m[0]; |
||
| 3319 | continue; |
||
| 3320 | } |
||
| 3321 | |||
| 3322 | break; |
||
| 3323 | } |
||
| 3324 | |||
| 3325 | if ($this->literal("]", false)) { |
||
| 3326 | $attrParts[] = "]"; |
||
| 3327 | foreach ($attrParts as $part) { |
||
| 3328 | $parts[] = $part; |
||
| 3329 | } |
||
| 3330 | $hasExpression = $hasExpression || $hasInterpolation; |
||
| 3331 | return true; |
||
| 3332 | } |
||
| 3333 | $this->seek($s); |
||
| 3334 | } |
||
| 3335 | |||
| 3336 | $this->seek($s); |
||
| 3337 | return false; |
||
| 3338 | } |
||
| 3339 | |||
| 3340 | // a space separated list of selectors |
||
| 3341 | protected function tag(&$tag, $simple = false) { |
||
| 3342 | if ($simple) { |
||
| 3343 | $chars = '^@,:;{}\][>\(\) "\''; |
||
| 3344 | } else { |
||
| 3345 | $chars = '^@,;{}["\''; |
||
| 3346 | } |
||
| 3347 | $s = $this->seek(); |
||
| 3348 | |||
| 3349 | $hasExpression = false; |
||
| 3350 | $parts = array(); |
||
| 3351 | while ($this->tagBracket($parts, $hasExpression)); |
||
| 3352 | |||
| 3353 | $oldWhite = $this->eatWhiteDefault; |
||
| 3354 | $this->eatWhiteDefault = false; |
||
| 3355 | |||
| 3356 | while (true) { |
||
| 3357 | if ($this->match('(['.$chars.'0-9]['.$chars.']*)', $m)) { |
||
| 3358 | $parts[] = $m[1]; |
||
| 3359 | if ($simple) break; |
||
| 3360 | |||
| 3361 | while ($this->tagBracket($parts, $hasExpression)); |
||
| 3362 | continue; |
||
| 3363 | } |
||
| 3364 | |||
| 3365 | if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] == "@") { |
||
| 3366 | if ($this->interpolation($interp)) { |
||
| 3367 | $hasExpression = true; |
||
| 3368 | $interp[2] = true; // don't unescape |
||
| 3369 | $parts[] = $interp; |
||
| 3370 | continue; |
||
| 3371 | } |
||
| 3372 | |||
| 3373 | if ($this->literal("@")) { |
||
| 3374 | $parts[] = "@"; |
||
| 3375 | continue; |
||
| 3376 | } |
||
| 3377 | } |
||
| 3378 | |||
| 3379 | if ($this->unit($unit)) { // for keyframes |
||
| 3380 | $parts[] = $unit[1]; |
||
| 3381 | $parts[] = $unit[2]; |
||
| 3382 | continue; |
||
| 3383 | } |
||
| 3384 | |||
| 3385 | break; |
||
| 3386 | } |
||
| 3387 | |||
| 3388 | $this->eatWhiteDefault = $oldWhite; |
||
| 3389 | if (!$parts) { |
||
| 3390 | $this->seek($s); |
||
| 3391 | return false; |
||
| 3392 | } |
||
| 3393 | |||
| 3394 | if ($hasExpression) { |
||
| 3395 | $tag = array("exp", array("string", "", $parts)); |
||
| 3396 | } else { |
||
| 3397 | $tag = trim(implode($parts)); |
||
| 3398 | } |
||
| 3399 | |||
| 3400 | $this->whitespace(); |
||
| 3401 | return true; |
||
| 3402 | } |
||
| 3403 | |||
| 3404 | // a css function |
||
| 3405 | protected function func(&$func) { |
||
| 3406 | $s = $this->seek(); |
||
| 3407 | |||
| 3408 | if ($this->match('(%|[\w\-_][\w\-_:\.]+|[\w_])', $m) && $this->literal('(')) { |
||
| 3409 | $fname = $m[1]; |
||
| 3410 | |||
| 3411 | $sPreArgs = $this->seek(); |
||
| 3412 | |||
| 3413 | $args = array(); |
||
| 3414 | while (true) { |
||
| 3415 | $ss = $this->seek(); |
||
| 3416 | // this ugly nonsense is for ie filter properties |
||
| 3417 | if ($this->keyword($name) && $this->literal('=') && $this->expressionList($value)) { |
||
| 3418 | $args[] = array("string", "", array($name, "=", $value)); |
||
| 3419 | } else { |
||
| 3420 | $this->seek($ss); |
||
| 3421 | if ($this->expressionList($value)) { |
||
| 3422 | $args[] = $value; |
||
| 3423 | } |
||
| 3424 | } |
||
| 3425 | |||
| 3426 | if (!$this->literal(',')) break; |
||
| 3427 | } |
||
| 3428 | $args = array('list', ',', $args); |
||
| 3429 | |||
| 3430 | if ($this->literal(')')) { |
||
| 3431 | $func = array('function', $fname, $args); |
||
| 3432 | return true; |
||
| 3433 | } elseif ($fname == 'url') { |
||
| 3434 | // couldn't parse and in url? treat as string |
||
| 3435 | $this->seek($sPreArgs); |
||
| 3436 | if ($this->openString(")", $string) && $this->literal(")")) { |
||
| 3437 | $func = array('function', $fname, $string); |
||
| 3438 | return true; |
||
| 3439 | } |
||
| 3440 | } |
||
| 3441 | } |
||
| 3442 | |||
| 3443 | $this->seek($s); |
||
| 3444 | return false; |
||
| 3445 | } |
||
| 3446 | |||
| 3447 | // consume a less variable |
||
| 3448 | protected function variable(&$name) { |
||
| 3449 | $s = $this->seek(); |
||
| 3450 | if ($this->literal($this->lessc->vPrefix, false) && |
||
| 3451 | ($this->variable($sub) || $this->keyword($name)) |
||
| 3452 | ) { |
||
| 3453 | if (!empty($sub)) { |
||
| 3454 | $name = array('variable', $sub); |
||
| 3455 | } else { |
||
| 3456 | $name = $this->lessc->vPrefix.$name; |
||
| 3457 | } |
||
| 3458 | return true; |
||
| 3459 | } |
||
| 3460 | |||
| 3461 | $name = null; |
||
| 3462 | $this->seek($s); |
||
| 3463 | return false; |
||
| 3464 | } |
||
| 3465 | |||
| 3466 | /** |
||
| 3467 | * Consume an assignment operator |
||
| 3468 | * Can optionally take a name that will be set to the current property name |
||
| 3469 | */ |
||
| 3470 | protected function assign($name = null) { |
||
| 3471 | if ($name) $this->currentProperty = $name; |
||
| 3472 | return $this->literal(':') || $this->literal('='); |
||
| 3473 | } |
||
| 3474 | |||
| 3475 | // consume a keyword |
||
| 3476 | protected function keyword(&$word) { |
||
| 3477 | if ($this->match('([\w_\-\*!"][\w\-_"]*)', $m)) { |
||
| 3478 | $word = $m[1]; |
||
| 3479 | return true; |
||
| 3480 | } |
||
| 3481 | return false; |
||
| 3482 | } |
||
| 3483 | |||
| 3484 | // consume an end of statement delimiter |
||
| 3485 | protected function end() { |
||
| 3486 | if ($this->literal(';', false)) { |
||
| 3487 | return true; |
||
| 3488 | } elseif ($this->count == strlen($this->buffer) || $this->buffer[$this->count] == '}') { |
||
| 3489 | // if there is end of file or a closing block next then we don't need a ; |
||
| 3490 | return true; |
||
| 3491 | } |
||
| 3492 | return false; |
||
| 3493 | } |
||
| 3494 | |||
| 3495 | protected function guards(&$guards) { |
||
| 3496 | $s = $this->seek(); |
||
| 3497 | |||
| 3498 | if (!$this->literal("when")) { |
||
| 3499 | $this->seek($s); |
||
| 3500 | return false; |
||
| 3501 | } |
||
| 3502 | |||
| 3503 | $guards = array(); |
||
| 3504 | |||
| 3505 | while ($this->guardGroup($g)) { |
||
| 3506 | $guards[] = $g; |
||
| 3507 | if (!$this->literal(",")) break; |
||
| 3508 | } |
||
| 3509 | |||
| 3510 | if (count($guards) == 0) { |
||
| 3511 | $guards = null; |
||
| 3512 | $this->seek($s); |
||
| 3513 | return false; |
||
| 3514 | } |
||
| 3515 | |||
| 3516 | return true; |
||
| 3517 | } |
||
| 3518 | |||
| 3519 | // a bunch of guards that are and'd together |
||
| 3520 | // TODO rename to guardGroup |
||
| 3521 | protected function guardGroup(&$guardGroup) { |
||
| 3536 | } |
||
| 3537 | |||
| 3538 | protected function guard(&$guard) { |
||
| 3539 | $s = $this->seek(); |
||
| 3540 | $negate = $this->literal("not"); |
||
| 3541 | |||
| 3542 | if ($this->literal("(") && $this->expression($exp) && $this->literal(")")) { |
||
| 3543 | $guard = $exp; |
||
| 3544 | if ($negate) $guard = array("negate", $guard); |
||
| 3545 | return true; |
||
| 3546 | } |
||
| 3547 | |||
| 3548 | $this->seek($s); |
||
| 3549 | return false; |
||
| 3550 | } |
||
| 3551 | |||
| 3552 | /* raw parsing functions */ |
||
| 3553 | |||
| 3554 | protected function literal($what, $eatWhitespace = null) { |
||
| 3555 | if ($eatWhitespace === null) $eatWhitespace = $this->eatWhiteDefault; |
||
| 3556 | |||
| 3557 | // shortcut on single letter |
||
| 3558 | if (!isset($what[1]) && isset($this->buffer[$this->count])) { |
||
| 3559 | if ($this->buffer[$this->count] == $what) { |
||
| 3560 | if (!$eatWhitespace) { |
||
| 3561 | $this->count++; |
||
| 3562 | return true; |
||
| 3563 | } |
||
| 3564 | // goes below... |
||
| 3565 | } else { |
||
| 3566 | return false; |
||
| 3567 | } |
||
| 3568 | } |
||
| 3569 | |||
| 3570 | if (!isset(self::$literalCache[$what])) { |
||
| 3571 | self::$literalCache[$what] = lessc::preg_quote($what); |
||
| 3572 | } |
||
| 3573 | |||
| 3574 | return $this->match(self::$literalCache[$what], $m, $eatWhitespace); |
||
| 3575 | } |
||
| 3576 | |||
| 3577 | protected function genericList(&$out, $parseItem, $delim = "", $flatten = true) { |
||
| 3578 | $s = $this->seek(); |
||
| 3579 | $items = array(); |
||
| 3580 | while ($this->$parseItem($value)) { |
||
| 3581 | $items[] = $value; |
||
| 3582 | if ($delim) { |
||
| 3583 | if (!$this->literal($delim)) break; |
||
| 3584 | } |
||
| 3585 | } |
||
| 3586 | |||
| 3587 | if (count($items) == 0) { |
||
| 3588 | $this->seek($s); |
||
| 3589 | return false; |
||
| 3590 | } |
||
| 3591 | |||
| 3592 | if ($flatten && count($items) == 1) { |
||
| 3593 | $out = $items[0]; |
||
| 3594 | } else { |
||
| 3595 | $out = array("list", $delim, $items); |
||
| 3596 | } |
||
| 3597 | |||
| 3598 | return true; |
||
| 3599 | } |
||
| 3600 | |||
| 3601 | |||
| 3602 | // advance counter to next occurrence of $what |
||
| 3603 | // $until - don't include $what in advance |
||
| 3604 | // $allowNewline, if string, will be used as valid char set |
||
| 3605 | protected function to($what, &$out, $until = false, $allowNewline = false) { |
||
| 3606 | if (is_string($allowNewline)) { |
||
| 3607 | $validChars = $allowNewline; |
||
| 3608 | } else { |
||
| 3609 | $validChars = $allowNewline ? "." : "[^\n]"; |
||
| 3610 | } |
||
| 3611 | if (!$this->match('('.$validChars.'*?)'.lessc::preg_quote($what), $m, !$until)) return false; |
||
| 3612 | if ($until) $this->count -= strlen($what); // give back $what |
||
| 3613 | $out = $m[1]; |
||
| 3614 | return true; |
||
| 3615 | } |
||
| 3616 | |||
| 3617 | // try to match something on head of buffer |
||
| 3618 | protected function match($regex, &$out, $eatWhitespace = null) { |
||
| 3619 | if ($eatWhitespace === null) $eatWhitespace = $this->eatWhiteDefault; |
||
| 3620 | |||
| 3621 | $r = '/'.$regex.($eatWhitespace && !$this->writeComments ? '\s*' : '').'/Ais'; |
||
| 3622 | if (preg_match($r, $this->buffer, $out, null, $this->count)) { |
||
| 3623 | $this->count += strlen($out[0]); |
||
| 3624 | if ($eatWhitespace && $this->writeComments) $this->whitespace(); |
||
| 3625 | return true; |
||
| 3626 | } |
||
| 3627 | return false; |
||
| 3628 | } |
||
| 3629 | |||
| 3630 | // match some whitespace |
||
| 3631 | protected function whitespace() { |
||
| 3632 | if ($this->writeComments) { |
||
| 3633 | $gotWhite = false; |
||
| 3634 | while (preg_match(self::$whitePattern, $this->buffer, $m, null, $this->count)) { |
||
| 3635 | if (isset($m[1]) && empty($this->seenComments[$this->count])) { |
||
| 3636 | $this->append(array("comment", $m[1])); |
||
| 3637 | $this->seenComments[$this->count] = true; |
||
| 3638 | } |
||
| 3639 | $this->count += strlen($m[0]); |
||
| 3640 | $gotWhite = true; |
||
| 3641 | } |
||
| 3642 | return $gotWhite; |
||
| 3643 | } else { |
||
| 3644 | $this->match("", $m); |
||
| 3645 | return strlen($m[0]) > 0; |
||
| 3646 | } |
||
| 3647 | } |
||
| 3648 | |||
| 3649 | // match something without consuming it |
||
| 3650 | protected function peek($regex, &$out = null, $from = null) { |
||
| 3651 | if (is_null($from)) $from = $this->count; |
||
| 3652 | $r = '/'.$regex.'/Ais'; |
||
| 3653 | $result = preg_match($r, $this->buffer, $out, null, $from); |
||
| 3654 | |||
| 3655 | return $result; |
||
| 3656 | } |
||
| 3657 | |||
| 3658 | // seek to a spot in the buffer or return where we are on no argument |
||
| 3659 | protected function seek($where = null) { |
||
| 3660 | if ($where === null) return $this->count; |
||
| 3661 | else $this->count = $where; |
||
| 3662 | return true; |
||
| 3663 | } |
||
| 3664 | |||
| 3665 | /* misc functions */ |
||
| 3666 | |||
| 3667 | public function throwError($msg = "parse error", $count = null) { |
||
| 3668 | $count = is_null($count) ? $this->count : $count; |
||
| 3669 | |||
| 3670 | $line = $this->line + |
||
| 3671 | substr_count(substr($this->buffer, 0, $count), "\n"); |
||
| 3672 | |||
| 3673 | if (!empty($this->sourceName)) { |
||
| 3674 | $loc = "$this->sourceName on line $line"; |
||
| 3675 | } else { |
||
| 3676 | $loc = "line: $line"; |
||
| 3677 | } |
||
| 3678 | |||
| 3679 | // TODO this depends on $this->count |
||
| 3680 | if ($this->peek("(.*?)(\n|$)", $m, $count)) { |
||
| 3681 | throw new exception("$msg: failed at `$m[1]` $loc"); |
||
| 3682 | } else { |
||
| 3683 | throw new exception("$msg: $loc"); |
||
| 3684 | } |
||
| 3685 | } |
||
| 3686 | |||
| 3687 | protected function pushBlock($selectors = null, $type = null) { |
||
| 3688 | $b = new stdclass; |
||
| 3689 | $b->parent = $this->env; |
||
| 3690 | |||
| 3691 | $b->type = $type; |
||
| 3692 | $b->id = self::$nextBlockId++; |
||
| 3693 | |||
| 3694 | $b->isVararg = false; // TODO: kill me from here |
||
| 3695 | $b->tags = $selectors; |
||
| 3696 | |||
| 3697 | $b->props = array(); |
||
| 3698 | $b->children = array(); |
||
| 3699 | |||
| 3700 | $this->env = $b; |
||
| 3701 | return $b; |
||
| 3702 | } |
||
| 3703 | |||
| 3704 | // push a block that doesn't multiply tags |
||
| 3705 | protected function pushSpecialBlock($type) { |
||
| 3707 | } |
||
| 3708 | |||
| 3709 | // append a property to the current block |
||
| 3710 | protected function append($prop, $pos = null) { |
||
| 3711 | if ($pos !== null) $prop[-1] = $pos; |
||
| 3712 | $this->env->props[] = $prop; |
||
| 3713 | } |
||
| 3714 | |||
| 3715 | // pop something off the stack |
||
| 3716 | protected function pop() { |
||
| 3717 | $old = $this->env; |
||
| 3718 | $this->env = $this->env->parent; |
||
| 3720 | } |
||
| 3721 | |||
| 3722 | // remove comments from $text |
||
| 3723 | // todo: make it work for all functions, not just url |
||
| 3724 | protected function removeComments($text) { |
||
| 3725 | $look = array( |
||
| 3726 | 'url(', '//', '/*', '"', "'" |
||
| 3727 | ); |
||
| 3728 | |||
| 3729 | $out = ''; |
||
| 3730 | $min = null; |
||
| 3731 | while (true) { |
||
| 3732 | // find the next item |
||
| 3733 | foreach ($look as $token) { |
||
| 3734 | $pos = strpos($text, $token); |
||
| 3735 | if ($pos !== false) { |
||
| 3736 | if (!isset($min) || $pos < $min[1]) $min = array($token, $pos); |
||
| 3737 | } |
||
| 3738 | } |
||
| 3739 | |||
| 3740 | if (is_null($min)) break; |
||
| 3741 | |||
| 3777 | } |
||
| 3778 | |||
| 3779 | } |
||
| 3780 | |||
| 3781 | class lessc_formatter_classic { |
||
| 3782 | public $indentChar = " "; |
||
| 3783 | |||
| 3784 | public $break = "\n"; |
||
| 3785 | public $open = " {"; |
||
| 3901 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.