[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/src/compiler/expression/ -> WactExpressionValueParser.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  require_once 'limb/wact/src/compiler/expression/node/WactTemplateExpressionNode.class.php';
  11  require_once 'limb/wact/src/compiler/expression/node/WactConstantExpressionNode.class.php';
  12  require_once 'limb/wact/src/compiler/expression/node/WactBinaryExpressionNode.class.php';
  13  require_once 'limb/wact/src/compiler/expression/node/WactParenthesisExpressionNode.class.php';
  14  require_once 'limb/wact/src/compiler/expression/node/WactUnaryExpressionNode.class.php';
  15  require_once 'limb/wact/src/compiler/expression/node/WactDataBindingExpressionNode.class.php';
  16  
  17  /**

  18   * class WactExpressionValueParser.

  19   *

  20   * @package wact

  21   * @version $Id: WactExpressionValueParser.class.php 5945 2007-06-06 08:31:43Z pachanga $

  22   */
  23  class WactExpressionValueParser
  24  {
  25    protected $text;
  26    protected $position;
  27    protected $length;
  28    protected $context;
  29  
  30    /**

  31    * Construct this parser

  32    */
  33    function __construct($context)
  34    {
  35      $this->context = $context;
  36    }
  37  
  38    function raiseError($message, $params = array())
  39    {
  40      $this->context->raiseCompilerError($message, $params);
  41    }
  42  
  43    /**

  44    */
  45    protected function getToken($pattern)
  46    {
  47      if (preg_match($pattern, $this->text, $match, PREG_OFFSET_CAPTURE, $this->position))
  48      {
  49        $this->position += strlen($match[0][0]);
  50        return $match[1][0];
  51      }
  52      else
  53        return FALSE;
  54    }
  55  
  56    /**

  57    */
  58    protected function parsePrimary()
  59    {
  60      $token = $this->getToken('/\G\s*(#|\^|\$|:|-|"|\'|!|\[|\(|[0-9]+|[A-Za-z][A-Za-z0-9_.]*)/u');
  61      if ($token === FALSE)
  62        $this->raiseError("Expecting primary operand in expression.");
  63  
  64      if ($token == '-')
  65        return new WactUnaryExpressionNode($this->parsePrimary(), '-');
  66  
  67      if ($token == '(')
  68      {
  69        $expr = $this->parseExpression();
  70        if ($this->getToken('/\G\s*(\))/u'))
  71          return new WactParenthesisExpressionNode($expr);
  72        else
  73          $this->raiseError('Expecting ) in expression');
  74      }
  75      // one of the DBE context modifier

  76      elseif($token == '^' || $token == '#' || $token == '[' || $token == ':')
  77      {
  78        if (!($token2 = $this->getToken('/\G([A-Za-z^:\[][A-Za-z0-9_.\[\]^]*)/u')))
  79          $this->raiseError("Expecting identifier after DBE modifier.");
  80  
  81        return new WactDataBindingExpressionNode($token . $token2, $this->context);
  82      }
  83      // php variable

  84      elseif($token == '$')
  85      {
  86        if (!($token2 = $this->getToken('/\G([A-Za-z][A-Za-z0-9_.\[\]^]*)/u')))
  87          $this->raiseError("Expecting variable name after \$ symbol.");
  88  
  89        return new WactDataBindingExpressionNode($token . $token2, $this->context);
  90      }
  91      // string

  92      elseif ($token == '"' || $token == "'")
  93      {
  94        $string = $this->getToken('/\G([^' . $token . ']*)' . $token . '/u');
  95        if ($string !== FALSE)
  96          return new WactConstantExpressionNode($string);
  97        else
  98          $this->raiseError("Expecting a string literal.");
  99      }
 100      // integer or float

 101      elseif (ctype_digit($token))
 102      {
 103        if ($decimalToken = $this->getToken('/\G\.(\d+)/u'))
 104          return new WactConstantExpressionNode(floatval($token . '.' . $decimalToken));
 105        else
 106          return new WactConstantExpressionNode(intval($token));
 107      }
 108      // logical not

 109      elseif($token == '!')
 110      {
 111        $expr = $this->parseExpression();
 112        return new WactUnaryExpressionNode($expr, '!');
 113      }
 114      elseif (strcasecmp($token, 'null') == 0)
 115        return new WactConstantExpressionNode(NULL);
 116      elseif (strcasecmp($token, 'true') == 0)
 117        return new WactConstantExpressionNode(TRUE);
 118      elseif (strcasecmp($token, 'false') == 0)
 119        return new WactConstantExpressionNode(FALSE);
 120      else
 121        return new WactDataBindingExpressionNode($token, $this->context);
 122    }
 123  
 124    protected function parseOperators()
 125    {
 126      $sum = $this->parsePrimary();
 127  
 128      while ($token = $this->getToken('/\G\s*(\*|\/|%|\+|-|>=|<=|==|!=|>|<|\|\||&&|&|\!)/u'))
 129      {
 130        $term = $this->parseOperators();
 131        $sum = new WactBinaryExpressionNode($sum, $term, $token);
 132      }
 133  
 134      return $sum;
 135    }
 136  
 137    protected function parseExpression()
 138    {
 139      return $this->parseOperators();
 140    }
 141  
 142    function parse($text)
 143    {
 144      $this->length = strlen($text);
 145  
 146      if ($this->length == 0) {
 147        return;
 148      }
 149  
 150      $this->text = $text;
 151      $this->position = 0;
 152  
 153      $expression = $this->parseExpression();
 154  
 155      if ($this->position < $this->length &&
 156        preg_match('/\G\s*$/u', $this->text, $match, PREG_OFFSET_CAPTURE, $this->position)) {
 157        $this->raiseError('Expection end of expression.');
 158      }
 159  
 160      return $expression;
 161  
 162    }
 163  }
 164  ?>


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