Commit b4172e5151

Benjamin Feng <benjamin.feng@glassdoor.com>
2019-06-21 15:13:03
Humanize tokenized symbol names
1 parent 7f23dac
Changed files (2)
std/zig/ast.zig
@@ -324,11 +324,11 @@ pub const Error = union(enum) {
                     return stream.print("`&&` is invalid. Note that `and` is boolean AND.");
                 },
                 .Invalid => {
-                    return stream.print("expected {}, found invalid bytes", @tagName(self.expected_id));
+                    return stream.print("expected {}, found invalid bytes", self.expected_id.symbol());
                 },
                 else => {
-                    const token_name = @tagName(found_token.id);
-                    return stream.print("expected {}, found {}", @tagName(self.expected_id), token_name);
+                    const token_name = found_token.id.symbol();
+                    return stream.print("expected {}, found {}", self.expected_id.symbol(), token_name);
                 },
             }
         }
@@ -339,8 +339,8 @@ pub const Error = union(enum) {
         end_id: Token.Id,
 
         pub fn render(self: *const ExpectedCommaOrEnd, tokens: *Tree.TokenList, stream: var) !void {
-            const token_name = @tagName(tokens.at(self.token).id);
-            return stream.print("expected ',' or {}, found {}", @tagName(self.end_id), token_name);
+            const actual_token = tokens.at(self.token);
+            return stream.print("expected ',' or {}, found {}", self.end_id.symbol(), actual_token.id.symbol());
         }
     };
 
@@ -351,8 +351,8 @@ pub const Error = union(enum) {
             token: TokenIndex,
 
             pub fn render(self: *const ThisError, tokens: *Tree.TokenList, stream: var) !void {
-                const token_name = @tagName(tokens.at(self.token).id);
-                return stream.print(msg, token_name);
+                const actual_token = tokens.at(self.token);
+                return stream.print(msg, actual_token.id.symbol());
             }
         };
     }
std/zig/tokenizer.zig
@@ -194,6 +194,130 @@ pub const Token = struct {
         Keyword_var,
         Keyword_volatile,
         Keyword_while,
+
+        pub fn symbol(id: Id) []const u8 {
+            return switch (id) {
+                .Invalid => "[Invalid]",
+                .Invalid_ampersands => "&&",
+                .Identifier => "[Identifier]",
+                .StringLiteral => "[StringLiteral]",
+                .MultilineStringLiteralLine => "[MultilineStringLiteralLine]",
+                .CharLiteral => "[CharLiteral]",
+                .Eof => "[Eof]",
+                .Builtin => "[Builtin]",
+                .IntegerLiteral => "[IntegerLiteral]",
+                .FloatLiteral => "[FloatLiteral]",
+                .LineComment => "[LineComment]",
+                .DocComment => "[DocComment]",
+                .ShebangLine => "[ShebangLine]",
+
+                .Bang => "!",
+                .Pipe => "|",
+                .PipePipe => "||",
+                .PipeEqual => "|=",
+                .Equal => "=",
+                .EqualEqual => "==",
+                .EqualAngleBracketRight => "=>",
+                .BangEqual => "!=",
+                .LParen => "(",
+                .RParen => ")",
+                .Semicolon => ";",
+                .Percent => "%",
+                .PercentEqual => "%=",
+                .LBrace => "{",
+                .RBrace => "}",
+                .LBracket => "[",
+                .RBracket => "]",
+                .Period => ".",
+                .Ellipsis2 => "..",
+                .Ellipsis3 => "...",
+                .Caret => "^",
+                .CaretEqual => "^=",
+                .Plus => "+",
+                .PlusPlus => "++",
+                .PlusEqual => "+=",
+                .PlusPercent => "+%",
+                .PlusPercentEqual => "+%=",
+                .Minus => "-",
+                .MinusEqual => "-=",
+                .MinusPercent => "-%",
+                .MinusPercentEqual => "-%=",
+                .Asterisk => "*",
+                .AsteriskEqual => "*=",
+                .AsteriskAsterisk => "**",
+                .AsteriskPercent => "*%",
+                .AsteriskPercentEqual => "*%=",
+                .Arrow => "->",
+                .Colon => ":",
+                .Slash => "/",
+                .SlashEqual => "/=",
+                .Comma => ",",
+                .Ampersand => "&",
+                .AmpersandEqual => "&=",
+                .QuestionMark => "?",
+                .AngleBracketLeft => "<",
+                .AngleBracketLeftEqual => "<=",
+                .AngleBracketAngleBracketLeft => "<<",
+                .AngleBracketAngleBracketLeftEqual => "<<=",
+                .AngleBracketRight => ">",
+                .AngleBracketRightEqual => ">=",
+                .AngleBracketAngleBracketRight => ">>",
+                .AngleBracketAngleBracketRightEqual => ">>=",
+                .Tilde => "~",
+                .BracketStarBracket => "[*]",
+                .BracketStarCBracket => "[*c]",
+                .Keyword_align => "align",
+                .Keyword_allowzero => "allowzero",
+                .Keyword_and => "and",
+                .Keyword_asm => "asm",
+                .Keyword_async => "async",
+                .Keyword_await => "await",
+                .Keyword_break => "break",
+                .Keyword_cancel => "cancel",
+                .Keyword_catch => "catch",
+                .Keyword_comptime => "comptime",
+                .Keyword_const => "const",
+                .Keyword_continue => "continue",
+                .Keyword_defer => "defer",
+                .Keyword_else => "else",
+                .Keyword_enum => "enum",
+                .Keyword_errdefer => "errdefer",
+                .Keyword_error => "error",
+                .Keyword_export => "export",
+                .Keyword_extern => "extern",
+                .Keyword_false => "false",
+                .Keyword_fn => "fn",
+                .Keyword_for => "for",
+                .Keyword_if => "if",
+                .Keyword_inline => "inline",
+                .Keyword_nakedcc => "nakedcc",
+                .Keyword_noalias => "noalias",
+                .Keyword_null => "null",
+                .Keyword_or => "or",
+                .Keyword_orelse => "orelse",
+                .Keyword_packed => "packed",
+                .Keyword_promise => "promise",
+                .Keyword_pub => "pub",
+                .Keyword_resume => "resume",
+                .Keyword_return => "return",
+                .Keyword_linksection => "linksection",
+                .Keyword_stdcallcc => "stdcallcc",
+                .Keyword_struct => "struct",
+                .Keyword_suspend => "suspend",
+                .Keyword_switch => "switch",
+                .Keyword_test => "test",
+                .Keyword_threadlocal => "threadlocal",
+                .Keyword_true => "true",
+                .Keyword_try => "try",
+                .Keyword_undefined => "undefined",
+                .Keyword_union => "union",
+                .Keyword_unreachable => "unreachable",
+                .Keyword_usingnamespace => "usingnamespace",
+                .Keyword_var => "var",
+                .Keyword_volatile => "volatile",
+                .Keyword_while => "while",
+            };
+        }
     };
 };