@@ -6,7 +6,6 @@ const std = @import("std");
6
6
const offsets = @import ("offsets.zig" );
7
7
const Ast = std .zig .Ast ;
8
8
const Node = Ast .Node ;
9
- const TokenTag = std .zig .Token .Tag ;
10
9
const full = Ast .full ;
11
10
12
11
fn fullPtrTypeComponents (tree : Ast , info : full.PtrType.Components ) full.PtrType {
@@ -55,41 +54,6 @@ fn fullPtrTypeComponents(tree: Ast, info: full.PtrType.Components) full.PtrType
55
54
return result ;
56
55
}
57
56
58
- fn findNextLBrace (token_tags : []const TokenTag , start : Ast.TokenIndex ) ? Ast.TokenIndex {
59
- for (token_tags [start .. ], 0.. ) | tag , i | {
60
- if (tag == TokenTag .l_brace ) {
61
- return start + @as (Ast .TokenIndex , @intCast (i ));
62
- }
63
- }
64
- return null ;
65
- }
66
-
67
- /// Given an l_brace, find the corresponding r_brace.
68
- /// If no corresponding r_brace is found, return null.
69
- /// Useful for finding the extent of a block/scope if the syntax is valid.
70
- fn findMatchingRBrace (token_tags : []const TokenTag , l_brace : Ast.TokenIndex ) ? Ast.TokenIndex {
71
- std .debug .assert (token_tags [l_brace ] == TokenTag .l_brace );
72
-
73
- const start = l_brace + 1 ;
74
- var depth : i32 = 0 ;
75
- var offset : Ast.TokenIndex = 0 ;
76
-
77
- for (token_tags [start .. ], 0.. ) | tag , i | {
78
- if (tag == TokenTag .l_brace ) {
79
- depth += 1 ;
80
- }
81
- if (tag == TokenTag .r_brace ) {
82
- if (depth == 0 ) {
83
- offset = @intCast (i );
84
- break ;
85
- }
86
- depth -= 1 ;
87
- }
88
- }
89
-
90
- return if (depth == 0 ) start + offset else null ;
91
- }
92
-
93
57
pub fn ptrTypeSimple (tree : Ast , node : Node.Index ) full.PtrType {
94
58
std .debug .assert (tree .nodes .items (.tag )[node ] == .ptr_type );
95
59
const data = tree .nodes .items (.data )[node ];
@@ -353,6 +317,10 @@ pub fn fullFor(tree: Ast, node: Node.Index) ?full.For {
353
317
};
354
318
}
355
319
320
+ fn findMatchingRBrace (tree : Ast , start : Ast.TokenIndex ) ? Ast.TokenIndex {
321
+ return if (std .mem .indexOfScalarPos (std .zig .Token .Tag , tree .tokens .items (.tag ), start , .r_brace )) | index | @intCast (index ) else null ;
322
+ }
323
+
356
324
pub fn lastToken (tree : Ast , node : Ast.Node.Index ) Ast.TokenIndex {
357
325
const TokenIndex = Ast .TokenIndex ;
358
326
const tags = tree .nodes .items (.tag );
@@ -614,13 +582,15 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
614
582
}
615
583
n = tree .extra_data [params .end - 1 ]; // last parameter
616
584
},
617
- .switch_comma , .@"switch" = > {
618
- const lhs = datas [n ].lhs ;
619
- var l_brace = tree .lastToken (lhs ) + 2 ; // + 2 => (last) token of the condition + the .r_paren
620
- // If the condition within the switch is invalid, eg `switch (a.) {}`,
621
- // l_brace would be the index of the .r_paren of the switch ----^
622
- if (token_tags [l_brace ] != .l_brace ) l_brace += 1 ;
623
- return findMatchingRBrace (token_tags , l_brace ) orelse @intCast (tree .tokens .len - 1 );
585
+ .@"switch" = > {
586
+ const cases = tree .extraData (datas [n ].rhs , Node .SubRange );
587
+ if (cases .end - cases .start == 0 ) {
588
+ const token = tree .lastToken (datas [n ].lhs ) + 3 ; // rparen, lbrace, rbrace
589
+ return end_offset + (findMatchingRBrace (tree , token ) orelse token );
590
+ } else {
591
+ var token = tree .lastToken (tree .extra_data [cases .end - 1 ]) + 1 ; // for the rbrace
592
+ return end_offset + (findMatchingRBrace (tree , token ) orelse token );
593
+ }
624
594
},
625
595
.@"asm" = > {
626
596
const extra = tree .extraData (datas [n ].rhs , Node .Asm );
@@ -636,6 +606,7 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
636
606
},
637
607
.array_init_comma ,
638
608
.struct_init_comma ,
609
+ .switch_comma ,
639
610
= > {
640
611
if (datas [n ].rhs != 0 ) {
641
612
const members = tree .extraData (datas [n ].rhs , Node .SubRange );
@@ -659,6 +630,8 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
659
630
},
660
631
.array_init_dot_comma ,
661
632
.struct_init_dot_comma ,
633
+ .block_semicolon ,
634
+ .container_decl_trailing ,
662
635
.tagged_union_trailing ,
663
636
.builtin_call_comma ,
664
637
= > {
@@ -676,26 +649,38 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
676
649
}
677
650
n = datas [n ].rhs ;
678
651
},
679
- .block ,
680
- .block_semicolon ,
681
- .block_two_semicolon ,
682
- .block_two ,
683
- = > {
684
- return findMatchingRBrace (token_tags , main_tokens [n ]) orelse @intCast (tree .tokens .len - 1 );
652
+ .block = > {
653
+ std .debug .assert (datas [n ].rhs - datas [n ].lhs > 0 );
654
+ const token = lastToken (tree , tree .extra_data [datas [n ].rhs - 1 ]) + 1 ; // for the rbrace
655
+ return end_offset + (findMatchingRBrace (tree , token ) orelse token );
685
656
},
686
- .container_decl_trailing ,
687
- .container_decl_two_trailing ,
688
- .container_decl_two ,
689
- = > {
690
- // + 1 for the lbrace
691
- return findMatchingRBrace (token_tags , main_tokens [n ] + 1 ) orelse @intCast (tree .tokens .len - 1 );
657
+ .block_two , .container_decl_two = > {
658
+ if (datas [n ].rhs != 0 ) {
659
+ const token = tree .lastToken (datas [n ].rhs ) + 1 ; // for the rparen/rbrace
660
+ return end_offset + (findMatchingRBrace (tree , token ) orelse token );
661
+ } else if (datas [n ].lhs != 0 ) {
662
+ const token = tree .lastToken (datas [n ].lhs ) + 1 ; // for the rparen/rbrace
663
+ return end_offset + (findMatchingRBrace (tree , token ) orelse token );
664
+ } else {
665
+ const token : TokenIndex = switch (tags [n ]) {
666
+ .block_two = > main_tokens [n ] + 1 , // rbrace
667
+ .container_decl_two = > main_tokens [n ] + 2 , // lbrace + rbrace
668
+ else = > unreachable ,
669
+ };
670
+ return end_offset + (findMatchingRBrace (tree , token ) orelse token );
671
+ }
692
672
},
693
673
.container_decl_arg ,
694
674
.container_decl_arg_trailing ,
695
675
= > {
696
- // + 4 for the lparen, identifier, rparen, lbrace
697
- const l_brace = findNextLBrace (token_tags , main_tokens [n ]) orelse return @intCast (tree .tokens .len - 1 );
698
- return findMatchingRBrace (token_tags , l_brace ) orelse @intCast (tree .tokens .len - 1 );
676
+ const members = tree .extraData (datas [n ].rhs , Node .SubRange );
677
+ if (members .end - members .start == 0 ) {
678
+ const token = tree .lastToken (datas [n ].lhs ) + 3 ; // // for the rparen + lbrace + rbrace
679
+ return end_offset + (findMatchingRBrace (tree , token ) orelse token );
680
+ } else {
681
+ const token = tree .lastToken (tree .extra_data [members .end - 1 ]) + 1 ; // for the rbrace
682
+ return end_offset + (findMatchingRBrace (tree , token ) orelse token );
683
+ }
699
684
},
700
685
.array_init_dot_two ,
701
686
.builtin_call_two ,
@@ -726,8 +711,10 @@ pub fn lastToken(tree: Ast, node: Ast.Node.Index) Ast.TokenIndex {
726
711
},
727
712
.array_init_dot_two_comma ,
728
713
.builtin_call_two_comma ,
714
+ .block_two_semicolon ,
729
715
.struct_init_dot_two_comma ,
730
716
.tagged_union_two_trailing ,
717
+ .container_decl_two_trailing ,
731
718
= > {
732
719
end_offset += 2 ; // for the comma/semicolon + rbrace/rparen
733
720
if (datas [n ].rhs != 0 ) {
0 commit comments