velocity_highlight_rules.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. define(function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var lang = require("../lib/lang");
  5. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  6. var HtmlHighlightRules = require("./html_highlight_rules").HtmlHighlightRules;
  7. var VelocityHighlightRules = function() {
  8. HtmlHighlightRules.call(this);
  9. var builtinConstants = lang.arrayToMap(
  10. ('true|false|null').split('|')
  11. );
  12. var builtinFunctions = lang.arrayToMap(
  13. ("_DateTool|_DisplayTool|_EscapeTool|_FieldTool|_MathTool|_NumberTool|_SerializerTool|_SortTool|_StringTool|_XPathTool").split('|')
  14. );
  15. var builtinVariables = lang.arrayToMap(
  16. ('$contentRoot|$foreach').split('|')
  17. );
  18. var keywords = lang.arrayToMap(
  19. ("#set|#macro|#include|#parse|" +
  20. "#if|#elseif|#else|#foreach|" +
  21. "#break|#end|#stop"
  22. ).split('|')
  23. );
  24. // regexp must not have capturing parentheses. Use (?:) instead.
  25. // regexps are ordered -> the first match is used
  26. this.$rules.start.push(
  27. {
  28. token : "comment",
  29. regex : "##.*$"
  30. },{
  31. token : "comment.block", // multi line comment
  32. regex : "#\\*",
  33. next : "vm_comment"
  34. }, {
  35. token : "string.regexp",
  36. regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)"
  37. }, {
  38. token : "string", // single line
  39. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  40. }, {
  41. token : "string", // single line
  42. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  43. }, {
  44. token : "constant.numeric", // hex
  45. regex : "0[xX][0-9a-fA-F]+\\b"
  46. }, {
  47. token : "constant.numeric", // float
  48. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  49. }, {
  50. token : "constant.language.boolean",
  51. regex : "(?:true|false)\\b"
  52. }, {
  53. token : function(value) {
  54. if (keywords.hasOwnProperty(value))
  55. return "keyword";
  56. else if (builtinConstants.hasOwnProperty(value))
  57. return "constant.language";
  58. else if (builtinVariables.hasOwnProperty(value))
  59. return "variable.language";
  60. else if (builtinFunctions.hasOwnProperty(value) || builtinFunctions.hasOwnProperty(value.substring(1)))
  61. return "support.function";
  62. else if (value == "debugger")
  63. return "invalid.deprecated";
  64. else
  65. if(value.match(/^(\$[a-zA-Z_][a-zA-Z0-9_]*)$/))
  66. return "variable";
  67. return "identifier";
  68. },
  69. // TODO: Unicode escape sequences
  70. // TODO: Unicode identifiers
  71. regex : "[a-zA-Z$#][a-zA-Z0-9_]*\\b"
  72. }, {
  73. token : "keyword.operator",
  74. regex : "!|&|\\*|\\-|\\+|=|!=|<=|>=|<|>|&&|\\|\\|"
  75. }, {
  76. token : "lparen",
  77. regex : "[[({]"
  78. }, {
  79. token : "rparen",
  80. regex : "[\\])}]"
  81. }, {
  82. token : "text",
  83. regex : "\\s+"
  84. }
  85. );
  86. this.$rules["vm_comment"] = [
  87. {
  88. token : "comment", // closing comment
  89. regex : "\\*#|-->",
  90. next : "start"
  91. }, {
  92. defaultToken: "comment"
  93. }
  94. ];
  95. this.$rules["vm_start"] = [
  96. {
  97. token: "variable",
  98. regex: "}",
  99. next: "pop"
  100. }, {
  101. token : "string.regexp",
  102. regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)"
  103. }, {
  104. token : "string", // single line
  105. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  106. }, {
  107. token : "string", // single line
  108. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  109. }, {
  110. token : "constant.numeric", // hex
  111. regex : "0[xX][0-9a-fA-F]+\\b"
  112. }, {
  113. token : "constant.numeric", // float
  114. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  115. }, {
  116. token : "constant.language.boolean",
  117. regex : "(?:true|false)\\b"
  118. }, {
  119. token : function(value) {
  120. if (keywords.hasOwnProperty(value))
  121. return "keyword";
  122. else if (builtinConstants.hasOwnProperty(value))
  123. return "constant.language";
  124. else if (builtinVariables.hasOwnProperty(value))
  125. return "variable.language";
  126. else if (builtinFunctions.hasOwnProperty(value) || builtinFunctions.hasOwnProperty(value.substring(1)))
  127. return "support.function";
  128. else if (value == "debugger")
  129. return "invalid.deprecated";
  130. else
  131. if(value.match(/^(\$[a-zA-Z_$][a-zA-Z0-9_]*)$/))
  132. return "variable";
  133. return "identifier";
  134. },
  135. // TODO: Unicode escape sequences
  136. // TODO: Unicode identifiers
  137. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  138. }, {
  139. token : "keyword.operator",
  140. regex : "!|&|\\*|\\-|\\+|=|!=|<=|>=|<|>|&&|\\|\\|"
  141. }, {
  142. token : "lparen",
  143. regex : "[[({]"
  144. }, {
  145. token : "rparen",
  146. regex : "[\\])}]"
  147. }, {
  148. token : "text",
  149. regex : "\\s+"
  150. }
  151. ];
  152. for (var i in this.$rules) {
  153. this.$rules[i].unshift({
  154. token: "variable",
  155. regex: "\\${",
  156. push: "vm_start"
  157. });
  158. }
  159. this.normalizeRules();
  160. };
  161. oop.inherits(VelocityHighlightRules, TextHighlightRules);
  162. exports.VelocityHighlightRules = VelocityHighlightRules;
  163. });