From d61c5aaa38d256ed4e8a224cd9aeffd61dc5c28f Mon Sep 17 00:00:00 2001 From: Martin Date: Sat, 27 Jul 2024 21:08:47 +0200 Subject: [PATCH] FpDebug: array-slice intrinsic, fix order for nested slices --- components/fpdebug/fppascalparser.pas | 31 +++++++++++++++--- components/fpdebug/test/testpascalparser.pas | 34 ++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/components/fpdebug/fppascalparser.pas b/components/fpdebug/fppascalparser.pas index 9cfb3a0b94..62994d5de5 100644 --- a/components/fpdebug/fppascalparser.pas +++ b/components/fpdebug/fppascalparser.pas @@ -689,6 +689,8 @@ type ATopPart: TFpPascalExpressionPart; AStartChar: PChar; AnEndChar: PChar = nil); function HandleNextPart(APart: TFpPascalExpressionPart): TFpPascalExpressionPart; override; + function GetFullText(AMaxLen: Integer = 0): String; override; + function DebugText(AIndent: String; AWithResults: Boolean): String; override; property SlicePart: TFpPascalExpressionPartOperatorArraySlice read FSlicePart; property DisableSlice: boolean read FDisableSlice write FDisableSlice; property CanDisableSlice: boolean read GetCanDisableSlice; @@ -7061,6 +7063,20 @@ begin Result := Self; end; +function TFpPascalExpressionPartOperatorArraySliceController.GetFullText(AMaxLen: Integer): String; +begin + Result := ''; + if FSlicePart <> nil then + Result := FSlicePart.GetFullText(AMaxLen); +end; + +function TFpPascalExpressionPartOperatorArraySliceController.DebugText(AIndent: String; + AWithResults: Boolean): String; +begin + Result := inherited DebugText(AIndent, AWithResults) + + AIndent +'// '+ GetFullText+LineEnding; +end; + function TFpPascalExpressionPartOperatorArraySliceController.GetCanDisableSlice: boolean; begin Result := (not FResultValDone); @@ -7071,6 +7087,7 @@ var tmp: TFpValue; begin FSlicePart.FCurrentIndex := FSlicePart.StartValue; + FSlicePart.EndValue; // needs to be touched FSlicePart.FTouched := False; ResetEvaluationForIndex; Result := Items[0].ResultValue; @@ -7126,7 +7143,11 @@ begin inherited Create(AExpression, AStartChar, AnEndChar); FSlicePart := ASlicePart; - while ATopPart is TFpPascalExpressionPartOperatorArraySliceController do + while (ATopPart is TFpPascalExpressionPartOperatorArraySliceController) and + (not FSlicePart.FindInParents( + TFpPascalExpressionPartOperatorArraySliceController(ATopPart).FSlicePart) + ) + do ATopPart := TFpPascalExpressionPartOperatorArraySliceController(ATopPart).Items[0]; ATopPart.ReplaceInParent(Self); Add(ATopPart); @@ -7143,11 +7164,13 @@ end; procedure TFpPascalExpressionPartOperatorArraySlice.SetParent( AValue: TFpPascalExpressionPartContainer); begin - if (Parent = nil) and (AValue <> nil) then + if (Parent = nil) and (AValue <> nil) then begin + inherited SetParent(AValue); FController := TFpPascalExpressionPartOperatorArraySliceController.Create(FExpression, Self, AValue.TopParent, FStartChar, FEndChar); - - inherited SetParent(AValue); + end + else + inherited SetParent(AValue); end; function TFpPascalExpressionPartOperatorArraySlice.DoGetResultValue: TFpValue; diff --git a/components/fpdebug/test/testpascalparser.pas b/components/fpdebug/test/testpascalparser.pas index 7153168dfe..fcbb26dc0c 100644 --- a/components/fpdebug/test/testpascalparser.pas +++ b/components/fpdebug/test/testpascalparser.pas @@ -554,6 +554,40 @@ begin // TestExpr([0,0,1], TFpPascalExpressionPartConstantNumber, '9', 0); + CreateExpr('a[b[1..2]..3]', True); + TestExpr([], TFpPascalExpressionPartOperatorArraySliceController, '..', 1); + AssertEquals('1..2', GetChild([]).GetFullText); + TestExpr([0], TFpPascalExpressionPartOperatorArraySliceController, '..', 1); + //AssertEquals('b[1..2]..3', GetChild([0]).GetFullText); + AssertEquals('[1..2]..3', GetChild([0]).GetFullText); + // + TestExpr([0,0], TFpPascalExpressionPartBracketIndex, '[', 2); + TestExpr([0,0,0], TFpPascalExpressionPartIdentifier, 'a', 0); + TestExpr([0,0,1], TFpPascalExpressionPartOperatorArraySlice, '..', 2); + TestExpr([0,0,1,0], TFpPascalExpressionPartBracketIndex, '[', 2); + TestExpr([0,0,1,0,0], TFpPascalExpressionPartIdentifier, 'b', 0); + TestExpr([0,0,1,0,1], TFpPascalExpressionPartOperatorArraySlice, '..', 2); + TestExpr([0,0,1,0,1,0], TFpPascalExpressionPartConstantNumber, '1', 0); + TestExpr([0,0,1,0,1,1], TFpPascalExpressionPartConstantNumber, '2', 0); + TestExpr([0,0,1,1], TFpPascalExpressionPartConstantNumber, '3', 0); + + + CreateExpr('a[3..b[1..2]]', True); + TestExpr([], TFpPascalExpressionPartOperatorArraySliceController, '..', 1); + AssertEquals('1..2', GetChild([]).GetFullText); + TestExpr([0], TFpPascalExpressionPartOperatorArraySliceController, '..', 1); + AssertEquals('3..b[1..2]', GetChild([0]).GetFullText); + TestExpr([0,0], TFpPascalExpressionPartBracketIndex, '[', 2); + TestExpr([0,0,0], TFpPascalExpressionPartIdentifier, 'a', 0); + TestExpr([0,0,1], TFpPascalExpressionPartOperatorArraySlice, '..', 2); + TestExpr([0,0,1,0], TFpPascalExpressionPartConstantNumber, '3', 0); + TestExpr([0,0,1,1], TFpPascalExpressionPartBracketIndex, '[', 2); + TestExpr([0,0,1,1,0], TFpPascalExpressionPartIdentifier, 'b', 0); + TestExpr([0,0,1,1,1], TFpPascalExpressionPartOperatorArraySlice, '..', 2); + TestExpr([0,0,1,1,1,0], TFpPascalExpressionPartConstantNumber, '1', 0); + TestExpr([0,0,1,1,1,1], TFpPascalExpressionPartConstantNumber, '2', 0); + + CreateExpr('a ? b : c', True); TestExpr([], TFpPascalExpressionPartOperatorQuestionMark, '?', 2); TestExpr([0], TFpPascalExpressionPartIdentifier, 'a', 0);