Skip to content

Commit

Permalink
Improve capture mechanism in examplewithoptionfinder.lua + fix unit t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
hansonchar committed Jun 18, 2024
1 parent 87d5bc2 commit 6bc302c
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions doc/generic/pgf/lib/examplewithoptionfinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,19 @@ if UNIT_TESTING then
package.path = pwd() .. "?.lua;" .. package.path
local luarocks_path = os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?.lua"
package.path = package.path .. ";" .. luarocks_path
print("package.path: " .. package.path)
-- print("package.path: " .. package.path)
-- local tostring = require "ml".tstring
end

local lpeg = require("lpeg")
local loc = lpeg.locale()
-- local u = require("utils")
local u = require("utils")
local str = require("stringmatcher")
local C, Ct, P, V = lpeg.C, lpeg.Ct, lpeg.P, lpeg.V

local t = {}
local SP = loc.space ^ 0

local result = {}
local function keyval(key, val)
result[key] = val
return result
end

-- local function sanitize(matches)
-- if type(matches) == "table" then
-- return matches
Expand All @@ -55,10 +49,12 @@ t.grammar =
example_begin = P "\n" ^ -1 * "example({" * SP,
example_end = "\n})" * SP,
-- content = C((1 - V "example_end") ^ 0),
content = Ct(V "options" ^ -1 * V "code"),
content = Ct(lpeg.Cf(Ct "" * V "options" ^ -1 * V "code", u.set)),
anything = (1 - V "example") ^ 0,
options = SP * C("options") * SP * "=" * str ^ -1 * (P ",") ^ -1 / keyval,
code = SP * C("code") * SP * "=" * str / keyval
options = SP * (V "optionskv") ^ -1 * (P ",") ^ -1,
optionskv = lpeg.Cg(C("options") * SP * "=" * str),
code = SP * V "codekv",
codekv = lpeg.Cg(C("code") * SP * "=" * str)
}

local function preamble(options)
Expand Down Expand Up @@ -88,8 +84,7 @@ if UNIT_TESTING then
local test_options = [[ preamble=\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{layered} ]]
local p = SP * P "preamble" * SP * "=" * SP * C(P(1) ^ 1)
local matches = p:match(test_options)
print("type(matches)", type(matches))
print("to_string(p:match(test_options)):", tostring(p:match(test_options)))
assert(matches == [[\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{layered} ]])
-- os.exit()

-- local tostring = require "ml".tstring
Expand Down Expand Up @@ -119,8 +114,24 @@ example({
-- local result = t.grammar:match(mini_test_case)
-- print("captured:", captured)
-- print("tostring(result): ", tostring(matches[1]))
print("t.get_options(matches):", tostring(t.get_options(matches)))
print("t.get_content(matches):", t.get_content(matches))
assert(#matches == 1)
local e = matches[1]
assert(tostring(t.get_options(e)) == [[{preamble="\\usetikzlibrary{graphs,graphdrawing} \\usegdlibrary{layered"}]])
-- print("t.get_content(matches):", t.get_content(e))
assert(
u.strip(t.get_content(e)) ==
u.strip(
[[
\begin{tikzpicture}
\draw [help lines] (0,0) grid (2,2);
\graph [layered layout, edges=rounded corners]
{ a -- {b, c [anchor here] } -- d -- a};
\end{tikzpicture
]]
)
)

-- print("captured.options:", captured.options)
-- print("captured.code:", captured.code)
-- print("result.options:", result.options)
Expand Down

0 comments on commit 6bc302c

Please sign in to comment.