fpspreadsheet: Reactivate the expression parser test cases which were commented (and forgotten) some time ago. Activate missing argument tests. Add some "exotic" formulas. All passed.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4186 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2015-06-08 17:03:55 +00:00
parent c4c1eeece9
commit e121751361
5 changed files with 129 additions and 186 deletions

View File

@ -1259,14 +1259,6 @@ begin
ParserError(SErrUnexpectedEndOfExpression);
end;
{ If the result types differ, they are converted to a common type if possible. }
{
procedure TsExpressionParser.CheckNodes(var ALeft, ARight: TsExprNode);
begin
ALeft := MatchNodes(ALeft, ARight);
ARight := MatchNodes(ARight, ALeft);
end;
}
procedure TsExpressionParser.CheckResultType(const Res: TsExpressionResult;
AType: TsResultType); inline;
begin
@ -1280,24 +1272,7 @@ begin
FHashList.Clear;
FreeAndNil(FExprNode);
end;
(*
function TsExpressionParser.ConvertNode(ToDo: TsExprNode;
ToType: TsResultType): TsExprNode;
begin
Result := ToDo;
case ToDo.NodeType of
rtInteger :
case ToType of
rtFloat : Result := TsIntToFloatExprNode.Create(self, Result);
rtDateTime : Result := TsIntToDateTimeExprNode.Create(self, Result);
end;
rtFloat :
case ToType of
rtDateTime : Result := TsFloatToDateTimeExprNode.Create(self, Result);
end;
end;
end;
*)
{ Prepares copy mode: The formula is contained in ASourceCell and will be
modified such as seen from ADestCell. }
procedure TsExpressionParser.PrepareCopyMode(ASourceCell, ADestCell: PCell);
@ -1487,7 +1462,6 @@ begin
GetToken;
CheckEOF;
Right := Level3;
//CheckNodes(Result, right);
case tt of
ttLessthan : C := TsLessExprNode;
ttLessthanEqual : C := TsLessEqualExprNode;
@ -1519,7 +1493,6 @@ begin
GetToken;
CheckEOF;
right := Level4;
//CheckNodes(Result, right);
case tt of
ttPlus : Result := TsAddExprNode.Create(self, Result, right);
ttMinus : Result := TsSubtractExprNode.Create(self, Result, right);
@ -1545,7 +1518,6 @@ begin
tt := TokenType;
GetToken;
right := Level5;
//CheckNodes(Result, right);
case tt of
ttMul : Result := TsMultiplyExprNode.Create(self, Result, right);
ttDiv : Result := TsDivideExprNode.Create(self, Result, right);
@ -1570,7 +1542,6 @@ begin
tt := TokenType;
GetToken;
right := Level6;
//CheckNodes(Result, right);
Result := TsPowerExprNode.Create(self, Result, right);
end;
except
@ -1581,13 +1552,10 @@ end;
function TsExpressionParser.Level6: TsExprNode;
var
//isPlus, isMinus: Boolean;
signs: String;
i: Integer;
begin
{$ifdef debugexpr} Writeln('Level 6 ',TokenName(TokenType),': ',CurrentToken);{$endif debugexpr}
// isPlus := false;
// isMinus := false;
signs := '';
while (TokenType in [ttPlus, ttMinus]) do
begin
@ -1595,10 +1563,6 @@ begin
ttPlus : signs := signs + '+';
ttMinus : signs := signs + '-';
end;
{
isPlus := (TokenType = ttPlus);
isMinus := (TokenType = ttMinus);
}
GetToken;
end;
Result := Level7;
@ -1610,23 +1574,11 @@ begin
end;
dec(i);
end;
{
if isPlus then
Result := TsUPlusExprNode.Create(self, Result);
if isMinus then
Result := TsUMinusExprNode.Create(self, Result);
}
while TokenType = ttPercent do begin
Result := TsPercentExprNode.Create(self, Result);
GetToken;
end;
{
if TokenType = ttPercent then begin
Result := TsPercentExprNode.Create(self, Result);
GetToken;
end;
}
end;
function TsExpressionParser.Level7: TsExprNode;
@ -1653,53 +1605,7 @@ begin
end
else
Result := Primitive;
{
if TokenType = ttPower then
begin
try
CheckEOF;
GetToken;
Right := Level1; //Primitive;
CheckNodes(Result, right);
Result := TsPowerExprNode.Create(self, Result, Right);
//GetToken;
except
Result.Free;
raise;
end;
end; }
{
if TokenType = ttPercent then begin
Result := TsPercentExprNode.Create(self, Result);
GetToken;
end;
}
end;
(*
{ Checks types of todo and match. If ToDO can be converted to it matches
the type of match, then a node is inserted.
For binary operations, this function is called for both operands. }
function TsExpressionParser.MatchNodes(ToDo, Match: TsExprNode): TsExprNode;
var
TT, MT : TsResultType;
begin
Result := ToDo;
TT := ToDo.NodeType;
MT := Match.NodeType;
if TT <> MT then
begin
if TT = rtInteger then
begin
if (MT in [rtFloat, rtDateTime]) then
Result := ConvertNode(ToDo, MT);
end
else if (TT = rtFloat) then
begin
if (MT = rtDateTime) then
Result := ConvertNode(ToDo, rtDateTime);
end;
end;
end; *)
procedure TsExpressionParser.ParserError(Msg: String);
begin
@ -2057,14 +1963,7 @@ begin
CreateNodeFromRPN(FExprNode, index);
if Assigned(FExprNode) then FExprNode.Check;
end;
(*
{ Signals that the parser is in SharedFormulaMode, i.e. there is an active cell
to which all relative addresses have to be adapted. }
function TsExpressionParser.SharedFormulaMode: Boolean;
begin
Result := (ActiveCell <> nil) and (ActiveCell^.SharedFormulaBase <> nil);
end;
*)
function TsExpressionParser.TokenType: TsTokenType;
begin
Result := FScanner.TokenType;
@ -2236,10 +2135,6 @@ begin
if ID.ExcelCode = AExcelCode then exit;
dec(Result);
end;
{
while (Result >= 0) and (GetI(Result).ExcelCode = AExcelCode) do
dec(Result);
}
end;
procedure TsExprIdentifierDefs.SetI(AIndex: Integer;
@ -2816,16 +2711,7 @@ function TsUPlusExprNode.AsString: String;
begin
Result := '+' + TrimLeft(Operand.AsString);
end;
{
procedure TsUPlusExprNode.Check;
const
AllowedTokens = [rtInteger, rtFloat, rtCell, rtEmpty, rtError];
begin
inherited;
if not (Operand.NodeType in AllowedTokens) then
RaiseParserError(SErrNoUPlus, [ResultTypeName(Operand.NodeType), Operand.AsString])
end;
}
procedure TsUPlusExprNode.GetNodeValue(out Result: TsExpressionResult);
var
cell: PCell;
@ -2875,16 +2761,7 @@ function TsUMinusExprNode.AsString: String;
begin
Result := '-' + TrimLeft(Operand.AsString);
end;
{
procedure TsUMinusExprNode.Check;
const
AllowedTokens = [rtInteger, rtFloat, rtCell, rtEmpty, rtError];
begin
inherited;
if not (Operand.NodeType in AllowedTokens) then
RaiseParserError(SErrNoNegation, [ResultTypeName(Operand.NodeType), Operand.AsString])
end;
}
procedure TsUMinusExprNode.GetNodeValue(out Result: TsExpressionResult);
var
cell: PCell;

View File

@ -212,7 +212,13 @@ begin
exit;
end;
if Length(Args) = 2 then begin
if (Length(Args) = 2) then
begin
if (Args[1].ResultType = rtMissingArg) then
begin
Result := ErrorResult(errOverflow); // #NUM! as tested by Excel
exit;
end;
base := ArgToFloat(Args[1]);
if base < 0 then begin
Result := ErrorResult(errOverflow); // #NUM!
@ -698,19 +704,19 @@ var
begin
s := ArgToString(Args[0]);
if s = '' then
Result.ResultType := rtEmpty
Result := EmptyResult
else
if Length(Args) > 2 then
Result := ErrorResult(errArgError)
else
begin
if Length(Args) = 1 then
count := 1
else
if Args[1].ResultType in [rtInteger, rtFloat] then
count := ArgToInt(Args[1])
if Args[1].ResultType = rtMissingArg then
count := 1
else
begin
Result := ErrorResult(errWrongType);
exit;
end;
count := ArgToInt(Args[1]);
Result := StringResult(UTF8LeftStr(s, count));
end;
end;
@ -780,18 +786,19 @@ var
begin
s := ArgToString(Args[0]);
if s = '' then
Result.ResultType := rtEmpty
else begin
Result := EmptyResult
else
if Length(Args) > 2 then
Result := ErrorResult(errArgError)
else
begin
if Length(Args) = 1 then
count := 1
else
if Args[1].ResultType in [rtInteger, rtFloat] then
count := ArgToInt(Args[1])
if Args[1].ResultType = rtMissingArg then
count := 1
else
begin
Result := ErrorResult(errWrongType);
exit;
end;
count := ArgToInt(Args[1]);
Result := StringResult(UTF8RightStr(s, count));
end;
end;

View File

@ -4349,7 +4349,7 @@ begin
FWorksheet.FindMergedRange(ACell, r1, c1, r2, c2);
rowsSpannedStr := Format(' table:number-rows-spanned="%d"', [r2 - r1 + 1]);
colsSpannedStr := Format(' table:number-columns-spanned="%d"', [c2 - c1 + 1]);
spannedStr := colsSpannedStr + ' ' + rowsSpannedStr;
spannedStr := colsSpannedStr + rowsSpannedStr;
end else
spannedStr := '';
@ -5088,7 +5088,7 @@ begin
FWorksheet.FindMergedRange(ACell, r1, c1, r2, c2);
rowsSpannedStr := Format(' table:number-rows-spanned="%d"', [r2 - r1 + 1]);
colsSpannedStr := Format(' table:number-columns-spanned="%d"', [c2 - c1 + 1]);
spannedStr := colsSpannedStr + ' ' + rowsSpannedStr;
spannedStr := colsSpannedStr + rowsSpannedStr;
end else
spannedStr := '';
@ -5211,7 +5211,7 @@ begin
FWorksheet.FindMergedRange(ACell, r1, c1, r2, c2);
rowsSpannedStr := Format(' table:number-rows-spanned="%d"', [r2 - r1 + 1]);
colsSpannedStr := Format(' table:number-columns-spanned="%d"', [c2 - c1 + 1]);
spannedStr := colsSpannedStr + ' ' + rowsSpannedStr;
spannedStr := colsSpannedStr + rowsSpannedStr;
end else
spannedStr := '';
@ -5311,7 +5311,7 @@ begin
FWorksheet.FindMergedRange(ACell, r1, c1, r2, c2);
rowsSpannedStr := Format(' table:number-rows-spanned="%d"', [r2 - r1 + 1]);
colsSpannedStr := Format(' table:number-columns-spanned="%d"', [c2 - c1 + 1]);
spannedStr := colsSpannedStr + ' ' + rowsSpannedStr;
spannedStr := colsSpannedStr + rowsSpannedStr;
end else
spannedStr := '';

View File

@ -1518,7 +1518,9 @@ begin
(Pos('&lt;', AppoSt) = 1) or
(Pos('&gt;', AppoSt) = 1) or
(Pos('&quot;', AppoSt) = 1) or
(Pos('&apos;', AppoSt) = 1) then begin
(Pos('&apos;', AppoSt) = 1) or
(Pos('&#37;', AppoSt) = 1) // %
then begin
//'&' is the first char of a special chat, it must not be converted
WrkStr:=WrkStr + AText[Idx];
end else begin
@ -1529,6 +1531,7 @@ begin
'>': WrkStr:=WrkStr + '&gt;';
'"': WrkStr:=WrkStr + '&quot;';
'''':WrkStr:=WrkStr + '&apos;';
'%': WrkStr:=WrkStr + '&#37;';
{
#10: WrkStr := WrkStr + '&#10;';
#13: WrkStr := WrkStr + '&#13;';

View File

@ -9,6 +9,8 @@
MyWorksheet.WriteNumber (1, 12, 1); // M2
MyWorksheet.WriteNumber (1, 13, 2); // N2
// Create an error in H2
MyWorksheet.WriteFormula (1, 7, '1/0');
{------------------------------------------------------------------------------}
{ Basic operations }
{------------------------------------------------------------------------------}
@ -52,7 +54,7 @@
{!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! DO NOT CHANGE THIS FORMULA - ITS RESULT (-9) IS HARD-CODED IN OTHER TESTS !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!}
(*
// Add cell values - relative addresses
inc(Row);
formula := 'B1+B2';
@ -317,6 +319,23 @@
SetLength(sollValues, Row+1);
sollValues[Row] := FloatResult(4 * power(2, 0.5));
// Exotic sample from OpenOffice test files
inc(Row);
formula := '50%^200%';
MyWorksheet.WriteUTF8Text(Row, 0, formula);
if UseRPNFormula then
MyWorksheet.WriteRPNFormula(Row, 1, CreateRPNFormula(
RPNNumber(50.0,
RPNFunc(fekPercent,
RPNNumber(200.0,
RPNFunc(fekPercent,
RPNFunc(fekPower, nil)))))))
else
MyWorksheet.WriteFormula(Row, 1, formula);
MyWorksheet.WriteNumber(Row, 2, power(0.5, 2));
SetLength(sollValues, Row+1);
sollValues[Row] := FloatResult(power(0.5, 2));
// Power function
if AFormat <> sfExcel2 then begin
// Power of constant
@ -428,6 +447,21 @@
SetLength(sollValues, Row+1);
sollValues[Row] := FloatResult(+cellB2);
// Exotic case from OpenOffice test files
inc(Row);
formula := '+-B2';
MyWorksheet.WriteUTF8Text(Row, 0, formula);
if UseRPNFormula then
MyWorksheet.WriteRPNFormula(Row, 1, CreateRPNFormula(
RPNCellvalue('B2',
RPNFunc(fekUMinus,
RPNFunc(fekUPlus, nil)))))
else
MyWorksheet.WriteFormula(Row, 1, formula);
MyWorksheet.WriteNumber(Row, 2, -cellB2);
SetLength(sollValues, Row+1);
sollValues[Row] := FloatResult(-cellB2);
// String result
inc(Row);
formula := '"Hallo"';
@ -1364,9 +1398,7 @@
SetLength(sollValues, Row+1);
sollValues[Row] := FloatResult(logn(2, number));
{ removed until new formula system supports missing arguments...
// LOG - valid result (2 arguments, base missing)
// LOG - error result (2 arguments, base missing)
inc(Row);
formula := 'LOG(0.1,)';
MyWorksheet.WriteUTF8Text(Row, 0, formula);
@ -1383,7 +1415,6 @@
sollValues[Row] := FloatResult(0)
else
sollValues[Row] := ErrorResult(errOverflow);
}
// LOG - valid result (1 argument)
inc(Row);
@ -3001,7 +3032,7 @@
SetLength(sollValues, Row+1);
sollValues[Row] := StringResult('Ha');
Myworksheet.WriteUTF8Text(Row, 2, sollValues[Row].ResString);
*)
// LEFT (2 parameters, utf8)
inc(Row);
formula := 'LEFT("Ändern",3)';
@ -3017,7 +3048,6 @@
sollValues[Row] := StringResult('Änd');
Myworksheet.WriteUTF8Text(Row, 2, sollValues[Row].ResString);
{ -- at the momement no missing arguments support
// LEFT (2 parameters, 1 of them missing)
inc(Row);
formula := 'LEFT("Hallo world",)';
@ -3032,7 +3062,7 @@
SetLength(sollValues, Row+1);
sollValues[Row] := StringResult('H');
Myworksheet.WriteUTF8Text(Row, 2, sollValues[Row].ResString);
}
// LEFT (1 parameter)
inc(Row);
formula := 'LEFT("Hallo world")';
@ -3169,7 +3199,6 @@
sollValues[Row] := StringResult('ld');
Myworksheet.WriteUTF8Text(Row, 2, sollValues[Row].ResString);
{ --- no missing parameter support now
// RIGHT (2 parameters, one of them missing)
inc(Row);
formula := 'RIGHT("Hallo world",)';
@ -3184,7 +3213,6 @@
SetLength(sollValues, Row+1);
sollValues[Row] := StringResult('d');
Myworksheet.WriteUTF8Text(Row, 2, sollValues[Row].ResString);
}
// RIGHT (1 parameter)
inc(Row);
@ -3444,8 +3472,37 @@
SetLength(sollValues, Row+1);
sollValues[Row] := CreateNumberArg(MyWorkbook.GetWorksheetCount);
*)
// IsBlank
// ERROR.TYPE
(* ---- fails for ODS ---> to be checked
inc(Row);
formula := 'ERROR.TYPE(A1)';
MyWorksheet.WriteUTF8Text(Row, 0, formula);
if UseRPNFormula then
MyWorksheet.WriteRPNFormula(Row, 1, CreateRPNFormula(
RPNCellRef('A1',
RPNFUNC('ERROR.TYPE', nil))))
else
MyWorksheet.WriteFormula(Row, 1, formula);
SetLength(sollvalues, Row+1);
sollValues[Row] := ErrorResult(errArgError);
MyWorksheet.WriteUTF8Text(Row, 2, '#N/A');
inc(Row);
formula := 'ERROR.TYPE(H2)'; // this cells contains "1/0" --> #DIV/0!
MyWorksheet.WriteUTF8Text(Row, 0, formula);
if UseRPNFormula then
MyWorksheet.WriteRPNFormula(Row, 1, CreateRPNFormula(
RPNCellRef('H2',
RPNFUNC('ERROR.TYPE', nil))))
else
MyWorksheet.WriteFormula(Row, 1, formula);
SetLength(sollvalues, Row+1);
sollValues[Row] := IntegerResult(ord(errDivideByZero));
MyWorksheet.WriteUTF8Text(Row, 2, IntToStr(ord(errDivideByZero)));
*)
// IsBlank
inc(Row);
formula := 'ISBLANK(A1)';
MyWorksheet.WriteUTF8Text(Row, 0, formula);
@ -3489,7 +3546,6 @@
// IsErr
inc(Row);
formula := 'ISERR(H2)';
MyWorksheet.WriteFormula(1, 7, '1/0'); // Create an error in H2
MyWorksheet.WriteUTF8Text(Row, 0, formula);
if UseRPNFormula then
myWorksheet.WriteRPNFormula(Row, 1, CreateRPNFormula(