-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjstle.js
181 lines (164 loc) · 4.49 KB
/
jstle.js
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
/**
* Jstle is free software provided under the MIT license.
* See LICENSE file for full text of the license.
* Copyright 2010 Dan Newcome.
*/
(function() {
Jstle = {};
Jstle.parse = outerparse;
var namespaces = {};
// TODO: return value is global as a hack to avoid passing
// too much data around int the case of parsing blank nodes,
// which need to return full triples outside of the normal scheme
// of parsing triples expressions. It isn't easy to do the blank
// nodes in a separate step since once we are out of scope where
// blank node was assigned ID, we don't have ready access to match
// up the nodeIDs later
var res = [];
function outerparse( expr ) {
res = [];
for( var i=0; i < expr.length; i++ ) {
// evaluate a directive statement
if( expr[i][0] == "@prefix" ) {
namespaces[ expr[i][1] ] = expr[i][2];
}
else if( typeOf( expr[i] ) == 'object' ) {
var blankNodeID = '_:' + Math.floor( Math.random() * 1001 );
parseBlankNode( blankNodeID, expr[i] );
}
else {
res = concat( parse( expr[i] ), res );
}
}
resolvePrefixes( res );
namespaces = {};
return res;
}
function resolvePrefixes( expr ) {
for( var i=0; i < expr.length; i++ ) {
for( var j=0; j < expr[i].length; j++ ) {
var prefix = expr[i][j].split(':')[0];
var suffix = expr[i][j].split(':')[1];
expr[i][j] = '<' + namespaces[ prefix ] + suffix + '>';
}
}
}
// evaluate a triples statement
function parse( expr, level ) {
// we start with level 3 since we are trying to end up
// with three-element triples
level = level || 3;
var ret = [];
var temp = [];
// iterate over the input elements
for( var i=0; i < expr.length; i++ ) {
if( typeOf( expr[i] ) == 'array' ) {
// TODO: this way of tracking the number
// of elements to caputure is kind of goofy
var effectivelevel;
if( level == 2 ) {
effectivelevel = 1;
}
else if( level == 3 ) {
effectivelevel = 3 - temp.length;
}
else {
throw "parse error - invalid nested array";
}
// this is the meat of it, we want to pare things
// down as much as we can and still keep this
ret = concat(
ret,
product(
[ temp ],
parse(
expr[i],
effectivelevel
)
)
);
}
else if( typeOf( expr[i] ) == 'object' ) {
var blankNodeID = '_:' + Math.floor( Math.random() * 1001 );
temp.push( blankNodeID );
if( temp.length == level ) {
ret.push( temp );
temp = [];
}
parseBlankNode( blankNodeID, expr[i] );
}
else {
// push non-array elements onto the temp array
// temp array gets pushed to output array once we
// have created a full triple. Partial triples on
// the temp array get joined with array coming back
// from recursion to form triples
temp.push( expr[i] );
if( temp.length == level ) {
ret.push( temp );
temp = [];
}
}
}
return ret;
}
// called after adding nodeid to current triple,
// so any assertions in the blank node are made w/
// same blank nodeid given as parameter
// note that results are directly added to response global
function parseBlankNode( nodeID, expr ) {
var tmp = [ nodeID ];
for( pred in expr ) {
tmp.push( pred );
// continue parsing if we don't have another nested blank
if( typeOf( expr[pred] ) != 'object' ) {
// object could possibly be a list, so parse
// TODO: change api of product and parse to take single element
// expressions without enclosing array - syntax here is awkward
var parsearg = typeOf( expr[pred] ) == 'array' ? expr[pred] : [ expr[pred] ];
res = concat( res, product( [ tmp ] , parse( parsearg, 1 ) ) );
}
else {
// we have a nested blank
var blankNodeID = '_:' + Math.floor( Math.random() * 1001 );
tmp.push( blankNodeID );
res.push( concat( res, tmp ) );
parseBlankNode( blankNodeID, expr[pred] );
}
tmp = [ nodeID ];
}
}
// return cartesian join of two arrays
function product( x, y ) {
var ret = [];
for( var i=0; i < x.length; i++ ) {
for( var j=0; j < y.length; j++ ) {
ret.push( x[i].concat( y[j] ) );
}
}
return ret;
}
// concatenate arrays
function concat( x, y ) {
return x.concat( y )
}
/**
* typeOf function published by Douglas Crockford in ECMAScript recommendations
* http://www.crockford.com/javascript/recommend.html
*/
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (typeof value.length === 'number' &&
!(value.propertyIsEnumerable('length')) &&
typeof value.splice === 'function') {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
}
})();