<?php
/*
Copyright 2008 Josh Heidenreich
This file is part of Pelzini.
Pelzini is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Pelzini is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Contains the {@link Analyser} class
*
* @package Parsers
* @author Josh
* @since 0.2
**/
/**
* Generic language analyser. Analysers are used to tranform the language-specific tokens into a set of {@link CodeParserItem ParserItems}
**/
abstract class Analyser
{
private $tokens;
private $pos;
private $token_pos;
/**
* Resets the analyser ready for more parsing work
**/
public function resetState()
{
$this->token_pos = 0;
}
/**
* Processes a set of token and populates a {@link ParserFile}
**/
abstract public function process($tokens, $parser_file);
/**
* Tells the analyser what tokens it should use
**/
final protected function setTokens($tokens)
{
$this->tokens = $tokens;
}
/**
* Sets the current position
**/
final protected function setPos($pos)
{
}
/**
* Moves the internal token pointer forwards
*
* @param $num integer The number of positions to move the pointer forwards
**/
final protected function movePosForward($num = 1)
{
}
/**
* Moves the internal token pointer backwards
*
* @param $num integer The number of positions to move the pointer backwards
**/
final protected function movePosBackward($num = 1)
{
}
/**
* Returns a token at a specific position
* If no position is specified, uses the current position
**/
final protected function getToken($pos = null)
{
if ($pos === null) $pos = $this->pos; return $this->tokens[$pos];
}
/**
* Returns the current position
**/
final protected function getPos()
{
}
/**
* Finds a token looking forward from the current position.
* Searching starts after the current token.
* The token must be of the type specified
*
* @param mixed $token_types A token type constant, or an array of token type constants
* @param mixed $stop_list Token(s) that should stop the search process
* @return Token The found token, or null if nothing was found
**/
final protected function findTokenForwards($token_types, $stop_list = null)
{
if (! is_array($token_types)) $token_types = array($token_types);
if ($pos != 0) $pos++;
while (true) {
if (empty($this->tokens[$pos])) break;
$tok = $this->tokens[$pos];
break;
}
$this->token_pos = $pos;
return $tok;
}
++$pos;
}
return null;
}
/**
* Finds a token looking backwards from the current position.
* Searching starts before the current token.
* The token must be of the type specified
*
* @param mixed $token_types A token type constant, or an array of token type constants
* @param mixed $stop_list Token(s) that should stop the search process
* @return Token The found token, or null if nothing was found
**/
final protected function findTokenBackwards($token_types, $stop_list = null)
{
if (! is_array($token_types)) $token_types = array($token_types);
while (true) {
if (empty($this->tokens[$pos])) break;
$tok = $this->tokens[$pos];
break;
}
$this->token_pos = $pos;
return $tok;
}
--$pos;
}
return null;
}
/**
* Gets the position of the last token found using one of the search functions
**/
final protected function getTokenPos()
{
return $this->token_pos;
}
}
?>