package jxl.biff.formula;

import common.Assert;
import common.Logger;
import java.util.Stack;
import jxl.Cell;
import jxl.WorkbookSettings;
import jxl.biff.WorkbookMethods;

/* JADX INFO: loaded from: classes.dex */
class TokenFormulaParser implements Parser {
    static /* synthetic */ Class class$jxl$biff$formula$TokenFormulaParser;
    private static Logger logger;
    private WorkbookMethods nameTable;
    private Cell relativeTo;
    private ParseItem root;
    private WorkbookSettings settings;
    private byte[] tokenData;
    private ExternalSheet workbook;
    private int pos = 0;
    private Stack tokenStack = new Stack();

    static {
        Class clsClass$ = class$jxl$biff$formula$TokenFormulaParser;
        if (clsClass$ == null) {
            clsClass$ = class$("jxl.biff.formula.TokenFormulaParser");
            class$jxl$biff$formula$TokenFormulaParser = clsClass$;
        }
        logger = Logger.getLogger(clsClass$);
    }

    public TokenFormulaParser(byte[] bArr, Cell cell, ExternalSheet externalSheet, WorkbookMethods workbookMethods, WorkbookSettings workbookSettings) {
        this.tokenData = bArr;
        this.relativeTo = cell;
        this.workbook = externalSheet;
        this.nameTable = workbookMethods;
        this.settings = workbookSettings;
        Assert.verify(this.nameTable != null);
    }

    private void addOperator(Operator operator) {
        operator.getOperands(this.tokenStack);
        this.tokenStack.push(operator);
    }

    static /* synthetic */ Class class$(String str) {
        try {
            return Class.forName(str);
        } catch (ClassNotFoundException e) {
            throw new NoClassDefFoundError(e.getMessage());
        }
    }

    private void handleMemoryFunction(SubExpression subExpression) throws FormulaException {
        int i2 = this.pos;
        this.pos = i2 + subExpression.read(this.tokenData, i2);
        Stack stack = this.tokenStack;
        this.tokenStack = new Stack();
        parseSubExpression(subExpression.getLength());
        ParseItem[] parseItemArr = new ParseItem[this.tokenStack.size()];
        int i3 = 0;
        while (!this.tokenStack.isEmpty()) {
            parseItemArr[i3] = (ParseItem) this.tokenStack.pop();
            i3++;
        }
        subExpression.setSubExpression(parseItemArr);
        this.tokenStack = stack;
        stack.push(subExpression);
    }

    private void parseSubExpression(int i2) throws FormulaException {
        Stack stack = new Stack();
        int i3 = this.pos + i2;
        while (true) {
            int i4 = this.pos;
            if (i4 >= i3) {
                return;
            }
            byte b = this.tokenData[i4];
            this.pos = i4 + 1;
            Token token = Token.getToken(b);
            Token token2 = Token.UNKNOWN;
            if (token == token2) {
                throw new FormulaException(FormulaException.UNRECOGNIZED_TOKEN, b);
            }
            Assert.verify(token != token2);
            if (token == Token.REF) {
                CellReference cellReference = new CellReference(this.relativeTo);
                int i5 = this.pos;
                this.pos = i5 + cellReference.read(this.tokenData, i5);
                this.tokenStack.push(cellReference);
            } else if (token == Token.REFERR) {
                CellReferenceError cellReferenceError = new CellReferenceError();
                int i6 = this.pos;
                this.pos = i6 + cellReferenceError.read(this.tokenData, i6);
                this.tokenStack.push(cellReferenceError);
            } else if (token == Token.ERR) {
                ErrorConstant errorConstant = new ErrorConstant();
                int i7 = this.pos;
                this.pos = i7 + errorConstant.read(this.tokenData, i7);
                this.tokenStack.push(errorConstant);
            } else if (token == Token.REFV) {
                SharedFormulaCellReference sharedFormulaCellReference = new SharedFormulaCellReference(this.relativeTo);
                int i8 = this.pos;
                this.pos = i8 + sharedFormulaCellReference.read(this.tokenData, i8);
                this.tokenStack.push(sharedFormulaCellReference);
            } else if (token == Token.REF3D) {
                CellReference3d cellReference3d = new CellReference3d(this.relativeTo, this.workbook);
                int i9 = this.pos;
                this.pos = i9 + cellReference3d.read(this.tokenData, i9);
                this.tokenStack.push(cellReference3d);
            } else if (token == Token.AREA) {
                Area area = new Area();
                int i10 = this.pos;
                this.pos = i10 + area.read(this.tokenData, i10);
                this.tokenStack.push(area);
            } else if (token == Token.AREAV) {
                SharedFormulaArea sharedFormulaArea = new SharedFormulaArea(this.relativeTo);
                int i11 = this.pos;
                this.pos = i11 + sharedFormulaArea.read(this.tokenData, i11);
                this.tokenStack.push(sharedFormulaArea);
            } else if (token == Token.AREA3D) {
                Area3d area3d = new Area3d(this.workbook);
                int i12 = this.pos;
                this.pos = i12 + area3d.read(this.tokenData, i12);
                this.tokenStack.push(area3d);
            } else if (token == Token.NAME) {
                Name name = new Name();
                int i13 = this.pos;
                this.pos = i13 + name.read(this.tokenData, i13);
                this.tokenStack.push(name);
            } else if (token == Token.NAMED_RANGE) {
                NameRange nameRange = new NameRange(this.nameTable);
                int i14 = this.pos;
                this.pos = i14 + nameRange.read(this.tokenData, i14);
                this.tokenStack.push(nameRange);
            } else if (token == Token.INTEGER) {
                IntegerValue integerValue = new IntegerValue();
                int i15 = this.pos;
                this.pos = i15 + integerValue.read(this.tokenData, i15);
                this.tokenStack.push(integerValue);
            } else if (token == Token.DOUBLE) {
                DoubleValue doubleValue = new DoubleValue();
                int i16 = this.pos;
                this.pos = i16 + doubleValue.read(this.tokenData, i16);
                this.tokenStack.push(doubleValue);
            } else if (token == Token.BOOL) {
                BooleanValue booleanValue = new BooleanValue();
                int i17 = this.pos;
                this.pos = i17 + booleanValue.read(this.tokenData, i17);
                this.tokenStack.push(booleanValue);
            } else if (token == Token.STRING) {
                StringValue stringValue = new StringValue(this.settings);
                int i18 = this.pos;
                this.pos = i18 + stringValue.read(this.tokenData, i18);
                this.tokenStack.push(stringValue);
            } else if (token == Token.MISSING_ARG) {
                MissingArg missingArg = new MissingArg();
                int i19 = this.pos;
                this.pos = i19 + missingArg.read(this.tokenData, i19);
                this.tokenStack.push(missingArg);
            } else if (token == Token.UNARY_PLUS) {
                UnaryPlus unaryPlus = new UnaryPlus();
                int i20 = this.pos;
                this.pos = i20 + unaryPlus.read(this.tokenData, i20);
                addOperator(unaryPlus);
            } else if (token == Token.UNARY_MINUS) {
                UnaryMinus unaryMinus = new UnaryMinus();
                int i21 = this.pos;
                this.pos = i21 + unaryMinus.read(this.tokenData, i21);
                addOperator(unaryMinus);
            } else if (token == Token.PERCENT) {
                Percent percent = new Percent();
                int i22 = this.pos;
                this.pos = i22 + percent.read(this.tokenData, i22);
                addOperator(percent);
            } else if (token == Token.SUBTRACT) {
                Subtract subtract = new Subtract();
                int i23 = this.pos;
                this.pos = i23 + subtract.read(this.tokenData, i23);
                addOperator(subtract);
            } else if (token == Token.ADD) {
                Add add = new Add();
                int i24 = this.pos;
                this.pos = i24 + add.read(this.tokenData, i24);
                addOperator(add);
            } else if (token == Token.MULTIPLY) {
                Multiply multiply = new Multiply();
                int i25 = this.pos;
                this.pos = i25 + multiply.read(this.tokenData, i25);
                addOperator(multiply);
            } else if (token == Token.DIVIDE) {
                Divide divide = new Divide();
                int i26 = this.pos;
                this.pos = i26 + divide.read(this.tokenData, i26);
                addOperator(divide);
            } else if (token == Token.CONCAT) {
                Concatenate concatenate = new Concatenate();
                int i27 = this.pos;
                this.pos = i27 + concatenate.read(this.tokenData, i27);
                addOperator(concatenate);
            } else if (token == Token.POWER) {
                Power power = new Power();
                int i28 = this.pos;
                this.pos = i28 + power.read(this.tokenData, i28);
                addOperator(power);
            } else if (token == Token.LESS_THAN) {
                LessThan lessThan = new LessThan();
                int i29 = this.pos;
                this.pos = i29 + lessThan.read(this.tokenData, i29);
                addOperator(lessThan);
            } else if (token == Token.LESS_EQUAL) {
                LessEqual lessEqual = new LessEqual();
                int i30 = this.pos;
                this.pos = i30 + lessEqual.read(this.tokenData, i30);
                addOperator(lessEqual);
            } else if (token == Token.GREATER_THAN) {
                GreaterThan greaterThan = new GreaterThan();
                int i31 = this.pos;
                this.pos = i31 + greaterThan.read(this.tokenData, i31);
                addOperator(greaterThan);
            } else if (token == Token.GREATER_EQUAL) {
                GreaterEqual greaterEqual = new GreaterEqual();
                int i32 = this.pos;
                this.pos = i32 + greaterEqual.read(this.tokenData, i32);
                addOperator(greaterEqual);
            } else if (token == Token.NOT_EQUAL) {
                NotEqual notEqual = new NotEqual();
                int i33 = this.pos;
                this.pos = i33 + notEqual.read(this.tokenData, i33);
                addOperator(notEqual);
            } else if (token == Token.EQUAL) {
                Equal equal = new Equal();
                int i34 = this.pos;
                this.pos = i34 + equal.read(this.tokenData, i34);
                addOperator(equal);
            } else if (token == Token.PARENTHESIS) {
                Parenthesis parenthesis = new Parenthesis();
                int i35 = this.pos;
                this.pos = i35 + parenthesis.read(this.tokenData, i35);
                addOperator(parenthesis);
            } else if (token == Token.ATTRIBUTE) {
                Attribute attribute = new Attribute(this.settings);
                int i36 = this.pos;
                this.pos = i36 + attribute.read(this.tokenData, i36);
                if (attribute.isSum()) {
                    addOperator(attribute);
                } else if (attribute.isIf()) {
                    stack.push(attribute);
                }
            } else if (token == Token.FUNCTION) {
                BuiltInFunction builtInFunction = new BuiltInFunction(this.settings);
                int i37 = this.pos;
                this.pos = i37 + builtInFunction.read(this.tokenData, i37);
                addOperator(builtInFunction);
            } else if (token == Token.FUNCTIONVARARG) {
                VariableArgFunction variableArgFunction = new VariableArgFunction(this.settings);
                int i38 = this.pos;
                this.pos = i38 + variableArgFunction.read(this.tokenData, i38);
                if (variableArgFunction.getFunction() != Function.ATTRIBUTE) {
                    addOperator(variableArgFunction);
                } else {
                    variableArgFunction.getOperands(this.tokenStack);
                    Attribute attribute2 = stack.empty() ? new Attribute(this.settings) : (Attribute) stack.pop();
                    attribute2.setIfConditions(variableArgFunction);
                    this.tokenStack.push(attribute2);
                }
            } else if (token == Token.MEM_FUNC) {
                handleMemoryFunction(new MemFunc());
            } else if (token == Token.MEM_AREA) {
                handleMemoryFunction(new MemArea());
            }
        }
    }

    @Override // jxl.biff.formula.Parser
    public void adjustRelativeCellReferences(int i2, int i3) {
        this.root.adjustRelativeCellReferences(i2, i3);
    }

    @Override // jxl.biff.formula.Parser
    public void columnInserted(int i2, int i3, boolean z) {
        this.root.columnInserted(i2, i3, z);
    }

    @Override // jxl.biff.formula.Parser
    public void columnRemoved(int i2, int i3, boolean z) {
        this.root.columnRemoved(i2, i3, z);
    }

    @Override // jxl.biff.formula.Parser
    public byte[] getBytes() {
        return this.root.getBytes();
    }

    @Override // jxl.biff.formula.Parser
    public String getFormula() {
        StringBuffer stringBuffer = new StringBuffer();
        this.root.getString(stringBuffer);
        return stringBuffer.toString();
    }

    @Override // jxl.biff.formula.Parser
    public boolean handleImportedCellReferences() {
        this.root.handleImportedCellReferences();
        return this.root.isValid();
    }

    @Override // jxl.biff.formula.Parser
    public void parse() throws FormulaException {
        parseSubExpression(this.tokenData.length);
        this.root = (ParseItem) this.tokenStack.pop();
        Assert.verify(this.tokenStack.empty());
    }

    @Override // jxl.biff.formula.Parser
    public void rowInserted(int i2, int i3, boolean z) {
        this.root.rowInserted(i2, i3, z);
    }

    @Override // jxl.biff.formula.Parser
    public void rowRemoved(int i2, int i3, boolean z) {
        this.root.rowRemoved(i2, i3, z);
    }
}
