[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/macro/src/ -> lmbMacroTokenizer.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 lmbMacroTokenizer.

  12   *

  13   * @package macro

  14   * @version $Id$

  15   */
  16  class lmbMacroTokenizer
  17  {
  18    protected $publicId;
  19    protected $observer;
  20    protected $rawtext;
  21    protected $position;
  22    protected $length;
  23  
  24    function __construct($observer)
  25    {
  26      $this->observer = $observer;
  27    }
  28  
  29    function getLineNumber()
  30    {
  31      return 1 + substr_count(substr($this->rawtext, 0, $this->position), "\n");
  32    }
  33  
  34    function getCurrentLocation()
  35    {
  36      return new lmbMacroSourceLocation($this->getPublicId(), $this->getLineNumber());
  37    }
  38  
  39    function getPublicId()
  40    {
  41      return $this->publicId;
  42    }
  43  
  44    /**

  45    * Moves the position forward past any whitespace characters

  46    */
  47    function ignoreWhitespace()
  48    {
  49      while($this->position < $this->length &&
  50          strpos(" \n\r\t", $this->rawtext{$this->position}) !== false)
  51        $this->position++;
  52    }
  53  
  54    /**

  55    * Begins the parsing operation, setting up any decorators, depending on

  56    * parse options invoking _parse() to execute parsing

  57    */
  58    function parse($data, $publicId = null)
  59    {
  60      $this->rawtext = $data;
  61      $this->length = strlen($data);
  62      $this->position = 0;
  63      $this->publicId = $publicId;
  64  
  65      do
  66      {
  67        $start = $this->position;
  68        $this->position = strpos($this->rawtext, '<%', $start);
  69        if($this->position === false)
  70        {
  71          if($start < $this->length)
  72            $this->observer->characters(substr($this->rawtext, $start));
  73          return;
  74        }
  75  
  76        if($this->position > $start)
  77        {
  78          $this->observer->characters(substr($this->rawtext, $start, $this->position - $start));
  79        }
  80  
  81        $this->position += 2;   // ignore '<%' string

  82        if($this->position >= $this->length)
  83        {
  84          $this->observer->unexpectedEOF('<%');
  85          return;
  86        }
  87  
  88        $element_pos = $this->position;
  89        $this->position += 1;
  90  
  91        switch($this->rawtext{$element_pos})
  92        {
  93          case '/':
  94            $start = $this->position;
  95            while($this->position < $this->length &&
  96                  $this->rawtext{$this->position} != '%' &&
  97                  $this->rawtext{$this->position+1} != '>')
  98              $this->position++;
  99  
 100            if($this->position >= $this->length)
 101            {
 102              $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 103              return;
 104            }
 105  
 106            $tag = substr($this->rawtext, $start, $this->position - $start);
 107  
 108            $this->observer->endElement($tag);
 109            $this->position += 2;   // ignore '%>' string

 110            break;
 111  
 112        default:
 113            while($this->position < $this->length && strpos("%/ \n\r\t", $this->rawtext{$this->position}) === false)
 114              $this->position++;
 115  
 116            if($this->position >= $this->length)
 117            {
 118              $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 119              return;
 120            }
 121  
 122            $tag = substr($this->rawtext, $element_pos, $this->position - $element_pos);
 123            $attributes = array();
 124  
 125            $this->ignoreWhitespace();
 126  
 127            //tag attributes

 128            while($this->position < $this->length &&
 129                  $this->rawtext{$this->position} != '%' &&
 130                  $this->rawtext{$this->position} != '/')
 131            {
 132              $start = $this->position;
 133              while($this->position < $this->length && strpos("%= \n\r\t", $this->rawtext{$this->position}) === false)
 134                $this->position++;
 135  
 136              if($this->position >= $this->length)
 137              {
 138                $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 139                return;
 140              }
 141  
 142              $attribute_name = substr($this->rawtext, $start, $this->position - $start);
 143              $attribute_value = null;
 144  
 145              $this->ignoreWhitespace();
 146              if($this->position >= $this->length)
 147              {
 148                $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 149                return;
 150              }
 151  
 152              if($this->rawtext{$this->position} == '=')
 153              {
 154                $attribute_value = "";
 155  
 156                $this->position++;
 157                $this->ignoreWhitespace();
 158                if($this->position >= $this->length)
 159                {
 160                  $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 161                  return;
 162                }
 163  
 164                $quote = $this->rawtext{$this->position};
 165                if($quote == '"' || $quote == "'")
 166                {
 167                  $start = $this->position + 1;
 168                  $this->position = strpos($this->rawtext, $quote, $start);
 169                  if($this->position === false)
 170                  {
 171                    $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 172                    return;
 173                  }
 174  
 175                  $attribute_value = substr($this->rawtext, $start, $this->position - $start);
 176  
 177                  $this->position++;
 178                  if($this->position >= $this->length)
 179                  {
 180                    $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 181                    return;
 182                  }
 183  
 184                  if(strpos("% \n\r\t", $this->rawtext{$this->position}) === false)
 185                    $this->observer->invalidAttributeSyntax();
 186  
 187                }
 188                else
 189                {
 190                  $start = $this->position;
 191                  while($this->position < $this->length && strpos("% \n\r\t", $this->rawtext{$this->position}) === false)
 192                    $this->position++;
 193  
 194                  if($this->position >= $this->length)
 195                  {
 196                    $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 197                    return;
 198                  }
 199                  $attribute_value = substr($this->rawtext, $start, $this->position - $start);
 200                }
 201              }
 202  
 203              $attributes[$attribute_name] = $attribute_value;
 204  
 205              $this->ignoreWhitespace();
 206            }
 207  
 208            if($this->position >= $this->length)
 209            {
 210              $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 211              return;
 212            }
 213  
 214            //self closing tag check

 215            if($this->rawtext{$this->position} == '/' && $this->rawtext{$this->position + 1} == '%')
 216            {
 217              $this->position += 2;
 218              if($this->position >= $this->length)
 219              {
 220                $this->observer->unexpectedEOF(substr($this->rawtext, $element_pos - 1));
 221                return;
 222              }
 223  
 224              if($this->rawtext{$this->position} != '>')
 225              {
 226                $start = $this->position;
 227                while($this->position < $this->length && $this->rawtext{$this->position} != '>')
 228                  $this->position++;
 229  
 230                if($this->position >= $this->length)
 231                {
 232                  $this->observer->invalidEntitySyntax(substr($this->rawtext, $element_pos - 2));
 233                  break;
 234                }
 235  
 236                $this->observer->invalidEntitySyntax(substr($this->rawtext, $element_pos - 2,
 237                                                            $this->position - $element_pos + 2));
 238                $this->position += 1;
 239                break;
 240              }
 241              $this->observer->emptyElement($tag, $attributes);
 242            }
 243            else
 244            {
 245              $this->observer->startElement($tag, $attributes);
 246              //skipping %

 247              $this->position += 1;
 248            }
 249  
 250            $this->position += 1;
 251  
 252            break;
 253          }
 254      }
 255      while ($this->position < $this->length);
 256    }
 257  }
 258  ?>


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7