Haanga_Compiler_Tokenizer::yylex PHP Method

yylex() public method

public yylex ( )
    function yylex()
    {
        $this->token = NULL;
        if ($this->length == $this->N) {
            if ($this->status != self::IN_NONE && $this->status != self::IN_HTML) {
                $this->Error("Unexpected end");
            }
            return FALSE;
        }
        if ($this->status == self::IN_NONE) {
            $i =& $this->N;
            $data = substr($this->data, $i, 12);
            static $lencache = array();
            foreach ($this->open_tags as $value => $token) {
                if (!isset($lencache[$value])) {
                    $lencache[$value] = strlen($value);
                }
                $len = $lencache[$value];
                if (strncmp($data, $value, $len) == 0) {
                    $this->value = $value;
                    $this->token = $token;
                    $i += $len;
                    switch ($this->token) {
                        case HG_Parser::T_TAG_OPEN:
                            $this->status = self::IN_TAG;
                            break;
                        case HG_Parser::T_COMMENT:
                            $zdata =& $this->data;
                            if (($pos = strpos($zdata, self::$end_comment, $i)) === FALSE) {
                                $this->error("unexpected end");
                            }
                            $this->value = substr($zdata, $i, $pos - 2);
                            $this->status = self::IN_NONE;
                            $i = $pos + 2;
                            break;
                        case HG_Parser::T_PRINT_OPEN:
                            $this->status = self::IN_ECHO;
                            $this->echoFirstToken = false;
                            break;
                    }
                    return TRUE;
                }
            }
            $this->status = self::IN_HTML;
        }
        switch ($this->status) {
            case self::IN_TAG:
            case self::IN_ECHO:
                $this->yylex_main();
                break;
            default:
                $this->yylex_html();
        }
        if (empty($this->token)) {
            if ($this->status != self::IN_NONE && $this->status != self::IN_HTML) {
                $this->Error("Unexpected end");
            }
            return FALSE;
        }
        return TRUE;
    }

Usage Example

Beispiel #1
0
 static function init($template, $compiler, $file = '')
 {
     $lexer = new Haanga_Compiler_Tokenizer($template, $compiler, $file);
     $parser = new Haanga_Compiler_Parser($lexer, $file);
     $parser->compiler = $compiler;
     try {
         for ($i = 0;; $i++) {
             if (!$lexer->yylex()) {
                 break;
             }
             $parser->doParse($lexer->token, $lexer->value);
         }
     } catch (Exception $e) {
         /* destroy the parser */
         try {
             $parser->doParse(0, 0);
         } catch (Exception $y) {
         }
         throw $e;
         /* re-throw exception */
     }
     $parser->doParse(0, 0);
     return (array) $parser->body;
 }