forked from jdehaan2014/GearLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utoken.pas
243 lines (208 loc) · 6.71 KB
/
utoken.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
unit uToken;
{ This unit contains the token class and the definition of all token types.
Copyright (C) 2018 J. de Haan [email protected]
This source is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free
Software Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
{$mode objfpc}{$H+}
{$modeswitch typehelpers}
interface
uses
Classes, SysUtils, Generics.Collections, Variants;
type
TTokenTyp = (
//Expressions - operators
ttPlus, ttMin, ttMul, ttDiv, ttRem,
ttPlusIs, ttMinIs, ttMulIs, ttDivIs, ttRemIs,
ttOr, ttAnd, ttNot, ttXor,
ttShl, ttShr, ttPow, ttConcat, ttConcatIs ,
ttEQ, ttNEQ, ttGT, ttGE, ttLT, ttLE,
//Keywords declarations
ttArray, ttClass, ttDictionary, ttEach, ttEnum, ttExtension, ttFunc,
ttInit, ttLet, ttVal, ttVar, ttTrait, ttStatic,
//Keywords statements and expressions
ttIf, ttThen, ttElse, ttElseif, ttWhile, ttDo, ttRepeat, ttUntil,
ttFor, ttIn, ttIs, ttReturn, ttEnd, ttMatch, ttWhere, ttSwitch, ttCase,
ttEnsure, ttPrint, ttInherited, ttSelf, ttUse, ttBreak, ttOn, ttContinue,
ttIdentifier,
//Constant values
ttFalse, ttTrue, ttNull, ttNumber, ttString, ttChar, ttInterpolated,
//Symbols and punctuation marks
ttComma, ttDot, ttDotDot, ttAssign, ttQuestion, ttArrow, ttColon, ttColons,
ttOpenParen, ttCloseParen, ttOpenBrace, ttCloseBrace,
ttOpenBrack, ttCloseBrack, ttComment, ttEOF, ttNone
);
TTokenTypHelper = type helper for TTokenTyp
function toString: string;
end;
TTokenTypSet = set of TTokenTyp;
TTokenTypSetHelper = type helper for TTokenTypSet
function Contains(TokenTyp: TTokenTyp): Boolean;
end;
TToken = class
private
FTyp: TTokenTyp;
FLexeme: String;
FValue: Variant;
FLine, FCol: LongInt;
FFileIndex: Integer;
public
property Typ: TTokenTyp read FTyp;
property Lexeme: String read FLexeme;
property Value: Variant read FValue;
property Line: LongInt read FLine;
property Col: LongInt read FCol;
property FileIndex: Integer read FFileIndex;
constructor Create(ATyp: TTokenTyp; ALexeme: String;
AValue: Variant; ALine, ACol: LongInt; AFileIndex: Integer = 0);
function toString: String; override;
function Copy: TToken;
end;
TTokens = specialize TObjectList<TToken>;
TTokensHelper = class helper for TTokens
function toText: String;
end;
TKeywords = specialize TDictionary<string, TTokenTyp>;
var
Keywords: TKeywords;
implementation
constructor TToken.Create(ATyp: TTokenTyp; ALexeme: String; AValue: Variant;
ALine, ACol: LongInt; AFileIndex: Integer);
begin
FTyp := ATyp;
FLexeme := ALexeme;
FValue := AValue;
FLine := ALine;
FCol := ACol;
FFileIndex := AFileIndex;
end;
function TToken.toString: String;
var
TypStr: String;
begin
WriteStr(TypStr, FTyp);
Result := TypStr.Substring(2) + ' ("' + FLexeme + '")';
Result += ' ' + VarToStr(FValue); // special function to print variant value
end;
function TToken.Copy: TToken;
begin
Result := TToken.Create(FTyp, FLexeme, FValue, FLine, FCol);
end;
{ TTokenTypHelper }
function TTokenTypHelper.toString: string;
begin
case Self of
ttPlus : Result := '+';
ttMin : Result := '-';
ttMul : Result := '*';
ttDiv : Result := '/';
ttRem : Result := '%';
ttPlusIs : Result := '+=';
ttMinIs : Result := '-=';
ttMulIs : Result := '*=';
ttDivIs : Result := '/=';
ttRemIs : Result := '%=';
ttOr : Result := '|';
ttAnd : Result := '&';
ttNot : Result := '!';
ttXor : Result := '~';
ttShl : Result := '<<';
ttShr : Result := '>>';
ttPow : Result := '^';
ttConcat: Result := '><';
ttConcatIs: Result := '><=';
ttEQ : Result := '=';
ttNEQ : Result := '<>';
ttGT : Result := '>';
ttGE : Result := '>=';
ttLT : Result := '<';
ttLE : Result := '<=';
ttComma: Result := ',';
ttDot: Result := '.';
ttDotDot: Result := '..';
ttAssign: Result := ':=';
ttQuestion: Result := '?';
ttArrow: Result := '=>';
ttColon: Result := ':';
ttColons: Result := '::';
ttOpenParen: Result := '(';
ttCloseParen: Result := ')';
ttOpenBrace: Result := '{';
ttCloseBrace: Result := '}';
ttOpenBrack: Result := '[';
ttCloseBrack: Result := ']';
ttEOF: Result := 'End of file';
else begin
WriteStr(Result, Self);
Result := Result.Substring(2);
end;
end;
end;
function TTokenTypSetHelper.Contains(TokenTyp: TTokenTyp): Boolean;
begin
Result := TokenTyp in Self;
end;
{ TTokensHelper }
function TTokensHelper.toText: String;
var
Token: TToken;
begin
Result := '';
for Token in Self do
Result += Token.toString + LineEnding;
end;
initialization
Keywords := TKeywords.Create();
// the constant values
Keywords.Add('False', ttFalse);
Keywords.Add('Null', ttNull);
Keywords.Add('True', ttTrue);
// the keywords
Keywords.Add('array', ttArray);
Keywords.Add('break', ttBreak);
Keywords.Add('case', ttCase);
Keywords.Add('class', ttClass);
Keywords.Add('continue', ttContinue);
Keywords.Add('dictionary', ttDictionary);
Keywords.Add('do', ttDo);
Keywords.Add('each', ttEach);
Keywords.Add('else', ttElse);
Keywords.Add('elseif', ttElseif);
Keywords.Add('end', ttEnd);
Keywords.Add('ensure', ttEnsure);
Keywords.Add('enum', ttEnum);
Keywords.Add('extension', ttExtension);
Keywords.Add('for', ttFor);
Keywords.Add('func', ttFunc);
Keywords.Add('if', ttIf);
Keywords.Add('in', ttIn);
Keywords.Add('is', ttIs);
Keywords.Add('inherited', ttInherited);
Keywords.Add('init', ttInit);
Keywords.Add('let', ttLet);
Keywords.Add('match', ttMatch);
Keywords.Add('on', ttOn);
Keywords.Add('print', ttPrint);
Keywords.Add('repeat', ttRepeat);
Keywords.Add('return', ttReturn);
Keywords.Add('self', ttSelf);
Keywords.Add('static', ttStatic);
Keywords.Add('switch', ttSwitch);
Keywords.Add('then', ttThen);
Keywords.Add('trait', ttTrait);
Keywords.Add('until', ttUntil);
Keywords.Add('use', ttUse);
Keywords.Add('val', ttVal);
Keywords.Add('var', ttVar);
Keywords.Add('where', ttWhere);
Keywords.Add('while', ttWhile);
finalization
Keywords.Free;
end.