Optimizing this syntax highlighting script

Yo! I got this script to do syntax highlighting from a token stream, but I can’t find a way to do it fast enough, for reference this script:

local highlights = {
String = reference.to.label, --Label as in highlight textlabel
...
}
local pointer = {
INC-String = "String",
...
}
local function findAllNums(nump)
	local num = 1
	local nums = {}
	while num <= nump do
		nums[#nums+1] = num
		num += 1
	end
	return nums
end
local function update()
	
	for _ ,v in pairs(highlights) do
		v.Text = ""
	end
	lex.reset(textBox.Text)
	local lines = {}
	local lineNum = 1
	lineCounter.TextLabel.Text = ""
	for token in lex.next do
		--if token.type == "exception" then continue end
		
		local highlightType = highlights[pointers[token.type]] or highlights[token.type] or false
		lineNum+= token.lineBreaks
		
		for i, v in pairs(highlights) do
			v.Text ..= highlightType == v and token.raw or string.gsub(token.raw, "[^\n\t\r]", "\32")
		end
	end
	
	lineCounter.TextLabel.Text = table.concat(findAllNums(lineNum), "\n")
end

Runs at about 50 ms, while the lexer itself does the same string in on average a 1 ms

Can’t do rich text because of this, Using RichText slightly offsets all text except for first line

So any suggestions on optimizing this?

1 Like