Search in sources :

Example 1 with IntPtg

use of org.apache.poi.ss.formula.ptg.IntPtg in project poi by apache.

the class FormulaParser method getNumberPtgFromString.

/**
     * Get a PTG for an integer from its string representation.
     * return Int or Number Ptg based on size of input
     */
private static Ptg getNumberPtgFromString(String number1, String number2, String exponent) {
    StringBuilder number = new StringBuilder();
    if (number2 == null) {
        number.append(number1);
        if (exponent != null) {
            number.append('E');
            number.append(exponent);
        }
        String numberStr = number.toString();
        int intVal;
        try {
            intVal = Integer.parseInt(numberStr);
        } catch (NumberFormatException e) {
            return new NumberPtg(numberStr);
        }
        if (IntPtg.isInRange(intVal)) {
            return new IntPtg(intVal);
        }
        return new NumberPtg(numberStr);
    }
    if (number1 != null) {
        number.append(number1);
    }
    number.append('.');
    number.append(number2);
    if (exponent != null) {
        number.append('E');
        number.append(exponent);
    }
    return new NumberPtg(number.toString());
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg)

Example 2 with IntPtg

use of org.apache.poi.ss.formula.ptg.IntPtg in project poi by apache.

the class FormulaParser method parseUnary.

private ParseNode parseUnary(boolean isPlus) {
    boolean numberFollows = IsDigit(look) || look == '.';
    ParseNode factor = powerFactor();
    if (numberFollows) {
        // + or - directly next to a number is parsed with the number
        Ptg token = factor.getToken();
        if (token instanceof NumberPtg) {
            if (isPlus) {
                return factor;
            }
            token = new NumberPtg(-((NumberPtg) token).getValue());
            return new ParseNode(token);
        }
        if (token instanceof IntPtg) {
            if (isPlus) {
                return factor;
            }
            int intVal = ((IntPtg) token).getValue();
            // note - cannot use IntPtg for negatives
            token = new NumberPtg(-intVal);
            return new ParseNode(token);
        }
    }
    return new ParseNode(isPlus ? UnaryPlusPtg.instance : UnaryMinusPtg.instance, factor);
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) ArrayPtg(org.apache.poi.ss.formula.ptg.ArrayPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) PercentPtg(org.apache.poi.ss.formula.ptg.PercentPtg) RangePtg(org.apache.poi.ss.formula.ptg.RangePtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) EqualPtg(org.apache.poi.ss.formula.ptg.EqualPtg) UnaryMinusPtg(org.apache.poi.ss.formula.ptg.UnaryMinusPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) DividePtg(org.apache.poi.ss.formula.ptg.DividePtg) GreaterThanPtg(org.apache.poi.ss.formula.ptg.GreaterThanPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) NotEqualPtg(org.apache.poi.ss.formula.ptg.NotEqualPtg) ValueOperatorPtg(org.apache.poi.ss.formula.ptg.ValueOperatorPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) GreaterEqualPtg(org.apache.poi.ss.formula.ptg.GreaterEqualPtg) LessThanPtg(org.apache.poi.ss.formula.ptg.LessThanPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) IntersectionPtg(org.apache.poi.ss.formula.ptg.IntersectionPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) LessEqualPtg(org.apache.poi.ss.formula.ptg.LessEqualPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) SubtractPtg(org.apache.poi.ss.formula.ptg.SubtractPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) OperationPtg(org.apache.poi.ss.formula.ptg.OperationPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg)

Example 3 with IntPtg

use of org.apache.poi.ss.formula.ptg.IntPtg in project poi by apache.

the class TestFormulaParser method testTooFewOperandArgs.

/**
     * Checks some internal error detecting logic ('stack underflow error' in toFormulaString)
     */
@Test
public void testTooFewOperandArgs() {
    // Simulating badly encoded cell formula of "=/1"
    // Not sure if Excel could ever produce this
    Ptg[] ptgs = { // Excel would probably have put tMissArg here
    new IntPtg(1), DividePtg.instance };
    try {
        toFormulaString(ptgs);
        fail("Expected exception was not thrown");
    } catch (IllegalStateException e) {
        // expected during successful test
        assertTrue(e.getMessage().startsWith("Too few arguments supplied to operation"));
    }
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) ArrayPtg(org.apache.poi.ss.formula.ptg.ArrayPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) PercentPtg(org.apache.poi.ss.formula.ptg.PercentPtg) RangePtg(org.apache.poi.ss.formula.ptg.RangePtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) EqualPtg(org.apache.poi.ss.formula.ptg.EqualPtg) UnaryMinusPtg(org.apache.poi.ss.formula.ptg.UnaryMinusPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) DividePtg(org.apache.poi.ss.formula.ptg.DividePtg) GreaterThanPtg(org.apache.poi.ss.formula.ptg.GreaterThanPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) IntersectionPtg(org.apache.poi.ss.formula.ptg.IntersectionPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) SubtractPtg(org.apache.poi.ss.formula.ptg.SubtractPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) Test(org.junit.Test)

Example 4 with IntPtg

use of org.apache.poi.ss.formula.ptg.IntPtg in project poi by apache.

the class TestFormulaParser method testParseNumber.

@Test
public void testParseNumber() {
    IntPtg ip;
    // bug 33160
    ip = (IntPtg) parseSingleToken("40", IntPtg.class);
    assertEquals(40, ip.getValue());
    ip = (IntPtg) parseSingleToken("40000", IntPtg.class);
    assertEquals(40000, ip.getValue());
    // check the upper edge of the IntPtg range:
    ip = (IntPtg) parseSingleToken("65535", IntPtg.class);
    assertEquals(65535, ip.getValue());
    NumberPtg np = (NumberPtg) parseSingleToken("65536", NumberPtg.class);
    assertEquals(65536, np.getValue(), 0);
    np = (NumberPtg) parseSingleToken("65534.6", NumberPtg.class);
    assertEquals(65534.6, np.getValue(), 0);
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) Test(org.junit.Test)

Example 5 with IntPtg

use of org.apache.poi.ss.formula.ptg.IntPtg in project poi by apache.

the class TestFormulaParser method confirmUnary.

private static void confirmUnary(String formulaText, double val, Class<?>... expectedTokenTypes) {
    Ptg[] ptgs = parseFormula(formulaText);
    confirmTokenClasses(ptgs, expectedTokenTypes);
    Ptg ptg0 = ptgs[0];
    if (ptg0 instanceof IntPtg) {
        IntPtg intPtg = (IntPtg) ptg0;
        assertEquals((int) val, intPtg.getValue());
    } else if (ptg0 instanceof NumberPtg) {
        NumberPtg numberPtg = (NumberPtg) ptg0;
        assertEquals(val, numberPtg.getValue(), 0.0);
    } else {
        fail("bad ptg0 " + ptg0);
    }
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) ArrayPtg(org.apache.poi.ss.formula.ptg.ArrayPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) PercentPtg(org.apache.poi.ss.formula.ptg.PercentPtg) RangePtg(org.apache.poi.ss.formula.ptg.RangePtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) EqualPtg(org.apache.poi.ss.formula.ptg.EqualPtg) UnaryMinusPtg(org.apache.poi.ss.formula.ptg.UnaryMinusPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) DividePtg(org.apache.poi.ss.formula.ptg.DividePtg) GreaterThanPtg(org.apache.poi.ss.formula.ptg.GreaterThanPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) IntersectionPtg(org.apache.poi.ss.formula.ptg.IntersectionPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) SubtractPtg(org.apache.poi.ss.formula.ptg.SubtractPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg)

Aggregations

IntPtg (org.apache.poi.ss.formula.ptg.IntPtg)7 NumberPtg (org.apache.poi.ss.formula.ptg.NumberPtg)6 AddPtg (org.apache.poi.ss.formula.ptg.AddPtg)5 AttrPtg (org.apache.poi.ss.formula.ptg.AttrPtg)5 BoolPtg (org.apache.poi.ss.formula.ptg.BoolPtg)5 FuncPtg (org.apache.poi.ss.formula.ptg.FuncPtg)5 FuncVarPtg (org.apache.poi.ss.formula.ptg.FuncVarPtg)5 MultiplyPtg (org.apache.poi.ss.formula.ptg.MultiplyPtg)5 Ptg (org.apache.poi.ss.formula.ptg.Ptg)5 RefPtg (org.apache.poi.ss.formula.ptg.RefPtg)5 StringPtg (org.apache.poi.ss.formula.ptg.StringPtg)5 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)4 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)4 ArrayPtg (org.apache.poi.ss.formula.ptg.ArrayPtg)4 ConcatPtg (org.apache.poi.ss.formula.ptg.ConcatPtg)4 DividePtg (org.apache.poi.ss.formula.ptg.DividePtg)4 EqualPtg (org.apache.poi.ss.formula.ptg.EqualPtg)4 ErrPtg (org.apache.poi.ss.formula.ptg.ErrPtg)4 GreaterThanPtg (org.apache.poi.ss.formula.ptg.GreaterThanPtg)4 IntersectionPtg (org.apache.poi.ss.formula.ptg.IntersectionPtg)4