From 949bea093b21a71cfabb24037e46de823e2fd601 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Fri, 23 May 2008 06:54:46 +0000 Subject: [PATCH] Bug 45025 - improved FormulaParser parse error messages (r659452 had wrong bug number) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@659455 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 2 +- src/documentation/content/xdocs/status.xml | 2 +- .../apache/poi/hssf/model/FormulaParser.java | 27 ++++++------------- .../poi/hssf/model/TestFormulaParser.java | 5 ++-- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 720cce0808..b36ccbffa9 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,7 +37,7 @@ - 45041 - improved FormulaParser parse error messages + 45025 - improved FormulaParser parse error messages 45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable 45066 - fixed sheet encoding size mismatch problems 45003 - Support embeded HDGF visio documents diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index e68afab268..1b37e157cd 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,7 +34,7 @@ - 45041 - improved FormulaParser parse error messages + 45025 - improved FormulaParser parse error messages 45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable 45066 - fixed sheet encoding size mismatch problems 45003 - Support embeded HDGF visio documents diff --git a/src/java/org/apache/poi/hssf/model/FormulaParser.java b/src/java/org/apache/poi/hssf/model/FormulaParser.java index c32df9396b..b6c7117290 100644 --- a/src/java/org/apache/poi/hssf/model/FormulaParser.java +++ b/src/java/org/apache/poi/hssf/model/FormulaParser.java @@ -140,29 +140,21 @@ public final class FormulaParser { return new FormulaParseException(msg); } - - /** Recognize an Alpha Character */ private boolean IsAlpha(char c) { return Character.isLetter(c) || c == '$' || c=='_'; } - - /** Recognize a Decimal Digit */ private boolean IsDigit(char c) { - //System.out.println("Checking digit for"+c); return Character.isDigit(c); } - - /** Recognize an Alphanumeric */ private boolean IsAlNum(char c) { return (IsAlpha(c) || IsDigit(c)); } - /** Recognize White Space */ private boolean IsWhite( char c) { return (c ==' ' || c== TAB); @@ -218,7 +210,6 @@ public final class FormulaParser { return Token.toString(); } - /** Get a Number */ private String GetNum() { StringBuffer value = new StringBuffer(); @@ -564,9 +555,6 @@ public final class FormulaParser { return new ParenthesisPtg(); case '"': return parseStringLiteral(); - case ',': - case ')': - return new MissingArgPtg(); // TODO - not quite the right place to recognise a missing arg } if (IsAlpha(look) || look == '\''){ return parseIdent(); @@ -710,8 +698,7 @@ public final class FormulaParser { } - private StringPtg parseStringLiteral() - { + private StringPtg parseStringLiteral() { Match('"'); StringBuffer token = new StringBuffer(); @@ -841,8 +828,9 @@ end; **/ - /** API call to execute the parsing of the formula - * + /** + * API call to execute the parsing of the formula + * @deprecated use Ptg[] FormulaParser.parse(String, HSSFWorkbook) directly */ public void parse() { pointer=0; @@ -866,11 +854,12 @@ end; * a result of the parsing */ public Ptg[] getRPNPtg() { - return getRPNPtg(FORMULA_TYPE_CELL); + return getRPNPtg(FORMULA_TYPE_CELL); } public Ptg[] getRPNPtg(int formulaType) { Node node = createTree(); + // RVA is for 'operand class': 'reference', 'value', 'array' setRootLevelRVA(node, formulaType); setParameterRVA(node,formulaType); return (Ptg[]) tokens.toArray(new Ptg[0]); @@ -951,7 +940,7 @@ end; } } /** - * Convience method which takes in a list then passes it to the + * Convenience method which takes in a list then passes it to the * other toFormulaString signature. * @param book workbook for 3D and named references * @param lptgs list of Ptg, can be null or empty @@ -966,7 +955,7 @@ end; return retval; } /** - * Convience method which takes in a list then passes it to the + * Convenience method which takes in a list then passes it to the * other toFormulaString signature. Works on the current * workbook for 3D and named references * @param lptgs list of Ptg, can be null or empty diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java index f4d6d62692..f2821140f3 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java +++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java @@ -55,9 +55,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * Test the low level formula parser functionality. High level tests are to - * be done via usermodel/HSSFCell.setFormulaValue() . - * Some tests are also done in scratchpad, if they need - * HSSFFormulaEvaluator, which is there + * be done via usermodel/HSSFCell.setFormulaValue(). */ public final class TestFormulaParser extends TestCase { @@ -76,6 +74,7 @@ public final class TestFormulaParser extends TestCase { Ptg[] ptgs = parseFormula("2+2"); assertEquals(3, ptgs.length); } + public void testFormulaWithSpace1() { Ptg[] ptgs = parseFormula(" 2 + 2 "); assertEquals(3, ptgs.length); -- 2.39.5