-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnquads.cpp
215 lines (203 loc) · 6.51 KB
/
nquads.cpp
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
#ifndef NOPARSER
#include <string>
#include <iostream>
#include <set>
#include <stdexcept>
#include "rdf.h"
#include <boost/algorithm/string.hpp>
#include "jsonld.h"
#include "misc.h"
using namespace boost::algorithm;
nqparser::nqparser() : t(new wchar_t[4096*4096]) { }
nqparser::~nqparser() { delete[] t; }
pnode nqparser::readcurly() {
setproc(L"readcurly");
while (iswspace(*s)) ++s;
if (*s != L'{') return (pnode)0;
++s;
while (iswspace(*s)) ++s;
auto r = gen_bnode_id();
if (*s == L'}') { ++s; return mkbnode(r); }
auto t = (*this)(s, *r);
return mkbnode(r);
}
pnode nqparser::readlist() {
setproc(L"readlist");
if (*s != L'(') return (pnode)0;
static int lastid = 0;
int lpos = 0, curid = lastid++;
auto id = [&]() { std::wstringstream ss; ss << L"_:list" << curid << '.' << lpos; return ss.str(); };
const string head = id();
pnode pn;
++s;
while (iswspace(*s)) ++s;
if (*s == L')') { ++s; return mkiri(RDF_NIL); }
do {
while (iswspace(*s)) ++s;
if (*s == L')') break;
if (!(pn = readany(true)))
throw wruntime_error(string(L"expected iri or bnode or list in list: ") + string(s,0,48));
pnode cons = mkbnode(pstr(id()));
lists.emplace_back(cons, mkiri(RDF_FIRST), pn);
qlists[head].push_back(pn);
++lpos;
while (iswspace(*s)) ++s;
if (*s == L')') lists.emplace_back(cons, mkiri(RDF_REST), mkiri(RDF_NIL));
else lists.emplace_back(cons, mkiri(RDF_REST), mkbnode(pstr(id())));
if (*s == L'.') while (iswspace(*s++));
if (*s == L'}') throw wruntime_error(string(L"expected { inside list: ") + string(s,0,48));
}
while (*s != L')');
do { ++s; } while (iswspace(*s));
return mkbnode(pstr(head));
}
pnode nqparser::readiri() {
setproc(L"readiri");
while (iswspace(*s)) ++s;
if (*s == L'<') {
while (*++s != L'>') t[pos++] = *s;
t[pos] = 0; pos = 0;
++s;
return mkiri(wstrim(t));
}
if (*s == L'=' && *(s+1) == L'>') { ++++s; return mkiri(pimplication); }
while (!iswspace(*s) && *s != L',' && *s != L';' && *s != L'.' && *s != L'}' && *s != L'{' && *s != L')') t[pos++] = *s++;
t[pos] = 0; pos = 0;
pstring iri = wstrim(t);
if (lower(*iri) == L"true")
return mkliteral(pstr(L"true"), XSD_BOOLEAN, 0);
if (lower(*iri) == L"false")
return mkliteral(pstr(L"false"), XSD_BOOLEAN, 0);
if (std::atoi(ws(*iri).c_str()))
return mkliteral(iri, XSD_INTEGER, 0);
if (std::atof(ws(*iri).c_str()))
return mkliteral(iri, XSD_DOUBLE, 0);
if (*iri == L"0") return mkliteral(iri, XSD_INTEGER, 0);
auto i = iri->find(L':');
if (i == string::npos) return mkiri(iri);
string p = iri->substr(0, ++i);
// TRACE(dout<<"extracted prefix \"" << p <<L'\"'<< endl);
auto it = prefixes.find(p);
if (it != prefixes.end()) {
// TRACE(dout<<"prefix: " << p << " subst: " << *it->second->value<<endl);
iri = pstr(*it->second->value + iri->substr(i));
}
return mkiri(iri);
}
pnode nqparser::readbnode() {
setproc(L"readbnode");
while (iswspace(*s)) ++s;
if (*s != L'_' || *(s+1) != L':') return pnode(0);
while (!iswspace(*s) && *s != L',' && *s != L';' && *s != L'.' && *s != L'}' && *s != L'{' && *s != L')') t[pos++] = *s++;
t[pos] = 0; pos = 0;
return mkbnode(wstrim(t));
}
void nqparser::readprefix() {
setproc(L"readprefix");
while (iswspace(*s)) ++s;
if (*s != L'@') return;
if (memcmp(s, L"@prefix ", 8*sizeof(*s)))
throw wruntime_error(string(L"@prefix expected: ") + string(s,0,48));
s += 8;
while (*s != L':') t[pos++] = *s++;
t[pos++] = *s++;
t[pos] = 0; pos = 0;
pstring tt = wstrim(t);
TRACE(dout<<"reading prefix: \"" << *tt<<L'\"' << endl);
prefixes[*tt] = readiri();
while (*s != '.') ++s;
++s;
}
pnode nqparser::readvar() {
setproc(L"readvar");
while (iswspace(*s)) ++s;
if (*s != L'?') return pnode(0);
while (!iswspace(*s) && *s != L',' && *s != L';' && *s != L'.' && *s != L'}' && *s != L'{' && *s != L')') t[pos++] = *s++;
t[pos] = 0; pos = 0;
return mkiri(wstrim(t));
}
pnode nqparser::readlit() {
setproc(L"readlit");
while (iswspace(*s)) ++s;
if (*s != L'\"') return pnode(0);
++s;
do { t[pos++] = *s++; } while (!(*(s-1) != L'\\' && *s == L'\"'));
string dt, lang;
++s;
while (!iswspace(*s) && *s != L',' && *s != L';' && *s != L'.' && *s != L'}' && *s != L'{' && *s != L')') {
if (*s == L'^' && *++s == L'^') {
if (*++s == L'<') {
++s;
while (*s != L'>') dt += *s++;
++s;
break;
}
} else if (*s == L'@') {
while (!iswspace(*s)) lang += *s++;
break;
}
else throw wruntime_error(string(L"expected langtag or iri:") + string(s,0,48));
}
t[pos] = 0; pos = 0;
string t1 = t;
boost::replace_all(t1, L"\\\\", L"\\");
return mkliteral(wstrim(t1), pstrtrim(dt), pstrtrim(lang));
}
pnode nqparser::readany(bool lit){
pnode pn;
readprefix();
if (!(pn = readbnode()) && !(pn = readvar()) && (!lit || !(pn = readlit())) && !(pn = readlist()) && !(pn = readcurly()) && !(pn = readiri()) )
return (pnode)0;
return pn;
}
std::pair<std::list<quad>, std::map<string, std::list<pnode>>> nqparser::operator()(const wchar_t* _s, string ctx/* = L"@default"*/) {
std::list<std::pair<pnode, plist>> preds;
s = _s;
string graph;
pnode subject, pn;
pos = 0;
auto pos1 = preds.rbegin();
while(*s) {
if (!(subject = readany(false)))
throw wruntime_error(string(L"expected iri or bnode subject:") + string(s,0,48));
do {
while (iswspace(*s) || *s == L';') ++s;
if (*s == L'.' || *s == L'}') break;
if ((pn = readiri()) || (pn = readcurly())) {
preds.emplace_back(pn, plist());
pos1 = preds.rbegin();
}
else throw wruntime_error(string(L"expected iri predicate:") + string(s,0,48));
do {
while (iswspace(*s) || *s == L',') ++s;
if (*s == L'.' || *s == L'}') break;
if ((pn = readany(true))) {
pos1->second.push_back(pn);
}
else throw wruntime_error(string(L"expected iri or bnode or literal object:") + string(s,0,48));
while (iswspace(*s)) ++s;
} while (*s == L',');
while (iswspace(*s)) ++s;
} while (*s == L';');
if (*s != L'.' && *s != L'}' && *s) {
if (!(pn = readbnode()) && !(pn = readiri()))
throw wruntime_error(string(L"expected iri or bnode graph:") + string(s,0,48));
graph = *pn->value;
} else
graph = ctx;
for (auto d : lists)
r.emplace_back(std::get<0>(d), std::get<1>(d), std::get<2>(d), graph);
for (auto x : preds)
for (pnode object : x.second)
r.emplace_back(subject, x.first, object, graph);
lists.clear();
preds.clear();
while (iswspace(*s)) ++s;
while (*s == '.') ++s;
while (iswspace(*s)) ++s;
if (*s == L'}') { ++s; return { r, qlists }; }
if (*s == L')') throw wruntime_error(string(L"expected ) outside list: ") + string(s,0,48));
}
return { r, qlists };
}
#endif