Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local gpu = require("component").gpu
- local unicode = require("unicode")
- local syntax = {}
- local colorSchemes = {
- ["custom"] = {
- -- ["recommendedBackground"] = 0x000000,
- ["functions"] = 0xff6699,
- ["comments"] = 0x336600,
- ["compares"] = 0xff3333,
- ["strings"] = 0x333333,
- ["boolean"] = 0xffcc33,
- ["numbers"] = 0x333399,
- ["default"] = 0xcccccc,
- ["loops"] = 0x6699ff,
- ["logic"] = 0xcc66cc,
- ["vars"] = 0x33cc33,
- },
- }
- local currentColorScheme = {}
- local patterns
- local patternsCount
- --Приоритет поиска шаблонов снижается сверху вниз
- local function definePatterns()
- local Scheme = currentColorScheme
- local Trim2 = function(Return, str, fs, fe, tPattern)
- --table.insert(Return, {
- -- ["word"] = string.sub(str, fs, fs),
- -- ["color"] = currentColorScheme.default
- --})
- table.insert(Return, {
- ["word"] = string.sub(str, fs, fe - 1),
- ["color"] = tPattern.color
- })
- table.insert(Return, {
- ["word"] = string.sub(str, fe, fe),
- ["color"] = currentColorScheme.default
- })
- return fe + 1
- end
- local TrimR = function(Return, str, fs, fe, tPattern)
- table.insert(Return, {
- ["word"] = string.sub(str, fs, fe - 1),
- ["color"] = tPattern.color
- })
- return fe
- end
- local TrimOffset = function(Return, str, fs, fe, tPattern)
- table.insert(Return, {
- ["word"] = string.sub(str, fs, fe - 1),
- ["color"] = tPattern.color
- })
- return fe
- end
- local FuncOnNum = function(Return, str, fs, fe, tPattern)
- local Color = currentColorScheme.default
- if string.find(string.sub(str, fs - 1, fs - 1), "%A") then
- Color = tPattern.color
- end
- table.insert(Return, {
- ["word"] = string.sub(str, fs, fe),
- ["color"] = Color
- })
- return fe + 1
- end
- patterns = {
- --Комментарии
- { ["pattern"] = "%-%-.*", ["color"] = Scheme.comments },
- --Строки
- { ["pattern"] = "\"\"", ["color"] = Scheme.strings },
- { ["pattern"] = "\'\'", ["color"] = Scheme.strings },
- { ["pattern"] = "\".-[^\\]\"", ["color"] = Scheme.strings },
- { ["pattern"] = "\'.-[^\\]\'", ["color"] = Scheme.strings },
- --Слова с большой буквы(переменые)
- { ["pattern"] = "%u[%a0-9_]*", ["color"] = Scheme.vars, ["func"] = FuncOnNum },
- --Числа
- { ["pattern"] = "0x[A-Fa-f0-9]+", ["color"] = Scheme.numbers, ["func"] = FuncOnNum },
- { ["pattern"] = "(%d+%.?%d*)", ["color"] = Scheme.numbers, ["func"] = FuncOnNum },
- --Сравнения и мат. операции
- { ["pattern"] = "%.%.", ["color"] = Scheme.compares },
- { ["pattern"] = "[%<%>%=%~]=?", ["color"] = Scheme.compares },
- { ["pattern"] = "[%+%-%*%/%#]", ["color"] = Scheme.compares },
- --Состояния переменной
- { ["pattern"] = "%Wtrue%W", ["color"] = Scheme.boolean, ["func"] = TrimR },
- { ["pattern"] = "%Wfalse%W", ["color"] = Scheme.boolean, ["func"] = TrimR },
- { ["pattern"] = "%Wnil%W", ["color"] = Scheme.boolean, ["func"] = TrimR },
- --And, or, not
- { ["pattern"] = "%Wand%W", ["color"] = Scheme.logic, ["func"] = TrimR },
- { ["pattern"] = "%Wor%W", ["color"] = Scheme.logic, ["func"] = TrimR },
- { ["pattern"] = "%Wnot%W", ["color"] = Scheme.logic, ["func"] = TrimR },
- --Циклы, условия, объявления
- { ["pattern"] = "%Wwhile%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wdo%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wend%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wfor%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Win%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wrepeat%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wif%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wthen%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wuntil%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wreturn%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wlocal%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wfunction%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Welse%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Welseif%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wbreak%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wrequire%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- { ["pattern"] = "%Wi?pairs%W", ["color"] = Scheme.loops, ["func"] = TrimR },
- --Функции
- { ["pattern"] = "[%s%.%:]?([%a%d%_]+)%(", ["color"] = Scheme.functions, ["func"] = Trim2 },
- --Библиотеки
- { ["pattern"] = "table%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "string%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "coroutine%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "package%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "term%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "math%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "bit32%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "io%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "filesystem%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "os%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "debug%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "unicode%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "component%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "computer%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "event%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "internet%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "keybord%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "thread%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "shell%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "utf8%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- { ["pattern"] = "gpu%.", ["color"] = Scheme.functions, ["func"] = TrimOffset },
- --Удаление задниго костыля
- { ["pattern"] = " $", ["func"] = function(t, s, fs, fe, t2) return fe + 1 end },
- }
- patternsCount = #patterns
- end
- function syntax.highlight(str)
- local Return = {}
- local Length = string.len(str)
- local Default = currentColorScheme.default
- local Offset = 1
- str = " ".. str .." " -- +Костыль что бы искало везде
- while Offset < Length do
- local collision = {}
- local Found = false
- for i=1, patternsCount do
- local tPattern = patterns[i]
- local fs, fe = string.find(str, tPattern.pattern, Offset)
- if fs then
- table.insert(collision, { fs, fe, i })
- -- Offset = fe + 1
- Found = true
- end
- end
- local Min = math.huge
- local Key = 0
- for k,v in ipairs(collision) do
- if v[1] < Min then
- Min = v[1]
- Key = k
- end
- end
- local fs, fe, key = table.unpack(collision[Key] or {})
- if Found then
- local tPattern = patterns[key]
- if fs ~= Offset then
- table.insert(Return,{
- ["word"] = string.sub(str, Offset, fs - 1),
- ["color"] = Default
- })
- end
- if tPattern.func then
- Offset = tPattern.func(Return, str, fs, fe, tPattern)
- else
- table.insert(Return,{
- ["word"] = string.sub(str, fs, fe),
- ["color"] = tPattern.color
- })
- Offset = fe + 1
- end
- end
- if not Found then
- table.insert(Return,{
- ["word"] = string.sub(str, Offset, Length),
- ["color"] = Default
- })
- break
- end
- end
- --Удаление переднего костыля из результата
- if #Return then
- local F = Return[1].word
- if string.len(F) > 1 then
- Return[1].word = string.sub(F, 2, string.len(F))
- else
- Return[1] = nil
- end
- end
- return Return
- end
- function syntax.setColorScheme(colorScheme)
- currentColorScheme = colorScheme
- definePatterns()
- end
- function syntax.highlightAndDraw(x, y, limit, str)
- local HL = syntax.highlight(str)
- for _,v in pairs(HL) do
- local Color = v.color
- local Word = v.word
- gpu.setForeground(Color)
- gpu.set(x, y, Word)
- x = x + unicode.len(Word)
- end
- end
- syntax.setColorScheme(colorSchemes.custom)
- return syntax
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement