Add helper function to report bad patterns in tokenizer

This commit is contained in:
Guldoman 2022-06-15 21:28:46 +02:00
parent 5027a0f12b
commit 2e37e85a48
No known key found for this signature in database
GPG Key ID: EA928C8BDA1A8825
1 changed files with 18 additions and 8 deletions

View File

@ -95,6 +95,16 @@ local function retrieve_syntax_state(incoming_syntax, state)
return current_syntax, subsyntax_info, current_pattern_idx, current_level return current_syntax, subsyntax_info, current_pattern_idx, current_level
end end
local function report_bad_pattern(log_fn, syntax, pattern_idx, msg, ...)
if not bad_patterns[syntax] then
bad_patterns[syntax] = { }
end
if bad_patterns[syntax][pattern_idx] then return end
bad_patterns[syntax][pattern_idx] = true
log_fn("Malformed pattern #%d in %s language plugin. " .. msg,
pattern_idx, syntax.name or "unnamed", ...)
end
---@param incoming_syntax table ---@param incoming_syntax table
---@param text string ---@param text string
---@param state integer ---@param state integer
@ -260,14 +270,14 @@ function tokenizer.tokenize(incoming_syntax, text, state)
for n, p in ipairs(current_syntax.patterns) do for n, p in ipairs(current_syntax.patterns) do
local find_results = { find_text(text, p, i, true, false) } local find_results = { find_text(text, p, i, true, false) }
if find_results[1] then if find_results[1] then
if #find_results - 1 > (type(p.type) == "table" and #p.type or 1) then local type_is_table = type(p.type) == "table"
if not bad_patterns[current_syntax] then local n_types = type_is_table and #p.type or 1
bad_patterns[current_syntax] = { } if #find_results - 1 > n_types then
end report_bad_pattern(core.error, current_syntax, n,
if not bad_patterns[current_syntax][n] then "Not enough token types: got %d needed %d.", n_types, #find_results - 1)
bad_patterns[current_syntax][n] = true elseif #find_results - 1 < n_types then
core.error("Malformed pattern #%d in %s language plugin", n, current_syntax.name or "unnamed") report_bad_pattern(core.warn, current_syntax, n,
end "Too many token types: got %d needed %d.", n_types, #find_results - 1)
end end
-- matched pattern; make and add tokens -- matched pattern; make and add tokens
push_tokens(res, current_syntax, p, text, find_results) push_tokens(res, current_syntax, p, text, find_results)