| ... | ... | @@ -6,7 +6,7 @@ pub const Source = struct { |
| 6 | 6 | file_name: []const u8, |
| 7 | 7 | tokens: TokenList, |
| 8 | 8 | |
| 9 | | pub const TokenList = SegmentedList(Token, 64); |
| 9 | pub const TokenList = std.SegmentedList(Token, 64); |
| 10 | 10 | }; |
| 11 | 11 | |
| 12 | 12 | pub const Token = struct { |
| ... | ... | @@ -134,6 +134,121 @@ pub const Token = struct { |
| 134 | 134 | Keyword_ifndef, |
| 135 | 135 | Keyword_error, |
| 136 | 136 | Keyword_pragma, |
| 137 | |
| 138 | pub fn symbol(tok: Token) []const u8 { |
| 139 | return switch (tok.id) { |
| 140 | .Invalid => "Invalid", |
| 141 | .Eof => "Eof", |
| 142 | .Nl => "NewLine", |
| 143 | .Identifier => "Identifier", |
| 144 | .MacroString => "MacroString", |
| 145 | .StringLiteral => "StringLiteral", |
| 146 | .CharLiteral => "CharLiteral", |
| 147 | .IntegerLiteral => "IntegerLiteral", |
| 148 | .FloatLiteral => "FloatLiteral", |
| 149 | .LineComment => "LineComment", |
| 150 | .MultiLineComment => "MultiLineComment", |
| 151 | |
| 152 | .Bang => "!", |
| 153 | .BangEqual => "!=", |
| 154 | .Pipe => "|", |
| 155 | .PipePipe => "||", |
| 156 | .PipeEqual => "|=", |
| 157 | .Equal => "=", |
| 158 | .EqualEqual => "==", |
| 159 | .LParen => "(", |
| 160 | .RParen => ")", |
| 161 | .LBrace => "{", |
| 162 | .RBrace => "}", |
| 163 | .LBracket => "[", |
| 164 | .RBracket => "]", |
| 165 | .Period => ".", |
| 166 | .Ellipsis => "...", |
| 167 | .Caret => "^", |
| 168 | .CaretEqual => "^=", |
| 169 | .Plus => "+", |
| 170 | .PlusPlus => "++", |
| 171 | .PlusEqual => "+=", |
| 172 | .Minus => "-", |
| 173 | .MinusMinus => "--", |
| 174 | .MinusEqual => "-=", |
| 175 | .Asterisk => "*", |
| 176 | .AsteriskEqual => "*=", |
| 177 | .Percent => "%", |
| 178 | .PercentEqual => "%=", |
| 179 | .Arrow => "->", |
| 180 | .Colon => ":", |
| 181 | .Semicolon => ";", |
| 182 | .Slash => "/", |
| 183 | .SlashEqual => "/=", |
| 184 | .Comma => ",", |
| 185 | .Ampersand => "&", |
| 186 | .AmpersandAmpersand => "&&", |
| 187 | .AmpersandEqual => "&=", |
| 188 | .QuestionMark => "?", |
| 189 | .AngleBracketLeft => "<", |
| 190 | .AngleBracketLeftEqual => "<=", |
| 191 | .AngleBracketAngleBracketLeft => "<<", |
| 192 | .AngleBracketAngleBracketLeftEqual => "<<=", |
| 193 | .AngleBracketRight => ">", |
| 194 | .AngleBracketRightEqual => ">=", |
| 195 | .AngleBracketAngleBracketRight => ">>", |
| 196 | .AngleBracketAngleBracketRightEqual => ">>=", |
| 197 | .Tilde => "~", |
| 198 | .Hash => "#", |
| 199 | .HashHash => "##", |
| 200 | .Keyword_auto => "auto", |
| 201 | .Keyword_break => "break", |
| 202 | .Keyword_case => "case", |
| 203 | .Keyword_char => "char", |
| 204 | .Keyword_const => "const", |
| 205 | .Keyword_continue => "continue", |
| 206 | .Keyword_default => "default", |
| 207 | .Keyword_do => "do", |
| 208 | .Keyword_double => "double", |
| 209 | .Keyword_else => "else", |
| 210 | .Keyword_enum => "enum", |
| 211 | .Keyword_extern => "extern", |
| 212 | .Keyword_float => "float", |
| 213 | .Keyword_for => "for", |
| 214 | .Keyword_goto => "goto", |
| 215 | .Keyword_if => "if", |
| 216 | .Keyword_int => "int", |
| 217 | .Keyword_long => "long", |
| 218 | .Keyword_register => "register", |
| 219 | .Keyword_return => "return", |
| 220 | .Keyword_short => "short", |
| 221 | .Keyword_signed => "signed", |
| 222 | .Keyword_sizeof => "sizeof", |
| 223 | .Keyword_static => "static", |
| 224 | .Keyword_struct => "struct", |
| 225 | .Keyword_switch => "switch", |
| 226 | .Keyword_typedef => "typedef", |
| 227 | .Keyword_union => "union", |
| 228 | .Keyword_unsigned => "unsigned", |
| 229 | .Keyword_void => "void", |
| 230 | .Keyword_volatile => "volatile", |
| 231 | .Keyword_while => "while", |
| 232 | .Keyword_bool => "_Bool", |
| 233 | .Keyword_complex => "_Complex", |
| 234 | .Keyword_imaginary => "_Imaginary", |
| 235 | .Keyword_inline => "inline", |
| 236 | .Keyword_restrict => "restrict", |
| 237 | .Keyword_alignas => "_Alignas", |
| 238 | .Keyword_alignof => "_Alignof", |
| 239 | .Keyword_atomic => "_Atomic", |
| 240 | .Keyword_generic => "_Generic", |
| 241 | .Keyword_noreturn => "_Noreturn", |
| 242 | .Keyword_static_assert => "_Static_assert", |
| 243 | .Keyword_thread_local => "_Thread_local", |
| 244 | .Keyword_include => "include", |
| 245 | .Keyword_define => "define", |
| 246 | .Keyword_ifdef => "ifdef", |
| 247 | .Keyword_ifndef => "ifndef", |
| 248 | .Keyword_error => "error", |
| 249 | .Keyword_pragma => "pragma", |
| 250 | }; |
| 251 | } |
| 137 | 252 | }; |
| 138 | 253 | |
| 139 | 254 | pub const Keyword = struct { |
| ... | ... | @@ -1121,8 +1236,7 @@ pub const Tokenizer = struct { |
| 1121 | 1236 | } |
| 1122 | 1237 | } else if (self.index == self.source.buffer.len) { |
| 1123 | 1238 | switch (state) { |
| 1124 | | .AfterStringLiteral, |
| 1125 | | .Start => {}, |
| 1239 | .AfterStringLiteral, .Start => {}, |
| 1126 | 1240 | .u, .u8, .U, .L, .Identifier => { |
| 1127 | 1241 | result.id = Token.getKeyword(self.source.buffer[result.start..self.index], self.prev_tok_id == .Hash and !self.pp_directive) orelse .Identifier; |
| 1128 | 1242 | }, |
| ... | ... | @@ -1416,6 +1530,7 @@ fn expectTokens(source: []const u8, expected_tokens: []const Token.Id) void { |
| 1416 | 1530 | .source = &Source{ |
| 1417 | 1531 | .buffer = source, |
| 1418 | 1532 | .file_name = undefined, |
| 1533 | .tokens = undefined, |
| 1419 | 1534 | }, |
| 1420 | 1535 | }; |
| 1421 | 1536 | for (expected_tokens) |expected_token_id| { |