Skip to content

Commit

Permalink
Parser: add basic test for node locations
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas committed Aug 13, 2024
1 parent 3474f38 commit 2ceb424
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/aro/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,17 @@ pub fn hasBuiltinFunction(comp: *const Compilation, builtin: Builtin) bool {
}
}

pub fn locSlice(comp: *const Compilation, loc: Source.Location) []const u8 {
var tmp_tokenizer = Tokenizer{
.buf = comp.getSource(loc.id).buf,
.langopts = comp.langopts,
.index = loc.byte_offset,
.source = .generated,
};
const tok = tmp_tokenizer.next();
return tmp_tokenizer.buf[tok.start..tok.end];
}

pub const CharUnitSize = enum(u32) {
@"1" = 1,
@"2" = 2,
Expand Down
39 changes: 39 additions & 0 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8856,3 +8856,42 @@ fn genericSelection(p: *Parser) Error!Result {
chosen.node = try p.addNode(generic_node);
return chosen;
}

test "Node locations" {
var comp = Compilation.init(std.testing.allocator, std.fs.cwd());
defer comp.deinit();

const file = try comp.addSourceFromBuffer("file.c",
\\int foo = 5;
\\int bar = 10;
\\int main(void) {}
\\
);

const builtin_macros = try comp.generateBuiltinMacros(.no_system_defines);

var pp = Preprocessor.init(&comp);
defer pp.deinit();
try pp.addBuiltinMacros();

_ = try pp.preprocess(builtin_macros);

const eof = try pp.preprocess(file);
try pp.addToken(eof);

var tree = try Parser.parse(&pp);
defer tree.deinit();

try std.testing.expectEqual(0, comp.diagnostics.list.items.len);
for (tree.root_decls, 0..) |node, i| {
const loc = tree.nodeLoc(node).?;
const slice = tree.comp.locSlice(loc);
const expected = switch (i) {
0 => "foo",
1 => "bar",
2 => "main",
else => unreachable,
};
try std.testing.expectEqualStrings(expected, slice);
}
}
16 changes: 9 additions & 7 deletions src/aro/Tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -783,14 +783,16 @@ pub fn childNodes(tree: *const Tree, node: NodeIndex) []const NodeIndex {
pub fn tokSlice(tree: *const Tree, tok_i: TokenIndex) []const u8 {
if (tree.tokens.items(.id)[tok_i].lexeme()) |some| return some;
const loc = tree.tokens.items(.loc)[tok_i];
var tmp_tokenizer = Tokenizer{
.buf = tree.comp.getSource(loc.id).buf,
.langopts = tree.comp.langopts,
.index = loc.byte_offset,
.source = .generated,
return tree.comp.locSlice(loc);
}

pub fn nodeLoc(tree: *const Tree, node: NodeIndex) ?Source.Location {
std.debug.assert(node != .none);
const loc = tree.nodes.items(.loc)[@intFromEnum(node)];
return switch (loc) {
.none => null,
else => |tok_i| tree.tokens.items(.loc)[@intFromEnum(tok_i)],
};
const tok = tmp_tokenizer.next();
return tmp_tokenizer.buf[tok.start..tok.end];
}

pub fn dump(tree: *const Tree, config: std.io.tty.Config, writer: anytype) !void {
Expand Down

0 comments on commit 2ceb424

Please sign in to comment.