| author | |
| committer | |
| log | 8c4f3e5a319b2c700a57d833e1aaf01e769f8d4a |
| tree | ff997f4cf39dc256acb38ae68ebcc1d2f0530303 |
| parent | a524e57090f3e3412292dbe6b3e4fe4fb7bad1ea |
3 files changed, 83 insertions(+), 48 deletions(-)
lib/std/zig/ast.zig+18-6| ... | ... | @@ -407,7 +407,9 @@ pub const Tree = struct { |
| 407 | 407 | => { |
| 408 | 408 | const main_token = main_tokens[n]; |
| 409 | 409 | return switch (token_tags[main_token]) { |
| 410 | .Asterisk => switch (token_tags[main_token - 1]) { | |
| 410 | .Asterisk, | |
| 411 | .AsteriskAsterisk, | |
| 412 | => switch (token_tags[main_token - 1]) { | |
| 411 | 413 | .LBracket => main_token - 1, |
| 412 | 414 | else => main_token, |
| 413 | 415 | }, |
| ... | ... | @@ -1625,7 +1627,9 @@ pub const Tree = struct { |
| 1625 | 1627 | // literals in some places here |
| 1626 | 1628 | const Kind = full.PtrType.Kind; |
| 1627 | 1629 | const kind: Kind = switch (token_tags[info.main_token]) { |
| 1628 | .Asterisk => switch (token_tags[info.main_token + 1]) { | |
| 1630 | .Asterisk, | |
| 1631 | .AsteriskAsterisk, | |
| 1632 | => switch (token_tags[info.main_token + 1]) { | |
| 1629 | 1633 | .RBracket => .many, |
| 1630 | 1634 | .Colon => .sentinel, |
| 1631 | 1635 | .Identifier => if (token_tags[info.main_token - 1] == .LBracket) Kind.c else .one, |
| ... | ... | @@ -2393,18 +2397,26 @@ pub const Node = struct { |
| 2393 | 2397 | /// `[*]align(lhs) rhs`. lhs can be omitted. |
| 2394 | 2398 | /// `*align(lhs) rhs`. lhs can be omitted. |
| 2395 | 2399 | /// `[]rhs`. |
| 2396 | /// main_token is the asterisk if a pointer or the lbrace if a slice | |
| 2400 | /// main_token is the asterisk if a pointer or the lbracket if a slice | |
| 2401 | /// main_token might be a ** token, which is shared with a parent/child | |
| 2402 | /// pointer type and may require special handling. | |
| 2397 | 2403 | PtrTypeAligned, |
| 2398 | 2404 | /// `[*:lhs]rhs`. lhs can be omitted. |
| 2399 | 2405 | /// `*rhs`. |
| 2400 | 2406 | /// `[:lhs]rhs`. |
| 2401 | /// main_token is the asterisk if a pointer or the lbrace if a slice | |
| 2407 | /// main_token is the asterisk if a pointer or the lbracket if a slice | |
| 2408 | /// main_token might be a ** token, which is shared with a parent/child | |
| 2409 | /// pointer type and may require special handling. | |
| 2402 | 2410 | PtrTypeSentinel, |
| 2403 | 2411 | /// lhs is index into PtrType. rhs is the element type expression. |
| 2404 | /// main_token is the asterisk if a pointer or the lbrace if a slice | |
| 2412 | /// main_token is the asterisk if a pointer or the lbracket if a slice | |
| 2413 | /// main_token might be a ** token, which is shared with a parent/child | |
| 2414 | /// pointer type and may require special handling. | |
| 2405 | 2415 | PtrType, |
| 2406 | 2416 | /// lhs is index into PtrTypeBitRange. rhs is the element type expression. |
| 2407 | /// main_token is the asterisk if a pointer or the lbrace if a slice | |
| 2417 | /// main_token is the asterisk if a pointer or the lbracket if a slice | |
| 2418 | /// main_token might be a ** token, which is shared with a parent/child | |
| 2419 | /// pointer type and may require special handling. | |
| 2408 | 2420 | PtrTypeBitRange, |
| 2409 | 2421 | /// `lhs[rhs..]` |
| 2410 | 2422 | /// main_token is the lbracket. |
lib/std/zig/parser_test.zig+55-42| ... | ... | @@ -2318,27 +2318,27 @@ test "zig fmt: alignment" { |
| 2318 | 2318 | ); |
| 2319 | 2319 | } |
| 2320 | 2320 | |
| 2321 | //test "zig fmt: C main" { | |
| 2322 | // try testCanonical( | |
| 2323 | // \\fn main(argc: c_int, argv: **u8) c_int { | |
| 2324 | // \\ const a = b; | |
| 2325 | // \\} | |
| 2326 | // \\ | |
| 2327 | // ); | |
| 2328 | //} | |
| 2329 | // | |
| 2330 | //test "zig fmt: return" { | |
| 2331 | // try testCanonical( | |
| 2332 | // \\fn foo(argc: c_int, argv: **u8) c_int { | |
| 2333 | // \\ return 0; | |
| 2334 | // \\} | |
| 2335 | // \\ | |
| 2336 | // \\fn bar() void { | |
| 2337 | // \\ return; | |
| 2338 | // \\} | |
| 2339 | // \\ | |
| 2340 | // ); | |
| 2341 | //} | |
| 2321 | test "zig fmt: C main" { | |
| 2322 | try testCanonical( | |
| 2323 | \\fn main(argc: c_int, argv: **u8) c_int { | |
| 2324 | \\ const a = b; | |
| 2325 | \\} | |
| 2326 | \\ | |
| 2327 | ); | |
| 2328 | } | |
| 2329 | ||
| 2330 | test "zig fmt: return" { | |
| 2331 | try testCanonical( | |
| 2332 | \\fn foo(argc: c_int, argv: **u8) c_int { | |
| 2333 | \\ return 0; | |
| 2334 | \\} | |
| 2335 | \\ | |
| 2336 | \\fn bar() void { | |
| 2337 | \\ return; | |
| 2338 | \\} | |
| 2339 | \\ | |
| 2340 | ); | |
| 2341 | } | |
| 2342 | 2342 | |
| 2343 | 2343 | test "zig fmt: function attributes" { |
| 2344 | 2344 | try testCanonical( |
| ... | ... | @@ -2356,27 +2356,40 @@ test "zig fmt: function attributes" { |
| 2356 | 2356 | ); |
| 2357 | 2357 | } |
| 2358 | 2358 | |
| 2359 | //test "zig fmt: pointer attributes" { | |
| 2360 | // try testCanonical( | |
| 2361 | // \\extern fn f1(s: *align(*u8) u8) c_int; | |
| 2362 | // \\extern fn f2(s: **align(1) *const *volatile u8) c_int; | |
| 2363 | // \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int; | |
| 2364 | // \\extern fn f4(s: *align(1) const volatile u8) c_int; | |
| 2365 | // \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int; | |
| 2366 | // \\ | |
| 2367 | // ); | |
| 2368 | //} | |
| 2369 | // | |
| 2370 | //test "zig fmt: slice attributes" { | |
| 2371 | // try testCanonical( | |
| 2372 | // \\extern fn f1(s: *align(*u8) u8) c_int; | |
| 2373 | // \\extern fn f2(s: **align(1) *const *volatile u8) c_int; | |
| 2374 | // \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int; | |
| 2375 | // \\extern fn f4(s: *align(1) const volatile u8) c_int; | |
| 2376 | // \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int; | |
| 2377 | // \\ | |
| 2378 | // ); | |
| 2379 | //} | |
| 2359 | test "zig fmt: nested pointers with ** tokens" { | |
| 2360 | try testCanonical( | |
| 2361 | \\const x: *u32 = undefined; | |
| 2362 | \\const x: **u32 = undefined; | |
| 2363 | \\const x: ***u32 = undefined; | |
| 2364 | \\const x: ****u32 = undefined; | |
| 2365 | \\const x: *****u32 = undefined; | |
| 2366 | \\const x: ******u32 = undefined; | |
| 2367 | \\const x: *******u32 = undefined; | |
| 2368 | \\ | |
| 2369 | ); | |
| 2370 | } | |
| 2371 | ||
| 2372 | test "zig fmt: pointer attributes" { | |
| 2373 | try testCanonical( | |
| 2374 | \\extern fn f1(s: *align(*u8) u8) c_int; | |
| 2375 | \\extern fn f2(s: **align(1) *const *volatile u8) c_int; | |
| 2376 | \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int; | |
| 2377 | \\extern fn f4(s: *align(1) const volatile u8) c_int; | |
| 2378 | \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int; | |
| 2379 | \\ | |
| 2380 | ); | |
| 2381 | } | |
| 2382 | ||
| 2383 | test "zig fmt: slice attributes" { | |
| 2384 | try testCanonical( | |
| 2385 | \\extern fn f1(s: []align(*u8) u8) c_int; | |
| 2386 | \\extern fn f2(s: []align(1) []const []volatile u8) c_int; | |
| 2387 | \\extern fn f3(s: []align(1) const [:0]align(1) volatile []const volatile u8) c_int; | |
| 2388 | \\extern fn f4(s: []align(1) const volatile u8) c_int; | |
| 2389 | \\extern fn f5(s: [:0]align(1) const volatile u8) c_int; | |
| 2390 | \\ | |
| 2391 | ); | |
| 2392 | } | |
| 2380 | 2393 | |
| 2381 | 2394 | test "zig fmt: test declaration" { |
| 2382 | 2395 | try testCanonical( |
lib/std/zig/render.zig+10| ... | ... | @@ -699,6 +699,16 @@ fn renderPtrType( |
| 699 | 699 | ) Error!void { |
| 700 | 700 | switch (ptr_type.kind) { |
| 701 | 701 | .one => { |
| 702 | // Since ** tokens exist and the same token is shared by two | |
| 703 | // nested pointer types, we check to see if we are the parent | |
| 704 | // in such a relationship. If so, skip rendering anything for | |
| 705 | // this pointer type and rely on the child to render our asterisk | |
| 706 | // as well when it renders the ** token. | |
| 707 | if (tree.tokens.items(.tag)[ptr_type.ast.main_token] == .AsteriskAsterisk and | |
| 708 | ptr_type.ast.main_token == tree.nodes.items(.main_token)[ptr_type.ast.child_type]) | |
| 709 | { | |
| 710 | return renderExpression(ais, tree, ptr_type.ast.child_type, space); | |
| 711 | } | |
| 702 | 712 | try renderToken(ais, tree, ptr_type.ast.main_token, .None); // asterisk |
| 703 | 713 | }, |
| 704 | 714 | .many => { |