-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSimpleDSLCompiler.Tokenizer.pas
212 lines (189 loc) · 5.23 KB
/
SimpleDSLCompiler.Tokenizer.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
unit SimpleDSLCompiler.Tokenizer;
interface
uses
System.Types,
System.Classes;
type
TTokenKind = (tkUnknown, tkWhitespace,
tkIdent, tkNumber,
tkLeftParen, tkRightParen, tkLeftCurly, tkRightCurly, tkLeftSquare, tkRightSquare,
tkLessThan, tkPlus, tkMinus,
tkComma, tkSemicolon,
tkEOF);
ISimpleDSLTokenizer = interface ['{086E9EFE-DB1E-4D81-A16A-C9F1F0F06D2B}']
function CurrentLocation: TPoint;
function GetToken(var kind: TTokenKind; var identifier: string): boolean;
procedure Initialize(const code: string);
function IsAtEnd: boolean;
end; { ISimpleDSLTokenizer }
TSimpleDSLTokenizerFactory = reference to function: ISimpleDSLTokenizer;
function CreateSimpleDSLTokenizer: ISimpleDSLTokenizer;
implementation
uses
System.SysUtils,
System.Character,
SimpleDSLCompiler.Base;
type
TSimpleDSLTokenizer = class(TSimpleDSLCompilerBase, ISimpleDSLTokenizer)
strict private
FCurrentLine: string;
FLastLine : integer;
FLastLineLen: integer;
FLookahead : char;
FNextChar : integer;
FNextLine : integer;
FProgram : TStringList;
strict protected
procedure PushBack(ch: char);
procedure SkipWhitespace;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
function CurrentLocation: TPoint; inline;
function GetChar(var ch: char): boolean;
function GetIdent: string;
function GetNumber: string;
function GetToken(var kind: TTokenKind; var identifier: string): boolean;
procedure Initialize(const code: string);
function IsAtEnd: boolean;
end; { TSimpleDSLTokenizer }
{ exports }
function CreateSimpleDSLTokenizer: ISimpleDSLTokenizer;
begin
Result := TSimpleDSLTokenizer.Create;
end; { CreateSimpleDSLTokenizer }
procedure TSimpleDSLTokenizer.AfterConstruction;
begin
inherited;
FProgram := TStringList.Create;
end; { TSimpleDSLTokenizer.AfterConstruction }
procedure TSimpleDSLTokenizer.BeforeDestruction;
begin
FreeAndNil(FProgram);
inherited;
end; { TSimpleDSLTokenizer.BeforeDestruction }
function TSimpleDSLTokenizer.CurrentLocation: TPoint;
begin
Result := Point(FNextLine + 1, FNextChar);
end; { TSimpleDSLTokenizer.CurrentLocation }
function TSimpleDSLTokenizer.GetChar(var ch: char): boolean;
begin
if FLookahead <> #0 then begin
ch := FLookahead;
FLookahead := #0;
Result := true;
end
else begin
Result := not IsAtEnd;
if Result then begin
ch := FCurrentLine[FNextChar];
Inc(FNextChar);
if FNextChar > Length(FCurrentLine) then begin
Inc(FNextLine);
if FNextLine < FProgram.Count then
FCurrentLine := FProgram[FNextLine];
FNextChar := 1;
end;
end;
end;
end; { TSimpleDSLTokenizer.GetChar }
function TSimpleDSLTokenizer.GetIdent: string;
var
ch: char;
begin
Result := '';
while GetChar(ch) do begin
if ch.IsLetter or ch.IsNumber or (ch = '_') then
Result := Result + ch
else begin
PushBack(ch);
Exit;
end;
end;
end; { TSimpleDSLTokenizer.GetIdent }
function TSimpleDSLTokenizer.GetNumber: string;
var
ch: char;
begin
Result := '';
while GetChar(ch) do begin
if CharInSet(ch, ['0'..'9']) then
Result := Result + ch
else begin
PushBack(ch);
Exit;
end;
end;
end; { TSimpleDSLTokenizer.GetNumber }
function TSimpleDSLTokenizer.GetToken(var kind: TTokenKind; var identifier: string): boolean;
var
ch: char;
begin
identifier := '';
Result := GetChar(ch);
if not Result then begin
kind := tkEOF;
Exit;
end;
case ch of
'(': kind := tkLeftParen;
')': kind := tkRightParen;
'{': kind := tkLeftCurly;
'}': kind := tkRightCurly;
'[': kind := tkLeftSquare;
']': kind := tkRightSquare;
'+': kind := tkPlus;
'-': kind := tkMinus;
'<': kind := tkLessThan;
',': kind := tkComma;
';': kind := tkSemicolon;
else if ch.IsLetter then begin
kind := tkIdent;
identifier := ch + GetIdent;
end
else if CharInSet(ch, ['0'..'9']) then begin
kind := tkNumber;
identifier := ch + GetNumber;
end
else if ch.IsWhiteSpace then begin
kind := tkWhitespace;
SkipWhitespace;
end
else
kind := tkUnknown;
end;
end; { TSimpleDSLTokenizer.GetToken }
procedure TSimpleDSLTokenizer.Initialize(const code: string);
begin
FProgram.Text := code;
FNextLine := 0;
FNextChar := 1;
FLookahead := #0;
FLastLine := FProgram.Count - 1;
if FLastLine >= 0 then begin
FLastLineLen := Length(FProgram[FLastLine]);
FCurrentLine := FProgram[FNextLine];
end;
end; { TSimpleDSLTokenizer.Initialize }
function TSimpleDSLTokenizer.IsAtEnd: boolean;
begin
Result :=
(FNextLine > FLastLine) or
((FNextLine = FLastLine) and (FNextChar > (FLastLineLen+1)));
end; { TSimpleDSLTokenizer.IsAtEnd }
procedure TSimpleDSLTokenizer.PushBack(ch: char);
begin
Assert(FLookahead = #0, 'TSimpleDSLTokenizer: Lookahead buffer is not empty');
FLookahead := ch;
end; { TSimpleDSLTokenizer.PushBack }
procedure TSimpleDSLTokenizer.SkipWhitespace;
var
ch: char;
begin
while GetChar(ch) do
if not ch.IsWhiteSpace then begin
PushBack(ch);
Exit;
end;
end; { TSimpleDSLTokenizer.SkipWhitespace }
end.