From 0c7c06beabc5a2ef4f89923cc8cf59331e2c32cf Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 26 May 2018 15:58:32 -0700 Subject: Moving back to capturing tables in LPEG and everything is a Source, not string. --- code_obj.moon | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'code_obj.moon') diff --git a/code_obj.moon b/code_obj.moon index 3f9e37a..14bc74a 100644 --- a/code_obj.moon +++ b/code_obj.moon @@ -10,6 +10,11 @@ Source = immutable {"filename","start","stop"}, { start, stop = 1, #FILE_CACHE[filename] if stop then assert(start <= stop+1, "Invalid range: #{start}, #{stop}") return filename, start, stop + from_string: (str)=> + filename,start,stop = str\match("^(.-)%[(%d+):(%d+)%]$") + unless filename + filename,start = str\match("^(.-)%[(%d+)%]$") + return Source(filename or str, tonumber(start or 1), tonumber(stop)) __tostring: => if @stop "\"#{@filename}[#{@start}:#{@stop}]\"" @@ -65,21 +70,9 @@ class Code @bits, @indents, @current_indent = {}, {}, 0 @append(...) if type(@source) == 'string' - filename,start,stop = @source\match("^(.-)%[(%d+):(%d+)%]$") - unless filename - filename,start = @source\match("^(.-)%[(%d+)%]$") - if start or stop - @source = Source(filename, tonumber(start), tonumber(stop)) - else - @source = Source(@source, 1, #tostring(self)+1) + @source = Source\from_string(@source) assert(@source) - sub: (start,stop)=> - -- TODO: implement this better - str = tostring(self)\sub(start,stop) - cls = @__class - return cls(@source\sub(start,stop), str) - append: (...)=> n = select("#",...) bits, indents = @bits, @indents -- cgit v1.2.3