@@ -1184,14 +1184,7 @@ pub fn lastToken(tree: Ast, node: Node.Index) TokenIndex {
1184
1184
n = extra .sentinel ;
1185
1185
},
1186
1186
1187
- .@"continue" = > {
1188
- if (datas [n ].lhs != 0 ) {
1189
- return datas [n ].lhs + end_offset ;
1190
- } else {
1191
- return main_tokens [n ] + end_offset ;
1192
- }
1193
- },
1194
- .@"break" = > {
1187
+ .@"continue" , .@"break" = > {
1195
1188
if (datas [n ].rhs != 0 ) {
1196
1189
n = datas [n ].rhs ;
1197
1190
} else if (datas [n ].lhs != 0 ) {
@@ -1895,6 +1888,25 @@ pub fn taggedUnionEnumTag(tree: Ast, node: Node.Index) full.ContainerDecl {
1895
1888
});
1896
1889
}
1897
1890
1891
+ pub fn switchFull (tree : Ast , node : Node.Index ) full.Switch {
1892
+ const data = & tree .nodes .items (.data )[node ];
1893
+ const main_token = tree .nodes .items (.main_token )[node ];
1894
+ const switch_token : TokenIndex , const label_token : ? TokenIndex = switch (tree .tokens .items (.tag )[main_token ]) {
1895
+ .identifier = > .{ main_token + 2 , main_token },
1896
+ .keyword_switch = > .{ main_token , null },
1897
+ else = > unreachable ,
1898
+ };
1899
+ const extra = tree .extraData (data .rhs , Ast .Node .SubRange );
1900
+ return .{
1901
+ .ast = .{
1902
+ .switch_token = switch_token ,
1903
+ .condition = data .lhs ,
1904
+ .cases = tree .extra_data [extra .start .. extra .end ],
1905
+ },
1906
+ .label_token = label_token ,
1907
+ };
1908
+ }
1909
+
1898
1910
pub fn switchCaseOne (tree : Ast , node : Node.Index ) full.SwitchCase {
1899
1911
const data = & tree .nodes .items (.data )[node ];
1900
1912
const values : * [1 ]Node.Index = & data .lhs ;
@@ -2206,6 +2218,21 @@ fn fullContainerDeclComponents(tree: Ast, info: full.ContainerDecl.Components) f
2206
2218
return result ;
2207
2219
}
2208
2220
2221
+ fn fullSwitchComponents (tree : Ast , info : full.Switch.Components ) full.Switch {
2222
+ const token_tags = tree .tokens .items (.tag );
2223
+ const tok_i = info .switch_token - | 1 ;
2224
+ var result : full.Switch = .{
2225
+ .ast = info ,
2226
+ .label_token = null ,
2227
+ };
2228
+ if (token_tags [tok_i ] == .colon and
2229
+ token_tags [tok_i - | 1 ] == .identifier )
2230
+ {
2231
+ result .label_token = tok_i - 1 ;
2232
+ }
2233
+ return result ;
2234
+ }
2235
+
2209
2236
fn fullSwitchCaseComponents (tree : Ast , info : full.SwitchCase.Components , node : Node.Index ) full.SwitchCase {
2210
2237
const token_tags = tree .tokens .items (.tag );
2211
2238
const node_tags = tree .nodes .items (.tag );
@@ -2477,6 +2504,13 @@ pub fn fullContainerDecl(tree: Ast, buffer: *[2]Ast.Node.Index, node: Node.Index
2477
2504
};
2478
2505
}
2479
2506
2507
+ pub fn fullSwitch (tree : Ast , node : Node.Index ) ? full.Switch {
2508
+ return switch (tree .nodes .items (.tag )[node ]) {
2509
+ .@"switch" , .switch_comma = > tree .switchFull (node ),
2510
+ else = > null ,
2511
+ };
2512
+ }
2513
+
2480
2514
pub fn fullSwitchCase (tree : Ast , node : Node.Index ) ? full.SwitchCase {
2481
2515
return switch (tree .nodes .items (.tag )[node ]) {
2482
2516
.switch_case_one , .switch_case_inline_one = > tree .switchCaseOne (node ),
@@ -2829,6 +2863,17 @@ pub const full = struct {
2829
2863
};
2830
2864
};
2831
2865
2866
+ pub const Switch = struct {
2867
+ ast : Components ,
2868
+ label_token : ? TokenIndex ,
2869
+
2870
+ pub const Components = struct {
2871
+ switch_token : TokenIndex ,
2872
+ condition : Node.Index ,
2873
+ cases : []const Node.Index ,
2874
+ };
2875
+ };
2876
+
2832
2877
pub const SwitchCase = struct {
2833
2878
inline_token : ? TokenIndex ,
2834
2879
/// Points to the first token after the `|`. Will either be an identifier or
@@ -3243,6 +3288,7 @@ pub const Node = struct {
3243
3288
/// main_token is the `(`.
3244
3289
async_call_comma ,
3245
3290
/// `switch(lhs) {}`. `SubRange[rhs]`.
3291
+ /// `main_token` is the identifier of a preceding label, if any; otherwise `switch`.
3246
3292
@"switch" ,
3247
3293
/// Same as switch except there is known to be a trailing comma
3248
3294
/// before the final rbrace
@@ -3287,7 +3333,8 @@ pub const Node = struct {
3287
3333
@"suspend" ,
3288
3334
/// `resume lhs`. rhs is unused.
3289
3335
@"resume" ,
3290
- /// `continue`. lhs is token index of label if any. rhs is unused.
3336
+ /// `continue :lhs rhs`
3337
+ /// both lhs and rhs may be omitted.
3291
3338
@"continue" ,
3292
3339
/// `break :lhs rhs`
3293
3340
/// both lhs and rhs may be omitted.
0 commit comments