-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepcook.language.js
More file actions
313 lines (266 loc) · 7.24 KB
/
prepcook.language.js
File metadata and controls
313 lines (266 loc) · 7.24 KB
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
/**
* @file
* prepcook.language
*
* This module defines the prepcook template language reserve words, and their behavior.
* It is used by the parser when building the parse tree, and evaluating behavior.
*
*/
var parsetoken = parsetoken || require('./prepcook.token'),
parseutil = parseutil || require('./includes/prepcook.utils.js');
const constants = require('./prepcook.config');
const BISTRO_FAILURE = constants.BISTRO_FAILURE;
var lang = (function languageFactory() {
// The list of reserve words used when parsing for reserve words.
var reserve_word_list = [
'#each',
'/each',
'#if',
'/if',
'#else',
'/else',
'#elif',
'/elif',
'#unless',
'/unless',
'#template',
'/template',
'#include',
'/include'
];
// The definition of each reserve word, it's type, and behavior.
const dictionary = {
'#each': {
type: 'block',
behavior: 'loop',
start: '#each',
end: '/each',
},
'#if': {
type: 'block',
behavior: 'conditional',
start: '#if',
end: '/if',
peers: [
'#else',
'#elif'
],
rules: [
'all'
]
},
'#else': {
type: 'block',
behavior: 'linked-conditional',
start: '#else',
end: "/else",
},
'#elif': {
type: 'block',
behavior: 'linked-conditional',
start: '#elif',
end: '/elif',
peers: [
'#else',
'#elif'
],
rules: [
'all'
]
},
'#unless': {
type: 'block',
behavior: 'conditional',
start: '#unless',
end: '/unless',
rules: [
'none'
]
},
'#template': {
type: 'loader',
behavior: 'loader',
start: '#template',
end: '/template'
},
'#include': {
type: 'loader',
behavior: 'include',
start: '#include',
end: '/include'
}
};
function getWordList () {
return reserve_word_list;
}
function getWordDefinition (word) {
var my_word = word.replace('/', '#');
if (dictionary[my_word]) {
// When we were passed a terminus, but we fetched a defintion for it's opening pair,
// convert that definition to terminus.
var result = {
type: (my_word != word) ? 'terminus' : dictionary[my_word].type,
behavior: dictionary[my_word].behavior,
start: dictionary[my_word].start,
end: dictionary[my_word].end,
rules: dictionary[my_word].rules,
peers: (typeof dictionary[my_word].peers !== 'undefined') ? dictionary[my_word].peers : []
};
return result;
}
return false;
}
function isConditional (type) {
return (getWordDefinition(type).behavior == 'conditional') ? true : false;
}
function isLinkedConditional (type) {
return (getWordDefinition(type).behavior == 'linked-conditional') ? true : false;
}
function isIterator (type) {
return (getWordDefinition(type).behavior == 'loop') ? true : false;
}
function isLoader (type) {
return (getWordDefinition(type).behavior == 'loader') ? true : false;
}
function isInclude (type) {
return (getWordDefinition(type).behavior == 'include') ? true : false;
}
function resolveContent (type, data, vars, var_path) {
if (type == 'constant' && typeof data !== 'undefined') {
return data.trim();
}
else if (type == 'expression') {
return (data.length > 0)
? parsetoken.parse(data, vars, [
{
left: '[',
right: ']',
callback: parsetoken.evalCommandBlock
}
],
var_path
)
: '';
}
return '';
}
function resolveConditional (type, data, vars, var_path) {
// Get the definition.
var my_word = getWordDefinition(type),
my_result = evalConditional(type, data, vars, var_path),
check_result = true;
if (my_word.rules) {
for (var i = 0; i < my_word.rules.length; i++) {
switch (my_word.rules[i]) {
case 'all':
if (my_result === false) { check_result = false; }
break;
case 'none':
if (my_result === true) { check_result = false; }
break;
}
}
return check_result;
}
return my_result;
}
/**
* Evaluate a conditional.
*
* @param {string} type
* Conditional type. Defaults to if.
* @param {string} expression
* The expression of the conditional.
* @param {variable} data
* The scope of variables passed to this level of the function.
*
* @return {boolean}
* TRUE if it evaluates to true.
*/
function evalConditional(type, expression, data, var_path) {
try {
// Since else has no expression, always return true right away.
if (type == '#else') { return true; }
// Trim whitespace before passing the regex.
expression = (typeof expression === 'string') ? expression.trim() : expression;
// Check for a single variable expression referencing the current base level of data.
if (/^([a-z0-9_\-\.]+)+$/i.test(expression)) {
var singleExp = parseutil.normalizeExpression(expression, data, var_path, true);
return (singleExp && singleExp !== BISTRO_FAILURE) ? true : false;
}
// Otherwise, we're dealing with something more complex.
else if (exp = expression.match(/^([\"\'a-z0-9_\-\.]+)[\s]?(>=|<|>|==|!=)[\s]?([a-z0-9_\-\.\'\"]+)$/i)) {
if (Array.isArray(exp) === false) {
throw new Error('Expression does not eval.');
}
else {
var op = (exp[2]) ? exp[2] : null,
left = parseutil.normalizeExpression(exp[1], data, var_path, true),
right = parseutil.normalizeExpression(exp[3], data, var_path, true);
if (left === BISTRO_FAILURE || right === BISTRO_FAILURE) {
console.warn('One or more vars in expression "' + expression + '" had errors.', left, right);
if (left === BISTRO_FAILURE) { left = null; }
if (right === BISTRO_FAILURE) { right = null; }
}
switch (op) {
case '==': return left == right; break;
case '>': return left > right; break;
case '>=': return left >= right; break;
case '<': return left < right; break;
case '<=': return left <= right; break;
case '!=': return left != right; break;
default:
throw new Error('Expression op could not be resolved.');
}
}
}
}
catch (e) {
console.warn('One or more errors occured while parsing expression. ', e, ' Passed expression: ', expression);
}
return BISTRO_FAILURE;
}
/**
* Is (end) a valid ending reserve word for (start)?
*
* @param {string} end
* A reserve word terminus.
* @param {string} start
* Some reserve word we should check for compatability with end.
*
* @return {boolean}
* TRUE if end is a valid match. Otherwise, FALSE.
*/
function terminusMatch (end, start) {
if (my_word = getWordDefinition(start)) {
if (my_word.end == end) {
return true;
}
}
return false;
}
return {
getWordList: getWordList,
getWord: getWordDefinition,
terminusMatch: terminusMatch,
isConditional: isConditional,
isLinkedConditional: isLinkedConditional,
isIterator: isIterator,
isLoader: isLoader,
isInclude: isInclude,
resolveContent: resolveContent,
resolveConditional: resolveConditional
};
})();
module.exports = {
reserveWordList: lang.getWordList,
getWord: lang.getWord,
terminusMatch: lang.terminusMatch,
isConditional: lang.isConditional,
isLinkedConditional: lang.isLinkedConditional,
isIterator: lang.isIterator,
isLoader: lang.isLoader,
isInclude: lang.isInclude,
resolveContent: lang.resolveContent,
resolveConditional: lang.resolveConditional
};