<?php

namespace Smarty\Lexer;

/*
 * This file is part of Smarty.
 *
 * (c) 2015 Uwe Tews
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * TemplateLexer
 * This is the template file lexer.
 * It is generated from the TemplateLexer.plex file
 *
 *
 * @author Uwe Tews <uwe.tews@googlemail.com>
 */
class TemplateLexer
{
    /**
     * Source
     *
     * @var string
     */
    public $data;

    /**
     * Source length
     *
     * @var int
     */
    public $dataLength = null;

    /**
     * byte counter
     *
     * @var int
     */
    public $counter;

    /**
     * token number
     *
     * @var int
     */
    public $token;

    /**
     * token value
     *
     * @var string
     */
    public $value;

    /**
     * current line
     *
     * @var int
     */
    public $line;

    /**
     * tag start line
     *
     * @var
     */
    public $taglineno;

   /**
     * state number
     *
     * @var int
     */
    public $state = 1;

    /**
     * Smarty object
     *
     * @var Smarty
     */
    public $smarty = null;

    /**
     * compiler object
     *
     * @var \Smarty\Compiler\Template
     */
    public $compiler = null;

    /**
     * trace file
     *
     * @var resource
     */
    public $yyTraceFILE;

    /**
     * trace prompt
     *
     * @var string
     */
    public $yyTracePrompt;

    /**
     * XML flag true while processing xml
     *
     * @var bool
     */
    public $is_xml = false;

    /**
     * state names
     *
     * @var array
     */
    public $state_name = array(1 => 'TEXT', 2 => 'TAG', 3 => 'TAGBODY', 4 => 'LITERAL', 5 => 'DOUBLEQUOTEDSTRING',);

    /**
     * token names
     *
     * @var array
     */
    public $smarty_token_names = array(        // Text for parser error messages
                                               'NOT'         => '(!,not)',
                                               'OPENP'       => '(',
                                               'CLOSEP'      => ')',
                                               'OPENB'       => '[',
                                               'CLOSEB'      => ']',
                                               'PTR'         => '->',
                                               'APTR'        => '=>',
                                               'EQUAL'       => '=',
                                               'NUMBER'      => 'number',
                                               'UNIMATH'     => '+" , "-',
                                               'MATH'        => '*" , "/" , "%',
                                               'INCDEC'      => '++" , "--',
                                               'SPACE'       => ' ',
                                               'DOLLAR'      => '$',
                                               'SEMICOLON'   => ';',
                                               'COLON'       => ':',
                                               'DOUBLECOLON' => '::',
                                               'AT'          => '@',
                                               'HATCH'       => '#',
                                               'QUOTE'       => '"',
                                               'BACKTICK'    => '`',
                                               'VERT'        => '"|" modifier',
                                               'DOT'         => '.',
                                               'COMMA'       => '","',
                                               'QMARK'       => '"?"',
                                               'ID'          => 'id, name',
                                               'TEXT'        => 'text',
                                               'LDELSLASH'   => '{/..} closing tag',
                                               'LDEL'        => '{...} Smarty tag',
                                               'COMMENT'     => 'comment',
                                               'AS'          => 'as',
                                               'TO'          => 'to',
                                               'LOGOP'       => '"<", "==" ... logical operator',
                                               'TLOGOP'      => '"lt", "eq" ... logical operator; "is div by" ... if condition',
                                               'SCOND'       => '"is even" ... if condition',
    );

    /**
     * literal tag nesting level
     *
     * @var int
     */
    private $literal_cnt = 0;

    /**
     * preg token pattern for state TEXT
     *
     * @var string
     */
    private $yy_global_pattern1 = null;

    /**
     * preg token pattern for state TAG
     *
     * @var string
     */
    private $yy_global_pattern2 = null;

    /**
     * preg token pattern for state TAGBODY
     *
     * @var string
     */
    private $yy_global_pattern3 = null;

    /**
     * preg token pattern for state LITERAL
     *
     * @var string
     */
    private $yy_global_pattern4 = null;

    /**
     * preg token pattern for state DOUBLEQUOTEDSTRING
     *
     * @var null
     */
    private $yy_global_pattern5 = null;

    /**
     * preg token pattern for text
     *
     * @var null
     */
    private $yy_global_text = null;

    /**
     * preg token pattern for literal
     *
     * @var null
     */
    private $yy_global_literal = null;

    /**
     * constructor
     *
     * @param   string                             $source template source
     * @param \Smarty\Compiler\Template $compiler
     */
    public function __construct($source, \Smarty\Compiler\Template $compiler)
    {
        $this->data = $source;
        $this->dataLength = strlen($this->data);
        $this->counter = 0;
        if (preg_match('/^\xEF\xBB\xBF/i', $this->data, $match)) {
            $this->counter += strlen($match[0]);
        }
        $this->line = 1;
        $this->smarty = $compiler->getTemplate()->getSmarty();
        $this->compiler = $compiler;
        $this->compiler->initDelimiterPreg();
        $this->smarty_token_names['LDEL'] = $this->smarty->getLeftDelimiter();
        $this->smarty_token_names['RDEL'] = $this->smarty->getRightDelimiter();
    }

    /**
     * open lexer/parser trace file
     *
     */
    public function PrintTrace()
    {
        $this->yyTraceFILE = fopen('php://output', 'w');
        $this->yyTracePrompt = '<br>';
    }

   /**
     * replace placeholders with runtime preg  code
     *
     * @param string $preg
     *
     * @return string
     */
   public function replace($preg)
   {
        return $this->compiler->replaceDelimiter($preg);
   }

    /**
     * check if current value is an autoliteral left delimiter
     *
     * @return bool
     */
    public function isAutoLiteral()
    {
        return $this->smarty->getAutoLiteral() && isset($this->value[ $this->compiler->getLdelLength() ]) ?
            strpos(" \n\t\r", $this->value[ $this->compiler->getLdelLength() ]) !== false : false;
    }

     /*!lex2php
     %input $this->data
     %counter $this->counter
     %token $this->token
     %value $this->value
     %line $this->line
     userliteral = ~(SMARTYldel)SMARTYautoliteral\s+SMARTYliteral~
     char = ~[\S\s]~
     textdoublequoted = ~([^"\\]*?)((?:\\.[^"\\]*?)*?)(?=((SMARTYldel)SMARTYal|\$|`\$|"SMARTYliteral))~
     namespace = ~([0-9]*[a-zA-Z_]\w*)?(\\[0-9]*[a-zA-Z_]\w*)+~
     emptyjava = ~[{][}]~
     slash = ~[/]~
     ldel = ~(SMARTYldel)SMARTYal~
     rdel = ~\s*SMARTYrdel~
     nocacherdel = ~(\s+nocache)?\s*SMARTYrdel~
     smartyblockchildparent = ~[\$]smarty\.block\.(child|parent)~
     integer = ~\d+~
     hex =  ~0[xX][0-9a-fA-F]+~
     math = ~\s*([*]{1,2}|[%/^&]|[<>]{2})\s*~
     comment = ~(SMARTYldel)SMARTYal[*]~
     incdec = ~([+]|[-]){2}~
     unimath = ~\s*([+]|[-])\s*~
     openP = ~\s*[(]\s*~
     closeP = ~\s*[)]~
     openB = ~\[\s*~
     closeB = ~\s*\]~
     dollar = ~[$]~
     dot = ~[.]~
     comma = ~\s*[,]\s*~
     doublecolon = ~[:]{2}~
     colon = ~\s*[:]\s*~
     at = ~[@]~
     hatch = ~[#]~
     semicolon = ~\s*[;]\s*~
     equal = ~\s*[=]\s*~
     space = ~\s+~
     ptr = ~\s*[-][>]\s*~
     aptr = ~\s*[=][>]\s*~
     singlequotestring = ~'[^'\\]*(?:\\.[^'\\]*)*'~
     backtick = ~[`]~
     vert = ~[|][@]?~
     qmark = ~\s*[?]\s*~
     constant = ~[_]+[A-Z0-9][0-9A-Z_]*|[A-Z][0-9A-Z_]*(?![0-9A-Z_]*[a-z])~
     attr = ~\s+[0-9]*[a-zA-Z_][a-zA-Z0-9_\-:]*\s*[=]\s*~
     id = ~[0-9]*[a-zA-Z_]\w*~
     literal = ~literal~
     strip = ~strip~
     lop = ~\s*([!=][=]{1,2}|[<][=>]?|[>][=]?|[&|]{2})\s*~
     slop = ~\s+(eq|ne|neq|gt|ge|gte|lt|le|lte|mod|and|or|xor)\s+~
     tlop = ~\s+is\s+(not\s+)?(odd|even|div)\s+by\s+~
     scond = ~\s+is\s+(not\s+)?(odd|even)~
     isin = ~\s+is\s+in\s+~
     as = ~\s+as\s+~
     to = ~\s+to\s+~
     step = ~\s+step\s+~
     if = ~(if|elseif|else if|while)\s+~
     for = ~for\s+~
     array = ~array~
     foreach = ~foreach(?![^\s])~
     setfilter = ~setfilter\s+~
     instanceof = ~\s+instanceof\s+~
     not = ~[!]\s*|not\s+~
     typecast = ~[(](int(eger)?|bool(ean)?|float|double|real|string|binary|array|object)[)]\s*~
     double_quote = ~["]~
     */
     /*!lex2php
     %statename TEXT
     emptyjava {
        $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
     comment {
       $to = $this->dataLength;
       preg_match("/[*]{$this->compiler->getRdelPreg()}[\n]?/",$this->data,$match,PREG_OFFSET_CAPTURE,$this->counter);
        if (isset($match[0][1])) {
            $to = $match[0][1] + strlen($match[0][0]);
        } else {
            $this->compiler->trigger_template_error ("missing or misspelled comment closing tag '{$this->smarty->getRightDelimiter()}'");
        }
        $this->value = substr($this->data,$this->counter,$to-$this->counter);
        return false;
     }
     userliteral {
        $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
     ldel literal rdel {
        $this->token = \Smarty\Parser\TemplateParser::TP_LITERALSTART;
        $this->yypushstate(self::LITERAL);
     }
     ldel slash literal rdel {
        $this->token = \Smarty\Parser\TemplateParser::TP_LITERALEND;
        $this->yypushstate(self::LITERAL);
     }
     ldel {
        $this->yypushstate(self::TAG);
        return true;
     }
     char {
       if (!isset($this->yy_global_text)) {
           $this->yy_global_text = $this->replace('/(SMARTYldel)SMARTYal/isS');
       }
       $to = $this->dataLength;
       preg_match($this->yy_global_text, $this->data,$match,PREG_OFFSET_CAPTURE,$this->counter);
       if (isset($match[0][1])) {
         $to = $match[0][1];
       }
       $this->value = substr($this->data,$this->counter,$to-$this->counter);
       $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
     */
     /*!lex2php
     %statename TAG
     ldel if {
        $this->token = \Smarty\Parser\TemplateParser::TP_LDELIF;
        $this->yybegin(self::TAGBODY);
        $this->taglineno = $this->line;
     }
     ldel for {
        $this->token = \Smarty\Parser\TemplateParser::TP_LDELFOR;
        $this->yybegin(self::TAGBODY);
        $this->taglineno = $this->line;
     }
     ldel foreach {
        $this->token = \Smarty\Parser\TemplateParser::TP_LDELFOREACH;
        $this->yybegin(self::TAGBODY);
        $this->taglineno = $this->line;
     }
     ldel setfilter {
        $this->token = \Smarty\Parser\TemplateParser::TP_LDELSETFILTER;
        $this->yybegin(self::TAGBODY);
        $this->taglineno = $this->line;
     }
     ldel id nocacherdel {
        $this->yypopstate();
        $this->token = \Smarty\Parser\TemplateParser::TP_SIMPLETAG;
        $this->taglineno = $this->line;
     }
     ldel smartyblockchildparent rdel {
         $this->yypopstate();
         $this->token = \Smarty\Parser\TemplateParser::TP_SMARTYBLOCKCHILDPARENT;
         $this->taglineno = $this->line;
     }
     ldel slash id rdel {
        $this->yypopstate();
        $this->token = \Smarty\Parser\TemplateParser::TP_CLOSETAG;
        $this->taglineno = $this->line;
     }
     ldel dollar id nocacherdel {
        if ($this->_yy_stack[count($this->_yy_stack)-1] === self::TEXT) {
            $this->yypopstate();
            $this->token = \Smarty\Parser\TemplateParser::TP_SIMPELOUTPUT;
            $this->taglineno = $this->line;
        } else {
            $this->value = $this->smarty->getLeftDelimiter();
            $this->token = \Smarty\Parser\TemplateParser::TP_LDEL;
            $this->yybegin(self::TAGBODY);
            $this->taglineno = $this->line;
        }
     }
     ldel slash {
        $this->token = \Smarty\Parser\TemplateParser::TP_LDELSLASH;
        $this->yybegin(self::TAGBODY);
        $this->taglineno = $this->line;
     }
     ldel {
        $this->token = \Smarty\Parser\TemplateParser::TP_LDEL;
        $this->yybegin(self::TAGBODY);
        $this->taglineno = $this->line;
     }
     */
     /*!lex2php
     %statename TAGBODY
     rdel {
        $this->token = \Smarty\Parser\TemplateParser::TP_RDEL;
        $this->yypopstate();
     }
     ldel {
        $this->yypushstate(self::TAG);
        return true;
     }
     double_quote {
        $this->token = \Smarty\Parser\TemplateParser::TP_QUOTE;
        $this->yypushstate(self::DOUBLEQUOTEDSTRING);
        $this->compiler->enterDoubleQuote();
     }
     singlequotestring {
        $this->token = \Smarty\Parser\TemplateParser::TP_SINGLEQUOTESTRING;
     }
     dollar id {
        $this->token = \Smarty\Parser\TemplateParser::TP_DOLLARID;
     }
     dollar {
        $this->token = \Smarty\Parser\TemplateParser::TP_DOLLAR;
     }
     isin {
        $this->token = \Smarty\Parser\TemplateParser::TP_ISIN;
     }
     as {
        $this->token = \Smarty\Parser\TemplateParser::TP_AS;
     }
     to {
        $this->token = \Smarty\Parser\TemplateParser::TP_TO;
     }
     step {
        $this->token = \Smarty\Parser\TemplateParser::TP_STEP;
     }
     instanceof {
        $this->token = \Smarty\Parser\TemplateParser::TP_INSTANCEOF;
     }
     lop {
        $this->token = \Smarty\Parser\TemplateParser::TP_LOGOP;
     }
     slop {
        $this->token = \Smarty\Parser\TemplateParser::TP_SLOGOP;
     }
     tlop {
        $this->token = \Smarty\Parser\TemplateParser::TP_TLOGOP;
     }
     scond {
        $this->token = \Smarty\Parser\TemplateParser::TP_SINGLECOND;
     }
     not{
        $this->token = \Smarty\Parser\TemplateParser::TP_NOT;
     }
     typecast {
        $this->token = \Smarty\Parser\TemplateParser::TP_TYPECAST;
     }
     openP {
        $this->token = \Smarty\Parser\TemplateParser::TP_OPENP;
     }
     closeP {
        $this->token = \Smarty\Parser\TemplateParser::TP_CLOSEP;
     }
     openB {
        $this->token = \Smarty\Parser\TemplateParser::TP_OPENB;
     }
     closeB {
        $this->token = \Smarty\Parser\TemplateParser::TP_CLOSEB;
     }
     ptr {
        $this->token = \Smarty\Parser\TemplateParser::TP_PTR;
     }
     aptr {
        $this->token = \Smarty\Parser\TemplateParser::TP_APTR;
     }
     equal {
        $this->token = \Smarty\Parser\TemplateParser::TP_EQUAL;
     }
     incdec {
        $this->token = \Smarty\Parser\TemplateParser::TP_INCDEC;
     }
     unimath {
        $this->token = \Smarty\Parser\TemplateParser::TP_UNIMATH;
     }
     math {
        $this->token = \Smarty\Parser\TemplateParser::TP_MATH;
     }
     at {
        $this->token = \Smarty\Parser\TemplateParser::TP_AT;
     }
     array openP {
        $this->token = \Smarty\Parser\TemplateParser::TP_ARRAYOPEN;
     }
     hatch {
        $this->token = \Smarty\Parser\TemplateParser::TP_HATCH;
     }
     attr {
        // resolve conflicts with shorttag and right_delimiter starting with '='
        if (substr($this->data, $this->counter + strlen($this->value) - 1, $this->compiler->getRdelLength()) === $this->smarty->getRightDelimiter()) {
            preg_match('/\s+/',$this->value,$match);
            $this->value = $match[0];
            $this->token = \Smarty\Parser\TemplateParser::TP_SPACE;
        } else {
            $this->token = \Smarty\Parser\TemplateParser::TP_ATTR;
        }
     }
     namespace {
        $this->token = \Smarty\Parser\TemplateParser::TP_NAMESPACE;
     }
     id {
        $this->token = \Smarty\Parser\TemplateParser::TP_ID;
     }
     integer {
        $this->token = \Smarty\Parser\TemplateParser::TP_INTEGER;
     }
     backtick {
        $this->token = \Smarty\Parser\TemplateParser::TP_BACKTICK;
        $this->yypopstate();
     }
     vert {
        $this->token = \Smarty\Parser\TemplateParser::TP_VERT;
     }
     dot {
        $this->token = \Smarty\Parser\TemplateParser::TP_DOT;
     }
     comma {
        $this->token = \Smarty\Parser\TemplateParser::TP_COMMA;
     }
     semicolon {
        $this->token = \Smarty\Parser\TemplateParser::TP_SEMICOLON;
     }
     doublecolon {
        $this->token = \Smarty\Parser\TemplateParser::TP_DOUBLECOLON;
     }
     colon {
        $this->token = \Smarty\Parser\TemplateParser::TP_COLON;
     }
     qmark {
        $this->token = \Smarty\Parser\TemplateParser::TP_QMARK;
     }
     hex {
        $this->token = \Smarty\Parser\TemplateParser::TP_HEX;
     }
     space {
        $this->token = \Smarty\Parser\TemplateParser::TP_SPACE;
     }
     char {
        $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
     */

     /*!lex2php
     %statename LITERAL
     ldel literal rdel {
        $this->literal_cnt++;
        $this->token = \Smarty\Parser\TemplateParser::TP_LITERAL;
     }
     ldel slash literal rdel {
        if ($this->literal_cnt) {
             $this->literal_cnt--;
            $this->token = \Smarty\Parser\TemplateParser::TP_LITERAL;
        } else {
            $this->token = \Smarty\Parser\TemplateParser::TP_LITERALEND;
            $this->yypopstate();
        }
     }
     char {
       if (!isset($this->yy_global_literal)) {
           $this->yy_global_literal = $this->replace('/(SMARTYldel)SMARTYal[\/]?literalSMARTYrdel/isS');
       }
       $to = $this->dataLength;
       preg_match($this->yy_global_literal, $this->data,$match,PREG_OFFSET_CAPTURE,$this->counter);
       if (isset($match[0][1])) {
         $to = $match[0][1];
       } else {
          $this->compiler->trigger_template_error ("missing or misspelled literal closing tag");
       }
       $this->value = substr($this->data,$this->counter,$to-$this->counter);
       $this->token = \Smarty\Parser\TemplateParser::TP_LITERAL;
     }
     */
     /*!lex2php
     %statename DOUBLEQUOTEDSTRING
     userliteral {
        $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
     ldel literal rdel {
        $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
     ldel slash literal rdel {
        $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
     ldel slash {
        $this->yypushstate(self::TAG);
        return true;
     }
     ldel id {
        $this->yypushstate(self::TAG);
        return true;
     }
     ldel {
        $this->token = \Smarty\Parser\TemplateParser::TP_LDEL;
        $this->taglineno = $this->line;
        $this->yypushstate(self::TAGBODY);
     }
     double_quote {
        $this->token = \Smarty\Parser\TemplateParser::TP_QUOTE;
        $this->yypopstate();
     }
     backtick dollar {
        $this->token = \Smarty\Parser\TemplateParser::TP_BACKTICK;
        $this->value = substr($this->value,0,-1);
        $this->yypushstate(self::TAGBODY);
        $this->taglineno = $this->line;
     }
     dollar id {
        $this->token = \Smarty\Parser\TemplateParser::TP_DOLLARID;
     }
     dollar {
        $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
     textdoublequoted {
        $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
     char {
        $to = $this->dataLength;
        $this->value = substr($this->data,$this->counter,$to-$this->counter);
        $this->token = \Smarty\Parser\TemplateParser::TP_TEXT;
     }
    */
  }

     