Skip to content

Commit

Permalink
some shit just flatout doesn't work (linestart and filestart)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaboards-dev committed Aug 2, 2023
1 parent 2cb2c47 commit af74760
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 12 deletions.
9 changes: 7 additions & 2 deletions libpreproc/init.lua
Expand Up @@ -34,10 +34,12 @@ function lpp.prefix(prefix, match)
return function(str, inst, offset)
offset = offset or 0
local positions = table.pack(prefix:find(str))
--print(prefix.pattern, table.unpack(positions))
if not positions[1] then return end
local st, en = table.remove(positions, 1), table.remove(positions, 1)
local result = match(str, inst, en+1, en+1)
if not result or result.start ~= en+1 then return end
--print(prefix.pattern, result)
if not result or (result.start ~= en+1 and result.size ~= 0 and en ~= 0) then return end
for i=1, #result.matches do
table.insert(positions, result.matches[i])
end
Expand All @@ -56,7 +58,7 @@ function lpp.linestart(match)
offset = offset or 0
local result = match(str, inst, offset)
if not result then return end
local dat = str:peek(result.start)
local dat = str:peek(offset-1)
if dat:sub(#dat, #dat) == "\n" then
return result
end
Expand Down Expand Up @@ -117,10 +119,13 @@ end
function lpp.inner_prefix(prefix, match)
prefix = lpp.pattern(prefix, true)
return function(str, inst, offset)
offset = offset or 0
local result = match(str, inst, offset)
--print("inner", prefix.pattern, result)
if not result then return end
local inner = table.remove(result.matches)
local matches = table.pack(inner:find(prefix.pattern, 1, prefix.raw))
--print("inner", prefix.pattern, table.unpack(matches))
if not matches[1] then return end
local st, en = table.remove(matches, 1), table.remove(matches, 1)
if en == 0 then
Expand Down
23 changes: 15 additions & 8 deletions libpreproc/parser.lua
Expand Up @@ -27,7 +27,8 @@ function parser:compile(code, name)
if result then
first_match = first_match or result
processor = processor or self.tokens[i].process
if first_match.start < result.start then
if first_match.start > result.start then
print(string.format("%d: closer match (%d < %d)", i, result.start, first_match.start))
processor = self.tokens[i].process
first_match = result
end
Expand All @@ -45,13 +46,19 @@ function parser:compile(code, name)
self:emit(emitting)
end
self.code = table.concat(self.gencode, " ")
local env = setmetatable({write = function(s)
table.insert(self.output, s)
end}, {__index=function(_, i)
return self.export[i] or self.env[i] or _G[i]
end, __newindex=function(_, k, v)
self.export[k] = v
end})
local env = setmetatable({
_INSTANCE = self,
write = function(s)
table.insert(self.output, s)
end
}, {
__index=function(_, i)
return self.export[i] or self.env[i] or _G[i]
end, __newindex=function(_, k, v)
self.export[k] = v
end
})
--print(self.code)
self.gen = assert(load(self.code, (name and "="..name), "t", env))
self.name = name
return num_matches
Expand Down
4 changes: 2 additions & 2 deletions tests/lhp.lua
Expand Up @@ -6,7 +6,7 @@ local blk = lpp.block("<?", "?>")
local eq = lpp.pattern("(=?)")
local php = lpp.pattern("(php%s)")
local lua = lpp.pattern("lua(=?)%s")
local args = lpp.pattern("args ")
local args = lpp.pattern("args ", true)
local whitespace = lpp.pattern("%s*")
local php_match = lpp.inner_prefix(php, blk)
local args_badmatch = lpp.prefix(whitespace, lpp.inner_prefix(args, blk))
Expand Down Expand Up @@ -44,5 +44,5 @@ local dat = f:read("*a")
f:close()
table.remove(arg, 1)
parser:compile(dat)
--print(parser.code)
print(parser.code)
print(parser:generate(table.unpack(arg)))
2 changes: 2 additions & 0 deletions tests/lhp/args.lhp
@@ -0,0 +1,2 @@
<?args str ?>
Hello, <?= str ?>!
3 changes: 3 additions & 0 deletions tests/lhp/badargs.lhp
@@ -0,0 +1,3 @@
lol
<?args lmao ?>
test
36 changes: 36 additions & 0 deletions tests/luapreproc.lua
@@ -0,0 +1,36 @@
-- dry run luapreproc for including files together
local lpp = require("libpreproc")

local directive_prefix = lpp.pattern("--#", true)
local dblquotes = lpp.block('"', '"')
local include = (lpp.prefix(directive_prefix, lpp.prefix("include ", dblquotes)))

local parser = lpp.instance()

parser.env.directives = {}

local function readfile(file)
local f = assert(io.open(file, "r"))
local dat = f:read("*a")
f:close()
return dat
end

function parser.env.directives.include(inst, path)
local newinst = inst:clone()
local d = readfile(path)
newinst:compile(d)
local code = newinst:generate()
return code
end

parser:add_token(include, function(stream, instance, result)
instance:write(string.format("write(directives.include(_INSTANCE, %q))", result.matches[1]))
end)

local file = arg[1]

table.remove(arg, 1)
parser:compile(readfile(file))
print(parser.code)
print(parser:generate(table.unpack(arg)))
2 changes: 2 additions & 0 deletions tests/luapreproc/test1.lua
@@ -0,0 +1,2 @@
print("test lol")
print("test lmao even")
3 changes: 3 additions & 0 deletions tests/luapreproc/test2.lua
@@ -0,0 +1,3 @@
print("lol lmao")
--#include "tests/luapreproc/test1.lua"
print("shit here")

0 comments on commit af74760

Please sign in to comment.