in juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/StringExpressionMatcher.java [96:218]
private Exp parse(String expression) throws ParseException {
if (StringUtils.isEmptyOrBlank(expression))
return new Never();
expression = expression.trim();
List<Exp> ors = list();
List<Exp> ands = list();
StateMachineState state = S01;
int i = 0, mark = -1;
int pDepth = 0;
boolean error = false;
for (i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (state == S01) {
// S01 = Looking for start
if (! WS.contains(c)) {
if (c == '(') {
state = S02;
pDepth = 0;
mark = i+1;
} else if (OP.contains(c)) {
error = true;
break;
} else {
state = S03;
mark = i;
}
}
} else if (state == S02) {
// S02 = Found [(], looking for [)].
if (c == '(')
pDepth++;
if (c == ')') {
if (pDepth > 0)
pDepth--;
else {
ands.add(parse(expression.substring(mark, i)));
mark = -1;
state = S04;
}
}
} else if (state == S03) {
// S03 = Found [A], looking for end of A.
if (WS.contains(c) || OP.contains(c)) {
ands.add(parseOperand(expression.substring(mark, i)));
mark = -1;
if (WS.contains(c)) {
state = S04;
} else {
i--;
state = S05;
}
}
} else if (state == S04) {
// S04 = Found [A ], looking for & or | or ,.
if (! WS.contains(c)) {
if (OP.contains(c)) {
i--;
state = S05;
} else {
error = true;
break;
}
}
} else if (state == S05) {
// S05 = Found & or | or ,.
if (c == '&') {
//ands.add(operand);
state = S06;
} else /* (c == '|' || c == ',') */ {
if (ands.size() == 1) {
ors.add(ands.get(0));
} else {
ors.add(new And(ands));
}
ands.clear();
if (c == '|') {
state = S07;
} else {
state = S01;
}
}
} else if (state == S06) {
// S06 = Found &, looking for & or other
if (! WS.contains(c)) {
if (c != '&')
i--;
state = S01;
}
} else /* (state == S07) */ {
// S07 = Found |, looking for | or other
if (! WS.contains(c)) {
if (c != '|')
i--;
state = S01;
}
}
}
if (error)
throw new ParseException("Invalid character in expression '"+expression+"' at position " + i + ". state=" + state, i);
if (state == S01)
throw new ParseException("Could not find beginning of clause in '"+expression+"'", i);
if (state == S02)
throw new ParseException("Could not find matching parenthesis in expression '"+expression+"'", i);
if (state == S05 || state == S06 || state == S07)
throw new ParseException("Dangling clause in expression '"+expression+"'", i);
if (mark != -1)
ands.add(parseOperand(expression.substring(mark, expression.length())));
if (ands.size() == 1)
ors.add(ands.get(0));
else
ors.add(new And(ands));
if (ors.size() == 1)
return ors.get(0);
return new Or(ors);
}