-
Notifications
You must be signed in to change notification settings - Fork 0
/
fSyntax.js
executable file
·337 lines (275 loc) · 10.4 KB
/
fSyntax.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
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
/*
* Copyright (C) 2007 - 2014 Hyperweb2 All rights reserved.
* GNU General Public License version 3; see www.hyperweb2.com/terms/
*/
'use strict';
var __global = typeof window === 'object' ? window : global, hwc_conf = __global.hwc_conf;
hwc_conf.paths.hwc_js_lib_class_class = hwc_conf.path_core + "js-lib-class/Class";
requirejs.config({paths: hwc_conf.paths});
define([
"hwc_js_kernel",
"hwc_js_lib_class_class",
], function ($) {
function buildMembers (members, modifiers) {
var m = [];
for (var name in members) {
var val, retType;
// if member value is type-hinted
// it will be processed instead of
// an eventually var name type-hint syntax
if (members[name] instanceof $.dType) {
retType = members[name].type;
val = members[name].value;
} else {
var descr = name.split(' ');
val = members[name];
if (descr.length > 1) {
// check if retType is an object
// that can be found inside this scope
// otherwise pass the string as is
// and remove quotes if any ( you can avoid collision with
// when you want check type using a string enclosing
// type inside quotes )
retType = $[descr[0]] || $.global[descr[0]] || descr[0].replace(/["']/g, "");
// redefine member name without return type
name = descr[1];
}
}
m.push({
attributes: modifiers || ["public"],
name: name,
val: val,
retType: retType
});
}
return m;
}
var mod = (function (modifiers) {
var obj = function __modifier () {
var members;
switch (arguments.length) {
case 1:
// if we've 1 argument
// it must be an object that contains
// various members definitions
members = arguments[0];
break;
case 2:
var vname = arguments[0];
// build the object with single definition
members = {};
members[vname] = arguments[1];
break;
case 3:
// when we've 3 arguments, first must
// be the return type
var retType = arguments[0];
var vname = arguments[1];
var val = arguments[2];
// build the object with single definition
members = {};
members[vname] = $.typeHint(retType, val);
break;
default:
throw new SyntaxError("Wrong number of parameters");
}
// we must return an object to make difference
// between array parameter and modifier
var res = {
modifiers: obj.modifiers
};
if (typeof members !== undefined) {
// add scope to avoid exception in strict mode
res.members = buildMembers.call(null, members, obj.modifiers);
}
return res;
};
obj.modifiers = modifiers;
Object.defineProperty(obj, "class", {
// the setter cannot work in this context because it will set value
// to $.class property in any case
get: function () {
var descriptor = {
type: obj.modifiers,
base: null,
use: [],
members: []
};
/**
* arguments: modifiers or a single array of modifiers
* @returns Class
*/
function cl () {
return cl.define.apply(null, arguments);
}
cl.extends = function (base) {
if (base !== undefined) {
if (descriptor.base) {
throw new SyntaxError("Multiple use of extends");
}
descriptor.base = base;
}
return cl;
};
cl.use = function (traits) {
if (traits !== undefined)
descriptor.use = descriptor.use.length ? descriptor.use.concat(traits) : traits;
return cl;
};
cl.members = function (members) {
// if there's a single member and it's an array
// it means that we're passing an array of modifiers
// and not directly modifiers in arguments
if (members.length==1 && Array.isArray(members[0])) {
members=members[0];
}
if (members !== undefined && Array.isArray(members)) {
members.forEach(function (m) {
descriptor.members = descriptor.members.length > 0 ? descriptor.members.concat(m.members) : m.members;
});
return cl;
}
throw new SyntaxError("Members must be defined as an array of properties!");
};
cl.define = function () {
var members = Array.prototype.slice.call(arguments);
if (members.length > 0) {
cl.members(members);
}
return $.Class(descriptor);
};
return cl;
}
});
// NOT IMPLEMENTED YET
/**
* INTERFACE
*/
/*obj.interface = (function () {
var descriptor={
type: obj.modifiers.push("interface"),
base: null,
use: [],
members: []
};
var iface=function(members) {
if (members!==undefined)
iface.members(members);
return $.Class(descriptor);
};
iface.extends=function(base) {
descriptor.use=descriptor.use.length ? descriptor.use.concat(traits) : traits;
return cl;
};
iface.members=function(members) {
descriptor.members=descriptor.members.length ? descriptor.use.concat(members) : members;
return iface;
};
iface.define=function() {
return iface();
};
return iface;
})();
*/
/**
* TEMPLATE
*/
/*
obj.template = (function () {
var descriptor={
type: obj.modifiers.push("template"),
base: null,
use: [],
members: []
};
var tmpl=function(members) {
if (members!==undefined)
tmpl.members(members);
return $.Class(descriptor);
};
tmpl.extends=function(base) {
if (descriptor.base) {
throw new SyntaxError("Multiple use of extends");
}
descriptor.base=base;
return tmpl;
};
tmpl.members=function(members) {
descriptor.members=descriptor.members.length ? descriptor.use.concat(members) : members;
return tmpl;
};
tmpl.define=function() {
return tmpl();
};
return tmpl;
})();
*/
return obj;
});
$.public = new mod(["public"]);
$.private = new mod(["private"]);
$.protected = new mod(["protected"]);
$.static = new mod(["static"]);
$.final = new mod(["final"]);
$.abstract = new mod(["abstract"]);
//public shortcut
var descr = Object.getOwnPropertyDescriptor($.public, "class");
Object.defineProperty($, "class", {
get: descr.get,
set: descr.set
});
//
// COMBINATIONS
//
//
// PUBLIC
$.public.final = new mod(["public", "final"]);
$.public.static = new mod(["public", "static"]);
$.public.final.static = new mod(["public", "final", "static"]);
$.public.static.final = $.public.final.static;
$.public.abstract = new mod(["public", "abstract"]);
// PRIVATE
$.private.final = new mod(["private", "final"]);
$.private.static = new mod(["private", "static"]);
$.private.final.static = new mod(["private", "final", "static"]);
$.private.static.final = $.private.final.static;
$.private.abstract = new mod(["private", "abstract"]);
// PROTECTED
$.protected.final = new mod(["protected", "final"]);
$.protected.static = new mod(["protected", "static"]);
$.protected.final.static = new mod(["protected", "final", "static"]);
$.protected.static.final = $.protected.final.static;
$.protected.abstract = new mod(["protected", "abstract"]);
// ABSTRACT
$.abstract.public = new mod(["abstract", "public"]);
$.abstract.private = new mod(["abstract", "private"]);
$.abstract.protected = new mod(["abstract", "protected"]);
// STATIC
$.static.public = new mod(["static", "public"]);
$.static.private = new mod(["static", "private"]);
$.static.protected = new mod(["static", "protected"]);
// FINAL
$.final.public = new mod(["final", "public"]);
$.final.private = new mod(["final", "private"]);
$.final.protected = new mod(["final", "protected"]);
// STATIC/FINAL COMBINATION
$.final.static = $.static.final = new mod(["static", "final"]);
$.final.static.public = $.static.final.public = new mod(["static", "final", "public"]);
$.final.static.private = $.static.final.private = new mod(["static", "final", "private"]);
$.final.static.protected = $.static.final.protected = new mod(["static", "final", "protected"]);
/*
*
* UTILS
*
*/
$.dType = function (type, value) {
this.type = type;
this.value = value;
};
$.typeHint = function (type, value) {
if (typeof value !== "function")
$.typeCompare(type, value);
return new $.dType(type, value);
};
return $;
});