* @throws reportgen.ren.exception.ReportException
*/
final protected Stack<MathExpression> buildStack(List<MathExpression> elem, int beg, int end,
Stack<MathExpression> stack) throws ReportException {
if(beg > end) {
throw new ReportException("Пустое выражение");
}
//get min priority operand
int pos = -1;
int priority = Integer.MAX_VALUE;
MathExpressionOperatorRef operator = null;
for(int j=end; j>=beg; j--) {
MathExpression element = elem.get(j);
if(element instanceof MathExpressionOperatorRef) {
MathExpressionOperatorRef iOperator = (MathExpressionOperatorRef) element;
int prior = iOperator.getRef().getPriority();
if(prior < priority) {
priority = prior;
pos = j;
operator = iOperator;
}
}
}
if(pos == -1) {
if(end == beg) {
//it allways be operand
stack.add(elem.get(0));
return stack;
}
throw new ReportException("Выражение составлено неверно, пропущен оператор");
}
if(pos == 0) {
throw new ReportException("Перед оператором '" + operator + "' должен быть операнд");
} else if(pos+1 == elem.size()) {
throw new ReportException("После оператора '" + operator + "' должен быть операнд");
}
int leftOperandPos = pos-1;
int rightOperandPos = pos+1;
MathExpression leftOperand = elem.get(leftOperandPos);
MathExpression rightOperand = elem.get(rightOperandPos);
if(!(leftOperand instanceof MathExpressionOperand)) {
throw new ReportException("Перед оператором '" + operator + "' должен быть операнд");
} else if (!(rightOperand instanceof MathExpressionOperand)) {
throw new ReportException("После оператора '" + operator + "' должен быть операнд");
}
stack.add(operator);
//right part