authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2019-06-21 08:13:03-05:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2019-08-05 21:03:46-05:00
logb4172e5151112b685f3109abbf4def9f754882b2
treec16b93a307a06b8dff4ecdd3dd932debd09aa0fa
parent7f23dac6dce2ce897295e8186f164f695cacdbc9

Humanize tokenized symbol names


2 files changed, 131 insertions(+), 7 deletions(-)

std/zig/ast.zig+7-7
......@@ -324,11 +324,11 @@ pub const Error = union(enum) {
324324 return stream.print("`&&` is invalid. Note that `and` is boolean AND.");
325325 },
326326 .Invalid => {
327 return stream.print("expected {}, found invalid bytes", @tagName(self.expected_id));
327 return stream.print("expected {}, found invalid bytes", self.expected_id.symbol());
328328 },
329329 else => {
330 const token_name = @tagName(found_token.id);
331 return stream.print("expected {}, found {}", @tagName(self.expected_id), token_name);
330 const token_name = found_token.id.symbol();
331 return stream.print("expected {}, found {}", self.expected_id.symbol(), token_name);
332332 },
333333 }
334334 }
......@@ -339,8 +339,8 @@ pub const Error = union(enum) {
339339 end_id: Token.Id,
340340
341341 pub fn render(self: *const ExpectedCommaOrEnd, tokens: *Tree.TokenList, stream: var) !void {
342 const token_name = @tagName(tokens.at(self.token).id);
343 return stream.print("expected ',' or {}, found {}", @tagName(self.end_id), token_name);
342 const actual_token = tokens.at(self.token);
343 return stream.print("expected ',' or {}, found {}", self.end_id.symbol(), actual_token.id.symbol());
344344 }
345345 };
346346
......@@ -351,8 +351,8 @@ pub const Error = union(enum) {
351351 token: TokenIndex,
352352
353353 pub fn render(self: *const ThisError, tokens: *Tree.TokenList, stream: var) !void {
354 const token_name = @tagName(tokens.at(self.token).id);
355 return stream.print(msg, token_name);
354 const actual_token = tokens.at(self.token);
355 return stream.print(msg, actual_token.id.symbol());
356356 }
357357 };
358358 }
std/zig/tokenizer.zig+124
......@@ -194,6 +194,130 @@ pub const Token = struct {
194194 Keyword_var,
195195 Keyword_volatile,
196196 Keyword_while,
197
198 pub fn symbol(id: Id) []const u8 {
199 return switch (id) {
200 .Invalid => "[Invalid]",
201 .Invalid_ampersands => "&&",
202 .Identifier => "[Identifier]",
203 .StringLiteral => "[StringLiteral]",
204 .MultilineStringLiteralLine => "[MultilineStringLiteralLine]",
205 .CharLiteral => "[CharLiteral]",
206 .Eof => "[Eof]",
207 .Builtin => "[Builtin]",
208 .IntegerLiteral => "[IntegerLiteral]",
209 .FloatLiteral => "[FloatLiteral]",
210 .LineComment => "[LineComment]",
211 .DocComment => "[DocComment]",
212 .ShebangLine => "[ShebangLine]",
213
214 .Bang => "!",
215 .Pipe => "|",
216 .PipePipe => "||",
217 .PipeEqual => "|=",
218 .Equal => "=",
219 .EqualEqual => "==",
220 .EqualAngleBracketRight => "=>",
221 .BangEqual => "!=",
222 .LParen => "(",
223 .RParen => ")",
224 .Semicolon => ";",
225 .Percent => "%",
226 .PercentEqual => "%=",
227 .LBrace => "{",
228 .RBrace => "}",
229 .LBracket => "[",
230 .RBracket => "]",
231 .Period => ".",
232 .Ellipsis2 => "..",
233 .Ellipsis3 => "...",
234 .Caret => "^",
235 .CaretEqual => "^=",
236 .Plus => "+",
237 .PlusPlus => "++",
238 .PlusEqual => "+=",
239 .PlusPercent => "+%",
240 .PlusPercentEqual => "+%=",
241 .Minus => "-",
242 .MinusEqual => "-=",
243 .MinusPercent => "-%",
244 .MinusPercentEqual => "-%=",
245 .Asterisk => "*",
246 .AsteriskEqual => "*=",
247 .AsteriskAsterisk => "**",
248 .AsteriskPercent => "*%",
249 .AsteriskPercentEqual => "*%=",
250 .Arrow => "->",
251 .Colon => ":",
252 .Slash => "/",
253 .SlashEqual => "/=",
254 .Comma => ",",
255 .Ampersand => "&",
256 .AmpersandEqual => "&=",
257 .QuestionMark => "?",
258 .AngleBracketLeft => "<",
259 .AngleBracketLeftEqual => "<=",
260 .AngleBracketAngleBracketLeft => "<<",
261 .AngleBracketAngleBracketLeftEqual => "<<=",
262 .AngleBracketRight => ">",
263 .AngleBracketRightEqual => ">=",
264 .AngleBracketAngleBracketRight => ">>",
265 .AngleBracketAngleBracketRightEqual => ">>=",
266 .Tilde => "~",
267 .BracketStarBracket => "[*]",
268 .BracketStarCBracket => "[*c]",
269 .Keyword_align => "align",
270 .Keyword_allowzero => "allowzero",
271 .Keyword_and => "and",
272 .Keyword_asm => "asm",
273 .Keyword_async => "async",
274 .Keyword_await => "await",
275 .Keyword_break => "break",
276 .Keyword_cancel => "cancel",
277 .Keyword_catch => "catch",
278 .Keyword_comptime => "comptime",
279 .Keyword_const => "const",
280 .Keyword_continue => "continue",
281 .Keyword_defer => "defer",
282 .Keyword_else => "else",
283 .Keyword_enum => "enum",
284 .Keyword_errdefer => "errdefer",
285 .Keyword_error => "error",
286 .Keyword_export => "export",
287 .Keyword_extern => "extern",
288 .Keyword_false => "false",
289 .Keyword_fn => "fn",
290 .Keyword_for => "for",
291 .Keyword_if => "if",
292 .Keyword_inline => "inline",
293 .Keyword_nakedcc => "nakedcc",
294 .Keyword_noalias => "noalias",
295 .Keyword_null => "null",
296 .Keyword_or => "or",
297 .Keyword_orelse => "orelse",
298 .Keyword_packed => "packed",
299 .Keyword_promise => "promise",
300 .Keyword_pub => "pub",
301 .Keyword_resume => "resume",
302 .Keyword_return => "return",
303 .Keyword_linksection => "linksection",
304 .Keyword_stdcallcc => "stdcallcc",
305 .Keyword_struct => "struct",
306 .Keyword_suspend => "suspend",
307 .Keyword_switch => "switch",
308 .Keyword_test => "test",
309 .Keyword_threadlocal => "threadlocal",
310 .Keyword_true => "true",
311 .Keyword_try => "try",
312 .Keyword_undefined => "undefined",
313 .Keyword_union => "union",
314 .Keyword_unreachable => "unreachable",
315 .Keyword_usingnamespace => "usingnamespace",
316 .Keyword_var => "var",
317 .Keyword_volatile => "volatile",
318 .Keyword_while => "while",
319 };
320 }
197321 };
198322};
199323