std::vector<SyntaxElement> largestMatches(const EBNF& grammar,const std::string& content, const SyntaxElement& previous) { /* Function that returns a vector of the largest, first occuring matches for further processing. */ std::vector<SyntaxElement> matches; std::vector<std::pair<std::string,std::vector<std::pair<std::string,uint_type> > > > all_matches; uint_type index = 0; /* Find all matches for all regular expressions. */ for (auto& elem : grammar.regex_map) { PARSE_OUT << "Finding matches for: " << elem.second.rule_id << std::endl; auto regex_matches = RegexHelper::getListOfMatches(elem.second.assemble(grammar.regex_map),content); all_matches.push_back(std::pair<std::string,std::vector<std::pair<std::string,uint_type> > >(elem.second.rule_id,regex_matches)); } /* Find the largest match by checking all matches. */ #ifndef EBNF_GIVE_UP_EASILY while (index < content.size()) { #endif bool are_matches = true; while(index < content.size() && are_matches) { std::pair<std::string,uint_type> largest("",0); std::string type_string; are_matches = false; for (uint_type iter_all = 0; iter_all < all_matches.size(); iter_all++) { for (uint_type iter = 0; iter < all_matches[iter_all].second.size(); iter++) { if (all_matches[iter_all].second[iter].second == index && all_matches[iter_all].second[iter].first.size() > largest.first.size() && all_matches[iter_all].second[iter].first != content) { type_string = all_matches[iter_all].first; largest = all_matches[iter_all].second[iter]; are_matches |= true; } } } if (are_matches) { index = largest.first.size() + largest.second; matches.push_back(SyntaxElement(largest.second + previous.index, type_string, largest.first)); } } #ifndef EBNF_GIVE_UP_EASILY index++; } #endif return matches; }
logical OSyntaxHighlighter :: Add (std_string szClassName, std_string szStartElement, std_string szEndElement ) { logical term = NO; BEGINSEQ SyntaxClass *pClass = NULL; std::vector<SyntaxClass*>::iterator it; for ( it = classes.begin(); it != classes.end(); ++it ){ if((*it)->name == szClassName) pClass = (*it); } if(!pClass) LEAVESEQ elements.push_back(SyntaxElement(pClass,szStartElement,szEndElement)); ENDSEQ return term; }
Trie<SyntaxElement> buildTree(const EBNF& grammar, const std::string& source) { return recurseParse(grammar,SyntaxElement(0,"__syntax_tree_whole__",source)); }