mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-23 12:24:18 +01:00
1843 lines
56 KiB
PHP
1843 lines
56 KiB
PHP
<?php
|
||
/**
|
||
* SCSSPHP
|
||
*
|
||
* @copyright 2012-2015 Leaf Corcoran
|
||
*
|
||
* @license http://opensource.org/licenses/MIT MIT
|
||
*
|
||
* @link http://leafo.github.io/scssphp
|
||
*/
|
||
namespace Leafo\ScssPhp;
|
||
|
||
use Leafo\ScssPhp\Block;
|
||
use Leafo\ScssPhp\Compiler;
|
||
use Leafo\ScssPhp\Exception\ParserException;
|
||
use Leafo\ScssPhp\Node;
|
||
use Leafo\ScssPhp\Type;
|
||
/**
|
||
* Parser
|
||
*
|
||
* @author Leaf Corcoran <leafot@gmail.com>
|
||
*/
|
||
class Parser
|
||
{
|
||
const SOURCE_INDEX = -1;
|
||
const SOURCE_LINE = -2;
|
||
const SOURCE_COLUMN = -3;
|
||
/**
|
||
* @var array
|
||
*/
|
||
protected static $precedence = array('=' => 0, 'or' => 1, 'and' => 2, '==' => 3, '!=' => 3, '<=>' => 3, '<=' => 4, '>=' => 4, '<' => 4, '>' => 4, '+' => 5, '-' => 5, '*' => 6, '/' => 6, '%' => 6);
|
||
protected static $commentPattern;
|
||
protected static $operatorPattern;
|
||
protected static $whitePattern;
|
||
private $sourceName;
|
||
private $sourceIndex;
|
||
private $sourcePositions;
|
||
private $charset;
|
||
private $count;
|
||
private $env;
|
||
private $inParens;
|
||
private $eatWhiteDefault;
|
||
private $buffer;
|
||
private $utf8;
|
||
private $encoding;
|
||
private $patternModifiers;
|
||
/**
|
||
* Constructor
|
||
*
|
||
* @api
|
||
*
|
||
* @param string $sourceName
|
||
* @param integer $sourceIndex
|
||
* @param string $encoding
|
||
*/
|
||
public function __construct($sourceName, $sourceIndex = 0, $encoding = 'utf-8')
|
||
{
|
||
$this->sourceName = $sourceName ?: '(stdin)';
|
||
$this->sourceIndex = $sourceIndex;
|
||
$this->charset = null;
|
||
$this->utf8 = !$encoding || strtolower($encoding) === 'utf-8';
|
||
$this->patternModifiers = $this->utf8 ? 'Aisu' : 'Ais';
|
||
if (empty(self::$operatorPattern)) {
|
||
self::$operatorPattern = '([*\\/%+-]|[!=]\\=|\\>\\=?|\\<\\=\\>|\\<\\=?|and|or)';
|
||
$commentSingle = '\\/\\/';
|
||
$commentMultiLeft = '\\/\\*';
|
||
$commentMultiRight = '\\*\\/';
|
||
self::$commentPattern = $commentMultiLeft . '.*?' . $commentMultiRight;
|
||
self::$whitePattern = $this->utf8 ? '/' . $commentSingle . '[^\\n]*\\s*|(' . self::$commentPattern . ')\\s*|\\s+/AisuS' : '/' . $commentSingle . '[^\\n]*\\s*|(' . self::$commentPattern . ')\\s*|\\s+/AisS';
|
||
}
|
||
}
|
||
/**
|
||
* Get source file name
|
||
*
|
||
* @api
|
||
*
|
||
* @return string
|
||
*/
|
||
public function getSourceName()
|
||
{
|
||
return $this->sourceName;
|
||
}
|
||
/**
|
||
* Throw parser error
|
||
*
|
||
* @api
|
||
*
|
||
* @param string $msg
|
||
*
|
||
* @throws \Leafo\ScssPhp\Exception\ParserException
|
||
*/
|
||
public function throwParseError($msg = 'parse error')
|
||
{
|
||
list($line, ) = $this->getSourcePosition($this->count);
|
||
$loc = empty($this->sourceName) ? "line: {$line}" : "{$this->sourceName} on line {$line}";
|
||
if ($this->peek('(.*?)(
|
||
|$)', $m, $this->count)) {
|
||
throw new ParserException("{$msg}: failed at `{$m['1']}` {$loc}");
|
||
}
|
||
throw new ParserException("{$msg}: {$loc}");
|
||
}
|
||
/**
|
||
* Parser buffer
|
||
*
|
||
* @api
|
||
*
|
||
* @param string $buffer
|
||
*
|
||
* @return \Leafo\ScssPhp\Block
|
||
*/
|
||
public function parse($buffer)
|
||
{
|
||
$this->count = 0;
|
||
$this->env = null;
|
||
$this->inParens = false;
|
||
$this->eatWhiteDefault = true;
|
||
$this->buffer = rtrim($buffer, ' |