[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/src/compiler/expression/ -> WactExpressionFilterParser.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  /**

  11   * class WactExpressionFilterParser.

  12   *

  13   * @package wact

  14   * @version $Id$

  15   */
  16  class WactExpressionFilterParser
  17  {
  18    protected $text;
  19    protected $position;
  20    protected $length;
  21    protected $context;
  22    protected $filters = array();
  23  
  24    /**

  25    * Construct this parser

  26    */
  27    function __construct($context)
  28    {
  29      $this->context = $context;
  30    }
  31  
  32    protected function getToken($pattern)
  33    {
  34      if (preg_match($pattern, $this->text, $match, PREG_OFFSET_CAPTURE, $this->position))
  35      {
  36        $this->position += strlen($match[0][0]);
  37        return $match[1][0];
  38      }
  39      else
  40        return FALSE;
  41    }
  42  
  43    function getFilters()
  44    {
  45      return $this->filters;
  46    }
  47  
  48    /**

  49    * Parse text for expressions and emit a stream of events for expression fragments

  50    */
  51    function parse($text)
  52    {
  53      if (strlen($text) == 0)
  54        return array();
  55  
  56      $filters = array();
  57  
  58      $this->text = $text;
  59      $this->position = 0;
  60  
  61      $filters_expressions = $this->_extractFiltersExpressions();
  62  
  63      foreach($filters_expressions as $filter_expression)
  64      {
  65        $result = preg_match('/\G\s*([A-Za-z][A-Za-z0-9_.]*)/u', $filter_expression, $match, PREG_OFFSET_CAPTURE);
  66  
  67        if(!$result)
  68           $this->context->raiseCompilerError('Filter name expected');
  69  
  70        $position = $match[0][1];
  71        $filter_name = $match[1][0];
  72        $filters[$filter_name] = array('name' => $filter_name,
  73                                       'expression' => $filter_expression,
  74                                       'params' => array());
  75  
  76        $params_expression = substr($filter_expression, strlen($filter_name));
  77        if(!$params_expression)
  78          continue;
  79  
  80        if(strlen($params_expression))
  81        {
  82          $params_start_position = strpos($params_expression, ':');
  83          if(($params_start_position === FALSE))
  84            $this->context->raiseCompilerError('Unexpected symbol after filter name');
  85        }
  86  
  87        $this->text = substr($params_expression, $params_start_position+1);
  88        $length = strlen($this->text);
  89        $this->position = 0;
  90  
  91        do
  92        {
  93          $token = $this->getToken('/\G\s*("|\'|[^,]+,?)/u');
  94          if ($token === FALSE)
  95            $this->context->raiseCompilerError('Filter params expected after ":" symbol');
  96  
  97          $token = rtrim($token, ',');
  98          if ($token == '"' || $token == "'")
  99          {
 100            $string = $this->getToken('/\G([^' . $token . ']*)' . $token . ',?/u');
 101  
 102            if ($string !== FALSE)
 103              $filters[$filter_name]['params'][] = $token . $string . $token;
 104            else
 105              $this->context->raiseCompilerError("Expecting a string literal");
 106          }
 107          else
 108            $filters[$filter_name]['params'][] = trim($token);
 109  
 110        }
 111        while($this->position < $length);
 112      }
 113  
 114      return $filters;
 115    }
 116  
 117    protected function _extractFiltersExpressions()
 118    {
 119      $length = strlen($this->text);
 120      $this->position = 0;
 121  
 122      $filters_expressions = array();
 123      do
 124      {
 125        $token = $this->getToken('/\G("|\'|\||[^\'"\|]+)/u');
 126        if ($token === FALSE)
 127        {
 128          $filters_expressions[] = $this->text;
 129          break;
 130        }
 131  
 132        if ($token == '"' || $token == "'")
 133        {
 134          $string = $this->getToken('/\G([^' . $token . ']*)' . $token . ',?/u');
 135  
 136          if ($string === FALSE)
 137            $this->context->raiseCompilerError("Expecting a string literal in filter param");
 138        }
 139        elseif($token == '|')
 140        {
 141          $filters_expressions[] = substr($this->text, 0, $this->position - 1);
 142          $this->text = substr($this->text, $this->position);
 143          $length = strlen($this->text);
 144          $this->position = 0;
 145        }
 146      }
 147      while($this->position < $length);
 148  
 149      //ensures the last filter expression added

 150      $filters_expressions[] = substr($this->text, 0, $this->position );
 151      $this->text = substr($this->text, $this->position);
 152      $length = strlen($this->text);
 153      $this->position = 0;
 154  
 155      return $filters_expressions;
 156    }
 157  }
 158  ?>


Generated: Mon Dec 1 03:56:46 2008 Cross-referenced by PHPXref 0.7