-
Notifications
You must be signed in to change notification settings - Fork 4
/
ex.c
73 lines (65 loc) · 2.1 KB
/
ex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
#include <stdlib.h>
#include <stdio.h>
#include "rejit.h"
#define ERR(...) fprintf(stderr, __VA_ARGS__)
rejit_matcher compile(char* regex) {
rejit_parse_error err;
rejit_matcher m = rejit_parse_compile(regex, &err, RJ_FUNICODE);
if (err.kind != RJ_PE_NONE) {
ERR("parse error at %zu: ", err.pos);
switch (err.kind) {
case RJ_PE_NONE: abort();
case RJ_PE_SYNTAX: ERR("invalid syntax"); break;
case RJ_PE_UBOUND: ERR("unbound parenthesis/bracket/curly brace"); break;
case RJ_PE_OVFLOW: ERR("too many nested parentheses"); break;
case RJ_PE_RANGE: ERR("invalid character range"); break;
case RJ_PE_INT: ERR("expected integer"); break;
case RJ_PE_LBVAR: ERR("lookbehind cannot be variable-length"); break;
case RJ_PE_MEM: ERR("out of memory"); break;
}
ERR("\n");
return NULL;
}
return m;
}
int run_regex(rejit_matcher m, char* string) {
rejit_group* groups = NULL;
int len;
if (m->groups && (groups = calloc(m->groups, sizeof(rejit_group))) == NULL) {
ERR("out of memory\n");
return 1;
}
len = rejit_match(m, string, groups);
if (len == -1) {
ERR("string did not match regex\n");
free(groups);
return 1;
} else {
int i;
printf("match successful! length: %d\n", len);
if (m->groups)
for (i=0; i<m->groups; ++i) {
printf("group %d: ", i);
if (groups[i].begin == NULL) puts("unmatched");
else
printf("\"%.*s\"\n", (int)(groups[i].end-groups[i].begin),
groups[i].begin);
}
free(groups);
return 0;
}
}
int main(int argc, char** argv) {
rejit_matcher m;
int res;
if (argc != 3) {
ERR("usage: %s <regex> <string>\n", argv[0]);
return 1;
}
if (!(m = compile(argv[1]))) return 1;
res = run_regex(m, argv[2]);
rejit_free_matcher(m);
return res;
}