@@ -185,13 +185,11 @@ pub fn sourceIndexToTokenIndex(tree: Ast, source_index: usize) Ast.TokenIndex {
185
185
}
186
186
187
187
test sourceIndexToTokenIndex {
188
- var tree = try std .zig .Ast .parse (std .testing .allocator , "🠁↉¶a " , .zig );
188
+ var tree = try std .zig .Ast .parse (std .testing .allocator , "🠁↉¶\n a " , .zig );
189
189
defer tree .deinit (std .testing .allocator );
190
190
191
191
try std .testing .expectEqualSlices (std .zig .Token .Tag , &.{
192
- .invalid , // 🠁
193
- .invalid , // ↉
194
- .invalid , // ¶
192
+ .invalid , // 🠁↉¶
195
193
.identifier , // a
196
194
.eof ,
197
195
}, tree .tokens .items (.tag ));
@@ -203,18 +201,22 @@ test sourceIndexToTokenIndex {
203
201
try std .testing .expectEqual (0 , sourceIndexToTokenIndex (tree , 3 ));
204
202
205
203
// ↉
206
- try std .testing .expectEqual (1 , sourceIndexToTokenIndex (tree , 4 ));
207
- try std .testing .expectEqual (1 , sourceIndexToTokenIndex (tree , 5 ));
208
- try std .testing .expectEqual (1 , sourceIndexToTokenIndex (tree , 6 ));
204
+ try std .testing .expectEqual (0 , sourceIndexToTokenIndex (tree , 4 ));
205
+ try std .testing .expectEqual (0 , sourceIndexToTokenIndex (tree , 5 ));
206
+ try std .testing .expectEqual (0 , sourceIndexToTokenIndex (tree , 6 ));
209
207
210
208
// ¶
211
- try std .testing .expectEqual (2 , sourceIndexToTokenIndex (tree , 7 ));
212
- try std .testing .expectEqual (2 , sourceIndexToTokenIndex (tree , 8 ));
209
+ try std .testing .expectEqual (0 , sourceIndexToTokenIndex (tree , 7 ));
210
+ try std .testing .expectEqual (0 , sourceIndexToTokenIndex (tree , 8 ));
211
+
212
+ // \n
213
+ try std .testing .expectEqual (0 , sourceIndexToTokenIndex (tree , 9 ));
213
214
214
215
// a
215
- try std .testing .expectEqual (3 , sourceIndexToTokenIndex (tree , 9 ));
216
+ try std .testing .expectEqual (1 , sourceIndexToTokenIndex (tree , 10 ));
216
217
217
- try std .testing .expectEqual (4 , sourceIndexToTokenIndex (tree , 10 ));
218
+ // EOF
219
+ try std .testing .expectEqual (2 , sourceIndexToTokenIndex (tree , 11 ));
218
220
}
219
221
220
222
fn identifierIndexToLoc (tree : Ast , source_index : usize ) Loc {
@@ -295,22 +297,18 @@ pub fn tokenToIndex(tree: Ast, token_index: Ast.TokenIndex) usize {
295
297
}
296
298
297
299
test tokenToIndex {
298
- var tree = try std .zig .Ast .parse (std .testing .allocator , "🠁↉¶a " , .zig );
300
+ var tree = try std .zig .Ast .parse (std .testing .allocator , "🠁↉¶\n a " , .zig );
299
301
defer tree .deinit (std .testing .allocator );
300
302
301
303
try std .testing .expectEqualSlices (std .zig .Token .Tag , &.{
302
- .invalid , // 🠁
303
- .invalid , // ↉
304
- .invalid , // ¶
304
+ .invalid , // 🠁↉¶
305
305
.identifier , // a
306
306
.eof ,
307
307
}, tree .tokens .items (.tag ));
308
308
309
- try std .testing .expectEqual (0 , tokenToIndex (tree , 0 )); // 🠁
310
- try std .testing .expectEqual (4 , tokenToIndex (tree , 1 )); // ↉
311
- try std .testing .expectEqual (7 , tokenToIndex (tree , 2 )); // ¶
312
- try std .testing .expectEqual (9 , tokenToIndex (tree , 3 )); // a
313
- try std .testing .expectEqual (10 , tokenToIndex (tree , 4 )); // a
309
+ try std .testing .expectEqual (0 , tokenToIndex (tree , 0 )); // 🠁↉¶
310
+ try std .testing .expectEqual (10 , tokenToIndex (tree , 1 )); // a
311
+ try std .testing .expectEqual (11 , tokenToIndex (tree , 2 )); // EOF
314
312
}
315
313
316
314
pub fn tokensToLoc (tree : Ast , first_token : Ast.TokenIndex , last_token : Ast.TokenIndex ) Loc {
0 commit comments