lean2/src/builtin/template.lua
Leonardo de Moura 66178ae65a refactor(extra): move extra to builtin
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2013-12-28 22:06:11 -08:00

31 lines
1 KiB
Lua

-- Parse a template expression string 'str'.
-- The string 'str' may contain placeholders of the form '% i', where 'i' is a numeral.
-- The placeholders are replaced with values from the array 'arg_array'.
local arg_array = {}
-- We implement this feature using macros available in Lean.
macro("%",
{ macro_arg.Int },
function (env, i)
if i <= 0 then
error("invalid template argument reference %" .. i .. ", first argument is 1")
elseif i > #arg_array then
error("invalid template argument reference %" .. i .. ", only " .. tostring(#arg_array) .. " argument(s) were provided")
else
return arg_array[i]
end
end
)
-- Parse a template.
-- Example:
-- r = parse_template("%1 + %2 + %1", {Const("a"), Const("b")})
-- print(r)
-- >> a + b + a
function parse_template(str, args, env, opts, fmt)
assert(type(str) == "string")
assert(type(args) == "table")
assert(env == nil or is_environment(env))
arg_array = args
return parse_lean(str, env, opts, fmt)
end