Skip to content

Commit

Permalink
Remove unused code
Browse files Browse the repository at this point in the history
RIP, beautiful and overflow-aware binary search. This code became dead
after we switched from lpeglabel to recursive descent.
  • Loading branch information
hugomg committed Jun 10, 2023
1 parent cdcd017 commit de93e46
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 104 deletions.
51 changes: 0 additions & 51 deletions spec/location_spec.lua

This file was deleted.

53 changes: 0 additions & 53 deletions src/pallene/Location.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,6 @@

local util = require "pallene.util"

-- @param xs An ordered sequence of comparable items
-- @param v A value comparable to the items in the list
-- @return The position of the first occurrence of `v` in the sequence or the
-- position of the first item greater than `v` in the sequence, otherwise.
-- Inserting `v` at the returned position will always keep the sequence ordered.
local function binary_search(xs, v)
-- Invariants:
-- 1 <= lo <= hi <= #xs + 1
-- xs[i] < v , if i < lo
-- xs[i] >= v, if i >= hi
local lo = 1
local hi = #xs + 1
while lo < hi do
-- Average, rounded down (lo <= mid < hi)
-- Lua's logical right shift works here even if the addition overflows
local mid = (lo + hi) >> 1
if xs[mid] < v then
lo = mid + 1
else
hi = mid
end
end
return lo
end

local newline_cache = setmetatable({}, { __mode = "k" })

-- Converts an Lpeg file position into more familiar line and column numbers.
--
-- @param subject The contents of an entire source code file
-- @param pos A position in this file, as an absolute integer index
-- @return The line and column number at the specified position.
local function get_line_number(subject, pos)
local new_lines
if newline_cache[subject] then
new_lines = newline_cache[subject]
else
new_lines = {}
for n in subject:gmatch("()\n") do
table.insert(new_lines, n)
end
newline_cache[subject] = new_lines
end
local line = binary_search(new_lines, pos)
local col = pos - (new_lines[line - 1] or 0)
return line, col
end

--
-- A datattype representing a point in a source code file
--
Expand All @@ -65,11 +17,6 @@ function Location:init(file_name, line, col, pos)
self.pos = pos
end

function Location.from_pos(file_name, source, pos) -- alternate constructor
local line, col = get_line_number(source, pos)
return Location.new(file_name, line, col, pos)
end

function Location:show_line()
return string.format("%s:%d", self.file_name, self.line)
end
Expand Down

0 comments on commit de93e46

Please sign in to comment.