Location: PHPKode > projects > PHP Project Navigator > ppnav-0.1/PHP/class.phpscanner.php
<?php

/**
 * @package PHP Project Navigator
 * @file PHPScanner.php
 * @copyright (C) 2006 Scott Brumbaugh
 * @license GPL Version 2 or later
 *
 * $Id: class.phpscanner.php 2 2006-10-04 20:11:31Z brumbs $
 */

# Token output format = sprintf "%s %d %d %d %d %d %d\n%s\n",
# 
# type start_line end_line start_col end_col start_offset length"\n"text"\n"

/**
 * @class PHPScanner
 *
 * Implement a lexer for the PHP parser
 */
class PHPScanner {

    private $startLine = 1;
    private $endLine = 1;
    private $startCol;
    private $endCol;
    private $startOffset;
    private $endOffset;
    private $length;
    private $tokenList;
    private $fullScriptPath;

    function __construct() {
    }

    function read($filename) {

        $script = file_get_contents($filename);
        if ($script === false) {
            throw new Exception("Could not read $filename\n");
        }      
        $this->fullScriptPath = realpath($filename);
        $this->fullScriptPath .= "\n";
        $this->tokenList = token_get_all($script);
    }

    function write($filename) {

	$handle = fopen($filename, "w");
	if (!$handle) {
	    throw new Exception("Unable to open $filename for writting");
        }

        if (fwrite($handle, $this->fullScriptPath) == false) {
	    throw new Exception("Failed writting token " . $this->_type($token) . " to $filename");
        }
	
	foreach ($this->tokenList as $token) {
	    $formattedToken = $this->_format($token);
	    if (fwrite($handle, $formattedToken) == false) {
		throw new Exception("Failed writting token " . $this->_type($token) . " to $filename");
            }
        }

	fclose($handle);

    }

    private function _format($token) {

	$this->_updateOffsets($token);

        $text = $this->_text($token);

	$output = sprintf("%s %d %d %d %d %d %d\n%s\n",
	    $this->_type($token),
            $this->startLine, 
            $this->endLine, 
            $this->startCol, 
            $this->endCol, 
            $this->startOffset, 
            strlen($text),
            $text);

        return $output;
    }

    private function _length($token) {
	return is_array($token) ? strlen($token[1]) : 1;
    }

    private function _text($token) {
        return is_array($token) ? $token[1] : $token;
    }

    private function _type($token) {
        return is_array($token) ? token_name($token[0]) : $token;
    }

    private function _updateOffsets($token) {

        $text = $this->_text($token);

        $this->startLine = $this->endLine;
        $this->startCol = $this->endCol;
        $this->startOffset = $this->endOffset;
        $this->endOffset += strlen($text);

        $newLines = substr_count($text, "\n");

        if ($newLines) {
            $this->endLine += $newLines;
            $remainderText = substr($text, strrpos($text, "\n") + 1);
            if ($remainderText === false) {
                $remainderText = '';
            }
            $this->endCol = $remainderText;
        }
        else {
            $this->endCol += strlen($text);
        }        
    }
}


?>
Return current item: PHP Project Navigator