| ... | ... | @@ -8,7 +8,7 @@ pub const TokenList = std.SegmentedList(CToken, 32); |
| 8 | 8 | |
| 9 | 9 | pub const CToken = struct { |
| 10 | 10 | id: Id, |
| 11 | | bytes: []const u8, |
| 11 | bytes: []const u8 = "", |
| 12 | 12 | num_lit_suffix: NumLitSuffix = .None, |
| 13 | 13 | |
| 14 | 14 | pub const Id = enum { |
| ... | ... | @@ -238,14 +238,14 @@ fn zigifyEscapeSequences(ctx: *Context, loc: ZigClangSourceLocation, name: []con |
| 238 | 238 | fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:0]const u8, i: *usize) !CToken { |
| 239 | 239 | var state: enum { |
| 240 | 240 | Start, |
| 241 | | GotLt, |
| 242 | | GotGt, |
| 243 | | GotPlus, |
| 244 | | GotMinus, |
| 245 | | GotAmpersand, |
| 246 | | GotPipe, |
| 247 | | GotBang, |
| 248 | | GotEq, |
| 241 | SawLt, |
| 242 | SawGt, |
| 243 | SawPlus, |
| 244 | SawMinus, |
| 245 | SawAmpersand, |
| 246 | SawPipe, |
| 247 | SawBang, |
| 248 | SawEq, |
| 249 | 249 | CharLit, |
| 250 | 250 | OpenComment, |
| 251 | 251 | Comment, |
| ... | ... | @@ -255,7 +255,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 255 | 255 | Identifier, |
| 256 | 256 | Decimal, |
| 257 | 257 | Octal, |
| 258 | | GotZero, |
| 258 | SawZero, |
| 259 | 259 | Hex, |
| 260 | 260 | Bin, |
| 261 | 261 | Float, |
| ... | ... | @@ -286,7 +286,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 286 | 286 | .Hex, |
| 287 | 287 | .Bin, |
| 288 | 288 | .Octal, |
| 289 | | .GotZero, |
| 289 | .SawZero, |
| 290 | 290 | .Float, |
| 291 | 291 | .FloatExp, |
| 292 | 292 | => { |
| ... | ... | @@ -294,19 +294,19 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 294 | 294 | return result; |
| 295 | 295 | }, |
| 296 | 296 | .Start, |
| 297 | | .GotMinus, |
| 297 | .SawMinus, |
| 298 | 298 | .Done, |
| 299 | 299 | .NumLitIntSuffixU, |
| 300 | 300 | .NumLitIntSuffixL, |
| 301 | 301 | .NumLitIntSuffixUL, |
| 302 | 302 | .NumLitIntSuffixLL, |
| 303 | | .GotLt, |
| 304 | | .GotGt, |
| 305 | | .GotPlus, |
| 306 | | .GotAmpersand, |
| 307 | | .GotPipe, |
| 308 | | .GotBang, |
| 309 | | .GotEq, |
| 303 | .SawLt, |
| 304 | .SawGt, |
| 305 | .SawPlus, |
| 306 | .SawAmpersand, |
| 307 | .SawPipe, |
| 308 | .SawBang, |
| 309 | .SawEq, |
| 310 | 310 | => { |
| 311 | 311 | return result; |
| 312 | 312 | }, |
| ... | ... | @@ -358,7 +358,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 358 | 358 | begin_index = i.*; |
| 359 | 359 | }, |
| 360 | 360 | '0' => { |
| 361 | | state = .GotZero; |
| 361 | state = .SawZero; |
| 362 | 362 | result.id = .NumLitInt; |
| 363 | 363 | begin_index = i.*; |
| 364 | 364 | }, |
| ... | ... | @@ -368,11 +368,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 368 | 368 | }, |
| 369 | 369 | '<' => { |
| 370 | 370 | result.id = .Lt; |
| 371 | | state = .GotLt; |
| 371 | state = .SawLt; |
| 372 | 372 | }, |
| 373 | 373 | '>' => { |
| 374 | 374 | result.id = .Gt; |
| 375 | | state = .GotGt; |
| 375 | state = .SawGt; |
| 376 | 376 | }, |
| 377 | 377 | '(' => { |
| 378 | 378 | result.id = .LParen; |
| ... | ... | @@ -388,15 +388,15 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 388 | 388 | }, |
| 389 | 389 | '+' => { |
| 390 | 390 | result.id = .Plus; |
| 391 | | state = .GotPlus; |
| 391 | state = .SawPlus; |
| 392 | 392 | }, |
| 393 | 393 | '-' => { |
| 394 | 394 | result.id = .Minus; |
| 395 | | state = .GotMinus; |
| 395 | state = .SawMinus; |
| 396 | 396 | }, |
| 397 | 397 | '!' => { |
| 398 | 398 | result.id = .Bang; |
| 399 | | state = .GotBang; |
| 399 | state = .SawBang; |
| 400 | 400 | }, |
| 401 | 401 | '~' => { |
| 402 | 402 | result.id = .Tilde; |
| ... | ... | @@ -404,7 +404,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 404 | 404 | }, |
| 405 | 405 | '=' => { |
| 406 | 406 | result.id = .Assign; |
| 407 | | state = .GotEq; |
| 407 | state = .SawEq; |
| 408 | 408 | }, |
| 409 | 409 | ',' => { |
| 410 | 410 | result.id = .Comma; |
| ... | ... | @@ -420,11 +420,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 420 | 420 | }, |
| 421 | 421 | '|' => { |
| 422 | 422 | result.id = .Pipe; |
| 423 | | state = .GotPipe; |
| 423 | state = .SawPipe; |
| 424 | 424 | }, |
| 425 | 425 | '&' => { |
| 426 | 426 | result.id = .Ampersand; |
| 427 | | state = .GotAmpersand; |
| 427 | state = .SawAmpersand; |
| 428 | 428 | }, |
| 429 | 429 | '?' => { |
| 430 | 430 | result.id = .QuestionMark; |
| ... | ... | @@ -441,7 +441,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 441 | 441 | } |
| 442 | 442 | }, |
| 443 | 443 | .Done => return result, |
| 444 | | .GotMinus => { |
| 444 | .SawMinus => { |
| 445 | 445 | switch (c) { |
| 446 | 446 | '>' => { |
| 447 | 447 | result.id = .Arrow; |
| ... | ... | @@ -454,7 +454,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 454 | 454 | else => return result, |
| 455 | 455 | } |
| 456 | 456 | }, |
| 457 | | .GotPlus => { |
| 457 | .SawPlus => { |
| 458 | 458 | switch (c) { |
| 459 | 459 | '+' => { |
| 460 | 460 | result.id = .Increment; |
| ... | ... | @@ -463,7 +463,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 463 | 463 | else => return result, |
| 464 | 464 | } |
| 465 | 465 | }, |
| 466 | | .GotLt => { |
| 466 | .SawLt => { |
| 467 | 467 | switch (c) { |
| 468 | 468 | '<' => { |
| 469 | 469 | result.id = .Shl; |
| ... | ... | @@ -476,7 +476,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 476 | 476 | else => return result, |
| 477 | 477 | } |
| 478 | 478 | }, |
| 479 | | .GotGt => { |
| 479 | .SawGt => { |
| 480 | 480 | switch (c) { |
| 481 | 481 | '>' => { |
| 482 | 482 | result.id = .Shr; |
| ... | ... | @@ -489,7 +489,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 489 | 489 | else => return result, |
| 490 | 490 | } |
| 491 | 491 | }, |
| 492 | | .GotPipe => { |
| 492 | .SawPipe => { |
| 493 | 493 | switch (c) { |
| 494 | 494 | '|' => { |
| 495 | 495 | result.id = .Or; |
| ... | ... | @@ -498,7 +498,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 498 | 498 | else => return result, |
| 499 | 499 | } |
| 500 | 500 | }, |
| 501 | | .GotAmpersand => { |
| 501 | .SawAmpersand => { |
| 502 | 502 | switch (c) { |
| 503 | 503 | '&' => { |
| 504 | 504 | result.id = .And; |
| ... | ... | @@ -507,7 +507,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 507 | 507 | else => return result, |
| 508 | 508 | } |
| 509 | 509 | }, |
| 510 | | .GotBang => { |
| 510 | .SawBang => { |
| 511 | 511 | switch (c) { |
| 512 | 512 | '=' => { |
| 513 | 513 | result.id = .Ne; |
| ... | ... | @@ -516,7 +516,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 516 | 516 | else => return result, |
| 517 | 517 | } |
| 518 | 518 | }, |
| 519 | | .GotEq => { |
| 519 | .SawEq => { |
| 520 | 520 | switch (c) { |
| 521 | 521 | '=' => { |
| 522 | 522 | result.id = .Eq; |
| ... | ... | @@ -557,7 +557,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 557 | 557 | '0'...'9' => { |
| 558 | 558 | state = .FloatExp; |
| 559 | 559 | }, |
| 560 | | else => { |
| 560 | else => { |
| 561 | 561 | try failDecl(ctx, loc, name, "macro tokenizing failed: expected a digit or '+' or '-'", .{}); |
| 562 | 562 | return error.TokenizingFailed; |
| 563 | 563 | }, |
| ... | ... | @@ -617,7 +617,7 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 617 | 617 | }, |
| 618 | 618 | } |
| 619 | 619 | }, |
| 620 | | .GotZero => { |
| 620 | .SawZero => { |
| 621 | 621 | switch (c) { |
| 622 | 622 | 'x', 'X' => { |
| 623 | 623 | state = .Hex; |
| ... | ... | @@ -829,9 +829,9 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 829 | 829 | unreachable; |
| 830 | 830 | } |
| 831 | 831 | |
| 832 | | |
| 833 | 832 | fn expectTokens(tl: *TokenList, src: [*:0]const u8, expected: []CToken) void { |
| 834 | | tokenizeCMacro(tl, src) catch unreachable; |
| 833 | // these can be undefined since they are only used for error reporting |
| 834 | tokenizeCMacro(undefined, undefined, undefined, tl, src) catch unreachable; |
| 835 | 835 | var it = tl.iterator(0); |
| 836 | 836 | for (expected) |t| { |
| 837 | 837 | var tok = it.next().?; |
| ... | ... | @@ -848,81 +848,74 @@ fn expectTokens(tl: *TokenList, src: [*:0]const u8, expected: []CToken) void { |
| 848 | 848 | tl.shrink(0); |
| 849 | 849 | } |
| 850 | 850 | |
| 851 | | |
| 852 | 851 | test "tokenize macro" { |
| 853 | 852 | var tl = TokenList.init(std.heap.page_allocator); |
| 854 | 853 | defer tl.deinit(); |
| 855 | 854 | |
| 856 | 855 | expectTokens(&tl, "TEST(0\n", &[_]CToken{ |
| 857 | | ctoken(.Identifier, "TEST"), |
| 858 | | ctoken(.Fn, ""), |
| 859 | | ctoken(.LParen, ""), |
| 860 | | ctoken(.NumLitInt, "0"), |
| 861 | | ctoken(.Eof, ""), |
| 856 | .{ .id = .Identifier, .bytes = "TEST" }, |
| 857 | .{ .id = .Fn }, |
| 858 | .{ .id = .LParen }, |
| 859 | .{ .id = .NumLitInt, .bytes = "0" }, |
| 860 | .{ .id = .Eof }, |
| 862 | 861 | }); |
| 863 | 862 | |
| 864 | 863 | expectTokens(&tl, "__FLT_MIN_10_EXP__ -37\n", &[_]CToken{ |
| 865 | | ctoken(.Identifier, "__FLT_MIN_10_EXP__"), |
| 866 | | ctoken(.Minus, ""), |
| 867 | | ctoken(.NumLitInt, "37"), |
| 868 | | ctoken(.Eof, ""), |
| 864 | .{ .id = .Identifier, .bytes = "__FLT_MIN_10_EXP__" }, |
| 865 | .{ .id = .Minus }, |
| 866 | .{ .id = .NumLitInt, .bytes = "37" }, |
| 867 | .{ .id = .Eof }, |
| 869 | 868 | }); |
| 870 | 869 | |
| 871 | 870 | expectTokens(&tl, "__llvm__ 1\n#define", &[_]CToken{ |
| 872 | | ctoken(.Identifier, "__llvm__"), |
| 873 | | ctoken(.NumLitInt, "1"), |
| 874 | | ctoken(.Eof, ""), |
| 875 | | |
| 871 | .{ .id = .Identifier, .bytes = "__llvm__" }, |
| 872 | .{ .id = .NumLitInt, .bytes = "1" }, |
| 873 | .{ .id = .Eof }, |
| 876 | 874 | }); |
| 877 | 875 | |
| 878 | 876 | expectTokens(&tl, "TEST 2", &[_]CToken{ |
| 879 | | ctoken(.Identifier, "TEST"), |
| 880 | | ctoken(.NumLitInt, "2"), |
| 881 | | ctoken(.Eof, ""), |
| 882 | | |
| 877 | .{ .id = .Identifier, .bytes = "TEST" }, |
| 878 | .{ .id = .NumLitInt, .bytes = "2" }, |
| 879 | .{ .id = .Eof }, |
| 883 | 880 | }); |
| 884 | 881 | |
| 885 | 882 | expectTokens(&tl, "FOO 0ull", &[_]CToken{ |
| 886 | | ctoken(.Identifier, "FOO"), |
| 887 | | cnumtoken(.LLU, "0"), |
| 888 | | ctoken(.Eof, ""), |
| 889 | | |
| 883 | .{ .id = .Identifier, .bytes = "FOO" }, |
| 884 | .{ .id = .NumLitInt, .bytes = "0", .num_lit_suffix = .LLU }, |
| 885 | .{ .id = .Eof }, |
| 890 | 886 | }); |
| 891 | | |
| 892 | 887 | } |
| 893 | 888 | |
| 894 | | |
| 895 | | |
| 896 | 889 | test "tokenize macro ops" { |
| 897 | 890 | var tl = TokenList.init(std.heap.page_allocator); |
| 898 | 891 | defer tl.deinit(); |
| 899 | 892 | |
| 900 | 893 | expectTokens(&tl, "ADD A + B", &[_]CToken{ |
| 901 | | ctoken(.Identifier, "ADD"), |
| 902 | | ctoken(.Identifier, "A"), |
| 903 | | ctoken(.Plus, ""), |
| 904 | | ctoken(.Identifier, "B"), |
| 905 | | ctoken(.Eof, ""), |
| 894 | .{ .id = .Identifier, .bytes = "ADD" }, |
| 895 | .{ .id = .Identifier, .bytes = "A" }, |
| 896 | .{ .id = .Plus }, |
| 897 | .{ .id = .Identifier, .bytes = "B" }, |
| 898 | .{ .id = .Eof }, |
| 906 | 899 | }); |
| 907 | 900 | |
| 908 | | expectTokens(&tl, "ADD (A) + B", &[_]CToken{ |
| 909 | | ctoken(.Identifier, "ADD"), |
| 910 | | ctoken(.LParen, ""), |
| 911 | | ctoken(.Identifier, "A"), |
| 912 | | ctoken(.RParen, ""), |
| 913 | | ctoken(.Plus, ""), |
| 914 | | ctoken(.Identifier, "B"), |
| 915 | | ctoken(.Eof, ""), |
| 901 | expectTokens(&tl, "ADD (A) + B", &[_]CToken{ |
| 902 | .{ .id = .Identifier, .bytes = "ADD" }, |
| 903 | .{ .id = .LParen }, |
| 904 | .{ .id = .Identifier, .bytes = "A" }, |
| 905 | .{ .id = .RParen }, |
| 906 | .{ .id = .Plus }, |
| 907 | .{ .id = .Identifier, .bytes = "B" }, |
| 908 | .{ .id = .Eof }, |
| 916 | 909 | }); |
| 917 | 910 | |
| 918 | 911 | expectTokens(&tl, "ADD (A) + B", &[_]CToken{ |
| 919 | | ctoken(.Identifier, "ADD"), |
| 920 | | ctoken(.LParen, ""), |
| 921 | | ctoken(.Identifier, "A"), |
| 922 | | ctoken(.RParen, ""), |
| 923 | | ctoken(.Plus, ""), |
| 924 | | ctoken(.Identifier, "B"), |
| 925 | | ctoken(.Eof, ""), |
| 912 | .{ .id = .Identifier, .bytes = "ADD" }, |
| 913 | .{ .id = .LParen }, |
| 914 | .{ .id = .Identifier, .bytes = "A" }, |
| 915 | .{ .id = .RParen }, |
| 916 | .{ .id = .Plus }, |
| 917 | .{ .id = .Identifier, .bytes = "B" }, |
| 918 | .{ .id = .Eof }, |
| 926 | 919 | }); |
| 927 | 920 | } |
| 928 | 921 | |
| ... | ... | @@ -930,19 +923,20 @@ test "escape sequences" { |
| 930 | 923 | var buf: [1024]u8 = undefined; |
| 931 | 924 | var alloc = std.heap.FixedBufferAllocator.init(buf[0..]); |
| 932 | 925 | const a = &alloc.allocator; |
| 933 | | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ |
| 926 | // these can be undefined since they are only used for error reporting |
| 927 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(undefined, undefined, undefined, a, .{ |
| 934 | 928 | .id = .StrLit, |
| 935 | 929 | .bytes = "\\x0077", |
| 936 | 930 | })).bytes, "\\x77")); |
| 937 | | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ |
| 931 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(undefined, undefined, undefined, a, .{ |
| 938 | 932 | .id = .StrLit, |
| 939 | 933 | .bytes = "\\24500", |
| 940 | 934 | })).bytes, "\\xa500")); |
| 941 | | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ |
| 935 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(undefined, undefined, undefined, a, .{ |
| 942 | 936 | .id = .StrLit, |
| 943 | 937 | .bytes = "\\x0077 abc", |
| 944 | 938 | })).bytes, "\\x77 abc")); |
| 945 | | expect(std.mem.eql(u8, (try zigifyEscapeSequences(a, .{ |
| 939 | expect(std.mem.eql(u8, (try zigifyEscapeSequences(undefined, undefined, undefined, a, .{ |
| 946 | 940 | .id = .StrLit, |
| 947 | 941 | .bytes = "\\045abc", |
| 948 | 942 | })).bytes, "\\x25abc")); |