コード例 #1
0
ファイル: modure.c プロジェクト: A-L-E-X/micropython
mp_obj_t mod_re_compile(uint n_args, const mp_obj_t *args) {
    const char *re_str = mp_obj_str_get_str(args[0]);
    int size = re1_5_sizecode(re_str);
    mp_obj_re_t *o = m_new_obj_var(mp_obj_re_t, char, size);
    o->base.type = &re_type;
    int flags = 0;
    if (n_args > 1) {
        flags = mp_obj_get_int(args[1]);
    }
    int error = re1_5_compilecode(&o->re, re_str);
    if (error != 0) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error in regex"));
    }
    if (flags & FLAG_DEBUG) {
        re1_5_dumpcode(&o->re);
    }
    return o;
}
コード例 #2
0
ファイル: main.c プロジェクト: ampli/re1.5
int
main(int argc, char **argv)
{
	int i, j, k, l;
	int is_anchored = 0;

	argv++;
	argc--;
	while (argc > 0 && argv[0][0] == '-') {
		for (char *arg = &argv[0][1]; *arg; arg++) {
			switch (*arg) {
				case 'h':
					usage();
					break;
				case 'm':
					is_anchored = 1;
					break;
#ifdef DEBUG
				case 'd':
					debug = 1;
					break;
#endif
				case 'e':
					if (argv[1] == NULL)
						re1_5_fatal("-e: Missing Regex engine argument");
					if (re_engine)
						re1_5_fatal("-e: Regex engine already specified");
					re_engine = argv[1];
					argv++;
					argc--;
					break;
				default:
					re1_5_fatal("Unknown flag");
			}
		}
		argv++;
		argc--;
	}

	if(argc < 2)
		usage();

#ifdef ODEBUG
	// Old and unmaintained code
	Regexp *re = parse(argv[0]);
	printre(re);
	printf("\n");

	Prog *prog = compile(re);
	printprog(prog);
	printf("=============\n");
#endif
	int sz = re1_5_sizecode(argv[0]);
#ifdef DEBUG
	if (debug) printf("Precalculated size: %d\n", sz);
#endif
	if (sz == -1) {
		re1_5_fatal("Error in regexp");
	}

	ByteProg *code = malloc(sizeof(ByteProg) + sz);
	int ret = re1_5_compilecode(code, argv[0]);
        if (ret != 0) {
		re1_5_fatal("Error in regexp");
	}

	int sub_els = (code->sub + 1) * 2;
#ifdef DEBUG
	if (debug) re1_5_dumpcode(code);
#endif
	const char *sub[sub_els];
	int engine_found = 0;
	for(i=1; i<argc; i++) {
		printf("#%d %s\n", i, argv[i]);
		for(j=0; j<nelem(tab); j++) {
			Subject subj = {argv[i], argv[i] + strlen(argv[i])};
			if (re_engine) {
				if (0 != strcmp(re_engine, tab[j].name))
					continue;
				engine_found = 1;
			}
			printf("%s ", tab[j].name);
			memset(sub, 0, sub_els * sizeof sub[0]);
			if(!tab[j].fn(code, &subj, sub, sub_els, is_anchored)) {
				printf("-no match-\n");
				continue;
			}
			printf("match");
			for(k=sub_els; k>0; k--)
				if(sub[k-1])
					break;
			for(l=0; l<k; l+=2) {
				printf(" (");
				if(sub[l] == nil)
					printf("?");
				else
					printf("%d", (int)(sub[l] - argv[i]));
				printf(",");
				if(sub[l+1] == nil)
					printf("?");
				else
					printf("%d", (int)(sub[l+1] - argv[i]));
				printf(")");
			}
			printf("\n");
		}
		if (re_engine && !engine_found)
			re1_5_fatal("-e: Unknown engine name");
	}

	free(code);
	return 0;
}
コード例 #3
0
ファイル: compilecode.c プロジェクト: LGTMCU/f32c
int main(int argc, char *argv[])
{
    int pc = 0;
    ByteProg *code = re1_5_compilecode(argv[1]);
    re1_5_dumpcode(code);
}