-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathulexer.pas
More file actions
211 lines (183 loc) · 4.02 KB
/
ulexer.pas
File metadata and controls
211 lines (183 loc) · 4.02 KB
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
unit uLexer;
{$mode ObjFPC}{$H+}
{$ModeSwitch advancedrecords}
{$ModeSwitch arrayoperators}
interface
uses
SysUtils, uToken;
type
TLexer = record
public
constructor Create(const aSource: String);
function nextToken: TToken;
function hasNextToken: Boolean;
private
Source: String;
lookAhead: Char;
atEOF: Boolean;
Index: Integer;
Location: TLocation;
function nextChar: Char;
function Peek: Char;
function PeekNext: Char;
procedure Advance;
procedure skipWhiteSpace;
procedure MultiLineComment;
procedure SingleLineComment;
function getNumber: TToken;
function getString: TToken;
function getSymbol: TToken;
end;
implementation
uses uChars;
const
validSymbols: set of char =
['a'..'z', 'A'..'Z', '_', '0'..'9', '-','+','*','=','!','<','>','/'];
constructor TLexer.Create(const aSource: String);
begin
Source := aSource;
atEOF := False;
Index := 1;
Location := TLocation.Create(1, 1);
Advance;
end;
function TLexer.nextToken: TToken;
begin
skipWhiteSpace;
if atEOF then
Result := TToken.Create(ttEOF, '', Location)
else if lookAhead.isDigit then
Result := getNumber
else if lookAhead.isDoubleQuote then
Result := getString
else if lookAhead = '/' then
case PeekNext of
'/': SingleLineComment;
'*': MultiLineComment;
otherwise
Result := getSymbol
end
else if lookAhead in validSymbols then
Result := getSymbol
else
begin
case lookAhead of
'(': Result := TToken.Create(ttLeftParen, '(', Location);
')': Result := TToken.Create(ttRightParen, ')', Location);
otherwise
Result := TToken.Create(ttError, 'Unknown symbol: ' + lookAhead, Location);;
end;
Advance;
end;
end;
function TLexer.hasNextToken: Boolean;
begin
Result := not atEOF;
end;
function TLexer.nextChar: Char;
begin
if Index <= Length(Source) then
begin
Result := Source[Index];
Inc(Index);
Inc(Location.Col);
if Result = LineEnding then
begin
Location.Col := 1;
Inc(Location.Line);
end;
end
else
atEOF := True;
end;
function TLexer.Peek: Char;
begin
Result := Source[Index];
end;
function TLexer.PeekNext: Char;
begin
Result := Source[Index+1];
end;
procedure TLexer.Advance;
begin
lookAhead := nextChar;
end;
procedure TLexer.skipWhiteSpace;
begin
while (not atEOF) and lookAhead.isWhiteSpace do
Advance;
end;
// single line comment starts with '//'
procedure TLexer.SingleLineComment;
begin
// skip characters until the end of the current line
while (Peek <> LineEnding) do
Advance;
end;
// nested comments are allowed
procedure TLexer.MultiLineComment;
var
Nesting: Integer = 1;
begin
Advance;
while Nesting > 0 do
begin
case Peek of
Null:
begin
WriteLn('@' + Location.toString + ': ' + 'comment block not terminated');
Exit;
end;
'/':
if PeekNext = '*' then
begin
Advance;
Inc(Nesting);
end;
'*':
if PeekNext = '/' then
begin
Advance;
Dec(Nesting);
end;
end;
Advance;
end;
end;
function TLexer.getNumber: TToken;
var
Value: String = '';
begin
while (not atEOF) and lookAhead.isDigit do
begin
Value += lookAhead;
Advance;
end;
Result := TToken.Create(ttNumber, Value, Location);
end;
function TLexer.getString: TToken;
var
Value: String = '"';
begin
Advance;
while (not atEOF) and (not lookAhead.isDoubleQuote) do
begin
Value += lookAhead;
Advance;
end;
Value += lookAhead;
Advance;
Result := TToken.Create(ttString, Value, Location);
end;
function TLexer.getSymbol: TToken;
var
Value: String = '';
begin
while (not atEOF) and (lookAhead in validSymbols) do
begin
Value += lookAhead;
Advance;
end;
Result := TToken.Create(ttSymbol, Value, Location);
end;
end.