<?php
/**
* Harmony
* Copyright (c) 2008 Maxime Bouroumeau-Fuseau
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category Harmony
* @package Harmony_Parser
* @author Maxime Bouroumeau-Fuseau
* @copyright 2008 (c) Maxime Bouroumeau-Fuseau
* @license http://www.opensource.org/licenses/mit-license.php
* @link http://www.harmony-framework.com
*/
/** Harmony_Parser_Context */
require_once 'Harmony/Parser/Context.php';
/**
* Linear Parser
*
* @category Harmony
* @package Harmony_Parser
* @copyright 2008 (c) Maxime Bouroumeau-Fuseau
* @license http://www.opensource.org/licenses/mit-license.php
*/
abstract class Harmony_Parser_Abstract
{
/**
* End of file token
*/
const EOF = 'Eof';
/**
* Paths where to find contexts
*
* @var array
*/
protected $_contextPaths = array();
/**
* Tokens
*
* @var array
*/
protected $_tokens = array();
/**
* Data to parse
*
* @var mixed
*/
protected $_data;
/**
* Adds a new path where to search for context class
*
* @param string $path
* @param string $prefix OPTIONAL Class prefix
*/
public function addContextPath($path, $prefix = '')
{
$this->_contextPaths[$path] = $prefix;
}
/**
* Loads a context class and returns its name
*
* @param string $context
* @return string
*/
public function getContextClassName($context)
{
foreach ($this->_contextPaths as $path => $prefix) {
$filename = rtrim($path, DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR . $context . '.php';
/* checks if the context file exists in the current directory */
if (file_exists($filename)) {
require_once $filename;
$classname = $prefix . $context;
/* checks if the context class inherits from Harmony_Parser_Context */
if (!in_array('Harmony_Parser_Context', class_parents($classname))) {
require_once 'Harmony/Parser/Exception.php';
throw new Harmony_Parser_Exception('Context must inherit from Harmony_Parser_Context');
}
return $classname;
}
}
require_once 'Harmony/Parser/Exception.php';
throw new Harmony_Parser_Exception('Context not found: '. $context);
}
/**
* Sets a token
*
* @param string $name
* @param string $regexp
*/
public function setToken($name, $regexp)
{
$this->_tokens[$name] = $regexp;
}
/**
* Removes a token
*
* @param string $name
*/
public function removeToken($name)
{
if (!array_key_exists($name, $this->_tokens)) {
require_once 'Harmony/Parser/Exception.php';
throw new Harmony_Parser_Exception('Token ' . $name . ' does not exists');
}
unset($this->_tokens[$name]);
}
/**
* Removes all tokens
*/
public function clearTokens()
{
$this->_tokens = array();
}
/**
* Gets all tokens
*
* @return array
*/
public function getTokens()
{
return $this->_tokens;
}
/**
* Sets the data to parse
*
* @param mixed $data
*/
public function setData($data)
{
$this->_data = $data;
}
/**
* Gets the data to parse
*
* @return mixed
*/
public function getData()
{
return $this->_data;
}
/**
* Enters a next context
*
* @param string $context
* @param array $params OPTIONAL Context parameters
* @return mixed Data returned by context
*/
public abstract function enterContext($context, $params = array());
}