/
grammar.js
167 lines (163 loc) · 4.47 KB
/
grammar.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
/*
* Jexl
* Copyright 2020 Tom Shawver
*/
/* eslint eqeqeq:0 */
exports.getGrammar = () => ({
/**
* A map of all expression elements to their properties. Note that changes
* here may require changes in the Lexer or Parser.
* @type {{}}
*/
elements: {
'.': { type: 'dot' },
'[': { type: 'openBracket' },
']': { type: 'closeBracket' },
'|': { type: 'pipe' },
'{': { type: 'openCurl' },
'}': { type: 'closeCurl' },
':': { type: 'colon' },
',': { type: 'comma' },
'(': { type: 'openParen' },
')': { type: 'closeParen' },
'?': { type: 'question' },
'+': {
type: 'binaryOp',
precedence: 30,
eval: (left, right) => left + right
},
'-': {
type: 'binaryOp',
precedence: 30,
eval: (left, right) => left - right
},
'*': {
type: 'binaryOp',
precedence: 40,
eval: (left, right) => left * right
},
'/': {
type: 'binaryOp',
precedence: 40,
eval: (left, right) => left / right
},
'//': {
type: 'binaryOp',
precedence: 40,
eval: (left, right) => Math.floor(left / right)
},
'%': {
type: 'binaryOp',
precedence: 50,
eval: (left, right) => left % right
},
'^': {
type: 'binaryOp',
precedence: 50,
eval: (left, right) => Math.pow(left, right)
},
'==': {
type: 'binaryOp',
precedence: 20,
eval: (left, right) => left == right
},
'!=': {
type: 'binaryOp',
precedence: 20,
eval: (left, right) => left != right
},
'>': {
type: 'binaryOp',
precedence: 20,
eval: (left, right) => left > right
},
'>=': {
type: 'binaryOp',
precedence: 20,
eval: (left, right) => left >= right
},
'<': {
type: 'binaryOp',
precedence: 20,
eval: (left, right) => left < right
},
'<=': {
type: 'binaryOp',
precedence: 20,
eval: (left, right) => left <= right
},
'&&': {
type: 'binaryOp',
precedence: 10,
evalOnDemand: (left, right) => {
return left.eval().then((leftVal) => {
if (!leftVal) return leftVal
return right.eval()
})
}
},
'||': {
type: 'binaryOp',
precedence: 10,
evalOnDemand: (left, right) => {
return left.eval().then((leftVal) => {
if (leftVal) return leftVal
return right.eval()
})
}
},
in: {
type: 'binaryOp',
precedence: 20,
eval: (left, right) => {
if (typeof right === 'string') {
return right.indexOf(left) !== -1
}
if (Array.isArray(right)) {
return right.some((elem) => elem === left)
}
return false
}
},
'!': {
type: 'unaryOp',
precedence: Infinity,
eval: (right) => !right
}
},
/**
* A map of function names to javascript functions. A Jexl function
* takes zero ore more arguemnts:
*
* - {*} ...args: A variable number of arguments passed to this function.
* All of these are pre-evaluated to their actual values before calling
* the function.
*
* The Jexl function should return either the transformed value, or
* a Promises/A+ Promise object that resolves with the value and rejects
* or throws only when an unrecoverable error occurs. Functions should
* generally return undefined when they don't make sense to be used on the
* given value type, rather than throw/reject. An error is only
* appropriate when the function would normally return a value, but
* cannot due to some other failure.
*/
functions: {},
/**
* A map of transform names to transform functions. A transform function
* takes one ore more arguemnts:
*
* - {*} val: A value to be transformed
* - {*} ...args: A variable number of arguments passed to this transform.
* All of these are pre-evaluated to their actual values before calling
* the function.
*
* The transform function should return either the transformed value, or
* a Promises/A+ Promise object that resolves with the value and rejects
* or throws only when an unrecoverable error occurs. Transforms should
* generally return undefined when they don't make sense to be used on the
* given value type, rather than throw/reject. An error is only
* appropriate when the transform would normally return a value, but
* cannot due to some other failure.
*/
transforms: {}
})