From 685e9eafc1fc9a80e30c234280cc264877bd8a23 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 6 Oct 2015 12:01:09 +0200 Subject: [PATCH] Added support for FollowUp line matches --- UApp.pas | 13 +++++++++++ UFilter.pas | 62 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/UApp.pas b/UApp.pas index 5a96c71..7bf122b 100644 --- a/UApp.pas +++ b/UApp.pas @@ -173,6 +173,10 @@ begin begin FLogFileName := AParams; end; + 'followup': + begin + FCurrentLineFilter.SetFollowUp(AParams); + end; end; end; @@ -253,10 +257,19 @@ var lineFilter: TLineFilter; groupRanges: TGroupRanges; begin + if (FCurrentLineFilter <> nil) + and (FCurrentLineFilter.FollowUpMatch(ALine, groupRanges)) then + begin + WriteContent(ALine, FCurrentLineFilter.Filters, groupRanges); + Exit; + end else + FCurrentLineFilter := nil; //We assume, nothing matched. + for lineFilter in FLineFilters do begin if lineFilter.Matches(ALine, groupRanges) then begin + FCurrentLineFilter := lineFilter; //Remember the last matching filter WriteContent(ALine, lineFilter.Filters, groupRanges); Break; end; diff --git a/UFilter.pas b/UFilter.pas index 92aca98..e20d162 100644 --- a/UFilter.pas +++ b/UFilter.pas @@ -60,9 +60,14 @@ type destructor Destroy; override; protected FExpression: TRegExpr; + FFollowUp: TRegExpr; //Used to evaluate lines belonging this the first match. FFilters: TFilterList; + function DoMatch(AExpression: TRegExpr; ALine: String; + var GroupRanges: TGroupRanges): Boolean; public function Matches(ALine: String; var GroupRanges: TGroupRanges): Boolean; + procedure SetFollowUp(AExpression: String); + function FollowUpMatch(ALine: String; var GroupRanges: TGroupRanges): Boolean; property Filters: TFilterList read FFilters; end; TLineFilters = specialize TFPGObjectList; @@ -126,32 +131,61 @@ constructor TLineFilter.Create(AExpression: String); begin FExpression := TRegExpr.Create(AExpression); FFilters := TFilterList.Create; + FFollowUp := nil; end; destructor TLineFilter.Destroy; begin FExpression.Free; FFilters.Free; + FFollowUp.Free; inherited Destroy; end; +function TLineFilter.DoMatch(AExpression: TRegExpr; ALine: String; + var GroupRanges: TGroupRanges): Boolean; +var + i: Integer; +begin + Result := AExpression.Exec(ALine); + if Result then + begin + {$ifdef debugmatches}writeln(' Match: ', AExpression.Match[0]);{$endif} + SetLength(GroupRanges, AExpression.SubExprMatchCount + 1); + for i := 0 to AExpression.SubExprMatchCount do + begin + {$ifdef debugmatches}writeln(' [', i, ']: ', AExpression.Match[i]);{$endif} + GroupRanges[i].StartIdx := AExpression.MatchPos[i]; + GroupRanges[i].EndIdx := GroupRanges[i].StartIdx + AExpression.MatchLen[i]; + end; + end; +end; + function TLineFilter.Matches(ALine: String; var GroupRanges: TGroupRanges ): Boolean; -var - i: Integer; begin - Result := FExpression.Exec(ALine); - if Result then - begin - {$ifdef debugmatches}writeln(' Match: ', FExpression.Match[0]);{$endif} - SetLength(GroupRanges, FExpression.SubExprMatchCount + 1); - for i := 0 to FExpression.SubExprMatchCount do - begin - {$ifdef debugmatches}writeln(' [', i, ']: ', FExpression.Match[i]);{$endif} - GroupRanges[i].StartIdx := FExpression.MatchPos[i]; - GroupRanges[i].EndIdx := GroupRanges[i].StartIdx + FExpression.MatchLen[i]; - end; - end; + Result := DoMatch(FExpression, ALine, GroupRanges); +end; + +procedure TLineFilter.SetFollowUp(AExpression: String); +begin + if FFollowUp = nil then + FFollowUp := TRegExpr.Create(AExpression) + else + FFollowUp.Expression := AExpression; +end; + +{ + If a FollowUp-Expression is set, we evaluate that and return the result. + If no expression is set, we simply treat that as having not matched. +} +function TLineFilter.FollowUpMatch(ALine: String; var GroupRanges: TGroupRanges + ): Boolean; +begin + if FFollowUp <> nil then + Result := DoMatch(FFollowUp, ALine, GroupRanges) + else + Result := False; end; { THighlight }