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 */
public class TokenFormulaParser implements Parser {
    public 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);
    }

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

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