hi, i am working on a project where i convert the jack programming language code to an intermediate xml representation
here is the code of the function that does it
void JackTokenizer::tokenizeCode() {
    CODE tokens;
    CODE temp_tokens;

    std::string lexiconType;
    std::string xmlElement;
    std::string str;

    pugi::xml_document xmlDoc;
    node class_node;
    node classVarNode;
    int counter = 0;
    node subroutineNode;
    node test2;
    node subroutineTypeNode;
    node parameterNode;

    for (auto &code: m_code) {

        auto tokenNoSpace = std::find_if_not(code.rbegin(), code.rend(), ::isspace).base();
        std::string token(code.begin(), tokenNoSpace);
        auto tokenNoSpace2 = std::find_if_not(token.begin(), token.end(), ::isspace).base();
        token = tokenNoSpace2;

        if (isNotEmpty(token)){
            temp_tokens = getCodeVector(token, ' ');

            for (auto &item: temp_tokens){
                if (isValid(validKeywords, item) || isValid(validSymbols, item)){

                    if (item == "class"){
                        class_node = xmlDoc.append_child("class");
                        codeInfo.classDec = true;
                    }

                    if (isValid(validSubroutineDec, item)){
                        codeInfo.subroutineDec = true;
                        subroutineNode = class_node.append_child("subroutineDec");
                    }

                    node typeNode;
                    lexiconType = isValid(validKeywords, item) ? "keyword" : "symbol";

                    if (isValid(validVarDecs, item)){
                        codeInfo.varDec = true;
                    }

                    if (codeInfo.classDec && (item == "{") && !(codeInfo.codeSwitch) && !(codeInfo.subroutineDec)){
                        codeInfo.classDecBegin = true;
                    }

                    if (!(codeInfo.varDec) && (codeInfo.classVarDec)){
                        codeInfo.classVarDec = false;
                    }

                    std::string temp = "classVarDec" + std::to_string(counter);

                    if (codeInfo.classVarDec && codeInfo.varDecEnd){
                        codeInfo.varDecEnd = false;
                        codeInfo.codeSwitch = true;
                        if (codeInfo.classDecBegin && codeInfo.varDec){
                            codeInfo.classVarDec = true;
                            codeInfo.classDecBegin = false;
                        }
                        classVarNode = class_node.append_child(temp.c_str());
                        counter++;
                    }
                    else if (codeInfo.classDec && codeInfo.classDecBegin && codeInfo.varDec){
                        classVarNode = class_node.append_child(temp.c_str());
                        codeInfo.classVarDec = true;
                        codeInfo.classDecBegin = false;
                        counter++;
                    }

                    if (codeInfo.classDec && (item == "{") && !(codeInfo.codeSwitch) && !(codeInfo.subroutineDec)){
                        codeInfo.classDecBegin = true;
                    }

                    if (codeInfo.classVarDec){
                        node childNode = classVarNode.append_child(lexiconType.c_str());
                        childNode.append_child(pugi::node_pcdata).set_value(item.c_str());
                    }
////                    else if ((codeInfo.subroutineDec) && (item.length() == 1)){
//                        std::cout << "INFO: " << item << std::endl;
//                    }
                    else if ((codeInfo.subroutineDec)){
                        if (item.length() == 1){
//                            codeInfo.subroutineDec = false;
                                std::cout << "INFO: " << item << std::endl;
                        }
                        subroutineTypeNode = subroutineNode.append_child(lexiconType.c_str());
                        auto it = std::find(temp_tokens.begin(), temp_tokens.end(), "(");
                        if (it != temp_tokens.end()) {
                            auto index = std::distance(temp_tokens.begin(), it);
                            auto it2 = std::find(temp_tokens.begin(), temp_tokens.end(), ")");
                            auto index2 = std::distance(temp_tokens.begin(), it2);
                            if ((index+1) != index2){
                                if (item == "("){
                                    subroutineTypeNode.append_child(pugi::node_pcdata).set_value(item.c_str());
                                    parameterNode = subroutineNode.append_child("parameterList");
                                    codeInfo.parameterDec = true;
                                    continue;
                                }
                                else if ((item == ")") && (codeInfo.parameterDec)){
                                    node closingChild = subroutineNode.insert_child_after("symbol", parameterNode);
                                    closingChild.append_child(pugi::node_pcdata).set_value(item.c_str());
                                    codeInfo.parameterDec = false;
                                    continue;
                                }
                                else if (item=="{"){
                                    // figure out how to fix this thing where we have invalid xml
//                                    generally before an {
                                       node childNode = classVarNode.append_child(lexiconType.c_str());
                                       childNode.append_child(pugi::node_pcdata).set_value(item.c_str());
                                }
                            }
                        }
                        if (codeInfo.parameterDec){
                            parameterNode.append_child(lexiconType.c_str()).text().set(item.c_str());
//                            subroutineTypeNode.append_child(pugi::node_pcdata).set_value(item.c_str());
                        }
                        else{
                            subroutineTypeNode.append_child(pugi::node_pcdata).set_value(item.c_str());
                        }
                    }
                    else{
                        typeNode = class_node.append_child(lexiconType.c_str());
                        typeNode.append_child(pugi::node_pcdata).set_value(item.c_str());

                    }
                }
                else{
                    lexiconType = "identifier";
                    if (isNotEmpty(item)) {
                        if (codeInfo.classVarDec){
                            node childNode = classVarNode.append_child(lexiconType.c_str());
                            childNode.append_child(pugi::node_pcdata).set_value(item.c_str());
                        }
                        else if (codeInfo.subroutineDec){
                            if (codeInfo.parameterDec){
                                subroutineTypeNode = parameterNode.append_child(lexiconType.c_str());
                                subroutineTypeNode.append_child(pugi::node_pcdata).set_value(item.c_str());
                            }
                            else{
                                node childNode = subroutineNode.append_child(lexiconType.c_str());
                                childNode.append_child(pugi::node_pcdata).set_value(item.c_str());
                            }
                        }
                        else{
                            node type_node = class_node.append_child(lexiconType.c_str());
                            type_node.append_child(pugi::node_pcdata).set_value(item.c_str());

                        }

                    }
                }
            }
            if (codeInfo.classVarDec && codeInfo.varDec){
                codeInfo.varDecEnd = true;
            }
            codeInfo.varDec = false;
            codeInfo.subroutineDec = false;
            codeInfo.parameterDec = false;
        }

    }

    xmlDoc.save(std::cout);
}

it takes a vector which has jack code as elements for example "method", "main", "(", etc.
and it is supposed to output that with xml formatting, for some reason it outputs everything correctly except the following output where it has for some reason incomplete xml tags
<class>
        <keyword>class</keyword>
        <identifier>Square</identifier>
        <symbol>{</symbol>
        <subroutineDec>
                <keyword>constructor</keyword>
                <identifier>Square</identifier>
                <identifier>new</identifier>
                <symbol>(</symbol>
                <parameterList>
                        <keyword>int</keyword>
                        <identifier>Ax</identifier>
                        <symbol>,</symbol>
                        <keyword>int</keyword>
                        <identifier>Ay</identifier>
                        <symbol>,</symbol>
                        <keyword>int</keyword>
                        <identifier>Asize</identifier>
                </parameterList>
                <symbol>)</symbol>
                <keyword /> <- incomplete
                <symbol /> <- incomplete
                <keyword /> <- incomplete
                <symbol /> <- incomplete
                <keyword /> <- incomplete
                <symbol /> <- incomplete
                <symbol>{</symbol>
        </subroutineDec>

this hapens with the following input
class Square {
   constructor Square new(int Ax, int Ay, int Asize) {

can you identify what part of the code is causing this problem