[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/src/compiler/parser/ -> WactSourceFileParser.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 WactSourceFileParser.

  12   *

  13   * @package wact

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

  15   */
  16  class WactSourceFileParser implements WactHTMLParserListener
  17  {
  18    /**

  19    * @var WactComponentParsingState

  20    */
  21    protected $component_parsing_state;
  22  
  23    /**

  24    * @var WactLiteralParsingState

  25    */
  26    protected $literal_parsing_state;
  27  
  28    protected $active_parsing_state;
  29  
  30    /**

  31     * @var WactConfig

  32     */
  33    protected $config;
  34  
  35    /**

  36     * @var WactTreeBuilder

  37     */
  38    protected $tree_builder;
  39  
  40    /**

  41     * @var WactTemplateLocator

  42     */
  43    protected $template_locator;
  44  
  45    function __construct($tree_builder, $template_locator, $tag_dictionary)
  46    {
  47      $this->tree_builder = $tree_builder;
  48  
  49      $this->template_locator = $template_locator;
  50  
  51      $this->component_parsing_state = $this->_createComponentParsingState($tag_dictionary);
  52  
  53      $this->literal_parsing_state = $this->_createLiteralParsingState();
  54  
  55      $this->changeToComponentParsingState();
  56    }
  57  
  58    // for testing purposes

  59    protected function _createComponentParsingState($tag_dictionary)
  60    {
  61      return new WactComponentParsingState($this, $this->tree_builder, $tag_dictionary);
  62    }
  63  
  64    // for testing purposes

  65    protected function _createLiteralParsingState()
  66    {
  67      return new WactLiteralParsingState($this, $this->tree_builder);
  68    }
  69  
  70    /**

  71    * Used to parse the source template.

  72    * Initially invoked by the CompileTemplate function,

  73    * the first component argument being a WactCompileTreeRootNode.

  74    */
  75    function parse($file_name, $compile_tree_root_node)
  76    {
  77      $source_file_path = $this->template_locator->locateSourceTemplate($file_name);
  78  
  79      if(empty($source_file_path))
  80          throw new WactException('Template source file not found', array('file_name' => $file_name));
  81  
  82      $tag_count_before_parse = $this->tree_builder->getExpectedTagCount();
  83  
  84      $this->tree_builder->setCursor($compile_tree_root_node);
  85  
  86      $this->changeToComponentParsingState();
  87  
  88      $parser = new WactHTMLParser($this);
  89  
  90      $template = $this->template_locator->readTemplateFile($source_file_path);
  91  
  92      $parser->parse($template, $source_file_path);
  93  
  94      if($tag_count_before_parse != $this->tree_builder->getExpectedTagCount())
  95      {
  96        $location = $this->tree_builder->getExpectedTagLocation();
  97        throw new WactException('Missing close tag',
  98                                array('tag' => $this->tree_builder->getExpectedTag(),
  99                                      'file' => $location->getFile(),
 100                                      'line' => $location->getLine()));
 101      }
 102    }
 103  
 104    function changeToComponentParsingState()
 105    {
 106      $this->active_parsing_state = $this->component_parsing_state;
 107    }
 108  
 109    function getActiveParsingState()
 110    {
 111      return $this->active_parsing_state;
 112    }
 113  
 114    function changeToLiteralParsingState($tag)
 115    {
 116      $this->active_parsing_state = $this->literal_parsing_state;
 117      $this->active_parsing_state->setLiteralTag($tag);
 118    }
 119  
 120    function startTag($tag, $attrs, $location)
 121    {
 122      $this->active_parsing_state->startTag($tag, $attrs, $location);
 123    }
 124  
 125    function endTag($tag, $location)
 126    {
 127      $this->active_parsing_state->endTag($tag, $location);
 128    }
 129  
 130    function emptyTag($tag, $attrs, $location)
 131    {
 132      $this->active_parsing_state->emptyTag($tag, $attrs, $location);
 133    }
 134  
 135    function characters($text, $location)
 136    {
 137      $this->active_parsing_state->characters($text, $location);
 138    }
 139  
 140    function instruction($target, $instruction, $location)
 141    {
 142      $this->active_parsing_state->instruction($target, $instruction, $location);
 143    }
 144  }
 145  
 146  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7