tokenLocation
Function parameters
Parameters
Index into `tokens`, or null.
Types
- OptionalTokenIndex
- Index into `tokens`, or null.
- TokenOffset
- A relative token index.
- OptionalTokenOffset
- A relative token index, or null.
- full
- Fully assembled AST node information.
- ExtraIndex
- Index into `extra_data`.
Functions in this namespace
Functions
- parse
- Result should be freed with tree.deinit() when there are
- renderAlloc
- `gpa` is used for allocating the resulting formatted source code.
- errorOffset
- Returns an extra offset for column and byte offset of errors that
- legacyAsm
- To be deleted after 0.15.0 is tagged
Source
Implementation
pub fn tokenLocation(self: Ast, start_offset: ByteOffset, token_index: TokenIndex) Location {
var loc = Location{
.line = 0,
.column = 0,
.line_start = start_offset,
.line_end = self.source.len,
};
const token_start = self.tokenStart(token_index);
// Scan to by line until we go past the token start
while (std.mem.indexOfScalarPos(u8, self.source, loc.line_start, '\n')) |i| {
if (i >= token_start) {
break; // Went past
}
loc.line += 1;
loc.line_start = i + 1;
}
const offset = loc.line_start;
for (self.source[offset..], 0..) |c, i| {
if (i + offset == token_start) {
loc.line_end = i + offset;
while (loc.line_end < self.source.len and self.source[loc.line_end] != '\n') {
loc.line_end += 1;
}
return loc;
}
if (c == '\n') {
loc.line += 1;
loc.column = 0;
loc.line_start = i + 1;
} else {
loc.column += 1;
}
}
return loc;
}