/**
 * Convert template text into tempcode format.
 *
 * @param  string		The template text
 * @param  integer		The position we are looking at in the text
 * @param  boolean		Whether this text is infact a directive, about to be put in the context of a wider template
 * @param  ID_TEXT		The codename of the template (e.g. foo)
 * @param  ?ID_TEXT		The theme it is for (NULL: current theme)
 * @param  ?ID_TEXT		The language it is for (NULL: current language)
 * @param  boolean		Whether to tolerate errors
 * @return mixed		The converted/compiled template as tempcode, OR if a directive, encoded directive information
 */
function template_to_tempcode($text, $symbol_pos = 0, $inside_directive = false, $codename = '', $theme = NULL, $lang = NULL, $tolerate_errors = false)
{
    require_code('comcode_conversion');
    return template_to_tempcode_static($text, $symbol_pos, $inside_directive, $codename, $theme, $lang);
}
/**
 * Convert template text into tempcode format.
 *
 * @param  string			The template text
 * @param  integer		The position we are looking at in the text
 * @param  boolean		Whether this text is infact a directive, about to be put in the context of a wider template
 * @param  ID_TEXT		The codename of the template (e.g. foo)
 * @param  ?ID_TEXT		The theme it is for (NULL: current theme)
 * @param  ?ID_TEXT		The language it is for (NULL: current language)
 * @return mixed			The converted/compiled template as tempcode, OR if a directive, encoded directive information
 */
function template_to_tempcode_static($text, $symbol_pos = 0, $inside_directive = false, $codename = '', $theme = NULL, $lang = NULL)
{
    if (is_null($theme)) {
        $theme = $GLOBALS['FORUM_DRIVER']->get_theme();
    }
    if (defined('HIPHOP_PHP')) {
        $out = new ocp_tempcode();
    } else {
        $out = new ocp_tempcode_static();
    }
    $continuation = '';
    $chr_10 = chr(10);
    $symbol_len = strlen($text);
    while (true) {
        $jump_to = strpos($text, '{', $symbol_pos);
        $jump_to_b = strpos($text, '\\', $symbol_pos);
        if ($jump_to_b !== false && ($jump_to === false || $jump_to_b < $jump_to)) {
            $continuation .= substr($text, $symbol_pos, $jump_to_b - $symbol_pos);
            $symbol_pos = $jump_to_b + 1;
            $nn = @$text[$symbol_pos];
            if ($nn == '{' || $nn == '}' || $nn == ',') {
                continue;
                // Used to escape. We will pick up on in next iteration and also re-detect it is escaped.
            } else {
                $continuation .= '\\';
                // Just a normal slash, not for escaping.
                continue;
            }
        }
        if ($jump_to === false) {
            $continuation .= substr($text, $symbol_pos);
            break;
        } else {
            $continuation .= substr($text, $symbol_pos, $jump_to - $symbol_pos);
            $symbol_pos = $jump_to + 1;
            $nn_pre = $symbol_pos >= 2 ? $text[$symbol_pos - 2] : '';
            $nn = @$text[$symbol_pos];
            if (preg_match('#[\\dA-Z\\$\\+\\!\\_]#', $nn) != 0 && $nn_pre !== '\\') {
                if ($continuation != '') {
                    $out->attach($continuation);
                }
                $continuation = '';
                $ret = read_single_uncompiled_variable($text, $symbol_pos, $symbol_len, $theme);
                if ($ret[1] == TC_DIRECTIVE) {
                    if ($ret[2] == 'START') {
                        $temp = template_to_tempcode_static($text, $symbol_pos, true);
                        if (is_array($temp)) {
                            list($_out, $symbol_pos) = $temp;
                            if ($ret[3] === NULL) {
                                $ret[3][] = $_out;
                                // The inside of the directive becomes the final parameter
                                $out->bits[] = array($ret[0], TC_DIRECTIVE, '', $ret[3]);
                            } else {
                                $name = array_shift($ret[3]);
                                $ret[3][] = $_out;
                                // The inside of the directive becomes the final parameter
                                $directive_type = $name->evaluate();
                                $out->bits[] = array($ret[0], TC_DIRECTIVE, $directive_type, $ret[3]);
                                //						$out->children[]=array(':directive: '.$directive_type,$_out->children,true);
                            }
                        }
                    } elseif ($ret[2] == 'END') {
                        if ($inside_directive) {
                            return array($out, $symbol_pos);
                        }
                        if (!$GLOBALS['LAX_COMCODE']) {
                            require_code('site');
                            attach_message(make_string_tempcode(do_lang('UNMATCHED_DIRECTIVE') . ' just before... ' . substr($text, $symbol_pos, 100) . ' [' . $codename . ']'), 'warn');
                        }
                    } else {
                        $out->bits[] = $ret;
                    }
                } else {
                    if ($ret[1] != TC_SYMBOL || $ret[2] != '') {
                        $out->bits[] = $ret;
                    }
                }
            } else {
                $continuation .= '{';
            }
        }
    }
    if ($continuation != '') {
        $out->attach($continuation);
    }
    return $out;
}