/** * Recursively process array declarations. * @return array * @throws Exception when there's an invalid token */ protected function process_array() { $t_array = array(); $t_count = 0; $this->tokens->ensure_matches(T_ARRAY); $this->tokens->ensure_matches('('); # Loop until we reach the end of the array while (!$this->tokens->matches(')')) { # A comma is required before each element except the first one if ($t_count > 0) { $this->tokens->ensure_matches(','); } switch ($this->tokens->type()) { # Nested array case T_ARRAY: $t_array[] = $this->process_array(); break; # Value # Value case T_CONSTANT_ENCAPSED_STRING: case T_STRING: case T_LNUMBER: case T_DNUMBER: $t_str = $this->process_value(); if ($this->tokens->matches(T_DOUBLE_ARROW)) { # key => value $this->tokens->pop(); if ($this->tokens->matches(T_ARRAY)) { $t_array[$t_str] = $this->process_array(); } else { $t_array[$t_str] = $this->process_value(); } } else { # Simple value $t_array[] = $t_str; } break; case ')': # Cover the trailing ',' case break; default: throw new Exception("Invalid token '" . $this->tokens->value() . "'"); } $t_count++; } $this->tokens->ensure_matches(')'); return $t_array; }