Bug 45041 - improved FormulaParser parse error messages

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@659452 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-05-23 06:43:51 +00:00
parent 143128be30
commit dc3f6b36b8
4 changed files with 767 additions and 821 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.1-final" date="2008-06-??">
<action dev="POI-DEVELOPERS" type="add">45041 - improved FormulaParser parse error messages</action>
<action dev="POI-DEVELOPERS" type="add">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
<action dev="POI-DEVELOPERS" type="add">45066 - fixed sheet encoding size mismatch problems</action>
<action dev="POI-DEVELOPERS" type="add">45003 - Support embeded HDGF visio documents</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.1-final" date="2008-06-??">
<action dev="POI-DEVELOPERS" type="add">45041 - improved FormulaParser parse error messages</action>
<action dev="POI-DEVELOPERS" type="add">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
<action dev="POI-DEVELOPERS" type="add">45066 - fixed sheet encoding size mismatch problems</action>
<action dev="POI-DEVELOPERS" type="add">45003 - Support embeded HDGF visio documents</action>

View File

@ -486,7 +486,6 @@ public final class FormulaParser {
}
boolean missedPrevArg = true;
int numArgs = 0;
while (true) {
SkipWhite();
@ -507,6 +506,10 @@ public final class FormulaParser {
addArgumentPointer(argumentPointers);
numArgs++;
missedPrevArg = false;
SkipWhite();
if (!isArgumentDelimiter(look)) {
throw expected("',' or ')'");
}
}
return numArgs;
}

View File

@ -65,7 +65,6 @@ public final class TestFormulaParser extends TestCase {
* @return parsed token array already confirmed not <code>null</code>
*/
private static Ptg[] parseFormula(String s) {
// TODO - replace multiple copies of this code with calls to this method
FormulaParser fp = new FormulaParser(s, null);
fp.parse();
Ptg[] result = fp.getRPNPtg();
@ -74,69 +73,48 @@ public final class TestFormulaParser extends TestCase {
}
public void testSimpleFormula() {
FormulaParser fp = new FormulaParser("2+2",null);
fp.parse();
Ptg[] ptgs = fp.getRPNPtg();
assertTrue("three tokens expected, got "+ptgs.length,ptgs.length == 3);
Ptg[] ptgs = parseFormula("2+2");
assertEquals(3, ptgs.length);
}
public void testFormulaWithSpace1() {
FormulaParser fp = new FormulaParser(" 2 + 2 ",null);
fp.parse();
Ptg[] ptgs = fp.getRPNPtg();
assertTrue("three tokens expected, got "+ptgs.length,ptgs.length == 3);
Ptg[] ptgs = parseFormula(" 2 + 2 ");
assertEquals(3, ptgs.length);
assertTrue("",(ptgs[0] instanceof IntPtg));
assertTrue("",(ptgs[1] instanceof IntPtg));
assertTrue("",(ptgs[2] instanceof AddPtg));
}
public void testFormulaWithSpace2() {
Ptg[] ptgs;
FormulaParser fp;
fp = new FormulaParser("2+ sum( 3 , 4) ",null);
fp.parse();
ptgs = fp.getRPNPtg();
assertTrue("five tokens expected, got "+ptgs.length,ptgs.length == 5);
Ptg[] ptgs = parseFormula("2+ sum( 3 , 4) ");
assertEquals(5, ptgs.length);
}
public void testFormulaWithSpaceNRef() {
Ptg[] ptgs;
FormulaParser fp;
fp = new FormulaParser("sum( A2:A3 )",null);
fp.parse();
ptgs = fp.getRPNPtg();
assertTrue("two tokens expected, got "+ptgs.length,ptgs.length == 2);
Ptg[] ptgs = parseFormula("sum( A2:A3 )");
assertEquals(2, ptgs.length);
}
public void testFormulaWithString() {
Ptg[] ptgs;
FormulaParser fp;
fp = new FormulaParser("\"hello\" & \"world\" ",null);
fp.parse();
ptgs = fp.getRPNPtg();
assertTrue("three token expected, got " + ptgs.length, ptgs.length == 3);
Ptg[] ptgs = parseFormula("\"hello\" & \"world\" ");
assertEquals(3, ptgs.length);
}
public void testTRUE() throws Exception {
FormulaParser fp = new FormulaParser("TRUE", null);
fp.parse();
Ptg[] asts = fp.getRPNPtg();
assertEquals(1, asts.length);
BoolPtg flag = (BoolPtg) asts[0];
public void testTRUE() {
Ptg[] ptgs = parseFormula("TRUE");
assertEquals(1, ptgs.length);
BoolPtg flag = (BoolPtg) ptgs[0];
assertEquals(true, flag.getValue());
}
public void testYN() throws Exception {
final String yn = "IF(TRUE,\"Y\",\"N\")";
FormulaParser fp = new FormulaParser(yn, null);
fp.parse();
Ptg[] asts = fp.getRPNPtg();
assertEquals(7, asts.length);
public void testYN() {
Ptg[] ptgs = parseFormula("IF(TRUE,\"Y\",\"N\")");
assertEquals(7, ptgs.length);
BoolPtg flag = (BoolPtg) asts[0];
AttrPtg funif = (AttrPtg) asts[1];
StringPtg y = (StringPtg) asts[2];
AttrPtg goto1 = (AttrPtg) asts[3];
StringPtg n = (StringPtg) asts[4];
BoolPtg flag = (BoolPtg) ptgs[0];
AttrPtg funif = (AttrPtg) ptgs[1];
StringPtg y = (StringPtg) ptgs[2];
AttrPtg goto1 = (AttrPtg) ptgs[3];
StringPtg n = (StringPtg) ptgs[4];
assertEquals(true, flag.getValue());
@ -146,29 +124,31 @@ public final class TestFormulaParser extends TestCase {
assertTrue("Goto ptg exists", goto1.isGoto());
}
public void testSimpleIf() throws Exception {
final String simpleif = "IF(1=1,0,1)";
FormulaParser fp = new FormulaParser(simpleif, null);
fp.parse();
Ptg[] asts = fp.getRPNPtg();
assertEquals(9, asts.length);
public void testSimpleIf() {
String formula = "IF(1=1,0,1)";
IntPtg op1 = (IntPtg) asts[0];
IntPtg op2 = (IntPtg) asts[1];
EqualPtg eq = (EqualPtg) asts[2];
AttrPtg ifPtg = (AttrPtg) asts[3];
IntPtg res1 = (IntPtg) asts[4];
Class[] expectedClasses = {
IntPtg.class,
IntPtg.class,
EqualPtg.class,
AttrPtg.class,
IntPtg.class,
AttrPtg.class,
IntPtg.class,
AttrPtg.class,
FuncVarPtg.class,
};
confirmTokenClasses(formula, expectedClasses);
AttrPtg ptgGoto= (AttrPtg) asts[5];
assertEquals("Goto 1 Length", (short)10, ptgGoto.getData());
Ptg[] ptgs = parseFormula(formula);
IntPtg res2 = (IntPtg) asts[6];
AttrPtg ptgGoto2 = (AttrPtg) asts[7];
assertEquals("Goto 2 Length", (short)3, ptgGoto2.getData());
AttrPtg ifPtg = (AttrPtg) ptgs[3];
AttrPtg ptgGoto= (AttrPtg) ptgs[5];
assertEquals("Goto 1 Length", 10, ptgGoto.getData());
assertEquals("If FALSE offset", (short)7, ifPtg.getData());
FuncVarPtg funcPtg = (FuncVarPtg)asts[8];
AttrPtg ptgGoto2 = (AttrPtg) ptgs[7];
assertEquals("Goto 2 Length", 3, ptgGoto2.getData());
assertEquals("If FALSE offset", 7, ifPtg.getData());
}
/**
@ -176,47 +156,36 @@ public final class TestFormulaParser extends TestCase {
*
*/
public void testNestedFunctionIf() {
String function = "IF(A1=B1,AVERAGE(A1:B1),AVERAGE(A2:B2))";
Ptg[] ptgs = parseFormula("IF(A1=B1,AVERAGE(A1:B1),AVERAGE(A2:B2))");
assertEquals(11, ptgs.length);
FormulaParser fp = new FormulaParser(function, null);
fp.parse();
Ptg[] asts = fp.getRPNPtg();
assertEquals("11 Ptgs expected", 11, asts.length);
assertTrue("IF Attr set correctly", (asts[3] instanceof AttrPtg));
AttrPtg ifFunc = (AttrPtg)asts[3];
assertTrue("IF Attr set correctly", (ptgs[3] instanceof AttrPtg));
AttrPtg ifFunc = (AttrPtg)ptgs[3];
assertTrue("It is not an if", ifFunc.isOptimizedIf());
assertTrue("Average Function set correctly", (asts[5] instanceof FuncVarPtg));
assertTrue("Average Function set correctly", (ptgs[5] instanceof FuncVarPtg));
}
public void testIfSingleCondition(){
String function = "IF(1=1,10)";
Ptg[] ptgs = parseFormula("IF(1=1,10)");
assertEquals(7, ptgs.length);
FormulaParser fp = new FormulaParser(function, null);
fp.parse();
Ptg[] asts = fp.getRPNPtg();
assertEquals("7 Ptgs expected", 7, asts.length);
assertTrue("IF Attr set correctly", (asts[3] instanceof AttrPtg));
AttrPtg ifFunc = (AttrPtg)asts[3];
assertTrue("IF Attr set correctly", (ptgs[3] instanceof AttrPtg));
AttrPtg ifFunc = (AttrPtg)ptgs[3];
assertTrue("It is not an if", ifFunc.isOptimizedIf());
assertTrue("Single Value is not an IntPtg", (asts[4] instanceof IntPtg));
IntPtg intPtg = (IntPtg)asts[4];
assertTrue("Single Value is not an IntPtg", (ptgs[4] instanceof IntPtg));
IntPtg intPtg = (IntPtg)ptgs[4];
assertEquals("Result", (short)10, intPtg.getValue());
assertTrue("Ptg is not a Variable Function", (asts[6] instanceof FuncVarPtg));
FuncVarPtg funcPtg = (FuncVarPtg)asts[6];
assertTrue("Ptg is not a Variable Function", (ptgs[6] instanceof FuncVarPtg));
FuncVarPtg funcPtg = (FuncVarPtg)ptgs[6];
assertEquals("Arguments", 2, funcPtg.getNumberOfOperands());
}
public void testSumIf() {
String function ="SUMIF(A1:A5,\">4000\",B1:B5)";
FormulaParser fp = new FormulaParser(function, null);
fp.parse();
Ptg[] asts = fp.getRPNPtg();
assertEquals("4 Ptgs expected", 4, asts.length);
Ptg[] ptgs = parseFormula("SUMIF(A1:A5,\">4000\",B1:B5)");
assertEquals(4, ptgs.length);
}
/**
@ -226,12 +195,10 @@ public final class TestFormulaParser extends TestCase {
*/
public void testNonAlphaFormula() {
String currencyCell = "F3";
String function="\"TOTAL[\"&"+currencyCell+"&\"]\"";
Ptg[] asts = parseFormula(function);
assertEquals("5 ptgs expected", 5, asts.length);
assertTrue ("Ptg[0] is a string", (asts[0] instanceof StringPtg));
StringPtg firstString = (StringPtg)asts[0];
Ptg[] ptgs = parseFormula("\"TOTAL[\"&"+currencyCell+"&\"]\"");
assertEquals(5, ptgs.length);
assertTrue ("Ptg[0] is a string", (ptgs[0] instanceof StringPtg));
StringPtg firstString = (StringPtg)ptgs[0];
assertEquals("TOTAL[", firstString.getValue());
//the PTG order isn't 100% correct but it still works - dmui
@ -239,20 +206,20 @@ public final class TestFormulaParser extends TestCase {
public void testSimpleLogical() {
Ptg[] ptgs = parseFormula("IF(A1<A2,B1,B2)");
assertEquals("Ptg array length", 9, ptgs.length);
assertEquals(9, ptgs.length);
assertEquals("3rd Ptg is less than", LessThanPtg.class, ptgs[2].getClass());
}
public void testParenIf() {
Ptg[] ptgs = parseFormula("IF((A1+A2)<=3,\"yes\",\"no\")");
assertEquals("Ptg array length", 12, ptgs.length);
assertEquals(12, ptgs.length);
assertEquals("6th Ptg is less than equal",LessEqualPtg.class,ptgs[5].getClass());
assertEquals("11th Ptg is not a goto (Attr) ptg",AttrPtg.class,ptgs[10].getClass());
}
public void testEmbeddedIf() {
Ptg[] ptgs = parseFormula("IF(3>=1,\"*\",IF(4<>1,\"first\",\"second\"))");
assertEquals("Ptg array length", 17, ptgs.length);
assertEquals(17, ptgs.length);
assertEquals("6th Ptg is not a goto (Attr) ptg",AttrPtg.class,ptgs[5].getClass());
assertEquals("9th Ptg is not a not equal ptg",NotEqualPtg.class,ptgs[8].getClass());
@ -274,23 +241,18 @@ public final class TestFormulaParser extends TestCase {
}
public void testEmbeddedSlash() {
FormulaParser fp = new FormulaParser("HYPERLINK(\"http://www.jakarta.org\",\"Jakarta\")",null);
fp.parse();
Ptg[] ptg = fp.getRPNPtg();
assertTrue("first ptg is string",ptg[0] instanceof StringPtg);
assertTrue("second ptg is string",ptg[1] instanceof StringPtg);
Ptg[] ptgs = parseFormula("HYPERLINK(\"http://www.jakarta.org\",\"Jakarta\")");
assertTrue("first ptg is string", ptgs[0] instanceof StringPtg);
assertTrue("second ptg is string", ptgs[1] instanceof StringPtg);
}
public void testConcatenate() {
FormulaParser fp = new FormulaParser("CONCATENATE(\"first\",\"second\")", null);
fp.parse();
Ptg[] ptg = fp.getRPNPtg();
assertTrue("first ptg is string", ptg[0] instanceof StringPtg);
assertTrue("second ptg is string", ptg[1] instanceof StringPtg);
Ptg[] ptgs = parseFormula("CONCATENATE(\"first\",\"second\")");
assertTrue("first ptg is string", ptgs[0] instanceof StringPtg);
assertTrue("second ptg is string", ptgs[1] instanceof StringPtg);
}
public void testWorksheetReferences()
{
public void testWorksheetReferences() {
HSSFWorkbook wb = new HSSFWorkbook();
wb.createSheet("NoQuotesNeeded");
@ -307,74 +269,54 @@ public final class TestFormulaParser extends TestCase {
cell.setCellFormula("'Quotes Needed Here &#$@'!A1");
}
public void testUnaryMinus()
{
FormulaParser fp = new FormulaParser("-A1", null);
fp.parse();
Ptg[] ptg = fp.getRPNPtg();
assertTrue("got 2 ptgs", ptg.length == 2);
assertTrue("first ptg is reference",ptg[0] instanceof ReferencePtg);
assertTrue("second ptg is Minus",ptg[1] instanceof UnaryMinusPtg);
public void testUnaryMinus() {
Ptg[] ptgs = parseFormula("-A1");
assertEquals(2, ptgs.length);
assertTrue("first ptg is reference",ptgs[0] instanceof ReferencePtg);
assertTrue("second ptg is Minus",ptgs[1] instanceof UnaryMinusPtg);
}
public void testUnaryPlus()
{
FormulaParser fp = new FormulaParser("+A1", null);
fp.parse();
Ptg[] ptg = fp.getRPNPtg();
assertTrue("got 2 ptgs", ptg.length == 2);
assertTrue("first ptg is reference",ptg[0] instanceof ReferencePtg);
assertTrue("second ptg is Plus",ptg[1] instanceof UnaryPlusPtg);
public void testUnaryPlus() {
Ptg[] ptgs = parseFormula("+A1");
assertEquals(2, ptgs.length);
assertTrue("first ptg is reference",ptgs[0] instanceof ReferencePtg);
assertTrue("second ptg is Plus",ptgs[1] instanceof UnaryPlusPtg);
}
public void testLeadingSpaceInString()
{
public void testLeadingSpaceInString() {
String value = " hi ";
FormulaParser fp = new FormulaParser("\"" + value + "\"", null);
fp.parse();
Ptg[] ptg = fp.getRPNPtg();
Ptg[] ptgs = parseFormula("\"" + value + "\"");
assertTrue("got 1 ptg", ptg.length == 1);
assertTrue("ptg0 is a StringPtg", ptg[0] instanceof StringPtg);
assertTrue("ptg0 contains exact value", ((StringPtg)ptg[0]).getValue().equals(value));
assertEquals(1, ptgs.length);
assertTrue("ptg0 is a StringPtg", ptgs[0] instanceof StringPtg);
assertTrue("ptg0 contains exact value", ((StringPtg)ptgs[0]).getValue().equals(value));
}
public void testLookupAndMatchFunctionArgs()
{
FormulaParser fp = new FormulaParser("lookup(A1, A3:A52, B3:B52)", null);
fp.parse();
Ptg[] ptg = fp.getRPNPtg();
public void testLookupAndMatchFunctionArgs() {
Ptg[] ptgs = parseFormula("lookup(A1, A3:A52, B3:B52)");
assertTrue("got 4 ptg", ptg.length == 4);
assertTrue("ptg0 has Value class", ptg[0].getPtgClass() == Ptg.CLASS_VALUE);
assertEquals(4, ptgs.length);
assertTrue("ptg0 has Value class", ptgs[0].getPtgClass() == Ptg.CLASS_VALUE);
fp = new FormulaParser("match(A1, A3:A52)", null);
fp.parse();
ptg = fp.getRPNPtg();
ptgs = parseFormula("match(A1, A3:A52)");
assertTrue("got 3 ptg", ptg.length == 3);
assertTrue("ptg0 has Value class", ptg[0].getPtgClass() == Ptg.CLASS_VALUE);
assertEquals(3, ptgs.length);
assertTrue("ptg0 has Value class", ptgs[0].getPtgClass() == Ptg.CLASS_VALUE);
}
/** bug 33160*/
public void testLargeInt() {
FormulaParser fp = new FormulaParser("40", null);
fp.parse();
Ptg[] ptg=fp.getRPNPtg();
assertTrue("ptg is Int, is "+ptg[0].getClass(),ptg[0] instanceof IntPtg);
Ptg[] ptgs = parseFormula("40");
assertTrue("ptg is Int, is "+ptgs[0].getClass(),ptgs[0] instanceof IntPtg);
fp = new FormulaParser("40000", null);
fp.parse();
ptg=fp.getRPNPtg();
assertTrue("ptg should be IntPtg, is "+ptg[0].getClass(), ptg[0] instanceof IntPtg);
ptgs = parseFormula("40000");
assertTrue("ptg should be IntPtg, is "+ptgs[0].getClass(), ptgs[0] instanceof IntPtg);
}
/** bug 33160, testcase by Amol Deshmukh*/
public void testSimpleLongFormula() {
FormulaParser fp = new FormulaParser("40000/2", null);
fp.parse();
Ptg[] ptgs = fp.getRPNPtg();
assertTrue("three tokens expected, got " + ptgs.length, ptgs.length == 3);
Ptg[] ptgs = parseFormula("40000/2");
assertEquals(3, ptgs.length);
assertTrue("IntPtg", (ptgs[0] instanceof IntPtg));
assertTrue("IntPtg", (ptgs[1] instanceof IntPtg));
assertTrue("DividePtg", (ptgs[2] instanceof DividePtg));
@ -392,37 +334,31 @@ public final class TestFormulaParser extends TestCase {
cell = row.createCell((short)0);
cell.setCellFormula("Cash_Flow!A1");
}
// bug 38396 : Formula with exponential numbers not parsed correctly.
public void testExponentialParsing() {
FormulaParser fp = new FormulaParser("1.3E21/2", null);
fp.parse();
Ptg[] ptgs = fp.getRPNPtg();
assertTrue("three tokens expected, got " + ptgs.length, ptgs.length == 3);
Ptg[] ptgs;
ptgs = parseFormula("1.3E21/2");
assertEquals(3, ptgs.length);
assertTrue("NumberPtg", (ptgs[0] instanceof NumberPtg));
assertTrue("IntPtg", (ptgs[1] instanceof IntPtg));
assertTrue("DividePtg", (ptgs[2] instanceof DividePtg));
fp = new FormulaParser("1322E21/2", null);
fp.parse();
ptgs = fp.getRPNPtg();
assertTrue("three tokens expected, got " + ptgs.length, ptgs.length == 3);
ptgs = parseFormula("1322E21/2");
assertEquals(3, ptgs.length);
assertTrue("NumberPtg", (ptgs[0] instanceof NumberPtg));
assertTrue("IntPtg", (ptgs[1] instanceof IntPtg));
assertTrue("DividePtg", (ptgs[2] instanceof DividePtg));
fp = new FormulaParser("1.3E1/2", null);
fp.parse();
ptgs = fp.getRPNPtg();
assertTrue("three tokens expected, got " + ptgs.length, ptgs.length == 3);
ptgs = parseFormula("1.3E1/2");
assertEquals(3, ptgs.length);
assertTrue("NumberPtg", (ptgs[0] instanceof NumberPtg));
assertTrue("IntPtg", (ptgs[1] instanceof IntPtg));
assertTrue("DividePtg", (ptgs[2] instanceof DividePtg));
}
public void testExponentialInSheet() throws Exception {
public void testExponentialInSheet() {
HSSFWorkbook wb = new HSSFWorkbook();
wb.createSheet("Cash_Flow");
@ -493,11 +429,6 @@ public final class TestFormulaParser extends TestCase {
assertEquals("Exponential formula string", "-1.0/310.0*4000.0/30000.0", formula);
}
public static void main(String [] args) {
System.out.println("Testing org.apache.poi.hssf.record.formula.FormulaParser");
junit.textui.TestRunner.run(TestFormulaParser.class);
}
public void testNumbers() {
HSSFWorkbook wb = new HSSFWorkbook();
@ -896,15 +827,15 @@ public final class TestFormulaParser extends TestCase {
* (e.g. COUNTIF) Excel fails to evaluate the formula, giving '#VALUE!' instead.
*/
public void testFuncPtgSelection() {
HSSFWorkbook book = new HSSFWorkbook();
Ptg[] ptgs;
ptgs = FormulaParser.parse("countif(A1:A2, 1)", book);
ptgs = parseFormula("countif(A1:A2, 1)");
assertEquals(3, ptgs.length);
if(FuncVarPtg.class == ptgs[2].getClass()) {
throw new AssertionFailedError("Identified bug 44675");
}
assertEquals(FuncPtg.class, ptgs[2].getClass());
ptgs = FormulaParser.parse("sin(1)", book);
ptgs = parseFormula("sin(1)");
assertEquals(2, ptgs.length);
assertEquals(FuncPtg.class, ptgs[1].getClass());
}
@ -925,4 +856,14 @@ public final class TestFormulaParser extends TestCase {
assertEquals(expectedMessage, e.getMessage());
}
}
public void testParseErrorExpecteMsg() {
try {
parseFormula("round(3.14;2)");
throw new AssertionFailedError("Didn't get parse exception as expected");
} catch (FormulaParseException e) {
assertEquals("Parse error near char 10 ';' in specified formula 'round(3.14;2)'. Expected ',' or ')'", e.getMessage());
}
}
}