Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local languages = {}
- local lang_lua = {
- keywords = {
- ["if"] = "Keyword.Control.If";
- ["else"] = "Keyword.Control.Else";
- ["elseif"] = "Keyword.Control.Elseif";
- ["repeat"] = "Keyword.Loop.Repeat";
- ["while"] = "Keyword.Loop.While";
- ["for"] = "Keyword.Loop.For";
- ["in"] = "Keyword";
- ["and"] = "Operator.And";
- ["or"] = "Operator.Or";
- ["not"] = "Operator.Not";
- ["break"] = "Keyword.Control.Break";
- ["return"] = "Keyword.Control.Return";
- ["do"] = "Keyword";
- ["then"] = "Keyword";
- ["until"] = "Keyword";
- ["end"] = "Keyword.Control";
- ["function"] = "Keyword.Function";
- ["local"] = "Keyword.Declare";
- ["true"] = "Constant.Boolean";
- ["false"] = "Constant.Boolean";
- ["nil"] = "Constant.Null";
- };
- symbols = {
- ["="] = "Symbol";
- ["["] = "Symbol.Bracket.Square";
- ["]"] = "Symbol.Bracket.Square";
- ["("] = "Symbol.Bracket.Round";
- [")"] = "Symbol.Bracket.Round";
- ["{"] = "Symbol.Bracket.Curly";
- ["}"] = "Symbol.Bracket.Curly";
- ["+"] = "Operator.Math.Add";
- ["-"] = "Operator.Math.Sub";
- ["*"] = "Operator.Math.Mul";
- ["/"] = "Operator.Math.Div";
- ["%"] = "Operator.Math.Mod";
- ["^"] = "Operator.Math.Pow";
- ["=="] = "Operator.Compare.Eq";
- ["~="] = "Operator.Compare.Neq";
- [">="] = "Operator.Compare.Gte";
- ["<="] = "Operator.Compare.Lte";
- [">"] = "Operator.Compare.Gt";
- ["<"] = "Operator.Compare.Lt";
- ["#"] = "Operator.Len";
- ["."] = "Symbol.Index";
- [":"] = "Symbol.Index";
- [","] = "Symbol.Sep";
- [";"] = "Symbol.Sep";
- };
- comments = {
- start = {
- line = "%-%-";
- multiline = "%-%-%[(=*)%[";
- };
- finish = {
- line = "\n";
- multiline = "%]%1%]";
- };
- };
- strings = {
- start = {
- line = "[\"\']";
- multiline = "%[(=*)%[";
- };
- finish = {
- line = "%1";
- multiline = "%]%1%]";
- };
- escape = {
- line = "\\";
- multiline = nil;
- };
- };
- }
- local function findEndingMatch( text, initial, final, escape, position )
- if not final then error("hey", 2)end
- local escaped = false
- local close = "^" .. initial:gsub( "^(.*)$", final )
- local esc = escape and "^" .. initial:gsub( "^(.*)$", escape )
- while position <= #text do
- if escaped then
- escaped = false
- elseif escape and text:find( esc, position ) then
- position = select( 2, text:find( esc, position ) )
- escaped = true
- else
- local s, f = text:find( close, position )
- if s then
- return f
- end
- end
- position = position + 1
- end
- end
- local function longestOf( text, position, a, b, c, d, A, B, C, D )
- local t = {
- { a, text:match( "^" .. a:gsub( "%(", "" ):gsub( "%)", "" ), position ) };
- { b, text:match( "^" .. b:gsub( "%(", "" ):gsub( "%)", "" ), position ) };
- { c, text:match( "^" .. c:gsub( "%(", "" ):gsub( "%)", "" ), position ) };
- { d, text:match( "^" .. d:gsub( "%(", "" ):gsub( "%)", "" ), position ) };
- }
- local l = 0
- local n = 0
- for i = 1, #t do
- if t[i][2] and #t[i][2] > l then
- n = i
- end
- end
- return n > 0 and text:match( "^" .. t[n][1], position ), n > 0 and ({A, B, C, D})[n]
- end
- local function formatText( text )
- return text:gsub( "\\", "\\\\" ):gsub( "{", "\\{" ):gsub( "}", "\\}" )
- end
- local function createLanguageFormatter(lang)
- return function(state, line)
- local i = 1
- local res = ""
- if state.in_section then
- local index = state.in_section:match "_(.+)"
- local sindex = (state.in_section == "comment_line" or state.in_section == "comment_multiline") and "comments" or "strings"
- local ending = findEndingMatch( line, state.string_match, lang[sindex].finish[index], lang[sindex].escape and lang[sindex].escape[index], 1 )
- if ending then
- state.in_section = false
- i = ending + 1
- res = (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line:sub( 1, ending ) ) .. "}"
- else
- return (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line ) .. "}"
- end
- end
- while i <= #line do
- local pat, fullindex = longestOf( line, i,
- lang.strings.start.line, lang.strings.start.multiline,
- lang.comments.start.line, lang.comments.start.multiline,
- "string_line", "string_multiline",
- "comment_line", "comment_multiline"
- )
- if pat then
- local index = fullindex:match "_(.+)"
- local sindex = (fullindex == "string_line" or fullindex == "string_multiline") and "strings" or "comments"
- local ending = findEndingMatch( line, pat, lang[sindex].finish[index], lang[sindex].escape and lang[sindex].escape[index], i + #pat )
- if ending then
- res = res .. (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line:sub( i, ending ) ) .. "}"
- i = ending + 1
- elseif fullindex == "comment_line" then
- res = res .. (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line:sub( i ) ) .. "}"
- return res
- else
- state.in_section = fullindex
- state.string_match = pat
- res = res .. (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line:sub( i ) ) .. "}"
- return res
- end
- elseif line:find( "^%d*%.?%d", i ) then
- local match = line:match( "^%d*%.?%d+e[%+%-]%d+", i ) or line:match( "^%d*%.?%d+", i )
- res = res .. "{Constant.Number:" .. match .. "}"
- i = i + #match
- elseif line:find( "^0x%x", i ) then
- local match = line:match( "^0x%x+", i )
- res = res .. "{Constant.Number:" .. match .. "}"
- i = i + #match
- elseif line:find( "^[%w_]", i ) then
- local match = line:match( "^[%w_]+", i )
- if lang.keywords[match] then
- res = res .. "{" .. lang.keywords[match] .. ":" .. match .. "}"
- else
- res = res .. "{Constant.Identifier:" .. match .. "}"
- end
- i = i + #match
- else
- local l, v = 0
- for k, sv in pairs( lang.symbols ) do
- if line:sub( i, i + #k - 1 ) == k then
- if #k > l then
- l = #k
- v = sv
- end
- end
- end
- if l > 0 then
- res = res .. "{" .. v .. ":" .. formatText( line:sub( i, i + l - 1 ) ) .. "}"
- else
- res = res .. formatText( line:sub( i, i ) )
- end
- i = i + 1
- end
- end
- return res
- end, {
- in_section = false;
- string_match = "";
- }
- end
- languages.lua = lang_lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement