Advertisement
Guest User

Untitled

a guest
May 30th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1.  
  2. local languages = {}
  3.  
  4. local lang_lua = {
  5. keywords = {
  6. ["if"] = "Keyword.Control.If";
  7. ["else"] = "Keyword.Control.Else";
  8. ["elseif"] = "Keyword.Control.Elseif";
  9.  
  10. ["repeat"] = "Keyword.Loop.Repeat";
  11. ["while"] = "Keyword.Loop.While";
  12.  
  13. ["for"] = "Keyword.Loop.For";
  14. ["in"] = "Keyword";
  15.  
  16. ["and"] = "Operator.And";
  17. ["or"] = "Operator.Or";
  18. ["not"] = "Operator.Not";
  19.  
  20. ["break"] = "Keyword.Control.Break";
  21. ["return"] = "Keyword.Control.Return";
  22.  
  23. ["do"] = "Keyword";
  24. ["then"] = "Keyword";
  25. ["until"] = "Keyword";
  26. ["end"] = "Keyword.Control";
  27.  
  28. ["function"] = "Keyword.Function";
  29.  
  30. ["local"] = "Keyword.Declare";
  31.  
  32. ["true"] = "Constant.Boolean";
  33. ["false"] = "Constant.Boolean";
  34. ["nil"] = "Constant.Null";
  35. };
  36.  
  37. symbols = {
  38. ["="] = "Symbol";
  39.  
  40. ["["] = "Symbol.Bracket.Square";
  41. ["]"] = "Symbol.Bracket.Square";
  42. ["("] = "Symbol.Bracket.Round";
  43. [")"] = "Symbol.Bracket.Round";
  44. ["{"] = "Symbol.Bracket.Curly";
  45. ["}"] = "Symbol.Bracket.Curly";
  46.  
  47. ["+"] = "Operator.Math.Add";
  48. ["-"] = "Operator.Math.Sub";
  49. ["*"] = "Operator.Math.Mul";
  50. ["/"] = "Operator.Math.Div";
  51. ["%"] = "Operator.Math.Mod";
  52. ["^"] = "Operator.Math.Pow";
  53.  
  54. ["=="] = "Operator.Compare.Eq";
  55. ["~="] = "Operator.Compare.Neq";
  56. [">="] = "Operator.Compare.Gte";
  57. ["<="] = "Operator.Compare.Lte";
  58. [">"] = "Operator.Compare.Gt";
  59. ["<"] = "Operator.Compare.Lt";
  60.  
  61. ["#"] = "Operator.Len";
  62.  
  63. ["."] = "Symbol.Index";
  64. [":"] = "Symbol.Index";
  65.  
  66. [","] = "Symbol.Sep";
  67. [";"] = "Symbol.Sep";
  68. };
  69.  
  70. comments = {
  71. start = {
  72. line = "%-%-";
  73. multiline = "%-%-%[(=*)%[";
  74. };
  75. finish = {
  76. line = "\n";
  77. multiline = "%]%1%]";
  78. };
  79. };
  80.  
  81. strings = {
  82. start = {
  83. line = "[\"\']";
  84. multiline = "%[(=*)%[";
  85. };
  86. finish = {
  87. line = "%1";
  88. multiline = "%]%1%]";
  89. };
  90. escape = {
  91. line = "\\";
  92. multiline = nil;
  93. };
  94. };
  95. }
  96.  
  97. local function findEndingMatch( text, initial, final, escape, position )
  98. if not final then error("hey", 2)end
  99. local escaped = false
  100. local close = "^" .. initial:gsub( "^(.*)$", final )
  101. local esc = escape and "^" .. initial:gsub( "^(.*)$", escape )
  102.  
  103. while position <= #text do
  104. if escaped then
  105. escaped = false
  106. elseif escape and text:find( esc, position ) then
  107. position = select( 2, text:find( esc, position ) )
  108. escaped = true
  109. else
  110. local s, f = text:find( close, position )
  111. if s then
  112. return f
  113. end
  114. end
  115.  
  116. position = position + 1
  117. end
  118. end
  119.  
  120. local function longestOf( text, position, a, b, c, d, A, B, C, D )
  121. local t = {
  122. { a, text:match( "^" .. a:gsub( "%(", "" ):gsub( "%)", "" ), position ) };
  123. { b, text:match( "^" .. b:gsub( "%(", "" ):gsub( "%)", "" ), position ) };
  124. { c, text:match( "^" .. c:gsub( "%(", "" ):gsub( "%)", "" ), position ) };
  125. { d, text:match( "^" .. d:gsub( "%(", "" ):gsub( "%)", "" ), position ) };
  126. }
  127. local l = 0
  128. local n = 0
  129.  
  130. for i = 1, #t do
  131. if t[i][2] and #t[i][2] > l then
  132. n = i
  133. end
  134. end
  135.  
  136. return n > 0 and text:match( "^" .. t[n][1], position ), n > 0 and ({A, B, C, D})[n]
  137. end
  138.  
  139. local function formatText( text )
  140. return text:gsub( "\\", "\\\\" ):gsub( "{", "\\{" ):gsub( "}", "\\}" )
  141. end
  142.  
  143. local function createLanguageFormatter(lang)
  144. return function(state, line)
  145.  
  146. local i = 1
  147. local res = ""
  148.  
  149. if state.in_section then
  150. local index = state.in_section:match "_(.+)"
  151. local sindex = (state.in_section == "comment_line" or state.in_section == "comment_multiline") and "comments" or "strings"
  152. local ending = findEndingMatch( line, state.string_match, lang[sindex].finish[index], lang[sindex].escape and lang[sindex].escape[index], 1 )
  153.  
  154. if ending then
  155. state.in_section = false
  156. i = ending + 1
  157. res = (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line:sub( 1, ending ) ) .. "}"
  158. else
  159. return (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line ) .. "}"
  160. end
  161. end
  162.  
  163. while i <= #line do
  164. local pat, fullindex = longestOf( line, i,
  165. lang.strings.start.line, lang.strings.start.multiline,
  166. lang.comments.start.line, lang.comments.start.multiline,
  167. "string_line", "string_multiline",
  168. "comment_line", "comment_multiline"
  169. )
  170.  
  171. if pat then
  172. local index = fullindex:match "_(.+)"
  173. local sindex = (fullindex == "string_line" or fullindex == "string_multiline") and "strings" or "comments"
  174. local ending = findEndingMatch( line, pat, lang[sindex].finish[index], lang[sindex].escape and lang[sindex].escape[index], i + #pat )
  175.  
  176. if ending then
  177. res = res .. (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line:sub( i, ending ) ) .. "}"
  178. i = ending + 1
  179. elseif fullindex == "comment_line" then
  180. res = res .. (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line:sub( i ) ) .. "}"
  181. return res
  182. else
  183. state.in_section = fullindex
  184. state.string_match = pat
  185. res = res .. (sindex == "strings" and "{Constant.String:" or "{Comment:") .. formatText( line:sub( i ) ) .. "}"
  186. return res
  187. end
  188.  
  189. elseif line:find( "^%d*%.?%d", i ) then
  190. local match = line:match( "^%d*%.?%d+e[%+%-]%d+", i ) or line:match( "^%d*%.?%d+", i )
  191. res = res .. "{Constant.Number:" .. match .. "}"
  192. i = i + #match
  193.  
  194. elseif line:find( "^0x%x", i ) then
  195. local match = line:match( "^0x%x+", i )
  196. res = res .. "{Constant.Number:" .. match .. "}"
  197. i = i + #match
  198.  
  199. elseif line:find( "^[%w_]", i ) then
  200. local match = line:match( "^[%w_]+", i )
  201.  
  202. if lang.keywords[match] then
  203. res = res .. "{" .. lang.keywords[match] .. ":" .. match .. "}"
  204. else
  205. res = res .. "{Constant.Identifier:" .. match .. "}"
  206. end
  207. i = i + #match
  208.  
  209. else
  210. local l, v = 0
  211. for k, sv in pairs( lang.symbols ) do
  212. if line:sub( i, i + #k - 1 ) == k then
  213. if #k > l then
  214. l = #k
  215. v = sv
  216. end
  217. end
  218. end
  219.  
  220. if l > 0 then
  221. res = res .. "{" .. v .. ":" .. formatText( line:sub( i, i + l - 1 ) ) .. "}"
  222. else
  223. res = res .. formatText( line:sub( i, i ) )
  224. end
  225.  
  226. i = i + 1
  227. end
  228. end
  229.  
  230. return res
  231.  
  232. end, {
  233. in_section = false;
  234. string_match = "";
  235. }
  236. end
  237.  
  238. languages.lua = lang_lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement