-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.lua
45 lines (37 loc) · 882 Bytes
/
source.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
---@class Source
---@field text string
---@field fname string
local Source = class("Source")
function Source:initialize(str, fname)
-- Fix line endings
str = str:gsub("\r\n", "\n"):gsub("\r", "\n")
if str:sub(#str, #str) ~= "\n" then
str = str .. "\n"
end
self.text = str
self.fname = fname
end
function Source:copy()
local source = Source(self.text, self.fname)
return source
end
function Source:getLine(line)
local text = self.text
while line > 1 do
line = line - 1
local npos = text:find("\n")
if not npos then return end
text = text:sub(npos + 1)
end
local npos = text:find("\n")
if npos then
return text:sub(1, npos)
else
return text
end
end
function Source:__tostring()
if self.fname then return self.fname end
return "stdin"
end
return Source