lua_highlight_rules.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../lib/oop");
  33. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  34. var LuaHighlightRules = function() {
  35. var keywords = (
  36. "break|do|else|elseif|end|for|function|if|in|local|repeat|"+
  37. "return|then|until|while|or|and|not"
  38. );
  39. var builtinConstants = ("true|false|nil|_G|_VERSION");
  40. var functions = (
  41. // builtinFunctions
  42. "string|xpcall|package|tostring|print|os|unpack|require|"+
  43. "getfenv|setmetatable|next|assert|tonumber|io|rawequal|"+
  44. "collectgarbage|getmetatable|module|rawset|math|debug|"+
  45. "pcall|table|newproxy|type|coroutine|_G|select|gcinfo|"+
  46. "pairs|rawget|loadstring|ipairs|_VERSION|dofile|setfenv|"+
  47. "load|error|loadfile|"+
  48. "sub|upper|len|gfind|rep|find|match|char|dump|gmatch|"+
  49. "reverse|byte|format|gsub|lower|preload|loadlib|loaded|"+
  50. "loaders|cpath|config|path|seeall|exit|setlocale|date|"+
  51. "getenv|difftime|remove|time|clock|tmpname|rename|execute|"+
  52. "lines|write|close|flush|open|output|type|read|stderr|"+
  53. "stdin|input|stdout|popen|tmpfile|log|max|acos|huge|"+
  54. "ldexp|pi|cos|tanh|pow|deg|tan|cosh|sinh|random|randomseed|"+
  55. "frexp|ceil|floor|rad|abs|sqrt|modf|asin|min|mod|fmod|log10|"+
  56. "atan2|exp|sin|atan|getupvalue|debug|sethook|getmetatable|"+
  57. "gethook|setmetatable|setlocal|traceback|setfenv|getinfo|"+
  58. "setupvalue|getlocal|getregistry|getfenv|setn|insert|getn|"+
  59. "foreachi|maxn|foreach|concat|sort|remove|resume|yield|"+
  60. "status|wrap|create|running|"+
  61. // metatableMethods
  62. "__add|__sub|__mod|__unm|__concat|__lt|__index|__call|__gc|__metatable|"+
  63. "__mul|__div|__pow|__len|__eq|__le|__newindex|__tostring|__mode|__tonumber"
  64. );
  65. var stdLibaries = ("string|package|os|io|math|debug|table|coroutine");
  66. var futureReserved = "";
  67. var deprecatedIn5152 = ("setn|foreach|foreachi|gcinfo|log10|maxn");
  68. var keywordMapper = this.createKeywordMapper({
  69. "keyword": keywords,
  70. "support.function": functions,
  71. "invalid.deprecated": deprecatedIn5152,
  72. "constant.library": stdLibaries,
  73. "constant.language": builtinConstants,
  74. "invalid.illegal": futureReserved,
  75. "variable.language": "self"
  76. }, "identifier");
  77. var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
  78. var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
  79. var integer = "(?:" + decimalInteger + "|" + hexInteger + ")";
  80. var fraction = "(?:\\.\\d+)";
  81. var intPart = "(?:\\d+)";
  82. var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
  83. var floatNumber = "(?:" + pointFloat + ")";
  84. this.$rules = {
  85. "start" : [{
  86. stateName: "bracketedComment",
  87. onMatch : function(value, currentState, stack){
  88. stack.unshift(this.next, value.length - 2, currentState);
  89. return "comment";
  90. },
  91. regex : /\-\-\[=*\[/,
  92. next : [
  93. {
  94. onMatch : function(value, currentState, stack) {
  95. if (value.length == stack[1]) {
  96. stack.shift();
  97. stack.shift();
  98. this.next = stack.shift();
  99. } else {
  100. this.next = "";
  101. }
  102. return "comment";
  103. },
  104. regex : /\]=*\]/,
  105. next : "start"
  106. }, {
  107. defaultToken : "comment"
  108. }
  109. ]
  110. },
  111. {
  112. token : "comment",
  113. regex : "\\-\\-.*$"
  114. },
  115. {
  116. stateName: "bracketedString",
  117. onMatch : function(value, currentState, stack){
  118. stack.unshift(this.next, value.length, currentState);
  119. return "comment";
  120. },
  121. regex : /\[=*\[/,
  122. next : [
  123. {
  124. onMatch : function(value, currentState, stack) {
  125. if (value.length == stack[1]) {
  126. stack.shift();
  127. stack.shift();
  128. this.next = stack.shift();
  129. } else {
  130. this.next = "";
  131. }
  132. return "comment";
  133. },
  134. regex : /\]=*\]/,
  135. next : "start"
  136. }, {
  137. defaultToken : "comment"
  138. }
  139. ]
  140. },
  141. {
  142. token : "string", // " string
  143. regex : '"(?:[^\\\\]|\\\\.)*?"'
  144. }, {
  145. token : "string", // ' string
  146. regex : "'(?:[^\\\\]|\\\\.)*?'"
  147. }, {
  148. token : "constant.numeric", // float
  149. regex : floatNumber
  150. }, {
  151. token : "constant.numeric", // integer
  152. regex : integer + "\\b"
  153. }, {
  154. token : keywordMapper,
  155. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  156. }, {
  157. token : "keyword.operator",
  158. regex : "\\+|\\-|\\*|\\/|%|\\#|\\^|~|<|>|<=|=>|==|~=|=|\\:|\\.\\.\\.|\\.\\."
  159. }, {
  160. token : "paren.lparen",
  161. regex : "[\\[\\(\\{]"
  162. }, {
  163. token : "paren.rparen",
  164. regex : "[\\]\\)\\}]"
  165. }, {
  166. token : "text",
  167. regex : "\\s+|\\w+"
  168. } ]
  169. };
  170. this.normalizeRules();
  171. }
  172. oop.inherits(LuaHighlightRules, TextHighlightRules);
  173. exports.LuaHighlightRules = LuaHighlightRules;
  174. });