less_highlight_rules.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 CssHighlightRules = require('./css_highlight_rules');
  35. var LessHighlightRules = function() {
  36. var keywordList = "@import|@media|@font-face|@keyframes|@-webkit-keyframes|@supports|" +
  37. "@charset|@plugin|@namespace|@document|@page|@viewport|@-ms-viewport|" +
  38. "or|and|when|not";
  39. var keywords = keywordList.split('|');
  40. var properties = CssHighlightRules.supportType.split('|');
  41. var keywordMapper = this.createKeywordMapper({
  42. "support.constant": CssHighlightRules.supportConstant,
  43. "keyword": keywordList,
  44. "support.constant.color": CssHighlightRules.supportConstantColor,
  45. "support.constant.fonts": CssHighlightRules.supportConstantFonts
  46. }, "identifier", true);
  47. // regexp must not have capturing parentheses. Use (?:) instead.
  48. // regexps are ordered -> the first match is used
  49. var numRe = "\\-?(?:(?:[0-9]+)|(?:[0-9]*\\.[0-9]+))";
  50. // regexp must not have capturing parentheses. Use (?:) instead.
  51. // regexps are ordered -> the first match is used
  52. this.$rules = {
  53. "start" : [
  54. {
  55. token : "comment",
  56. regex : "\\/\\/.*$"
  57. },
  58. {
  59. token : "comment", // multi line comment
  60. regex : "\\/\\*",
  61. next : "comment"
  62. }, {
  63. token: "paren.lparen",
  64. regex: "[^@]\\{",
  65. push: "ruleset"
  66. }, {
  67. token : "string", // single line
  68. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  69. }, {
  70. token : "string", // single line
  71. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  72. }, {
  73. token : ["constant.numeric", "keyword"],
  74. regex : "(" + numRe + ")(ch|cm|deg|em|ex|fr|gd|grad|Hz|in|kHz|mm|ms|pc|pt|px|rad|rem|s|turn|vh|vm|vw|%)"
  75. }, {
  76. token : "constant.numeric", // hex6 color
  77. regex : "#[a-f0-9]{6}"
  78. }, {
  79. token : "constant.numeric", // hex3 color
  80. regex : "#[a-f0-9]{3}"
  81. }, {
  82. token : "constant.numeric",
  83. regex : numRe
  84. }, {
  85. token : ["support.function", "paren.lparen", "string", "paren.rparen"],
  86. regex : "(url)(\\()(.*)(\\))"
  87. }, {
  88. token : ["support.function", "paren.lparen"],
  89. regex : "(:extend|[a-z0-9_\\-]+)(\\()"
  90. }, {
  91. token : function(value) {
  92. if (keywords.indexOf(value.toLowerCase()) > -1)
  93. return "keyword";
  94. else
  95. return "variable";
  96. },
  97. regex : "[@\\$][a-z0-9_\\-@\\$]*\\b"
  98. }, {
  99. token : "variable",
  100. regex : "[@\\$]\\{[a-z0-9_\\-@\\$]*\\}"
  101. }, {
  102. token : function(first, second) {
  103. if(properties.indexOf(first.toLowerCase()) > -1) {
  104. return ["support.type.property", "text"];
  105. }
  106. else {
  107. return ["support.type.unknownProperty", "text"];
  108. }
  109. },
  110. regex : "([a-z0-9-_]+)(\\s*:)"
  111. }, {
  112. token : "keyword",
  113. regex : "&" // special case - always treat as keyword
  114. }, {
  115. token : keywordMapper,
  116. regex : "\\-?[@a-z_][@a-z0-9_\\-]*"
  117. }, {
  118. token: "variable.language",
  119. regex: "#[a-z0-9-_]+"
  120. }, {
  121. token: "variable.language",
  122. regex: "\\.[a-z0-9-_]+"
  123. }, {
  124. token: "variable.language",
  125. regex: ":[a-z_][a-z0-9-_]*"
  126. }, {
  127. token: "constant",
  128. regex: "[a-z0-9-_]+"
  129. }, {
  130. token : "keyword.operator",
  131. regex : "<|>|<=|>=|=|!=|-|%|\\+|\\*"
  132. }, {
  133. token : "paren.lparen",
  134. regex : "[[(]"
  135. }, {
  136. token : "paren.rparen",
  137. regex : "[\\])]"
  138. }, {
  139. token : "text",
  140. regex : "\\s+"
  141. }, {
  142. caseInsensitive: true
  143. }
  144. ],
  145. "comment" : [
  146. {
  147. token : "comment", // closing comment
  148. regex : ".*?\\*\\/",
  149. next : "start"
  150. }, {
  151. token : "comment", // comment spanning whole line
  152. regex : ".+"
  153. }
  154. ],
  155. "ruleset" : [
  156. {
  157. token : "paren.rparen",
  158. regex : "\\}",
  159. next: "pop"
  160. }
  161. ]
  162. };
  163. this.$rules.ruleset = this.$rules.ruleset.concat(this.$rules.start);
  164. this.normalizeRules();
  165. };
  166. oop.inherits(LessHighlightRules, TextHighlightRules);
  167. exports.LessHighlightRules = LessHighlightRules;
  168. });