-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathregex.c
178 lines (162 loc) · 5.6 KB
/
regex.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "object.h"
#include "vm.h"
#include "runtime.h"
#include "table.h"
#include "memory.h"
#include "compiler.h"
#include "regex_lib.h"
ObjClass *lxRegexClass = NULL;
ObjClass *lxRegexErrClass = NULL;
ObjNative *nativeRegexInit = NULL;
ObjClass *lxMatchDataClass = NULL;
static Value lxRegexInit(int argCount, Value *args) {
CHECK_ARITY("Regex#init", 2, 2, argCount);
Value reStr = args[1];
CHECK_ARG_IS_A(reStr, lxStringClass, 1);
Regex *re = ALLOCATE(Regex, 1);
regex_init(re, AS_STRING(reStr)->chars, NULL);
AS_REGEX(*args)->regex = re;
RegexCompileResult compRes = regex_compile(re);
if (compRes != REGEX_COMPILE_SUCCESS) {
// this object will be GCed and the `regex` struct will be freed then
throwErrorFmt(lxRegexErrClass, "Regex compilation error");
}
return *args;
}
static Value lxRegexInspect(int argCount, Value *args) {
CHECK_ARITY("Regex#inspect", 1, 1, argCount);
Value self = args[0];
Regex *re = AS_REGEX(self)->regex;
ObjString *buf = emptyString();
pushCStringFmt(buf, "%s", "#<Regex %\"");
pushCString(buf, re->src, strlen(re->src));
pushCString(buf, "\">", 2);
return OBJ_VAL(buf);
}
static LxMatchData *getMatchData(Value self) {
ObjInstance *selfInst = AS_INSTANCE(self);
ObjInternal *internal = selfInst->internal;
return (LxMatchData*)internal->data;
}
static void matchDataSetRegex(Value md, Value regex) {
LxMatchData *lmd = getMatchData(md);
lmd->re = AS_REGEX(regex);
}
static void matchDataPopulateCaptures(Value md) {
LxMatchData *lmd = getMatchData(md);
Regex *re = lmd->re->regex;
GroupNode *gn = re->groups;
Value captures = newArray();
while (gn) {
if (gn->group->capture_beg && gn->group->capture_end) {
ObjString *capture = copyString(gn->group->capture_beg,
gn->group->capture_end-gn->group->capture_beg,
NEWOBJ_FLAG_NONE);
arrayPush(captures, OBJ_VAL(capture));
} else {
break;
}
gn = gn->next;
}
lmd->captures = captures;
}
static Value lxRegexMatch(int argCount, Value *args) {
CHECK_ARITY("Regex#match", 2, 3, argCount);
Value self = args[0];
Value str = args[1];
CHECK_ARG_IS_A(str, lxStringClass, 1);
bool giveMatchData = false;
if (argCount == 3) {
giveMatchData = isTruthy(args[2]);
}
Regex *re = AS_REGEX(*args)->regex;
DBG_ASSERT(re);
MatchData mdata = regex_match(re, AS_STRING(str)->chars);
if (mdata.matched) {
if (giveMatchData) {
Value mdArgs[2] = {
NUMBER_VAL(mdata.match_start),
NUMBER_VAL(mdata.match_len)
};
Value md = callFunctionValue(OBJ_VAL(lxMatchDataClass), 2, mdArgs);
matchDataSetRegex(md, self);
matchDataPopulateCaptures(md);
return md;
} else {
return NUMBER_VAL(mdata.match_start);
}
} else {
return NIL_VAL;
}
}
static void markInternalMatchData(Obj *obj) {
ObjInternal *internal = (ObjInternal*)obj;
LxMatchData *md = internal->data;
if (md->re) {
grayObject(TO_OBJ(md->re));
}
if (md->captures != NIL_VAL) {
grayObject(TO_OBJ(md->captures));
}
}
static void freeInternalMatchData(Obj *obj) {
ASSERT(obj->type == OBJ_T_INTERNAL);
ObjInternal *internal = (ObjInternal*)obj;
FREE(LxMatchData, internal->data);
}
static Value lxMatchDataInit(int argCount, Value *args) {
CHECK_ARITY("MatchData#init", 3, 3, argCount);
Value self = args[0];
Value start = args[1];
Value len = args[2];
CHECK_ARG_BUILTIN_TYPE(start, IS_NUMBER_FUNC, "number", 1);
CHECK_ARG_BUILTIN_TYPE(len, IS_NUMBER_FUNC, "number", 2);
ObjInstance *selfInst = AS_INSTANCE(self);
ObjInternal *internalObj = newInternalObject(false, NULL, sizeof(MatchData), markInternalMatchData, freeInternalMatchData,
NEWOBJ_FLAG_NONE);
hideFromGC((Obj*)internalObj);
LxMatchData *lmd = ALLOCATE(LxMatchData, 1);
lmd->md.match_start = AS_NUMBER(start);
lmd->md.match_len = AS_NUMBER(len);
lmd->re = NULL;
lmd->captures = NIL_VAL;
internalObj->data = lmd;
selfInst->internal = internalObj;
unhideFromGC((Obj*)internalObj);
return self;
}
static Value lxMatchDataCaptures(int argCount, Value *args) {
CHECK_ARITY("MatchData#captures", 1, 1, argCount);
Value self = args[0];
LxMatchData *lmd = getMatchData(self);
if (!lmd->re) {
return newArray();
}
if (lmd->captures == NIL_VAL) {
return newArray();
}
return lmd->captures;
}
static Value lxMatchDataStart(int argCount, Value *args) {
Value self = args[0];
LxMatchData *lmd = getMatchData(self);
return NUMBER_VAL(lmd->md.match_start);
}
static Value lxMatchDataLength(int argCount, Value *args) {
Value self = args[0];
LxMatchData *lmd = getMatchData(self);
return NUMBER_VAL(lmd->md.match_len);
}
void Init_RegexClass() {
lxRegexClass = addGlobalClass("Regex", lxObjClass);
lxRegexErrClass = addGlobalClass("RegexError", lxErrClass);
nativeRegexInit = addNativeMethod(lxRegexClass, "init", lxRegexInit);
addNativeMethod(lxRegexClass, "inspect", lxRegexInspect);
addNativeMethod(lxRegexClass, "match", lxRegexMatch);
ObjClass *matchDataClass = addGlobalClass("MatchData", lxObjClass);
lxMatchDataClass = matchDataClass;
addNativeMethod(matchDataClass, "init", lxMatchDataInit);
addNativeGetter(matchDataClass, "start", lxMatchDataStart);
addNativeGetter(matchDataClass, "length", lxMatchDataLength);
addNativeGetter(matchDataClass, "captures", lxMatchDataCaptures);
}