formulaparser.java
来自「EXCEL read and write」· Java 代码 · 共 1,059 行 · 第 1/3 页
JAVA
1,059 行
} private Object[] parseArrayRow() { List temp = new ArrayList(); while (true) { temp.add(parseArrayItem()); SkipWhite(); switch(look) { case '}': case ';': break; case ',': Match(','); continue; default: throw expected("'}' or ','"); } break; } Object[] result = new Object[temp.size()]; temp.toArray(result); return result; } private Object parseArrayItem() { SkipWhite(); switch(look) { case '"': return new UnicodeString(parseStringLiteral()); case '#': return ErrorConstant.valueOf(parseErrorLiteral()); case 'F': case 'f': case 'T': case 't': return parseBooleanLiteral(); } // else assume number return convertArrayNumber(parseNumber()); } private Boolean parseBooleanLiteral() { String iden = parseUnquotedIdentifier(); if ("TRUE".equalsIgnoreCase(iden)) { return Boolean.TRUE; } if ("FALSE".equalsIgnoreCase(iden)) { return Boolean.FALSE; } throw expected("'TRUE' or 'FALSE'"); } private static Double convertArrayNumber(Ptg ptg) { if (ptg instanceof IntPtg) { return new Double(((IntPtg)ptg).getValue()); } if (ptg instanceof NumberPtg) { return new Double(((NumberPtg)ptg).getValue()); } throw new RuntimeException("Unexpected ptg (" + ptg.getClass().getName() + ")"); } private Ptg parseNumber() { String number2 = null; String exponent = null; String number1 = GetNum(); if (look == '.') { GetChar(); number2 = GetNum(); } if (look == 'E') { GetChar(); String sign = ""; if (look == '+') { GetChar(); } else if (look == '-') { GetChar(); sign = "-"; } String number = GetNum(); if (number == null) { throw expected("Integer"); } exponent = sign + number; } if (number1 == null && number2 == null) { throw expected("Integer"); } return getNumberPtgFromString(number1, number2, exponent); } private int parseErrorLiteral() { Match('#'); String part1 = parseUnquotedIdentifier().toUpperCase(); switch(part1.charAt(0)) { case 'V': if(part1.equals("VALUE")) { Match('!'); return HSSFErrorConstants.ERROR_VALUE; } throw expected("#VALUE!"); case 'R': if(part1.equals("REF")) { Match('!'); return HSSFErrorConstants.ERROR_REF; } throw expected("#REF!"); case 'D': if(part1.equals("DIV")) { Match('/'); Match('0'); Match('!'); return HSSFErrorConstants.ERROR_DIV_0; } throw expected("#DIV/0!"); case 'N': if(part1.equals("NAME")) { Match('?'); // only one that ends in '?' return HSSFErrorConstants.ERROR_NAME; } if(part1.equals("NUM")) { Match('!'); return HSSFErrorConstants.ERROR_NUM; } if(part1.equals("NULL")) { Match('!'); return HSSFErrorConstants.ERROR_NULL; } if(part1.equals("N")) { Match('/'); if(look != 'A' && look != 'a') { throw expected("#N/A"); } Match(look); // Note - no '!' or '?' suffix return HSSFErrorConstants.ERROR_NA; } throw expected("#NAME?, #NUM!, #NULL! or #N/A"); } throw expected("#VALUE!, #REF!, #DIV/0!, #NAME?, #NUM!, #NULL! or #N/A"); } /** * 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) { StringBuffer number = new StringBuffer(); 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()); } private String parseStringLiteral() { Match('"'); StringBuffer token = new StringBuffer(); while (true) { if (look == '"') { GetChar(); if (look != '"') { break; } } token.append(look); GetChar(); } return token.toString(); } /** Parse and Translate a Math Term */ private ParseNode Term() { ParseNode result = powerFactor(); while(true) { SkipWhite(); Ptg operator; switch(look) { case '*': Match('*'); operator = MultiplyPtg.instance; break; case '/': Match('/'); operator = DividePtg.instance; break; default: return result; // finished with Term } ParseNode other = powerFactor(); result = new ParseNode(operator, result, other); } } private ParseNode comparisonExpression() { ParseNode result = concatExpression(); while (true) { SkipWhite(); switch(look) { case '=': case '>': case '<': Ptg comparisonToken = getComparisonToken(); ParseNode other = concatExpression(); result = new ParseNode(comparisonToken, result, other); continue; } return result; // finished with predicate expression } } private Ptg getComparisonToken() { if(look == '=') { Match(look); return EqualPtg.instance; } boolean isGreater = look == '>'; Match(look); if(isGreater) { if(look == '=') { Match('='); return GreaterEqualPtg.instance; } return GreaterThanPtg.instance; } switch(look) { case '=': Match('='); return LessEqualPtg.instance; case '>': Match('>'); return NotEqualPtg.instance; } return LessThanPtg.instance; } private ParseNode concatExpression() { ParseNode result = additiveExpression(); while (true) { SkipWhite(); if(look != '&') { break; // finished with concat expression } Match('&'); ParseNode other = additiveExpression(); result = new ParseNode(ConcatPtg.instance, result, other); } return result; } /** Parse and Translate an Expression */ private ParseNode additiveExpression() { ParseNode result = Term(); while (true) { SkipWhite(); Ptg operator; switch(look) { case '+': Match('+'); operator = AddPtg.instance; break; case '-': Match('-'); operator = SubtractPtg.instance; break; default: return result; // finished with additive expression } ParseNode other = Term(); result = new ParseNode(operator, result, other); } } //{--------------------------------------------------------------} //{ Parse and Translate an Assignment Statement } /**procedure Assignment;var Name: string[8];begin Name := GetName; Match('='); Expression;end; **/ /** * API call to execute the parsing of the formula * */ private void parse() { pointer=0; GetChar(); _rootNode = comparisonExpression(); if(pointer <= formulaLength) { String msg = "Unused input [" + formulaString.substring(pointer-1) + "] after attempting to parse the formula [" + formulaString + "]"; throw new FormulaParseException(msg); } } private Ptg[] getRPNPtg(int formulaType) { OperandClassTransformer oct = new OperandClassTransformer(formulaType); // RVA is for 'operand class': 'reference', 'value', 'array' oct.transformFormula(_rootNode); return ParseNode.toTokenArray(_rootNode); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?