diff options
author | 2021-04-22 12:38:51 -0700 | |
---|---|---|
committer | 2021-04-22 12:38:51 -0700 | |
commit | bee68ecc004864d1a1bbdfb0d49acf42ffe1f0d3 (patch) | |
tree | a64dfab8c4fbf304e1fad8a3de34ee4ffa4763c6 /src/logger.zig | |
parent | ba472faea1a5ed8943916c5a602b415f3242f5c9 (diff) | |
download | bun-bee68ecc004864d1a1bbdfb0d49acf42ffe1f0d3.tar.gz bun-bee68ecc004864d1a1bbdfb0d49acf42ffe1f0d3.tar.zst bun-bee68ecc004864d1a1bbdfb0d49acf42ffe1f0d3.zip |
wip
Diffstat (limited to 'src/logger.zig')
-rw-r--r-- | src/logger.zig | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/logger.zig b/src/logger.zig index 492761f8d..ccc922cbe 100644 --- a/src/logger.zig +++ b/src/logger.zig @@ -28,10 +28,16 @@ pub const Kind = enum { pub const Loc = packed struct { start: i32 = -1, + // TODO: remove this stupidity pub fn toUsize(self: *Loc) usize { return @intCast(usize, self.start); } + // TODO: remove this stupidity + pub fn i(self: *const Loc) usize { + return @intCast(usize, self.start); + } + pub const Empty = Loc{ .start = -1 }; pub fn eql(loc: *Loc, other: Loc) bool { @@ -226,7 +232,31 @@ pub const Source = struct { } pub fn textForRange(self: *Source, r: Range) string { - return self.contents[std.math.lossyCast(usize, r.loc.start)..r.endI()]; + return self.contents[r.loc.i()..r.endI()]; + } + + pub fn rangeOfOperatorBefore(self: *Source, loc: Loc, op: string) Range { + const text = self.contents[0..loc.i()]; + const index = strings.index(text, op); + if (index >= 0) { + return Range{ .loc = Loc{ + .start = loc.start + index, + }, .len = @intCast(i32, op.len) }; + } + + return Range{ .loc = loc }; + } + + pub fn rangeOfOperatorAfter(self: *Source, loc: Loc, op: string) Range { + const text = self.contents[loc.i()..]; + const index = strings.index(text, op); + if (index >= 0) { + return Range{ .loc = Loc{ + .start = loc.start + index, + }, .len = op.len }; + } + + return Range{ .loc = loc }; } pub fn initErrorPosition(self: *const Source, _offset: Loc) ErrorPosition { |