@@ -167,12 +167,53 @@ mixin template TokenTriviaFields()
167167 int opCmp (ref const typeof (this ) other) const pure nothrow @safe @nogc {
168168 return opCmp (other.index);
169169 }
170+
171+ string toString () const @safe pure
172+ {
173+ import std.array : appender;
174+
175+ auto sink = appender! string ;
176+ toString(sink);
177+ return sink.data;
178+ }
179+
180+ void toString (R)(auto ref R sink) const
181+ {
182+ import std.conv : to;
183+ import dparse.lexer : str;
184+
185+ sink.put(" tok!\" " );
186+ sink.put(str(type));
187+ sink.put(" \" (" );
188+ sink.put(" text: " );
189+ sink.put([text].to! string [1 .. $ - 1 ]); // escape hack
190+ sink.put(" , index: " );
191+ sink.put(index.to! string );
192+ sink.put(" , line: " );
193+ sink.put(line.to! string );
194+ sink.put(" , column: " );
195+ sink.put(column.to! string );
196+ sink.put(" , trivia: { leading: [" );
197+ foreach (i, tok; leadingTrivia)
198+ {
199+ if (i != 0 ) sink.put(" , " );
200+ tok.toString(sink);
201+ }
202+ sink.put(" ], trailing: [" );
203+ foreach (i, tok; trailingTrivia)
204+ {
205+ if (i != 0 ) sink.put(" , " );
206+ tok.toString(sink);
207+ }
208+ sink.put(" ]}" );
209+ sink.put(" )" );
210+ }
170211}
171212
172213// mixin in from dparse.lexer to make error messages more managable size as the
173214// entire string is dumped when there is a type mismatch.
174215private immutable extraFields = " import dparse.lexer:TokenTriviaFields,TriviaToken; mixin TokenTriviaFields;" ;
175- private immutable extraFieldsBare = "
216+ private immutable extraFieldsBare = q{
176217 import dparse.lexer : Token ;
177218
178219 this (Token token) pure nothrow @safe @nogc {
@@ -188,7 +229,35 @@ private immutable extraFieldsBare = "
188229 int opCmp (ref const typeof (this ) other) const pure nothrow @safe @nogc {
189230 return opCmp (other.index);
190231 }
191- " ;
232+
233+ string toString () const @safe pure
234+ {
235+ import std.array : appender;
236+
237+ auto sink = appender! string ;
238+ toString(sink);
239+ return sink.data;
240+ }
241+
242+ void toString (R)(auto ref R sink) const
243+ {
244+ import std.conv : to;
245+ import dparse.lexer : str;
246+
247+ sink.put(" trivia!\" " );
248+ sink.put(str(type));
249+ sink.put(" \" (" );
250+ sink.put(" text: " );
251+ sink.put([text].to! string [1 .. $ - 1 ]); // escape hack
252+ sink.put(" , index: " );
253+ sink.put(index.to! string );
254+ sink.put(" , line: " );
255+ sink.put(line.to! string );
256+ sink.put(" , column: " );
257+ sink.put(column.to! string );
258+ sink.put(" )" );
259+ }
260+ };
192261
193262// / The token type in the D lexer
194263public alias Token = std.experimental.lexer.TokenStructure ! (IdType, extraFields);
0 commit comments