syntax.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. local syntax = {}
  2. syntax.items = {}
  3. local plain_text_syntax = { patterns = {}, symbols = {} }
  4. local function matches_pattern(text, pattern)
  5. if type(pattern) == "string" then
  6. return text:find(pattern)
  7. end
  8. for _, p in ipairs(pattern) do
  9. if matches_pattern(text, p) then return true end
  10. end
  11. return false
  12. end
  13. function syntax.add(t)
  14. table.insert(syntax.items, t)
  15. end
  16. function syntax.get(filename)
  17. for i = #syntax.items, 1, -1 do
  18. local t = syntax.items[i]
  19. if matches_pattern(filename, t.files) then
  20. return t
  21. end
  22. end
  23. return plain_text_syntax
  24. end
  25. syntax.add {
  26. files = "%.lua$",
  27. comment = "--",
  28. patterns = {
  29. { pattern = { '"', '"', '\\' }, type = "string" },
  30. { pattern = { "'", "'", '\\' }, type = "string" },
  31. { pattern = { "%[%[", "%]%]" }, type = "string" },
  32. { pattern = { "--%[%[", "%]%]"}, type = "comment" },
  33. { pattern = "%-%-.-\n", type = "comment" },
  34. { pattern = "-?0x%x+", type = "number" },
  35. { pattern = "-?%d+[%d%.eE]*", type = "number" },
  36. { pattern = "-?%.?%d+", type = "number" },
  37. { pattern = "%.%.%.?", type = "operator" },
  38. { pattern = "[<>~=]=", type = "operator" },
  39. { pattern = "[%+%-=/%*%^%%#<>]", type = "operator" },
  40. { pattern = "[%a_][%w_]*%s*%f[(\"{]", type = "function" },
  41. { pattern = "[%a_][%w_]*", type = "symbol" },
  42. },
  43. symbols = {
  44. ["if"] = "keyword",
  45. ["then"] = "keyword",
  46. ["else"] = "keyword",
  47. ["elseif"] = "keyword",
  48. ["end"] = "keyword",
  49. ["do"] = "keyword",
  50. ["function"] = "keyword",
  51. ["repeat"] = "keyword",
  52. ["until"] = "keyword",
  53. ["while"] = "keyword",
  54. ["for"] = "keyword",
  55. ["break"] = "keyword",
  56. ["return"] = "keyword",
  57. ["local"] = "keyword",
  58. ["in"] = "keyword",
  59. ["not"] = "keyword",
  60. ["and"] = "keyword",
  61. ["or"] = "keyword",
  62. ["self"] = "keyword2",
  63. ["true"] = "literal",
  64. ["false"] = "literal",
  65. ["nil"] = "literal",
  66. },
  67. }
  68. syntax.add {
  69. files = { "%.c$", "%.h$", "%.inl$", "%.cpp$", "%.hpp$" },
  70. comment = "//",
  71. patterns = {
  72. { pattern = "//.-\n", type = "comment" },
  73. { pattern = { "/%*", "%*/" }, type = "comment" },
  74. { pattern = { "#", "[^\\]\n" }, type = "comment" },
  75. { pattern = { '"', '"', '\\' }, type = "string" },
  76. { pattern = { "'", "'", '\\' }, type = "string" },
  77. { pattern = "-?0x%x+", type = "number" },
  78. { pattern = "-?%d+[%d%.eE]*f?", type = "number" },
  79. { pattern = "-?%.?%d+f?", type = "number" },
  80. { pattern = "[%+%-=/%*%^%%<>!~|&]", type = "operator" },
  81. { pattern = "[%a_][%w_]*%f[(]", type = "function" },
  82. { pattern = "[%a_][%w_]*", type = "symbol" },
  83. },
  84. symbols = {
  85. ["if"] = "keyword",
  86. ["then"] = "keyword",
  87. ["else"] = "keyword",
  88. ["elseif"] = "keyword",
  89. ["do"] = "keyword",
  90. ["while"] = "keyword",
  91. ["for"] = "keyword",
  92. ["break"] = "keyword",
  93. ["continue"] = "keyword",
  94. ["return"] = "keyword",
  95. ["goto"] = "keyword",
  96. ["struct"] = "keyword",
  97. ["typedef"] = "keyword",
  98. ["enum"] = "keyword",
  99. ["extern"] = "keyword",
  100. ["static"] = "keyword",
  101. ["volatile"] = "keyword",
  102. ["const"] = "keyword",
  103. ["inline"] = "keyword",
  104. ["switch"] = "keyword",
  105. ["case"] = "keyword",
  106. ["default"] = "keyword",
  107. ["auto"] = "keyword",
  108. ["const"] = "keyword",
  109. ["void"] = "keyword",
  110. ["int"] = "keyword2",
  111. ["float"] = "keyword2",
  112. ["double"] = "keyword2",
  113. ["char"] = "keyword2",
  114. ["unsigned"] = "keyword2",
  115. ["bool"] = "keyword2",
  116. ["true"] = "literal",
  117. ["false"] = "literal",
  118. ["NULL"] = "literal",
  119. },
  120. }
  121. syntax.add {
  122. files = { "%.md$", "%.markdown$" },
  123. patterns = {
  124. { pattern = "\\.", type = "normal" },
  125. { pattern = { "<!%-%-", "%-%->" }, type = "comment" },
  126. { pattern = { "```", "```" }, type = "string" },
  127. { pattern = { "`", "`", "\\" }, type = "string" },
  128. { pattern = { "~~", "~~", "\\" }, type = "keyword2" },
  129. { pattern = "%-%-%-+", type = "comment" },
  130. { pattern = "%*%s+", type = "operator" },
  131. { pattern = { "%*", "[%*\n]", "\\" }, type = "operator" },
  132. { pattern = { "%_", "[%_\n]", "\\" }, type = "keyword2" },
  133. { pattern = "#.-\n", type = "keyword" },
  134. { pattern = "!?%[.*%]%(.*%)", type = "function" },
  135. { pattern = "https?://%S+", type = "function" },
  136. },
  137. symbols = { },
  138. }
  139. return syntax