-
Notifications
You must be signed in to change notification settings - Fork 2
/
codefmtparsers.pas
62 lines (49 loc) · 1.41 KB
/
codefmtparsers.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
unit CodeFmtParsers;
{$mode Delphi}
interface
uses
Classes, SysUtils, Recognizers, Tokenizers, Parsers, TokenTypes;
function IsIdentifierRemaining(Ch: Char): Boolean;
function IdentifierRecognizer: TTokenRecognizer;
type
TListToFmtMapper = class(TMapParser<TTokenLinkedList, TFmt>)
private
FKind: THigherTokenType;
protected
function Map(List: TTokenLinkedList): TFmt; override;
public
constructor Create(Parser: TParser<TTokenLinkedList>; Kind: THigherTokenType);
procedure Undo(Source: TUndoTokenizer; Data: TFmt); override;
end;
implementation
function IsIdentifierRemaining(Ch: Char): Boolean;
begin
Result := IsLetter(Ch) or IsDigit(Ch) or (Ch = '_') or (Ch = '_');
end;
function IdentifierRecognizer: TTokenRecognizer;
begin
Result := TLeadingPredicateRecognizer.Create(IsLetter, IsIdentifierRemaining);
end;
(* TListToFmtMapper *)
constructor TListToFmtMapper.Create(Parser: TParser<TTokenLinkedList>; Kind: THigherTokenType);
begin
inherited Create(Parser);
FKind := Kind;
end;
function TListToFmtMapper.Map(List: TTokenLinkedList): TFmt;
var
Buffer: String;
begin
Buffer := '';
while not List.IsEmpty do
{ list is LIFO }
Buffer := List.Pop.Text + Buffer;
List.Free;
Result.Kind := FKind;
Result.Text := Buffer;
end;
procedure TListToFmtMapper.Undo(Source: TUndoTokenizer; Data: TFmt);
begin
raise Exception.Create('TListToFmtMapper.Undo is not possible');
end;
end.