-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtamgusynode.cxx
executable file
·404 lines (331 loc) · 13 KB
/
tamgusynode.cxx
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Tamgu (탐구)
*
* Copyright 2019-present NAVER Corp.
* under BSD 3-clause
*/
/* --- CONTENTS ---
Project : Tamgu (탐구)
Version : See tamgu.cxx for the version number
filename : tamgusynode.cxx
Date : 2017/09/01
Purpose :
Programmer : Claude ROUX (claude.roux@naverlabs.com)
Reviewer :
*/
#include "tamgu.h"
#include "tamguversion.h"
#include "tamgusynode.h"
#include "tamguautomaton.h"
//We need to declare once again our local definitions.
Exporting basebin_hash<synodeMethod> Tamgusynode::methods;
static ThreadLock classlock;
Tamgumapss* Tamgusynode::validfeatures = NULL;
bool Tamgusynode::testvalid = false;
//------------------------------------------------------------------------------------------------------------------
//MethodInitialization will add the right references to "name", which is always a new method associated to the object we are creating
Exporting void Tamgusynode::AddMethod(TamguGlobal* global, string name, synodeMethod func, unsigned long arity, string infos) {
short idname = global->Getid(name);
methods[idname] = func;
if (global->infomethods.find(a_synode) != global->infomethods.end() &&
global->infomethods[a_synode].find(name) != global->infomethods[a_synode].end())
return;
global->infomethods[a_synode][name] = infos;
global->RecordArity(a_synode, idname, arity);
}
void Tamgusynode::Setidtype(TamguGlobal* global) {
Locking lock(classlock);
if (Tamgusynode::methods.isEmpty())
Tamgusynode::InitialisationModule(global,"");
}
bool Tamgusynode::InitialisationModule(TamguGlobal* global, string version) {
methods.clear();
validfeatures = NULL;
Tamgusynode::testvalid = false;
Tamgusynode::AddMethod(global, "_initial", &Tamgusynode::MethodInitial, P_ONE, "_initial(map m): Creates a syntactic node with some features.");
Tamgusynode::AddMethod(global, "test", &Tamgusynode::MethodTest, P_ONE, "test(string attribute): Test if an attribute is part of the feature structure.");
Tamgusynode::AddMethod(global, "addchild", &Tamgusynode::MethodAddChild, P_ATLEASTONE, "addchild(synode): Add a first child node.");
Tamgusynode::AddMethod(global, "addprevious", &Tamgusynode::MethodAddPrevious, P_ONE, "addprevious(synode): Add as a previous node.");
Tamgusynode::AddMethod(global, "addnext", &Tamgusynode::MethodAddNext, P_ONE, "addnext(synode): Add as a next node.");
Tamgusynode::AddMethod(global, "parent", &Tamgusynode::MethodParent, P_NONE | P_ONE, "parent(): Return the parent node or check it against the parameter.");
Tamgusynode::AddMethod(global, "child", &Tamgusynode::MethodChild, P_NONE | P_ONE, "child(): Return the first child node or check it against the parameter.");
Tamgusynode::AddMethod(global, "last", &Tamgusynode::MethodLast, P_NONE | P_ONE, "last(): Return the last child node or check it against the parameter.");
Tamgusynode::AddMethod(global, "previous", &Tamgusynode::MethodPrevious, P_NONE | P_ONE, "previous(synode): Return the previous node or check it against the parameter.");
Tamgusynode::AddMethod(global, "next", &Tamgusynode::MethodNext, P_NONE | P_ONE, "next(synode): Return the next node or check it against the parameter.");
Tamgusynode::AddMethod(global, "after", &Tamgusynode::MethodAfter, P_NONE | P_ONE, "after(synode): Return true if the node is after.");
Tamgusynode::AddMethod(global, "before", &Tamgusynode::MethodBefore, P_NONE | P_ONE, "before(synode): Return true if the node is before.");
Tamgusynode::AddMethod(global, "succeed", &Tamgusynode::MethodSucceed, P_NONE | P_ONE, "succeed(synode): Return true if the node is after.");
Tamgusynode::AddMethod(global, "precede", &Tamgusynode::MethodPrecede, P_NONE | P_ONE, "precede(synode): Return true if the node is before.");
Tamgusynode::AddMethod(global, "sisters", &Tamgusynode::MethodSisters, P_NONE | P_ONE, "sisters(synode): Return the list of sister nodes or check if the node is a sister node.");
Tamgusynode::AddMethod(global, "children", &Tamgusynode::MethodChildren, P_NONE | P_ONE, "children(): Return the list of children for a given node or test if the node is a child.");
Tamgusynode::AddMethod(global, "sibling", &Tamgusynode::MethodSibling, P_NONE | P_ONE, "sibling(synode): test if the node is a sibling (either a sister or a descendant).");
Tamgusynode::AddMethod(global, "attributes", &Tamgusynode::MethodAttributes, P_NONE, "attributes(): Return the feature attributes as a vector.");
Tamgusynode::AddMethod(global, "values", &Tamgusynode::MethodValues, P_NONE, "values(): Return the feature values as a vector.");
Tamgusynode::AddMethod(global, "nbchildren", &Tamgusynode::MethodNbChildren, P_NONE, "nbchildren(): Return the number of direct children.");
global->returntypes[global->Getid("nbchildren")] = a_int;
Tamgusynode::AddMethod(global, "definitions", &Tamgusynode::MethodDefinitions, P_ONE, "definitions(mapss): Set the valid feature definitions for all 'synodes'.");
if (version != "") {
global->newInstance[a_synode] = new Tamgusynode(-1,global);
global->RecordCompatibilities(a_synode);
}
return true;
}
Exporting bool Tamgusynode::Checkattribute(string key) {
if (testvalid == false)
return true;
if (validfeatures->values.find(key) == validfeatures->values.end())
return false;
return true;
}
Exporting bool Tamgusynode::Checkfeature(string key, string value) {
if (testvalid == false)
return true;
if (validfeatures->values.find(key) == validfeatures->values.end())
return false;
if (validfeatures->values[key] != value) {
//In the case of a feature with a variable value, we use a blank string
if (validfeatures->values[key].empty())
return true;
return false;
}
return true;
}
Exporting Tamgu* Tamgusynode::Put(Tamgu* idx, Tamgu* kval, short idthread) {
if (!idx->isIndex()) {
if (kval->isMapContainer()) {
if (kval == features)
return aTRUE;
if (features == aNULL) {
features = globalTamgu->Providemapss();
features->Setreference(reference);
}
if (!Checkfeatures(kval, idthread))
return globalTamgu->Errorobject(idthread);
features->Put(aNULL, kval, idthread);
return aTRUE;
}
if (kval->Type() != a_synode)
return globalTamgu->Returnerror(e_expecting_a_synode, idthread);
Tamgusynode* as = (Tamgusynode*)kval;
if (as->features == aNULL) {
if (features != aNULL) {
features->Resetreference(reference);
features = aNULL;
}
return aTRUE;
}
if (features == aNULL) {
features = globalTamgu->Providemapss();
features->Setreference(reference);
}
features->Put(aNULL, as->features, idthread);
return aTRUE;
}
if (features == aNULL) {
features = globalTamgu->Providemapss();
features->Setreference(reference);
}
if (kval->String() == "~") {
((Tamgumapss*)features)->values.erase(idx->String());
}
else
features->Put(idx, kval, idthread);
return aTRUE;
}
Exporting Tamgu* Tamgusynode::Merge(Tamgu* a) {
if (a->Type() != a_mapss)
return aFALSE;
if (features == aNULL)
features = globalTamgu->Providemapss();
hmap<string, string >& values = ((Tamgumapss*)a)->values;
hmap<string, string >& avalues = ((Tamgumapss*)features)->values;
hmap<string, string> assignation;
bool aff;
bool neg;
bool rgx;
string key;
string val;
for (auto &it: values) {
aff = neg = rgx = false;
key = it.first;
switch (key[0]) {
case ':':
rgx = true;
if (key[1] == '~') {
neg = true;
key = &key.c_str()[2];
}
else
key = &key.c_str()[1];
break;
case '=':
aff = true;
key = &key.c_str()[1];
break;
case '~':
neg = true;
key = &key.c_str()[1];
}
val = it.second;
if (val == "~") {
if (avalues.find(key) != avalues.end()) {
if (!aff) {
if (!neg)
return aFALSE;
}
else
assignation[key] = val;
}
continue;
}
if (rgx) {
if (avalues.find(key) == avalues.end()) {
if (!neg)
return aFALSE;
continue;
}
Au_automaton* au = globalTamgu->rules[val];
if (au == NULL)
globalTamgu->rules[val] = au = new Au_automaton(val);
string reg = avalues[key];
if (au->match(reg)==false) {
if (!neg)
return aFALSE;
}
else {
if (neg)
return aFALSE;
}
continue;
}
if (avalues.find(key) != avalues.end()) {
if (val != avalues[key]) {
if (!neg)
return aFALSE;
}
else {
if (neg)
return aFALSE;
}
}
else {
if (aff)
assignation[key] = val;
else {
if (!neg)
return aFALSE;
}
}
}
for (auto &it : assignation) {
if (it.second == "~")
avalues.erase(it.first);
else
avalues[it.first] = it.second;
}
return aTRUE;
}
Exporting Tamgu* Tamgusynode::same(Tamgu* a) {
if (a == this)
return aTRUE;
if (a == aNULL && features == aNULL)
return aTRUE;
return aFALSE;
}
Exporting Tamgu* Tamgusynode::different(Tamgu* a) {
if (a == this)
return aFALSE;
if (a == aNULL && features == aNULL)
return aFALSE;
return aTRUE;
}
Exporting bool Tamgusynode::Unify(TamguDeclaration* dom, Tamgu* a) {
if (a == this)
return true;
if (a->Type() != a_synode) {
if (a == aNULL && features == aNULL)
return true;
return false;
}
Tamgusynode* as = (Tamgusynode*)a;
if (features == aNULL || as->features == aNULL)
return true;
if (as->Merge(features) == aTRUE)
return true;
return false;
}
Exporting bool Tamgusynode::isUnified(TamguDeclaration* dom) {
if (!id || id == a_universal)
return true;
return false;
}
Exporting Tamgu* Tamgusynode::plus(Tamgu* a, bool itself) {
if (!a->isMapContainer() && a->Type() != a_synode)
return this;
Tamgumapss* feat;
if (itself) {
if (features == aNULL) {
feat = globalTamgu->Providemapss();
features = feat;
}
else
feat = (Tamgumapss*)features;
}
else {
feat = globalTamgu->Providemapss();
if (features != aNULL)
feat->values = ((Tamgumapss*)features)->values;
}
if (a->isMapContainer()) {
short idthread = globalTamgu->GetThreadid();
if (!Checkfeatures(a, idthread))
return globalTamgu->Errorobject(idthread);
}
TamguIteration* itr = a->Newiteration(false);
string val;
string key;
for (itr->Begin(); itr->End() != aTRUE; itr->Next()) {
key = itr->Keystring();
if (key[0] == '=')
key = key.substr(1, key.size() - 1);
val = itr->Valuestring();
if (val == "~") {
if (feat->values.find(key) != feat->values.end())
feat->values.erase(key);
}
else
feat->values[key] = val;
}
itr->Release();
return feat;
}
Exporting Tamgu* TamguCallsynode::Eval(Tamgu* actual, Tamgu* value, short idthread) {
if (affectation)
return this;
actual = syn->Eval(actual, value, idthread);
if (actual->Type() != a_synode)
return aFALSE;
//we might have some action to do over "as"
return ((Tamgusynode*)actual)->Merge(features);
}
Exporting Tamgu* TamguCallsynode::Put(Tamgu* actual, Tamgu* value, short idthread) {
actual = syn->Eval(actual, aNULL, idthread);
if (actual->Type() != a_synode)
return aFALSE;
string vstr = value->String();
Tamgusynode* asyn = (Tamgusynode*)actual;
if (vstr == "~") {
if (asyn->features == aNULL)
return aFALSE;
hmap<string,string>& asynfeat = ((Tamgumapss*)asyn->features)->values;
for (const auto& it : ((Tamgumapss*)features)->values) {
if (asynfeat.find(it.first) != asynfeat.end())
asynfeat.erase(it.first);
}
return aTRUE;
}
Tamgumapss feats;
for (const auto& it : ((Tamgumapss*)features)->values)
feats.values[it.first] = vstr;
return asyn->plus(&feats, true);
}