-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcompile-observer.js
126 lines (114 loc) · 4.8 KB
/
compile-observer.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
var Observers = require("./observers"),
Operators = require("./operators"),
LITERAL = "literal",
VALUE = "value",
PARAMETERS = "parameters",
ELEMENT = "element",
COMPONENT = "component",
RECORD = "record";
module.exports = compile;
function compile(syntax) {
return semantics.compile(syntax);
}
var semantics = compile.semantics = {
compilers: {
property: Observers.makePropertyObserver,
get: Observers.makeGetObserver,
path: Observers.makePathObserver,
"with": Observers.makeWithObserver,
"if": Observers.makeConditionalObserver,
parent: Observers.makeParentObserver,
not: Observers.makeNotObserver,
and: Observers.makeAndObserver,
or: Observers.makeOrObserver,
"default": Observers.makeDefaultObserver,
defined: Observers.makeDefinedObserver,
rangeContent: Function.identity,
mapContent: Function.identity,
keys: Observers.makeKeysObserver,
keysArray: Observers.makeKeysObserver,
values: Observers.makeValuesObserver,
valuesArray: Observers.makeValuesObserver,
items: Observers.makeEntriesObserver, // XXX deprecated
entries: Observers.makeEntriesObserver,
entriesArray: Observers.makeEntriesObserver,
toMap: Observers.makeToMapObserver,
mapBlock: Observers.makeMapBlockObserver,
filterBlock: Observers.makeFilterBlockObserver,
everyBlock: Observers.makeEveryBlockObserver,
someBlock: Observers.makeSomeBlockObserver,
sortedBlock: Observers.makeSortedBlockObserver,
sortedSetBlock: Observers.makeSortedSetBlockObserver,
groupBlock: Observers.makeGroupBlockObserver,
groupMapBlock: Observers.makeGroupMapBlockObserver,
minBlock: Observers.makeMinBlockObserver,
maxBlock: Observers.makeMaxBlockObserver,
min: Observers.makeMinObserver,
max: Observers.makeMaxObserver,
enumerate: Observers.makeEnumerationObserver,
reversed: Observers.makeReversedObserver,
flatten: Observers.makeFlattenObserver,
concat: Observers.makeConcatObserver,
view: Observers.makeViewObserver,
sum: Observers.makeSumObserver,
average: Observers.makeAverageObserver,
last: Observers.makeLastObserver,
only: Observers.makeOnlyObserver,
one: Observers.makeOneObserver,
has: Observers.makeHasObserver,
// TODO zip
tuple: Observers.makeArrayObserver,
range: Observers.makeRangeObserver,
startsWith: Observers.makeStartsWithObserver,
endsWith: Observers.makeEndsWithObserver,
contains: Observers.makeContainsObserver,
join: Observers.makeJoinObserver,
toArray: Observers.makeToArrayObserver,
asArray: Observers.makeToArrayObserver // XXX deprecated
},
compile: function compile(syntax) {
var compilers = this.compilers,
syntaxType = syntax.type;
if (syntax.type === LITERAL) {
return Observers.makeLiteralObserver(syntax.value);
} else if (syntaxType === VALUE) {
return Observers.observeValue;
} else if (syntaxType === PARAMETERS) {
return Observers.observeParameters;
} else if (syntaxType === ELEMENT) {
return Observers.makeElementObserver(syntax.id);
} else if (syntaxType === COMPONENT) {
return Observers.makeComponentObserver(syntax.label, syntax);
} else if (syntaxType === RECORD) {
var observers = {},
args = syntax.args,
names = Object.keys(args);
for (var i=0;(name = names[i]);i++) {
observers[name] = this.compile(args[name]);
}
return Observers.makeObjectObserver(observers);
} else {
if (!compilers.hasOwnProperty(syntaxType)) {
compilers[syntaxType] = Observers.makeMethodObserverMaker(syntaxType);
}
var argObservers = [];
for(var i=0, args = syntax.args, countI = args.length;i<countI;i++) {
argObservers[i] = this.compile(args[i]);
}
return (countI === 1)
? compilers[syntaxType].call(null, argObservers[0])
: (countI === 2)
? compilers[syntaxType].call(null, argObservers[0], argObservers[1])
: compilers[syntaxType].apply(null, argObservers);
}
}
};
var compilers = semantics.compilers;
var operatorsKeys = Object.keys(Operators);
for(var i=0, name;(name = operatorsKeys[i]); i++) {
if (!compilers[name]) {
compilers[name] = Observers.makeOperatorObserverMaker(Operators[name]);
}
}
// a special Hell for non-enumerable inheritance
compilers.toString = Observers.makeOperatorObserverMaker(Operators.toString);