Skip to content

Commit 56c2e2e

Browse files
committed
Make D tokens opaque
1 parent 4a31620 commit 56c2e2e

File tree

2 files changed

+64
-44
lines changed

2 files changed

+64
-44
lines changed

src/source/dlexer.d

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -118,55 +118,81 @@ enum TokenType {
118118
}
119119

120120
struct Token {
121-
TokenType type;
121+
private:
122+
TokenType _type;
122123

123124
import source.name;
124-
Name name;
125+
Name _name;
125126

126127
import source.location;
127-
Location location;
128+
public Location _location;
128129

130+
public:
131+
132+
@property
133+
TokenType type() const {
134+
return _type;
135+
}
136+
137+
@property
138+
Location location() const {
139+
return _location;
140+
}
141+
142+
@property
143+
Name name() const {
144+
return _name;
145+
}
146+
147+
import source.context;
148+
string toString(Context context) {
149+
return (type >= TokenType.Identifier)
150+
? name.toString(context)
151+
: location.getFullLocation(context).getSlice();
152+
}
153+
154+
public:
129155
static getError(Location location, Name message) {
130156
Token t;
131-
t.type = TokenType.Invalid;
132-
t.name = message;
133-
t.location = location;
157+
t._type = TokenType.Invalid;
158+
t._name = message;
159+
t._location = location;
134160

135161
return t;
136162
}
137163

138164
static getBegin(Location location, Name name) {
139165
Token t;
140-
t.type = TokenType.Begin;
141-
t.name = name;
142-
t.location = location;
166+
t._type = TokenType.Begin;
167+
t._name = name;
168+
t._location = location;
143169

144170
return t;
145171
}
146172

147173
static getEnd(Location location) {
148174
Token t;
149-
t.type = TokenType.End;
150-
t.name = BuiltinName!"\0";
151-
t.location = location;
175+
t._type = TokenType.End;
176+
t._name = BuiltinName!"\0";
177+
t._location = location;
152178

153179
return t;
154180
}
155181

156182
static getComment(string s)(Location location) {
157183
Token t;
158-
t.type = TokenType.Comment;
159-
t.name = BuiltinName!s;
160-
t.location = location;
184+
t._type = TokenType.Comment;
185+
t._name = BuiltinName!s;
186+
t._location = location;
161187

162188
return t;
163189
}
164190

165191
static getStringLiteral(Location location, Name value) {
166192
Token t;
167-
t.type = TokenType.StringLiteral;
168-
t.name = value;
169-
t.location = location;
193+
t._type = TokenType.StringLiteral;
194+
t._name = value;
195+
t._location = location;
170196

171197
return t;
172198
}
@@ -175,34 +201,34 @@ struct Token {
175201
static getCharacterLiteral(Location location, Name name,
176202
DecodedChar value) {
177203
Token t;
178-
t.type = TokenType.CharacterLiteral;
179-
t.name = name;
180-
t.location = location;
204+
t._type = TokenType.CharacterLiteral;
205+
t._name = name;
206+
t._location = location;
181207

182208
return t;
183209
}
184210

185211
static getIntegerLiteral(Location location, ulong value) {
186212
Token t;
187-
t.type = TokenType.IntegerLiteral;
188-
t.location = location;
213+
t._type = TokenType.IntegerLiteral;
214+
t._location = location;
189215

190216
return t;
191217
}
192218

193219
static getFloatLiteral(Location location, double value) {
194220
Token t;
195-
t.type = TokenType.FloatLiteral;
196-
t.location = location;
221+
t._type = TokenType.FloatLiteral;
222+
t._location = location;
197223

198224
return t;
199225
}
200226

201227
static getIdentifier(Location location, Name name) {
202228
Token t;
203-
t.type = TokenType.Identifier;
204-
t.name = name;
205-
t.location = location;
229+
t._type = TokenType.Identifier;
230+
t._name = name;
231+
t._location = location;
206232

207233
return t;
208234
}
@@ -211,9 +237,9 @@ struct Token {
211237
enum Type = DLexer.KeywordMap[kw];
212238

213239
Token t;
214-
t.type = Type;
215-
t.name = BuiltinName!kw;
216-
t.location = location;
240+
t._type = Type;
241+
t._name = BuiltinName!kw;
242+
t._location = location;
217243

218244
return t;
219245
}
@@ -222,19 +248,12 @@ struct Token {
222248
enum Type = DLexer.OperatorMap[op];
223249

224250
Token t;
225-
t.type = Type;
226-
t.name = BuiltinName!op;
227-
t.location = location;
251+
t._type = Type;
252+
t._name = BuiltinName!op;
253+
t._location = location;
228254

229255
return t;
230256
}
231-
232-
import source.context;
233-
string toString(Context context) {
234-
return (type >= TokenType.Identifier)
235-
? name.toString(context)
236-
: location.getFullLocation(context).getSlice();
237-
}
238257
}
239258

240259
auto lex(Position base, Context context) {

src/source/lexpreprocessor.d

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ mixin template LexPreprocessorImpl(Token, alias TokenHandlers,
88

99
Token getPreprocessorComment(uint begin, Token end)
1010
in(end.type == TokenType.End) {
11-
auto t = getComment!"#"(begin, begin);
12-
t.location.spanTo(end.location.start);
13-
return t;
11+
auto location = base.getWithOffsets(begin, begin);
12+
location.spanTo(end.location.start);
13+
14+
return Token.getComment!"#"(location);
1415
}
1516

1617
Token getNextPreprocessorToken() {

0 commit comments

Comments
 (0)