Exemplo n.º 1
0
function processTokenStream(TokenStream $tokenStream, ConverterStateMachine $stateMachine, $originalFilename)
{
    $name = '';
    $value = '';
    $stateMachine->currentTokenStream = $tokenStream;
    while ($tokenStream->hasMoreTokens() == TRUE) {
        $tokenStream->next($name, $value);
        $count = 0;
        $parsedToken = $stateMachine->parseToken($name, $value, $count);
        //TODO - both of these should be somewhere more logical.
        if ($name == 'T_CONSTANT_ENCAPSED_STRING') {
            $value = convertMultiLineString($value);
        }
        if ($name == 'T_ENCAPSED_AND_WHITESPACE') {
            $parsedToken = convertMultiLineString($parsedToken);
        }
        $stateMachine->accountForOpenBrackets($name);
        $stateMachine->accountForQuotes($name);
        $stateMachine->scopePreStateMagic($name, $value);
        do {
            $reprocess = $stateMachine->processToken($name, $value, $parsedToken);
            if ($count > 5) {
                throw new \Exception("Stuck converting same token.");
            }
            $count++;
        } while ($reprocess == TRUE);
        $stateMachine->accountForCloseBrackets($name);
        $stateMachine->scopePostStateMagic($name, $value);
        if ($name == 'T_VARIABLE') {
            //If there's a token that needs to be inserted e.g. 'var'
            if ($stateMachine->insertToken != FALSE) {
                $stateMachine->addJS($stateMachine->insertToken);
                $stateMachine->insertToken = FALSE;
            }
        }
        if (FALSE) {
            $requiredFile = $stateMachine->getRequiredFile();
            if ($requiredFile != NULL) {
                //echo "Figure out where $requiredFile is from original file path $originalFilename";
                //TraitInclude.php' is from original file path TraitExample.php
                $pathParts = pathinfo($originalFilename);
                $requireFilePath = $pathParts['dirname'] . '/' . $requiredFile;
                //$requireFilePath = realpath($requireFilePath);
                if (PHPToJavascript::$TRACE == TRUE) {
                    echo "Including file [{$requiredFile}] on path [{$requireFilePath}].";
                }
                $code = file_get_contents($requireFilePath);
                if ($code === FALSE) {
                    throw new \Exception("Could not open file [{$requiredFile}] on path [{$requireFilePath}].");
                }
                $requireTokenStream = new TokenStream($code);
                processTokenStream($requireTokenStream, $stateMachine, $originalFilename);
                $stateMachine->addJS("\n//End of require\n");
                //TODO Add a new state to tidy up semi-colon after include
            }
        }
    }
}