-
Notifications
You must be signed in to change notification settings - Fork 3
/
expressions.pas
152 lines (128 loc) · 3.66 KB
/
expressions.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
unit Expressions;
interface
uses
SysUtils, Classes;
function IsFen(const AStr: string): boolean;
function ExtractFen(const ACommand: string; var AFen: string): boolean;
function ExtractMoves(const ACommand: string): boolean;
var
LMoveList: TStringList;
implementation
uses
RegExpr;
type
TFenValidator = class
public
function ExpandEmptySquares(AExpr: TRegExpr): string;
function IsFen(const AInputStr: string): boolean;
end;
function IsFen(const AStr: string): boolean;
var
LValidator: TFenValidator;
begin
LValidator := TFenValidator.Create;
result := LValidator.IsFen(AStr);
LValidator.Free;
end;
function TFenValidator.ExpandEmptySquares(AExpr: TRegExpr): string;
begin
result := StringOfChar('-', StrToInt(AExpr.Match[0]));
end;
function TFenValidator.IsFen(const AInputStr: string): boolean;
const
CWhiteKing = 'K';
CBlackKing = 'k';
CPieces = '^[1-8BKNPQRbknpqr]+$';
CActive = '^[wb]$';
CCastling = '^[KQkq]+$|^[A-Ha-h]+$|^\-$';
CEnPassant = '^[a-h][36]$|^\-$';
CHalfMove = '^\d+$';
CFullMove = '^[1-9]\d*$';
var
LFields, LPieces: TStringList;
LExpr: TRegExpr;
i: integer;
s: string;
begin
LFields := TStringList.Create;
LPieces := TStringList.Create;
LExpr := TRegExpr.Create('\d');
(*
ExtractStrings([' '], [], PChar(AInputStr), LFields);
ExtractStrings(['/'], [], PChar(LFields[0]), LPieces);
*)
SplitRegExpr(' ', AInputStr, LFields);
result := (LFields.Count = 6);
if result then
begin
SplitRegExpr('/', LFields[0], LPieces);
result := (LPieces.Count = 8);
end;
if result then
begin
result := result and ExecRegExpr(CWhiteKing, LFields[0]);
result := result and ExecRegExpr(CBlackKing, LFields[0]);
for i := 0 to 7 do
begin
result := result and ExecRegExpr(CPieces, LPieces[i]);
if result then
begin
s := LPieces[i];
repeat
s := LExpr.Replace(s, @ExpandEmptySquares);
until not ExecRegExpr('\d', s);
result := result and (Length(s) = 8);
end;
end;
result := result and ExecRegExpr(CActive, LFields[1]);
result := result and ExecRegExpr(CCastling, LFields[2]);
result := result and ExecRegExpr(CEnPassant, LFields[3]);
result := result and ExecRegExpr(CHalfMove, LFields[4]);
result := result and ExecRegExpr(CFullMove, LFields[5]);
end;
LFields.Free;
LPieces.Free;
LExpr.Free;
end;
const
CPatternPieces = '[1-8BKNPQRbknpqr]+';
CPatternActiveColor = '[wb]';
CPatternCastling = '([KQkq]+|-)';
CPatternEnPassant = '([a-h][1-8]|-)';
CPatternNumber = '\d+';
CPatternMove = '\b[a-h][1-8][a-h][1-8][nbrq]?\b';
var
LFenPattern: string;
LExprFen, LExprMove: TRegExpr;
function ExtractFen(const ACommand: string; var AFen: string): boolean;
begin
result := LExprFen.Exec(ACommand);
if result then
AFen := LExprFen.Match[0];
end;
function ExtractMoves(const ACommand: string): boolean;
begin
LMoveList.Clear;
result := LExprMove.Exec(ACommand);
if result then
repeat
LMoveList.Append(LExprMove.Match[0]);
until not LExprMove.ExecNext;
end;
initialization
LFenPattern := Format('%s %s %s %s %s %s', [
ReplaceRegExpr('x', 'x/x/x/x/x/x/x/x', CPatternPieces, false),
CPatternActiveColor,
CPatternCastling,
CPatternEnPassant,
CPatternNumber,
CPatternNumber
]);
LMoveList := TStringList.Create;
LExprFen := TRegExpr.Create(LFenPattern);
LExprMove := TRegExpr.Create(CPatternMove);
finalization
LMoveList.Free;
LExprFen.Free;
LExprMove.Free;
end.