Made Source a little more concise.

This commit is contained in:
Bruce Hill 2018-04-19 17:38:31 -07:00
parent ecb4341bdc
commit 310712385b
2 changed files with 13 additions and 8 deletions

View File

@ -19,9 +19,9 @@ Source = immutable({
end, end,
__tostring = function(self) __tostring = function(self)
if self.stop then if self.stop then
return "Source(\"" .. tostring(self.filename) .. "\", " .. tostring(self.start) .. ", " .. tostring(self.stop) .. ")" return "\"" .. tostring(self.filename) .. "[" .. tostring(self.start) .. ":" .. tostring(self.stop) .. "]\""
else else
return "Source(\"" .. tostring(self.filename) .. "\", " .. tostring(self.start) .. ")" return "\"" .. tostring(self.filename) .. "[" .. tostring(self.start) .. "]\""
end end
end, end,
__lt = function(self, other) __lt = function(self, other)
@ -148,11 +148,14 @@ do
... ...
} }
if type(self.source) == 'string' then if type(self.source) == 'string' then
local filename, start, stop = self.source:match("^(.-)[(%d+):(%d+)]$") local filename, start, stop = self.source:match("^(.-)%[(%d+):(%d+)%]$")
if not (filename) then
filename, start = self.source:match("^(.-)%[(%d+)%]$")
end
if start or stop then if start or stop then
self.source = Source(filename, tonumber(start), tonumber(stop)) self.source = Source(filename, tonumber(start), tonumber(stop))
else else
self.source = Source(self.source, 1, #self) self.source = Source(self.source, 1, #self + 1)
end end
end end
end, end,

View File

@ -10,9 +10,9 @@ Source = immutable {"filename","start","stop"}, {
return filename, start, stop return filename, start, stop
__tostring: => __tostring: =>
if @stop if @stop
"Source(\"#{@filename}\", #{@start}, #{@stop})" "\"#{@filename}[#{@start}:#{@stop}]\""
else else
"Source(\"#{@filename}\", #{@start})" "\"#{@filename}[#{@start}]\""
__lt: (other)=> __lt: (other)=>
assert(@filename == other.filename, "Cannot compare sources from different files") assert(@filename == other.filename, "Cannot compare sources from different files")
return if @start == other.start return if @start == other.start
@ -62,11 +62,13 @@ class Code
new: (@source, ...)=> new: (@source, ...)=>
@bits = {...} @bits = {...}
if type(@source) == 'string' if type(@source) == 'string'
filename,start,stop = @source\match("^(.-)[(%d+):(%d+)]$") filename,start,stop = @source\match("^(.-)%[(%d+):(%d+)%]$")
unless filename
filename,start = @source\match("^(.-)%[(%d+)%]$")
if start or stop if start or stop
@source = Source(filename, tonumber(start), tonumber(stop)) @source = Source(filename, tonumber(start), tonumber(stop))
else else
@source = Source(@source, 1, #self) @source = Source(@source, 1, #self+1)
clone: => clone: =>
cls = @__class cls = @__class