-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtokenparsers.pas
195 lines (157 loc) · 5.32 KB
/
tokenparsers.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
unit TokenParsers;
{$mode Delphi}
interface
uses
Classes, SysUtils, Types, Parsers, ParseResult, BinaryParsers, Tokenizers;
type
TAnyTokenParser = class(TParser<TToken>)
public
function Parse(Source: TUndoTokenizer): TParseResult<TToken>; override;
procedure Undo(Source: TUndoTokenizer; Data: TToken); override;
end;
type
TTokenTypeFilterParser = class(TFilterParser<TToken>)
private
FTokenTypes: TByteDynArray;
protected
function Filter(Data: TToken): Boolean; override;
public
constructor Create(Parser: TParser<TToken>; TokenType: Byte); overload;
constructor Create(Parser: TParser<TToken>; TokenTypes: TByteDynArray); overload;
end;
type
TMapTokenToTokenListParser = class(TMapParser<TToken, TTokenLinkedList>)
protected
function Map(Token: TToken): TTokenLinkedList; override;
public
procedure Undo(Source: TUndoTokenizer; Data: TTokenLinkedList); override;
end;
TManyTokensParser = class(TManyParser<TToken, TTokenLinkedList>)
protected
function CreateSeed: TTokenLinkedList; override;
procedure Collect(Aggregate: TTokenLinkedList; Data: TToken); override;
public
procedure Undo(Source: TUndoTokenizer; Data: TTokenLinkedList); override;
end;
TAndTokenListsParser = class(TAbstractAndParser<TTokenLinkedList, TTokenLinkedList, TTokenLinkedList>)
protected
function Combine(Left: TTokenLinkedList; Right: TTokenLinkedList): TTokenLinkedList; override;
public
procedure Undo(Source: TUndoTokenizer; Data: TTokenLinkedList); override;
end;
function Seq(Left: TParser<TTokenLinkedList>; Right: TParser<TTokenLinkedList>): TParser<TTokenLinkedList>; overload;
function Seq(Left: TParser<TToken>; Right: TParser<TTokenLinkedList>): TParser<TTokenLinkedList>; overload;
function Seq(Left: TParser<TTokenLinkedList>; Right: TParser<TToken>): TParser<TTokenLinkedList>; overload;
function FilterTokenType(TokenType: Byte): TParser<TToken>; overload;
function FilterTokenTypes(TokenTypes: TByteDynArray): TParser<TToken>; overload;
function MapTokenToList(Parser: TParser<TToken>): TParser<TTokenLinkedList>; overload;
function ManyTokens(Parser: TParser<TToken>): TParser<TTokenLinkedList>; overload;
implementation
function Seq(Left: TParser<TTokenLinkedList>; Right: TParser<TTokenLinkedList>): TParser<TTokenLinkedList>; overload;
begin
Result := TAndTokenListsParser.Create(Left, Right);
end;
function Seq(Left: TParser<TToken>; Right: TParser<TTokenLinkedList>): TParser<TTokenLinkedList>; overload;
begin
Result := Seq(MapTokenToList(Left), Right);
end;
function Seq(Left: TParser<TTokenLinkedList>; Right: TParser<TToken>): TParser<TTokenLinkedList>; overload;
begin
Result := Seq(Left, MapTokenToList(Right));
end;
function FilterTokenType(TokenType: Byte): TParser<TToken>; overload;
begin
Result := TTokenTypeFilterParser.Create(TAnyTokenParser.Create, TokenType);
end;
function FilterTokenTypes(TokenTypes: TByteDynArray): TParser<TToken>; overload;
begin
Result := TTokenTypeFilterParser.Create(TAnyTokenParser.Create, TokenTypes);
end;
function MapTokenToList(Parser: TParser<TToken>): TParser<TTokenLinkedList>; overload;
begin
Result := TMapTokenToTokenListParser.Create(Parser);
end;
function ManyTokens(Parser: TParser<TToken>): TParser<TTokenLinkedList>; overload;
begin
Result := TManyTokensParser.Create(Parser);
end;
(* AnyToken *)
function TAnyTokenParser.Parse(Source: TUndoTokenizer): TParseResult<TToken>;
var
Next: TToken;
begin
Next := Source.Read;
if Next.Kind >= 0 then
Result := SuccessParseResult<TToken>(Next)
else
Result := FailedParseResult<TToken>()
end;
procedure TAnyTokenParser.Undo(Source: TUndoTokenizer; Data: TToken);
begin
Source.Undo(Data);
end;
(* TokenType Filter *)
constructor TTokenTypeFilterParser.Create(Parser: TParser<TToken>; TokenType: Byte);
begin
inherited Create(Parser);
FTokenTypes := [TokenType];
end;
constructor TTokenTypeFilterParser.Create(Parser: TParser<TToken>; TokenTypes: TByteDynArray);
begin
inherited Create(Parser);
FTokenTypes := TokenTypes;
end;
function TTokenTypeFilterParser.Filter(Data: TToken): Boolean;
var
i: Integer;
begin
i := Low(FTokenTypes);
while (i <= High(FTokenTypes)) and (Data.Kind <> FTokenTypes[i]) do
Inc(i);
Result := i <= High(FTokenTypes);
end;
(* MapTokenToTokenList *)
function TMapTokenToTokenListParser.Map(Token: TToken): TTokenLinkedList;
begin
Result := TTokenLinkedList.Create;
Result.Push(Token);
end;
procedure TMapTokenToTokenListParser.Undo(Source: TUndoTokenizer; Data: TTokenLinkedList);
begin
while not Data.IsEmpty do
FParser.Undo(Source, Data.Pop);
Data.Free;
end;
(* ManyTokens *)
function TManyTokensParser.CreateSeed: TTokenLinkedList;
begin
Result := TTokenLinkedList.Create;
end;
procedure TManyTokensParser.Collect(Aggregate: TTokenLinkedList; Data: TToken);
begin
Aggregate.Push(Data);
end;
procedure TManyTokensParser.Undo(Source: TUndoTokenizer; Data: TTokenLinkedList);
begin
Source.Undo(Data);
end;
(* AndTokenLists *)
function TAndTokenListsParser.Combine(Left: TTokenLinkedList; Right: TTokenLinkedList): TTokenLinkedList;
begin
if Right.IsEmpty then
begin
Right.Free;
Result := Left;
end
else
begin
Right.Append(Left);
Left.Free;
Result := Right;
end
end;
procedure TAndTokenListsParser.Undo(Source: TUndoTokenizer; Data: TTokenLinkedList);
begin
Source.Undo(Data);
end;
end.