/* Match a string against the given regular expression.
 * Returns true on a successful match.
 */
bool d_match(dstr line, const char *regex)
{
   re_cache_t *re;
   TRexMatch match;
   int i;

   re = compile_regex(regex);

   if (!trex_search(re->reg, line, NULL, NULL)) {
      return false;
   }

   trex_getsubexp(re->reg, 0, &match);
   d_assignn(d_before_match, line, match.begin - line);
   d_assign(d_after_match, match.begin + match.len);

   for (i = 0; i < MAX_MATCH; i++) {
      if (trex_getsubexp(re->reg, i, &match)) {
         strncpy(d_submatches[i], match.begin, match.len);
         d_submatches[i][match.len] = '\0';
      }
      else {
         d_submatches[i][0] = '\0';
      }
   }

   return true;
}
Example #2
0
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, &params_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, &param_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;
}
Example #3
0
int main(int argc, char* argv[])
{
	const TRexChar *begin,*end;
	TRexChar sTemp[200];
	const TRexChar *error = NULL;
	TRex *x = trex_compile(_TREXC("(x{1,5})xx"),&error);
	if(x) {
		trex_sprintf(sTemp,_TREXC("xxxxxxx"));
		if(trex_search(x,sTemp,&begin,&end))
		{
			int i,n = trex_getsubexpcount(x);
			TRexMatch match;
			for(i = 0; i < n; i++)
			{
				TRexChar t[200];
				trex_getsubexp(x,i,&match);
				trex_sprintf(t,_TREXC("[%%d]%%.%ds\n"),match.len);
				trex_printf(t,i,match.begin);
			}
			trex_printf(_TREXC("match! %d sub matches\n"),trex_getsubexpcount(x));
		}
		else {
			trex_printf(_TREXC("no match!\n"));
		}
		trex_free(x);
	}
	else {
		trex_printf(_TREXC("compilation error [%s]!\n"),error?error:_TREXC("undefined"));
	}
	return 0;
}
Example #4
0
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, &params_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, &param_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;
}
Example #5
0
File: zrex.c Project: Prarrot/czmq
const char *
zrex_hit (zrex_t *self, uint index)
{
    assert (self);
    assert (self->trex);

    if (index < self->hits) {
        //  We collect hits opportunistically to minimize use of the heap for
        //  complex expressions where the caller wants only a few hits.
        if (self->hit [index] == NULL) {
            //  We haven't fetched this hit yet, so grab it now
            TRexMatch match = { 0 };
            trex_getsubexp (self->trex, index, &match);
            self->hit [index] = (char *) malloc (match.len + 1);
            memcpy (self->hit [index], match.begin, match.len);
            self->hit [index][match.len] = 0;
        }
        return self->hit [index];
    }
    else
        return NULL;
}
Example #6
0
void test1()
{
	const char *error = NULL;
	const char *begin,*end;
	char *exp1 = "^([^ ]+) (/[^?# ]*)(\\?[^# ]*)?(#[^ ]*)? HTTP/([^ ]+)$";
	char *exp2 = "^([^ ]+): (.+)$";
	char *exp3 = "([^=]+)(=[^&])?(&([^=]+)(=[^&]))*";


	long long t = counter.Tick();
	TRex *x = trex_compile(exp3, &error);
	printf("compile %lld\n", counter.Tick() - t);

	t = counter.Tick();
	if(trex_search(x,"aaa=bbb&ccc=ddd&eee",&begin,&end))
	{
		printf("execute %lld\n", counter.Tick() - t);
		int i,n = trex_getsubexpcount(x);
		TRexMatch match;
		for(i = 1; i < n; i++)
		{
			t = counter.Tick();
			trex_getsubexp(x,i,&match);
			printf("trex_getsubexp %lld ", counter.Tick() - t);

			for(int j=0;j<match.len;j++)
				putchar(match.begin[j]);
			printf("\n");
		}
		printf("match! %d sub matches\n",trex_getsubexpcount(x));
	}
	else
	{
		printf("execute %lld\n", counter.Tick() - t);
		printf("no match!\n");
	}
}
int reSubEndTX (RegExpInfo *rx, int idx) {
  if (!rx->tx || idx < 0 || idx >= trex_getsubexpcount(rx->tx)) return -1;
  TRexMatch m;
  if (trex_getsubexp(rx->tx, idx, &m) != TRex_True) return -1;
  return (intptr_t)(m.begin-rx->bos)+m.len;
}