-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.lua
More file actions
66 lines (55 loc) · 1.36 KB
/
compiler.lua
File metadata and controls
66 lines (55 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local PATTERN_FILE_PATH = "^(.-)([^\\/]-)%.([^\\/%.]-)%.?$"
local PATTERN_MACRO_TAG = "[^ ]+ +=%*= +lang: +([^ ]+) +=%*=[^\n]*(\n.*)"
local extensions = {}
local state = {
default = _COMPILER
}
function _COMPILER(src, namespace_name, script_name)
local mac = nil
if script_name then
local _,_,ext = script_name:match(PATTERN_FILE_PATH)
if extensions[ext] then
mac = extensions[ext]
end
end
local tag,rest = src:match(PATTERN_MACRO_TAG)
if tag ~= nil then
src = rest
mac = function_object(tag)
end
if mac == nil then
mac = state.default
end
local res, out = pcall(mac, src, namespace_name, script_name)
if not res then
print(out)
error(out)
end
return out
end
local function register_extension(k, v)
print(_PACKAGE .. ": registering script extension: " .. k)
_REGISTER_PATHS(
"?." .. k,
"?/init." .. k
)
extensions[k] = v
end
local function get_extensions()
local out = {}
for k in pairs(extensions) do
table.insert(out, k)
end
return out
end
local function set_default_macro(mac)
state.default = mac
end
return {
PATTERN_FILE_PATH = PATTERN_FILE_PATH,
PATTERN_MACRO_TAG = PATTERN_MACRO_TAG,
compile = _COMPILER,
register_extension = register_extension,
get_extensions = get_extensions,
set_default_macro = set_default_macro
}