1
0
mirror of https://github.com/Adorable-Catgirl/LuaComp.git synced 2024-11-23 02:18:06 +11:00

fixed escape related regression

This commit is contained in:
Sam Roxanne 2021-06-25 10:49:40 -05:00
parent ddd8de4d67
commit 42b2ee6676
6 changed files with 19 additions and 9 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
luacomp.lua luacomp.lua
luacomp luacomp
build/* build/*
*.out

View File

@ -87,7 +87,7 @@ do
function ast.parser_error(str, err) function ast.parser_error(str, err)
local y, x = str:get_yx() local y, x = str:get_yx()
--print(y, x) --print(y, x)
lc_error(@[{_GENERATOR.fname}], string.format("%s(%d:%d): %s\n", str.file, y or 0, x or 0, err)) lc_error("@[{_GENERATOR.fname}]", string.format("%s(%d:%d): %s\n", str.file, y or 0, x or 0, err))
end end
function ast.unescape(escaped_string) function ast.unescape(escaped_string)
@ -114,8 +114,9 @@ do
function ast.remove_escapes(escaped_string) function ast.remove_escapes(escaped_string)
local i = 1 local i = 1
local out_string = "" local out_string = ""
while i < #escaped_string do while i <= #escaped_string do
local c = escaped_string:sub(i,i) local c = escaped_string:sub(i,i)
--lc_warning(c, tostring(i).." "..#escaped_string)
if (c == "\\") then if (c == "\\") then
i = i + 1 i = i + 1
else else
@ -123,17 +124,19 @@ do
end end
i = i + 1 i = i + 1
end end
--lc_warning("debug", out_string)
return out_string return out_string
end end
function ast.back_escape_count(str, start) function ast.back_escape_count(str, start)
local i=1 local i=2
while str:peek(-i):sub(1,1) == "\\" do while str:peek(-i):sub(1,1) == "\\" do
i = i + 1 i = i + 1
if (str:tell()-i < start) then if (str:tell()-i < start) then
ast.error(str, "internal error") ast.error(str, "internal error")
end end
end end
--lc_warning(tostring(i), #str:peek(1-i).." "..str:peek(1-i))
return str:peek(1-i) return str:peek(1-i)
end end
@ -152,7 +155,7 @@ do
end end
str:set(rpos) str:set(rpos)
if str:peek(-1) == "\\" then if str:peek(-1) == "\\" then
local parsed = ast.remove_escapes(ast.back_unescape(str)) local parsed = ast.remove_escapes(ast.back_escape_count(str, spos))
if parsed:sub(#parsed) == "\'" then if parsed:sub(#parsed) == "\'" then
goto found_end goto found_end
end end
@ -183,14 +186,16 @@ do
end end
end end
str:set(rpos) str:set(rpos)
if str:peek(-1) == "\\" then --lc_warning(str:peek(-2), "test")
local parsed = ast.remove_escapes(ast.back_unescape(str)) if str:peek(-2):sub(1,1) == "\\" then
local parsed = ast.remove_escapes(ast.back_escape_count(str, spos))
if parsed:sub(#parsed) == "\"" then if parsed:sub(#parsed) == "\"" then
goto found_end goto found_end
end end
else else
goto found_end goto found_end
end end
--str:set(rpos)
end end
::found_end:: ::found_end::
local epos = str:tell() local epos = str:tell()

View File

@ -46,9 +46,9 @@ do
fenv._G = fenv fenv._G = fenv
fenv._GENERATOR = env fenv._GENERATOR = env
function fenv.call_directive(dname, ...) function fenv.call_directive(dname, ...)
if not directives[dname] then lc_error(@[{_GENERATOR.fname}], "invalid directive "..dname) end if not directives[dname] then lc_error("@[{_GENERATOR.fname}]", "invalid directive "..dname) end
local r, er = directives[dname](env, ...) local r, er = directives[dname](env, ...)
if not r then lc_error(@[{_GENERATOR.fname}], er) end if not r then lc_error("@[{_GENERATOR.fname}]", er) end
end end
function fenv.write_out(code) function fenv.write_out(code)

1
tests/bad_include.lua Normal file
View File

@ -0,0 +1 @@
--#include "nonexistant"

2
tests/bad_string.lua Normal file
View File

@ -0,0 +1,2 @@
--#include "fuck
"

1
tests/bad_string_2.lua Normal file
View File

@ -0,0 +1 @@
--#include "wtf\"