<?php
/* Driver template for the PHP_CSS_ParserrGenerator parser generator. (PHP port of LEMON)
*/

/**
 * This can be used to store both the string representation of
 * a token, and any useful meta-data associated with the token.
 *
 * meta-data should be stored as an array
 */
class CSS_ParseryyToken implements ArrayAccess
{
    public 
$string '';
    public 
$metadata = array();

    function 
__construct($s$m = array())
    {
        if (
$s instanceof CSS_ParseryyToken) {
            
$this->string $s->string;
            
$this->metadata $s->metadata;
        } else {
            
$this->string = (string) $s;
            if (
$m instanceof CSS_ParseryyToken) {
                
$this->metadata $m->metadata;
            } elseif (
is_array($m)) {
                
$this->metadata $m;
            }
        }
    }

    function 
__toString()
    {
        return 
$this->_string;
    }

    function 
offsetExists($offset)
    {
        return isset(
$this->metadata[$offset]);
    }

    function 
offsetGet($offset)
    {
        return 
$this->metadata[$offset];
    }

    function 
offsetSet($offset$value)
    {
        if (
$offset === null) {
            if (isset(
$value[0])) {
                
$x = ($value instanceof CSS_ParseryyToken) ?
                    
$value->metadata $value;
                
$this->metadata array_merge($this->metadata$x);
                return;
            }
            
$offset count($this->metadata);
        }
        if (
$value === null) {
            return;
        }
        if (
$value instanceof CSS_ParseryyToken) {
            if (
$value->metadata) {
                
$this->metadata[$offset] = $value->metadata;
            }
        } elseif (
$value) {
            
$this->metadata[$offset] = $value;
        }
    }

    function 
offsetUnset($offset)
    {
        unset(
$this->metadata[$offset]);
    }
}

/** The following structure represents a single element of the
 * parser's stack.  Information stored includes:
 *
 *   +  The state number for the parser at this level of the stack.
 *
 *   +  The value of the token stored at this level of the stack.
 *      (In other words, the "major" token.)
 *
 *   +  The semantic value stored at this level of the stack.  This is
 *      the information used by the action routines in the grammar.
 *      It is sometimes called the "minor" token.
 */
class CSS_ParseryyStackEntry
{
    public 
$stateno;       /* The state-number */
    
public $major;         /* The major token value.  This is the code
                     ** number for the token at this stack level */
    
public $minor/* The user-supplied minor token value.  This
                     ** is the value of the token  */
};

// code external to the class is included here
#line 4 "Parser.y"

/*
require_once('CSS/DOM/CSSCharsetRule.php');
require_once('CSS/DOM/CSSImportRule.php');
require_once('CSS/DOM/CSSMediaRule.php');
*/
require_once('Structures/CSS.php');
require_once(
'Structures/CSS/Value/Generic.php');
require_once(
'Structures/CSS/Value/Numeric.php');
require_once(
'Structures/CSS/Value/Unit.php');
require_once(
'Structures/CSS/Value/Color.php');
require_once(
'Structures/CSS/Value/Function.php');
require_once(
'Structures/CSS/Value/Uri.php');
require_once(
'Structures/CSS/Declaration.php');
require_once(
'Structures/CSS/Rule/Set.php');
require_once(
'Structures/CSS/Rule/AtPage.php');
require_once(
'Structures/CSS/Rule/AtMedia.php');
require_once(
'Structures/CSS/Rule/AtImport.php');
require_once(
'Structures/CSS/Selector.php');
require_once(
'CSS/Parser/Exception/Syntax.php');
#line 120 "Parser.php"

// declare_class is output here
#line 24 "Parser.y"
class CSS_Parser#line 125 "Parser.php"
{
/* First off, code is included which follows the "include_class" declaration
** in the input file. */
#line 31 "Parser.y"

    
public $result;
    public function 
tokenizeAndParse($lexer$input
    {
        
$lexer->setInput($input);
        try {
            
$lastTokenLength 0;
            while (
$token $lexer->nextToken()) {
                
$this->doParse($token['token'], $token['value']);
                
$lastTokenLength strlen($token['value']); // We may need this for error reporting
            
}
            
$unexpectedEOF true;
            
$this->doParse(0,0);
            unset(
$unexpectedEOF);
            return 
$this->result;
        } catch (
Exception $e) {
            
// We have a parsing error. Collect some usefull info
            // 1. Was this an EOF error?
            
if (isset($unexpectedEOF) && $unexpectedEOF) {
                require_once(
'CSS/Parser/Exception/EOF.php');
                throw new 
CSS_Parser_Exception_EOF;
            } else {
                require_once(
'CSS/Parser/Exception/Syntax.php');
                
// 2. No eof error, find out which line/column it ocurred in
                
$rows explode("\n"$input);
                
$errorCharacter $lexer->_cursor $lastTokenLength;

                
$line 0;
                
$marker 0;
                while (
$marker <= $errorCharacter) {
                    
$marker += strlen($rows[$line]) + 1// explode removed the \n so we add 1 phantom character
                    
$line++;
                }
                
$line--; // The cycle overshoots the line, so go back one line
                
$marker -= strlen($rows[$line]) + 1// and reset the marker
                
$column $errorCharacter $marker// What's leftover in the marker is the column
                
$line++; $column++; // Humans tend to start counting by 1, not 0

                // 3. Let's grab contextual input for presentation purposes
                
$contextual = array();
                for (
$i 0$i 3$i++) if (array_key_exists($line $i$rows)) $contextual[$i] = $rows[$line $i]; else $contextual[$i] = null;

                throw new 
CSS_Parser_Exception_Syntax($line$column$lastTokenLength$contextual);
            }
        }
    }
#line 177 "Parser.php"

/* Next is all token values, as class constants
*/
/* 
** These constants (all generated automatically by the parser generator)
** specify the various kinds of tokens (terminals) that the parser
** understands. 
**
** Each symbol here is a terminal symbol in the grammar.
*/
    
const SEMICOLON                      =  1;
    const 
CHARSET_SYM                    =  2;
    const 
SS                             =  3;
    const 
STRING                         =  4;
    const 
CDO                            =  5;
    const 
CDC                            =  6;
    const 
IMPORT_SYM                     =  7;
    const 
URI                            =  8;
    const 
COMMA                          =  9;
    const 
LBRACE                         10;
    const 
RBRACE                         11;
    const 
MEDIA_SYM                      12;
    const 
IDENT                          13;
    const 
PAGE_SYM                       14;
    const 
COLON                          15;
    const 
SLASH                          16;
    const 
GREATER                        17;
    const 
PLUS                           18;
    const 
MINUS                          19;
    const 
HASH                           20;
    const 
DOT                            21;
    const 
STAR                           22;
    const 
LBRACKET                       23;
    const 
RBRACKET                       24;
    const 
INCLUDES                       25;
    const 
DASHMATCH                      26;
    const 
EQUAL                          27;
    const 
CSSFUNCTION                    28;
    const 
RPAREN                         29;
    const 
IMPORTANT_SYM                  30;
    const 
NUMBER                         31;
    const 
PERCENTAGE                     32;
    const 
LENGTH                         33;
    const 
EMS                            34;
    const 
EXS                            35;
    const 
ANGLE                          36;
    const 
TIME                           37;
    const 
FREQ                           38;
    const 
DIMENSION                      39;
    const 
COMMENT                        40;
    const 
YY_NO_ACTION 399;
    const 
YY_ACCEPT_ACTION 398;
    const 
YY_ERROR_ACTION 397;

/* Next are that tables used to determine what action to take based on the
** current state and lookahead token.  These tables are used to implement
** functions that take a state number and lookahead value and return an
** action integer.  
**
** Suppose the action integer is N.  Then the action is determined as
** follows
**
**   0 <= N < self::YYNSTATE                              Shift N.  That is,
**                                                        push the lookahead
**                                                        token onto the stack
**                                                        and goto state N.
**
**   self::YYNSTATE <= N < self::YYNSTATE+self::YYNRULE   Reduce by rule N-YYNSTATE.
**
**   N == self::YYNSTATE+self::YYNRULE                    A syntax error has occurred.
**
**   N == self::YYNSTATE+self::YYNRULE+1                  The parser accepts its
**                                                        input. (and concludes parsing)
**
**   N == self::YYNSTATE+self::YYNRULE+2                  No such action.  Denotes unused
**                                                        slots in the yy_action[] table.
**
** The action table is constructed as a single large static array $yy_action.
** Given state S and lookahead X, the action is computed as
**
**      self::$yy_action[self::$yy_shift_ofst[S] + X ]
**
** If the index value self::$yy_shift_ofst[S]+X is out of range or if the value
** self::$yy_lookahead[self::$yy_shift_ofst[S]+X] is not equal to X or if
** self::$yy_shift_ofst[S] is equal to self::YY_SHIFT_USE_DFLT, it means that
** the action is not in the table and that self::$yy_default[S] should be used instead.  
**
** The formula above is for computing the action when the lookahead is
** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
** a reduce action) then the static $yy_reduce_ofst array is used in place of
** the static $yy_shift_ofst array and self::YY_REDUCE_USE_DFLT is used in place of
** self::YY_SHIFT_USE_DFLT.
**
** The following are the tables generated in this section:
**
**  self::$yy_action        A single table containing all actions.
**  self::$yy_lookahead     A table containing the lookahead for each entry in
**                          yy_action.  Used to detect hash collisions.
**  self::$yy_shift_ofst    For each state, the offset into self::$yy_action for
**                          shifting terminals.
**  self::$yy_reduce_ofst   For each state, the offset into self::$yy_action for
**                          shifting non-terminals after a reduce.
**  self::$yy_default       Default action for each state.
*/
    
const YY_SZ_ACTTAB 495;
static public 
$yy_action = array(
 
/*     0 */   398,  218,    1,  129,   32,   42,   81,   83,   15,  146,
 
/*    10 */     2,    3,   30,  143,  135,  134,   36,   63,  170,  120,
 
/*    20 */    38,   98,  138,   49,  102,   21,  193,  108,  118,   35,
 
/*    30 */   153,  152,  185,   48,  116,   55,   45,   20,   23,  119,
 
/*    40 */   194,  195,  192,  174,   39,   22,  119,  194,  195,  192,
 
/*    50 */     2,    3,   30,  143,  135,  134,   36,   44,  131,  111,
 
/*    60 */    38,  146,  138,   49,   96,  172,  150,  151,  157,  187,
 
/*    70 */   211,  210,  185,   48,  116,   82,   45,   20,   23,  119,
 
/*    80 */   194,  195,  192,   58,   82,   80,    4,   31,  143,  135,
 
/*    90 */   134,   36,   62,  126,  120,   38,   98,  138,   49,   84,
 
/*   100 */    82,  193,  108,  118,   35,   84,  219,  185,   48,  116,
 
/*   110 */    79,   45,   20,   23,  119,  194,  195,  192,   82,   57,
 
/*   120 */    60,   59,   18,   53,   85,  181,  114,  104,   56,   61,
 
/*   130 */   120,   64,   98,  154,  167,   52,   78,  193,  108,  118,
 
/*   140 */    35,  169,  144,   10,  144,  144,   94,   71,   93,   89,
 
/*   150 */    90,   91,   73,   51,   50,   57,  105,  224,   12,   53,
 
/*   160 */   205,  194,  195,  192,   56,   61,  120,   64,   98,  154,
 
/*   170 */   167,   52,   33,  193,  108,  118,   35,   95,   16,   10,
 
/*   180 */    39,  177,   94,   71,   93,   89,   90,   91,   73,   51,
 
/*   190 */    50,  175,  176,  173,  188,  202,  201,  198,   38,   25,
 
/*   200 */   138,   49,  208,  151,  157,  187,  211,  210,   34,    5,
 
/*   210 */   185,   48,  116,   21,   45,   20,   23,  119,  194,  195,
 
/*   220 */   192,   87,  144,   69,  144,  144,   85,   97,  172,   75,
 
/*   230 */    75,   61,  120,   64,   98,   21,   72,   72,    6,  193,
 
/*   240 */   108,  118,   35,  117,   66,   68,  120,   82,   98,   70,
 
/*   250 */    67,    8,  125,  193,  108,  118,   35,  117,   88,  130,
 
/*   260 */    34,  178,  185,   48,  116,    7,   45,   20,   23,  119,
 
/*   270 */   194,  195,  192,  184,  113,   78,  185,   48,  116,   74,
 
/*   280 */    45,   20,   23,  119,  194,  195,  192,   15,   82,   11,
 
/*   290 */   171,  128,  185,   48,  116,   19,   45,   20,   23,  119,
 
/*   300 */   194,  195,  192,   43,   27,  127,   65,  146,   80,  216,
 
/*   310 */   152,   39,   94,   71,   93,   89,   90,   91,   73,   51,
 
/*   320 */    50,  100,  183,  179,   39,   46,   29,  110,   77,  146,
 
/*   330 */    40,  122,  197,  208,  151,  157,  187,  211,  210,   39,
 
/*   340 */   106,  203,   92,   24,  168,   21,  208,  151,  157,  187,
 
/*   350 */   211,  210,  190,  156,  149,   54,  204,  124,   41,  109,
 
/*   360 */   123,  208,  151,  157,  187,  211,  210,  199,  133,  220,
 
/*   370 */    37,   45,   20,   23,  119,  194,  195,  192,  206,  115,
 
/*   380 */    45,   20,   23,  119,  194,  195,  192,   86,   27,  120,
 
/*   390 */    82,   98,   82,  351,   28,   27,  193,  108,  118,   35,
 
/*   400 */    84,   75,   80,  351,  164,  103,  183,  179,   72,  353,
 
/*   410 */   189,  110,  101,  183,  179,   98,   82,   75,  110,  353,
 
/*   420 */   193,  108,   76,   35,   72,   17,   79,   26,  168,   27,
 
/*   430 */    88,    9,  120,  180,   98,  168,  191,  110,   76,  193,
 
/*   440 */   108,  118,   35,  160,   99,  183,  179,  182,  179,  166,
 
/*   450 */   110,  163,  110,  200,  168,  162,  196,  121,   15,  159,
 
/*   460 */   132,  214,  207,  158,  141,   80,   14,  168,  212,  168,
 
/*   470 */   213,  137,  186,   13,  275,  145,  107,  161,  275,  112,
 
/*   480 */   155,  223,  209,  221,  136,  215,  222,  165,  139,  217,
 
/*   490 */   148,   47,  140,  142,  147,
    );
    static public 
$yy_lookahead = array(
 
/*     0 */    42,   43,   44,   45,   46,   47,   48,   50,   70,   51,
 
/*    10 */    52,   53,   54,   55,   56,   57,   58,   11,   61,   13,
 
/*    20 */    62,   15,   64,   65,   50,    1,   20,   21,   22,   23,
 
/*    30 */    92,   93,   74,   75,   76,   11,   78,   79,   80,   81,
 
/*    40 */    82,   83,   84,   45,   72,   80,   81,   82,   83,   84,
 
/*    50 */    52,   53,   54,   55,   56,   57,   58,   47,  100,   85,
 
/*    60 */    62,   51,   64,   65,   60,   61,   94,   95,   96,   97,
 
/*    70 */    98,   99,   74,   75,   76,    3,   78,   79,   80,   81,
 
/*    80 */    82,   83,   84,   11,    3,   13,   53,   54,   55,   56,
 
/*    90 */    57,   58,   11,    1,   13,   62,   15,   64,   65,   13,
 
/*   100 */     3,   20,   21,   22,   23,   13,    3,   74,   75,   76,
 
/*   110 */    13,   78,   79,   80,   81,   82,   83,   84,    3,    4,
 
/*   120 */    17,   18,   10,    8,    7,   50,   29,   15,   13,   12,
 
/*   130 */    13,   14,   15,   18,   19,   20,    4,   20,   21,   22,
 
/*   140 */    23,   50,    3,   28,    5,    6,   31,   32,   33,   34,
 
/*   150 */    35,   36,   37,   38,   39,    4,   49,   13,   50,    8,
 
/*   160 */    81,   82,   83,   84,   13,   12,   13,   14,   15,   18,
 
/*   170 */    19,   20,   28,   20,   21,   22,   23,    9,   10,   28,
 
/*   180 */    72,   50,   31,   32,   33,   34,   35,   36,   37,   38,
 
/*   190 */    39,   55,   56,   57,   24,   25,   26,   27,   62,   91,
 
/*   200 */    64,   65,   94,   95,   96,   97,   98,   99,    9,   10,
 
/*   210 */    74,   75,   76,    1,   78,   79,   80,   81,   82,   83,
 
/*   220 */    84,    2,    3,   11,    5,    6,    7,   60,   61,    9,
 
/*   230 */     9,   12,   13,   14,   15,    1,   16,   16,   50,   20,
 
/*   240 */    21,   22,   23,   55,   11,   11,   13,    3,   15,   29,
 
/*   250 */    29,   63,    1,   20,   21,   22,   23,   55,   13,   40,