-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseconditions.js
202 lines (173 loc) · 6.97 KB
/
parseconditions.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
function evaluateExpression(cond, arr) {
/*
The main function for parsing the conditions.
Algorithm works as follows:
For each character in the expression:
If the character is an operator (one of '(', ')', '+', '-' ),
then push it to the operator stack
otherwise push the character to the expression stack
If also, the expression stack and operator stack contains
then evaluate pop the composites, calculate and push the result back on to the expression stack
The expression stack thus keeps composite expressions together and works through the entire expression from left to right until complete.
*/
cond = '(' + cond + ')'
var newCond = cond.replace(/ AND /, '*').replace(/ OR /, '+')
while(newCond != cond){
newCond = newCond.replace(/ AND /, '*').replace(/ OR /, '+')
cond = cond.replace(/ AND /, '*').replace(/ OR /, '+')
}
cond = cond.split('')
Logger.log(cond)
//also take out double whitespace
var el
var index
//pad with ( + expr + )
expressionStack = ['']
operatorStack = []
for(c in cond){
index = parseInt(c)
el = cond[index]
if(['(', ')', '*', '+'].indexOf(el) > -1){
operatorStack.push(el)
var prevOperator = operatorStack[operatorStack.length - 2]
var prevExpression = expressionStack[expressionStack.length - 2]
var curExpression = expressionStack[expressionStack.length - 1]
Logger.log(operatorStack)
Logger.log(expressionStack)
Logger.log(el)
Logger.log(prevOperator)
if(el == ')' && prevOperator == '('){
//()
expressionStack[expressionStack.length - 1] =
evaluateBasicExpression(curExpression, arr)
operatorStack.pop()
operatorStack.pop()
continue
}
if(el == '*' || el == '+' || el == ')'){
if(prevOperator == '+' || prevOperator == '*'){
//++ or +)
//statement finished, evaluate base and push up level
expressionStack[expressionStack.length - 1] =
evaluateBasicExpression(curExpression, arr)
//combine base with row above
expressionStack[expressionStack.length - 2] =
compoundBooleanArr(expressionStack[expressionStack.length - 1],
prevExpression,
prevOperator)
//two into one
expressionStack.pop()
//used three
if (el == ')'){
operatorStack.pop()
operatorStack.pop()
operatorStack.pop()
}
if (el == '*' || el == '+'){
operatorStack.pop()
operatorStack.pop()
operatorStack.push(el)
}
}
if (prevOperator == '(' || prevOperator == null){
//statement finished, evaluate
expressionStack[expressionStack.length - 1] =
evaluateBasicExpression(curExpression, arr)
}
}
if(['(', '*', '+', ')'].indexOf(cond[index+1]) == -1){
//only push if you know next thing is not a symbol and it's not the last char, dont push first in this variation
if(index != 0 && index < cond.length - 1){
expressionStack.push('')
} else {
// Logger.log("last element up next, no push")
// Logger.log(cond[index+1])
}
}
} else {
//partial push to string only
expressionStack[expressionStack.length - 1] = expressionStack[expressionStack.length - 1] + el
}
}
//confirm the structure we have here
return expressionStack[0]
}
function isBasicExpression(statement) {
if(statement.indexOf(" AND ") == -1 && statement.indexOf(" OR ") == -1){
return false
} else {
return true
}
}
function evaluateBasicExpression(statement, arr) {
if(isArray(statement)){
//check if it has already been evaluated
return(statement)
}
var parsedStatement = parseBasicExpression(statement)
var lh_index = namedFieldIndex(parsedStatement["lh"], arr)
switch (parsedStatement["comparator"]) {
case ' is not ':
return arr[lh_index].map(function(val) {return val != parsedStatement["rh"]});
break;
case ' is ':
return arr[lh_index].map(function(val) {return val == parsedStatement["rh"]});
break;
case ' contains ':
return arr[lh_index].map(function(val) {return (val.indexOf(parsedStatement["rh"]) > -1)});
break;
case ' contains not ':
return arr[lh_index].map(function(val) {return (val.indexOf(parsedStatement["rh"]) == -1)});
break;
case ' after ':
return arr[lh_index].map(function(val) {return (val > parsedStatement["rh"])});
break;
case ' before ':
return arr[lh_index].map(function(val) {return (val < parsedStatement["rh"])});
break;
}
}
function parseBasicExpression(statement){
//trim whitespace
statement = statement.trim()
//parseables have the 'not' option first so that we can simply detect
var parseables = [' is not ', ' is ', ' contains not ', ' contains ', ' after ', ' before '];
var comparator
for (comparator in parseables){
comparator = parseables[comparator]
if(statement.indexOf(comparator) > -1){
comparator = comparator
break
}
}
//if we get to the end and no match found, then return NULL
if (!(parseables.indexOf(comparator) > -1)){return 'comparator not in parseables'}
if ((comparator == ' after ' || comparator == ' before ') && statement.split(comparator)[0] != 'date'){
return
}
return {lh: statement.split(comparator)[0], comparator: comparator, rh: statement.split(comparator)[1]}
}
function checkConditions(cond){
//check if any remaining parenthesis anywhere
if (cond.match(/\(/g).length != cond.match(/\)/g).length){
return false
}
//reduce pairings of parenthesis
while (cond.match(/\(([^()]+)\)/g)){
//check innermost
var innermost = cond.match(/\(([^()]+)\)/g);
for (match in innermost){
if(!(parseBoolStatement(innermost[match]))){
return false
}
}
cond = cond.replace(/\(([^()]+)\)/g, 'valid is valid')
Logger.log('list of conditions becomes:')
Logger.log(cond)
}
//check if any remaining parenthesis anywhere
if ((cond.match(/\(/g) || cond.match(/\)/g))){
return false
}
return true
}