2020-05-07 11:27:37 +02:00
|
|
|
local common = require "core.common"
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-05-07 11:27:37 +02:00
|
|
|
local syntax = {}
|
2019-12-28 12:16:32 +01:00
|
|
|
syntax.items = {}
|
|
|
|
|
2021-12-02 22:34:49 +01:00
|
|
|
local plain_text_syntax = { name = "Plain Text", patterns = {}, symbols = {} }
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
function syntax.add(t)
|
2022-03-29 02:51:09 +02:00
|
|
|
-- the rule %s+ gives us a performance gain for the tokenizer in lines with
|
|
|
|
-- long amounts of consecutive spaces, to not affect other patterns we
|
|
|
|
-- insert it after any rule that starts with spaces to prevent conflicts
|
2022-03-25 16:25:32 +01:00
|
|
|
if t.patterns then
|
2022-03-29 02:51:09 +02:00
|
|
|
local temp_patterns = {}
|
|
|
|
::pattern_remove_loop::
|
|
|
|
for pos, pattern in ipairs(t.patterns) do
|
|
|
|
local pattern_str = ""
|
|
|
|
local ptype = pattern.pattern
|
|
|
|
and "pattern" or (pattern.regex and "regex" or nil)
|
|
|
|
if ptype then
|
|
|
|
if type(pattern[ptype]) == "table" then
|
|
|
|
pattern_str = pattern[ptype][1]
|
|
|
|
else
|
|
|
|
pattern_str = pattern[ptype]
|
|
|
|
end
|
|
|
|
if (ptype == "pattern" and(
|
|
|
|
pattern_str:find("^%^?%%s")
|
|
|
|
or
|
|
|
|
pattern_str:find("^%^?%s")
|
|
|
|
))
|
|
|
|
or
|
|
|
|
(ptype == "regex" and (
|
|
|
|
pattern_str:find("^%^?\\s")
|
|
|
|
or
|
|
|
|
pattern_str:find("^%^?%s")
|
|
|
|
))
|
|
|
|
then
|
|
|
|
table.insert(temp_patterns, table.remove(t.patterns, pos))
|
|
|
|
-- since we are removing from iterated table we need to start
|
|
|
|
-- from the beginning again to prevent any issues
|
|
|
|
goto pattern_remove_loop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for pos, pattern in ipairs(temp_patterns) do
|
|
|
|
table.insert(t.patterns, pos, pattern)
|
|
|
|
end
|
|
|
|
local pos = 1
|
|
|
|
if #temp_patterns > 0 then pos = #temp_patterns+1 end
|
|
|
|
table.insert(t.patterns, pos, { pattern = "%s+", type = "normal" })
|
2022-03-25 16:25:32 +01:00
|
|
|
end
|
2022-03-29 02:51:09 +02:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
table.insert(syntax.items, t)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-06-08 10:44:51 +02:00
|
|
|
local function find(string, field)
|
2019-12-28 12:16:32 +01:00
|
|
|
for i = #syntax.items, 1, -1 do
|
|
|
|
local t = syntax.items[i]
|
2020-06-08 10:44:51 +02:00
|
|
|
if common.match_pattern(string, t[field] or {}) then
|
2019-12-28 12:16:32 +01:00
|
|
|
return t
|
|
|
|
end
|
|
|
|
end
|
2020-06-08 10:44:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function syntax.get(filename, header)
|
|
|
|
return find(filename, "files")
|
2021-10-25 14:06:07 +02:00
|
|
|
or (header and find(header, "headers"))
|
2020-06-08 10:44:51 +02:00
|
|
|
or plain_text_syntax
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return syntax
|