Keywords parse_c(const ParserSettings *ps, const char *text) { static TrRegex tr_function("(?:([\\w:]+)[*&]*\\s+(?:[*&=]*\\s+)?[*&]*)?([^\\s\\(]+)\\s*(\\([^)]*\\))"); static TrRegex tr_parameters("(\\$?\\w+|\\.\\.\\.)(\\s*=\\s*[\\\"\\w\\.']+)?\\s*[,)]"); Keywords keywords; const TRexChar *begin, *end; int chevron = 0; // HACK: Duplicate the string so it can be modified and any commas within <...> needs removed to support templates e.g. std::pair<int, double> std::string dup(text); for (char &c : dup) { if (c == '<') chevron++; else if (c == '>') chevron--; else if (c == ',' && chevron > 0) c = ' '; } if (trex_search(tr_function.regex, dup.c_str(), &begin, &end)) { std::vector<std::string> params; std::vector<std::string> function; TRexMatch params_match, func_match; const TRexChar *cur_params; const TRexChar *p_begin, *p_end; //trex_getsubexp(tr_function, 1, &return_match); // not used for now trex_getsubexp(tr_function.regex, 2, &func_match); trex_getsubexp(tr_function.regex, 3, ¶ms_match); keywords.function = std::string(func_match.begin, func_match.len); // For each param cur_params = params_match.begin; while (trex_searchrange(tr_parameters.regex, cur_params, end, &p_begin, &p_end)) { TRexMatch param_match; trex_getsubexp(tr_parameters.regex, 1, ¶m_match); // handle "func(void)" by skipping it if (strncmp(param_match.begin, "void", 4) != 0) keywords.parameters.push_back(std::string(param_match.begin, param_match.len)); cur_params = p_end; } } return keywords; }
Keywords Parse_C(const ParserDefinition *pd, const char *text) { Keywords keywords; std::vector<std::string> params; std::vector<std::string> function; const TRexChar *begin,*end; if(trex_search(tr_function, text, &begin, &end)) { TRexMatch params_match, func_match; const TRexChar *cur_params; const TRexChar *p_begin, *p_end; //trex_getsubexp(tr_function, 1, &return_match); // not used for now trex_getsubexp(tr_function, 2, &func_match); trex_getsubexp(tr_function, 3, ¶ms_match); function.push_back(std::string(func_match.begin, func_match.len)); // For each param cur_params = params_match.begin; while(trex_searchrange(tr_parameters, cur_params, end, &p_begin, &p_end)) { TRexMatch param_match; trex_getsubexp(tr_parameters, 1, ¶m_match); // handle "func(void)" by skipping it if(strncmp(param_match.begin, "void", 4) != 0) params.push_back(std::string(param_match.begin, param_match.len)); cur_params = p_end; } keywords["$PARAM"] = params; keywords["$FUNCTION"] = function; } return keywords; }
int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags) { const char _begin, _end; const char *begin = &_begin, *end = &_end; const char *str_beg = string, *str_end = string + strlen(string); int count = 0; if (!pmatch || !preg) { return -EINVAL; } if (!preg->regex_extended) { return -EINVAL; } while (trex_searchrange(preg->regex_extended, str_beg, str_end, &begin, &end)) { if (nmatch <= count) { break; } pmatch[count].rm_so = begin - string; pmatch[count].rm_eo = end - string; str_beg = begin + 1; count++; } pmatch[count].rm_so = 0; pmatch[count].rm_eo = 0; if (!count) { return -REG_NOMATCH; } return 0; }