void Lexer::PushBack(Token& t) { DEBUG("Pushback: %s", t.ToString().c_str() ); pushback_tokens.push_back(t); }
void ParseTokens(vector<Token>& tokens, XMLDocument& outDoc){ Stack<Token> tokenStack; Stack<XMLElement> elementStack; bool inTag = false; for(auto iter = tokens.begin(); iter != tokens.end(); iter++){ Token token = *iter; //cout << "Parsing token: |" << token << "|" << endl; if(token == "<"){ inTag = true; tokenStack.Push(token); } else if(token == "/"){ if(tokenStack.Top() == "<"){ //Add the element to the doc XMLElement elem = elementStack.Pop(); if(elementStack.count == 0){ outDoc.contents.push_back(elem); } else{ elementStack.Top().children.push_back(elem); } iter++; iter++; } else{ tokenStack.Push(token); } } else if(token == ">"){ inTag = false; bool startList = true; if(tokenStack.Top() == "/"){ startList = false; tokenStack.Pop(); } Token currToken = tokenStack.Pop(); XMLAttribute attr; attr.data = attr.name = ""; bool gotData = false; while(currToken != "<"){ if(currToken == "="){ //do nothing } else if(!gotData){ attr.data = currToken.ToString(); gotData = true; } else{ attr.name = currToken.ToString(); elementStack.Top().AddAttribute(attr.name, attr.data); attr.data = attr.name = ""; gotData = false; } currToken = tokenStack.Pop(); } if(!startList){ XMLElement elem = elementStack.Pop(); if(elementStack.count == 0){ outDoc.contents.push_back(elem); } else{ elementStack.Top().children.push_back(elem); } } } else{ if(!inTag){ XMLElement elem; elem.name = "__plaintext__"; elem.AddAttribute("val", token.ToString()); if(elementStack.count == 0){ outDoc.contents.push_back(elem); } else{ elementStack.Top().children.push_back(elem); } continue; } else if(tokenStack.Top() == "<"){ XMLElement elem; elem.name = token.ToString(); //cout << "Pushing to element stack: " << token << endl; elementStack.Push(elem); } //cout << "Pushing token: "<< token << endl; tokenStack.Push(token); } } }