Added support for FollowUp line matches
This commit is contained in:
parent
9a59bafc16
commit
685e9eafc1
13
UApp.pas
13
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;
|
||||
|
|
62
UFilter.pas
62
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<TLineFilter>;
|
||||
|
@ -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 }
|
||||
|
|
Loading…
Reference in New Issue