2022-05-31 02:03:42 +02:00
|
|
|
local core = require "core"
|
2021-05-01 11:45:30 +02:00
|
|
|
local syntax = require "core.syntax"
|
2023-04-01 18:12:39 +02:00
|
|
|
local config = require "core.config"
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-05-01 11:45:30 +02:00
|
|
|
local tokenizer = {}
|
2022-05-31 02:03:42 +02:00
|
|
|
local bad_patterns = {}
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local function push_token(t, type, text)
|
2022-05-31 02:03:42 +02:00
|
|
|
type = type or "normal"
|
2019-12-28 12:16:32 +01:00
|
|
|
local prev_type = t[#t-1]
|
|
|
|
local prev_text = t[#t]
|
2023-04-01 18:12:39 +02:00
|
|
|
if prev_type and (prev_type == type or (prev_text:ufind("^%s*$") and type ~= "incomplete")) then
|
2019-12-28 12:16:32 +01:00
|
|
|
t[#t-1] = type
|
|
|
|
t[#t] = prev_text .. text
|
|
|
|
else
|
|
|
|
table.insert(t, type)
|
|
|
|
table.insert(t, text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-05-19 22:35:28 +02:00
|
|
|
local function push_tokens(t, syn, pattern, full_text, find_results)
|
|
|
|
if #find_results > 2 then
|
|
|
|
-- We do some manipulation with find_results so that it's arranged
|
|
|
|
-- like this:
|
|
|
|
-- { start, end, i_1, i_2, i_3, …, i_last }
|
|
|
|
-- Each position spans characters from i_n to ((i_n+1) - 1), to form
|
|
|
|
-- consecutive spans of text.
|
|
|
|
--
|
|
|
|
-- If i_1 is not equal to start, start is automatically inserted at
|
|
|
|
-- that index.
|
|
|
|
if find_results[3] ~= find_results[1] then
|
|
|
|
table.insert(find_results, 3, find_results[1])
|
|
|
|
end
|
|
|
|
-- Copy the ending index to the end of the table, so that an ending index
|
|
|
|
-- always follows a starting index after position 3 in the table.
|
|
|
|
table.insert(find_results, find_results[2] + 1)
|
|
|
|
-- Then, we just iterate over our modified table.
|
|
|
|
for i = 3, #find_results - 1 do
|
|
|
|
local start = find_results[i]
|
|
|
|
local fin = find_results[i + 1] - 1
|
|
|
|
local type = pattern.type[i - 2]
|
|
|
|
-- ↑ (i - 2) to convert from [3; n] to [1; n]
|
2022-04-26 15:42:02 +02:00
|
|
|
local text = full_text:usub(start, fin)
|
2021-05-19 22:35:28 +02:00
|
|
|
push_token(t, syn.symbols[text] or type, text)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local start, fin = find_results[1], find_results[2]
|
2022-04-26 15:42:02 +02:00
|
|
|
local text = full_text:usub(start, fin)
|
2021-05-19 22:35:28 +02:00
|
|
|
push_token(t, syn.symbols[text] or pattern.type, text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 23:56:20 +01:00
|
|
|
-- State is a string of bytes, where the count of bytes represents the depth
|
|
|
|
-- of the subsyntax we are currently in. Each individual byte represents the
|
|
|
|
-- index of the pattern for the current subsyntax in relation to its parent
|
|
|
|
-- syntax. Using a string of bytes allows us to have as many subsyntaxes as
|
|
|
|
-- bytes can be stored on a string while keeping some level of performance in
|
|
|
|
-- comparison to a Lua table. The only limitation is that a syntax would not
|
|
|
|
-- be able to contain more than 255 patterns.
|
|
|
|
--
|
|
|
|
-- Lets say a state contains 2 bytes byte #1 with value `3` and byte #2 with
|
|
|
|
-- a value of `5`. This would mean that on the parent syntax at index `3` a
|
|
|
|
-- pattern subsyntax that matched current text was found, then inside that
|
|
|
|
-- subsyntax another subsyntax pattern at index `5` that matched current text
|
|
|
|
-- was also found.
|
|
|
|
|
|
|
|
-- Calling `push_subsyntax` appends the current subsyntax pattern index to the
|
|
|
|
-- state and increases the stack depth. Calling `pop_subsyntax` clears the
|
|
|
|
-- last appended subsyntax and decreases the stack.
|
2021-05-19 22:35:28 +02:00
|
|
|
|
2021-05-01 11:45:30 +02:00
|
|
|
local function retrieve_syntax_state(incoming_syntax, state)
|
2021-05-20 21:58:27 +02:00
|
|
|
local current_syntax, subsyntax_info, current_pattern_idx, current_level =
|
2022-11-03 23:56:20 +01:00
|
|
|
incoming_syntax, nil, state:byte(1) or 0, 1
|
|
|
|
if
|
|
|
|
current_pattern_idx > 0
|
|
|
|
and
|
|
|
|
current_syntax.patterns[current_pattern_idx]
|
|
|
|
then
|
|
|
|
-- If the state is not empty we iterate over each byte, and find which
|
2021-05-18 17:52:18 +02:00
|
|
|
-- syntax we're using. Rather than walking the bytes, and calling into
|
2021-05-01 11:45:30 +02:00
|
|
|
-- `syntax` each time, we could probably cache this in a single table.
|
2022-11-03 23:56:20 +01:00
|
|
|
for i = 1, #state do
|
|
|
|
local target = state:byte(i)
|
2021-05-01 11:45:30 +02:00
|
|
|
if target ~= 0 then
|
|
|
|
if current_syntax.patterns[target].syntax then
|
|
|
|
subsyntax_info = current_syntax.patterns[target]
|
2021-05-18 17:52:18 +02:00
|
|
|
current_syntax = type(subsyntax_info.syntax) == "table" and
|
2021-05-01 11:45:30 +02:00
|
|
|
subsyntax_info.syntax or syntax.get(subsyntax_info.syntax)
|
2021-05-20 21:58:27 +02:00
|
|
|
current_pattern_idx = 0
|
2021-05-01 11:45:30 +02:00
|
|
|
current_level = i+1
|
|
|
|
else
|
2021-05-20 21:58:27 +02:00
|
|
|
current_pattern_idx = target
|
2021-05-01 11:45:30 +02:00
|
|
|
break
|
|
|
|
end
|
2021-05-18 17:52:18 +02:00
|
|
|
else
|
2021-05-01 11:45:30 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-05-20 21:58:27 +02:00
|
|
|
return current_syntax, subsyntax_info, current_pattern_idx, current_level
|
2021-05-01 11:45:30 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-11-14 14:08:33 +01:00
|
|
|
---Return the list of syntaxes used in the specified state.
|
|
|
|
---@param base_syntax table @The initial base syntax (the syntax of the file)
|
|
|
|
---@param state string @The state of the tokenizer to extract from
|
|
|
|
---@return table @Array of syntaxes starting from the innermost one
|
|
|
|
function tokenizer.extract_subsyntaxes(base_syntax, state)
|
|
|
|
local current_syntax
|
|
|
|
local t = {}
|
|
|
|
repeat
|
|
|
|
current_syntax = retrieve_syntax_state(base_syntax, state)
|
|
|
|
table.insert(t, current_syntax)
|
|
|
|
state = string.sub(state, 2)
|
|
|
|
until #state == 0
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2022-06-15 21:28:46 +02:00
|
|
|
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
|
|
|
|
|
2022-04-26 15:42:02 +02:00
|
|
|
---@param incoming_syntax table
|
|
|
|
---@param text string
|
2022-11-03 23:56:20 +01:00
|
|
|
---@param state string
|
2023-04-01 18:12:39 +02:00
|
|
|
function tokenizer.tokenize(incoming_syntax, text, state, resume)
|
|
|
|
local res
|
2019-12-28 12:16:32 +01:00
|
|
|
local i = 1
|
|
|
|
|
2021-05-01 11:45:30 +02:00
|
|
|
if #incoming_syntax.patterns == 0 then
|
2020-05-14 11:08:12 +02:00
|
|
|
return { "normal", text }
|
|
|
|
end
|
2021-05-18 17:52:18 +02:00
|
|
|
|
2022-11-15 14:46:14 +01:00
|
|
|
state = state or string.char(0)
|
2023-04-01 18:12:39 +02:00
|
|
|
|
|
|
|
if resume then
|
|
|
|
res = resume.res
|
|
|
|
-- Remove "incomplete" tokens
|
|
|
|
while res[#res-1] == "incomplete" do
|
|
|
|
table.remove(res, #res)
|
|
|
|
table.remove(res, #res)
|
|
|
|
end
|
|
|
|
i = resume.i
|
|
|
|
state = resume.state
|
|
|
|
end
|
|
|
|
|
|
|
|
res = res or {}
|
|
|
|
|
2021-05-20 21:58:27 +02:00
|
|
|
-- incoming_syntax : the parent syntax of the file.
|
2022-11-03 23:56:20 +01:00
|
|
|
-- state : a string of bytes representing syntax state (see above)
|
2022-04-26 15:42:02 +02:00
|
|
|
|
2021-05-20 21:58:27 +02:00
|
|
|
-- current_syntax : the syntax we're currently in.
|
|
|
|
-- subsyntax_info : info about the delimiters of this subsyntax.
|
|
|
|
-- current_pattern_idx: the index of the pattern we're on for this syntax.
|
|
|
|
-- current_level : how many subsyntaxes deep we are.
|
|
|
|
local current_syntax, subsyntax_info, current_pattern_idx, current_level =
|
2021-05-01 11:45:30 +02:00
|
|
|
retrieve_syntax_state(incoming_syntax, state)
|
2022-04-26 15:42:02 +02:00
|
|
|
|
2021-05-20 21:58:27 +02:00
|
|
|
-- Should be used to set the state variable. Don't modify it directly.
|
|
|
|
local function set_subsyntax_pattern_idx(pattern_idx)
|
|
|
|
current_pattern_idx = pattern_idx
|
2022-11-03 23:56:20 +01:00
|
|
|
local state_len = #state
|
|
|
|
if current_level > state_len then
|
|
|
|
state = state .. string.char(pattern_idx)
|
|
|
|
elseif state_len == 1 then
|
|
|
|
state = string.char(pattern_idx)
|
|
|
|
else
|
|
|
|
state = ("%s%s%s"):format(
|
|
|
|
state:sub(1,current_level-1),
|
|
|
|
string.char(pattern_idx),
|
|
|
|
state:sub(current_level+1)
|
|
|
|
)
|
|
|
|
end
|
2021-05-20 21:58:27 +02:00
|
|
|
end
|
2022-04-26 15:42:02 +02:00
|
|
|
|
|
|
|
|
2021-05-20 21:58:27 +02:00
|
|
|
local function push_subsyntax(entering_syntax, pattern_idx)
|
|
|
|
set_subsyntax_pattern_idx(pattern_idx)
|
|
|
|
current_level = current_level + 1
|
|
|
|
subsyntax_info = entering_syntax
|
|
|
|
current_syntax = type(entering_syntax.syntax) == "table" and
|
|
|
|
entering_syntax.syntax or syntax.get(entering_syntax.syntax)
|
|
|
|
current_pattern_idx = 0
|
|
|
|
end
|
2022-04-26 15:42:02 +02:00
|
|
|
|
2021-05-20 21:58:27 +02:00
|
|
|
local function pop_subsyntax()
|
|
|
|
current_level = current_level - 1
|
2022-11-03 23:56:20 +01:00
|
|
|
state = string.sub(state, 1, current_level)
|
2021-05-20 21:58:27 +02:00
|
|
|
set_subsyntax_pattern_idx(0)
|
2022-04-26 15:42:02 +02:00
|
|
|
current_syntax, subsyntax_info, current_pattern_idx, current_level =
|
2021-05-20 21:58:27 +02:00
|
|
|
retrieve_syntax_state(incoming_syntax, state)
|
2021-06-02 21:27:00 +02:00
|
|
|
end
|
2022-04-26 15:42:02 +02:00
|
|
|
|
2021-06-02 21:27:00 +02:00
|
|
|
local function find_text(text, p, offset, at_start, close)
|
2022-03-04 11:27:01 +01:00
|
|
|
local target, res = p.pattern or p.regex, { 1, offset - 1 }
|
|
|
|
local p_idx = close and 2 or 1
|
|
|
|
local code = type(target) == "table" and target[p_idx] or target
|
|
|
|
|
|
|
|
if p.whole_line == nil then p.whole_line = { } end
|
|
|
|
if p.whole_line[p_idx] == nil then
|
|
|
|
-- Match patterns that start with '^'
|
2022-04-26 15:42:02 +02:00
|
|
|
p.whole_line[p_idx] = code:umatch("^%^") and true or false
|
2022-03-04 11:27:01 +01:00
|
|
|
if p.whole_line[p_idx] then
|
|
|
|
-- Remove '^' from the beginning of the pattern
|
|
|
|
if type(target) == "table" then
|
2022-04-26 15:42:02 +02:00
|
|
|
target[p_idx] = code:usub(2)
|
2022-03-04 11:27:01 +01:00
|
|
|
else
|
2022-04-26 15:42:02 +02:00
|
|
|
p.pattern = p.pattern and code:usub(2)
|
|
|
|
p.regex = p.regex and code:usub(2)
|
2022-03-04 11:27:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-02 21:27:00 +02:00
|
|
|
if p.regex and type(p.regex) ~= "table" then
|
|
|
|
p._regex = p._regex or regex.compile(p.regex)
|
|
|
|
code = p._regex
|
2022-03-04 11:27:01 +01:00
|
|
|
end
|
|
|
|
|
2021-06-02 21:27:00 +02:00
|
|
|
repeat
|
2021-10-11 22:37:31 +02:00
|
|
|
local next = res[2] + 1
|
2022-03-04 11:27:01 +01:00
|
|
|
-- If the pattern contained '^', allow matching only the whole line
|
|
|
|
if p.whole_line[p_idx] and next > 1 then
|
|
|
|
return
|
|
|
|
end
|
2022-04-26 15:42:02 +02:00
|
|
|
res = p.pattern and { text:ufind((at_start or p.whole_line[p_idx]) and "^" .. code or code, next) }
|
2022-12-12 03:25:42 +01:00
|
|
|
or { regex.find(code, text, text:ucharpos(next), (at_start or p.whole_line[p_idx]) and regex.ANCHORED or 0) }
|
2022-05-11 07:05:36 +02:00
|
|
|
if p.regex and #res > 0 then -- set correct utf8 len for regex result
|
2022-12-12 03:25:42 +01:00
|
|
|
local char_pos_1 = res[1] > next and string.ulen(text:sub(1, res[1])) or next
|
|
|
|
local char_pos_2 = string.ulen(text:sub(1, res[2]))
|
|
|
|
for i=3,#res do
|
|
|
|
res[i] = string.ulen(text:sub(1, res[i] - 1)) + 1
|
2022-05-28 01:38:22 +02:00
|
|
|
end
|
2022-06-12 02:55:36 +02:00
|
|
|
res[1] = char_pos_1
|
|
|
|
res[2] = char_pos_2
|
2022-05-11 07:05:36 +02:00
|
|
|
end
|
2022-06-12 04:19:05 +02:00
|
|
|
if res[1] and target[3] then
|
|
|
|
-- Check to see if the escaped character is there,
|
|
|
|
-- and if it is not itself escaped.
|
2021-06-02 21:27:00 +02:00
|
|
|
local count = 0
|
|
|
|
for i = res[1] - 1, 1, -1 do
|
2022-06-12 02:55:36 +02:00
|
|
|
if text:ubyte(i) ~= target[3]:ubyte() then break end
|
2021-06-02 21:27:00 +02:00
|
|
|
count = count + 1
|
|
|
|
end
|
2022-06-12 04:19:05 +02:00
|
|
|
if count % 2 == 0 then
|
|
|
|
-- The match is not escaped, so confirm it
|
|
|
|
break
|
|
|
|
elseif not close then
|
|
|
|
-- The *open* match is escaped, so avoid it
|
|
|
|
return
|
|
|
|
end
|
2021-06-02 21:27:00 +02:00
|
|
|
end
|
|
|
|
until not res[1] or not close or not target[3]
|
2021-08-29 03:14:12 +02:00
|
|
|
return table.unpack(res)
|
2021-05-20 21:58:27 +02:00
|
|
|
end
|
2022-04-26 15:42:02 +02:00
|
|
|
|
2022-05-11 07:05:36 +02:00
|
|
|
local text_len = text:ulen()
|
2023-04-01 18:12:39 +02:00
|
|
|
local start_time = system.get_time()
|
|
|
|
local starting_i = i
|
2022-05-11 07:05:36 +02:00
|
|
|
while i <= text_len do
|
2023-04-01 18:12:39 +02:00
|
|
|
-- Every 200 chars, check if we're out of time
|
|
|
|
if i - starting_i > 200 then
|
|
|
|
starting_i = i
|
|
|
|
if system.get_time() - start_time > 0.5 / config.fps then
|
|
|
|
-- We're out of time
|
|
|
|
push_token(res, "incomplete", string.usub(text, i))
|
|
|
|
return res, string.char(0), {
|
|
|
|
res = res,
|
|
|
|
i = i,
|
|
|
|
state = state
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
-- continue trying to match the end pattern of a pair if we have a state set
|
2021-05-20 21:58:27 +02:00
|
|
|
if current_pattern_idx > 0 then
|
|
|
|
local p = current_syntax.patterns[current_pattern_idx]
|
2021-06-02 21:27:00 +02:00
|
|
|
local s, e = find_text(text, p, i, false, true)
|
2021-05-18 17:52:18 +02:00
|
|
|
|
2021-05-01 11:45:30 +02:00
|
|
|
local cont = true
|
|
|
|
-- If we're in subsyntax mode, always check to see if we end our syntax
|
2021-05-20 21:58:27 +02:00
|
|
|
-- first, before the found delimeter, as ending the subsyntax takes
|
|
|
|
-- precedence over ending the delimiter in the subsyntax.
|
2021-05-01 11:45:30 +02:00
|
|
|
if subsyntax_info then
|
2021-06-02 21:27:00 +02:00
|
|
|
local ss, se = find_text(text, subsyntax_info, i, false, true)
|
2022-04-26 15:42:02 +02:00
|
|
|
-- If we find that we end the subsyntax before the
|
2021-05-20 21:58:27 +02:00
|
|
|
-- delimiter, push the token, and signal we shouldn't
|
|
|
|
-- treat the bit after as a token to be normally parsed
|
|
|
|
-- (as it's the syntax delimiter).
|
2021-05-01 11:45:30 +02:00
|
|
|
if ss and (s == nil or ss < s) then
|
2022-04-26 15:42:02 +02:00
|
|
|
push_token(res, p.type, text:usub(i, ss - 1))
|
2021-05-01 11:45:30 +02:00
|
|
|
i = ss
|
|
|
|
cont = false
|
|
|
|
end
|
|
|
|
end
|
2021-05-20 21:58:27 +02:00
|
|
|
-- If we don't have any concerns about syntax delimiters,
|
|
|
|
-- continue on as normal.
|
2021-05-01 11:45:30 +02:00
|
|
|
if cont then
|
|
|
|
if s then
|
2022-04-26 15:42:02 +02:00
|
|
|
push_token(res, p.type, text:usub(i, e))
|
2021-05-20 21:58:27 +02:00
|
|
|
set_subsyntax_pattern_idx(0)
|
2021-05-01 11:45:30 +02:00
|
|
|
i = e + 1
|
|
|
|
else
|
2022-04-26 15:42:02 +02:00
|
|
|
push_token(res, p.type, text:usub(i))
|
2021-05-01 11:45:30 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-05-20 21:58:27 +02:00
|
|
|
-- General end of syntax check. Applies in the case where
|
|
|
|
-- we're ending early in the middle of a delimiter, or
|
|
|
|
-- just normally, upon finding a token.
|
2022-12-28 01:24:52 +01:00
|
|
|
while subsyntax_info do
|
2021-06-02 21:27:00 +02:00
|
|
|
local s, e = find_text(text, subsyntax_info, i, true, true)
|
2019-12-28 12:16:32 +01:00
|
|
|
if s then
|
2022-04-26 15:42:02 +02:00
|
|
|
push_token(res, subsyntax_info.type, text:usub(i, e))
|
2021-05-20 21:58:27 +02:00
|
|
|
-- On finding unescaped delimiter, pop it.
|
|
|
|
pop_subsyntax()
|
2019-12-28 12:16:32 +01:00
|
|
|
i = e + 1
|
2022-12-28 01:24:52 +01:00
|
|
|
else
|
|
|
|
break
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- find matching pattern
|
|
|
|
local matched = false
|
2021-05-01 11:45:30 +02:00
|
|
|
for n, p in ipairs(current_syntax.patterns) do
|
2021-06-02 21:27:00 +02:00
|
|
|
local find_results = { find_text(text, p, i, true, false) }
|
|
|
|
if find_results[1] then
|
2022-06-15 21:28:46 +02:00
|
|
|
local type_is_table = type(p.type) == "table"
|
|
|
|
local n_types = type_is_table and #p.type or 1
|
2022-06-15 21:31:16 +02:00
|
|
|
if #find_results == 2 and type_is_table then
|
|
|
|
report_bad_pattern(core.warn, current_syntax, n,
|
|
|
|
"Token type is a table, but a string was expected.")
|
|
|
|
p.type = p.type[1]
|
|
|
|
elseif #find_results - 1 > n_types then
|
2022-06-15 21:28:46 +02:00
|
|
|
report_bad_pattern(core.error, current_syntax, n,
|
|
|
|
"Not enough token types: got %d needed %d.", n_types, #find_results - 1)
|
|
|
|
elseif #find_results - 1 < n_types then
|
|
|
|
report_bad_pattern(core.warn, current_syntax, n,
|
|
|
|
"Too many token types: got %d needed %d.", n_types, #find_results - 1)
|
2022-06-15 19:33:58 +02:00
|
|
|
end
|
2021-05-19 22:35:28 +02:00
|
|
|
-- matched pattern; make and add tokens
|
|
|
|
push_tokens(res, current_syntax, p, text, find_results)
|
2019-12-28 12:16:32 +01:00
|
|
|
-- update state if this was a start|end pattern pair
|
2021-06-02 21:27:00 +02:00
|
|
|
if type(p.pattern or p.regex) == "table" then
|
2021-05-20 21:58:27 +02:00
|
|
|
-- If we have a subsyntax, push that onto the subsyntax stack.
|
2021-05-01 11:45:30 +02:00
|
|
|
if p.syntax then
|
2021-05-20 21:58:27 +02:00
|
|
|
push_subsyntax(p, n)
|
2022-04-26 15:42:02 +02:00
|
|
|
else
|
2021-05-20 21:58:27 +02:00
|
|
|
set_subsyntax_pattern_idx(n)
|
2021-05-01 11:45:30 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
-- move cursor past this token
|
2021-06-02 21:27:00 +02:00
|
|
|
i = find_results[2] + 1
|
2019-12-28 12:16:32 +01:00
|
|
|
matched = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- consume character if we didn't match
|
|
|
|
if not matched then
|
2022-05-11 07:05:36 +02:00
|
|
|
push_token(res, "normal", text:usub(i, i))
|
|
|
|
i = i + 1
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return res, state
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function iter(t, i)
|
|
|
|
i = i + 2
|
|
|
|
local type, text = t[i], t[i+1]
|
|
|
|
if type then
|
|
|
|
return i, type, text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-07 22:14:46 +02:00
|
|
|
function tokenizer.each_token(t)
|
2019-12-28 12:16:32 +01:00
|
|
|
return iter, t, -1
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-07 22:14:46 +02:00
|
|
|
return tokenizer
|