-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (162 loc) · 3.6 KB
/
index.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
function JSLContext(parent, nodes, position, name)
{
const _parent = parent;
const _nodes = nodes;
const _position = position;
const _name = name;
this.nodes = function(){
return _nodes;
}
this.node = function(){
return _nodes;
}
this.name = function(){
return _name;
}
this.parent = function(){
return _parent;
}
this.position = function(){
return _position;
}
this.isLast = function(){
return _position === _nodes.length;
}
this.isFirst = function(){
return _position === 0;
}
this.toString = function(){
return JSON.stringify({ position: position, nodes: nodes});
}
}
function JSLProcessor()
{
var exprs = {};
var contextStack = [];
var context = new JSLContext(null, null, 0);
this.valueOf = function( x ) {
if ( typeof( x ) !== "function" ) return x;
try{
return x();
}
catch{
return undefined;
}
};
var valueOf = this.valueOf;
this.position = function(){
return context.position();
}
this.isLast = function(){
return context.isLast();
}
this.isFirst = function(){
return context.isFirst();
}
this.parent = function() {
return context.parent();
}
this.node = function() {
return context.node();
}
this.name = function() {
return context.name();
}
this.match = function(mode=null, expr, method)
{
if ( arguments.length === 2) {
expr = arguments['0'];
method = arguments['1'];
mode = null;
}
if ( typeof expr === "string" ) {
var name = expr;
expr = (x) => this.name() === name;
}
if ( exprs[mode] === undefined ) {
exprs[mode] = [];
}
exprs[mode].push( { expr: expr, method: method } );
};
this.apply = function( mode, nodes ) {
if ( arguments.length === 1) {
nodes = arguments[0];
mode = null;
}
contextStack.push(context);
context = new JSLContext(context, nodes);
if ( Array.isArray(nodes) ) {
var result = nodes.map( (x, idx) => {
var rc = this.applyForNode(mode, x, idx );
return rc;
});
}
else if ( nodes && typeof nodes === 'object' && nodes.constructor === Object ) {
var result = {};
var idx = 0;
for( var name in nodes ) {
++idx;
result[name] = this.applyForNode(mode, nodes[name], idx, name);
}
}
else {
result = this.applyForNode(mode, nodes, 0, 'object'); ;
}
context = contextStack.pop();
return result;
};
this.applyForNode = function(mode, node, idx=0, name='object'){
contextStack.push(context);
context = new JSLContext(context, node, idx, name);
var tmpls = exprs[mode];
var rank = -1;
var selected = { rank : -1, method : (node)=> node };
for( var i in tmpls ) {
var current = tmpls[i];
var expr = current.expr(node);
if ( typeof(expr) === "boolean" ) {
expr = { result : expr, rank : 1 };
}
if ( !expr.result ) continue;
if (expr.rank <= rank ) continue;
rank = expr.rank;
selected = current;
}
var result = selected.method(node);
context = contextStack.pop();
return result;
};
this.any = function()
{
return { rank: 0, result: true };
};
this.eq = function(x, y)
{
return { rank: 1, result: valueOf(x) === valueOf(y) };
};
this.neq = function(x, y)
{
return { rank: 1, result: valueOf(x) !== valueOf(y) };
};
this.and = function(...args){
var rank = 1;
var rc = true;
for( var i in args ) {
var arg = args[i];
rank+= arg.rank;
rc = rc && arg.result;
}
return { rank: rank, result : rc }
};
this.or = function(...args){
var rank = 1;
var rc = false;
for( var i in args ) {
var arg = args[i];
rank+= arg.rank;
rc = rc || arg.result;
}
return { rank: rank, result : rc }
};
}
exports.JSLProcessor = JSLProcessor;