ftl_highlight_rules.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 HtmlHighlightRules = require("./html_highlight_rules").HtmlHighlightRules;
  34. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  35. var FtlLangHighlightRules = function () {
  36. var stringBuiltIns = "\\?|substring|cap_first|uncap_first|capitalize|chop_linebreak|date|time|datetime|"
  37. + "ends_with|html|groups|index_of|j_string|js_string|json_string|last_index_of|length|lower_case|"
  38. + "left_pad|right_pad|contains|matches|number|replace|rtf|url|split|starts_with|string|trim|"
  39. + "upper_case|word_list|xhtml|xml";
  40. var numberBuiltIns = "c|round|floor|ceiling";
  41. var dateBuiltIns = "iso_[a-z_]+";
  42. var seqBuiltIns = "first|last|seq_contains|seq_index_of|seq_last_index_of|reverse|size|sort|sort_by|chunk";
  43. var hashBuiltIns = "keys|values";
  44. var xmlBuiltIns = "children|parent|root|ancestors|node_name|node_type|node_namespace";
  45. var expertBuiltIns = "byte|double|float|int|long|short|number_to_date|number_to_time|number_to_datetime|"
  46. + "eval|has_content|interpret|is_[a-z_]+|namespacenew";
  47. var allBuiltIns = stringBuiltIns + numberBuiltIns + dateBuiltIns + seqBuiltIns + hashBuiltIns
  48. + xmlBuiltIns + expertBuiltIns;
  49. var deprecatedBuiltIns = "default|exists|if_exists|web_safe";
  50. var variables = "data_model|error|globals|lang|locale|locals|main|namespace|node|current_node|"
  51. + "now|output_encoding|template_name|url_escaping_charset|vars|version";
  52. var operators = "gt|gte|lt|lte|as|in|using";
  53. var reserved = "true|false";
  54. var attributes = "encoding|parse|locale|number_format|date_format|time_format|datetime_format|time_zone|"
  55. + "url_escaping_charset|classic_compatible|strip_whitespace|strip_text|strict_syntax|ns_prefixes|"
  56. + "attributes";
  57. this.$rules = {
  58. "start" : [{
  59. token : "constant.character.entity",
  60. regex : /&[^;]+;/
  61. }, {
  62. token : "support.function",
  63. regex : "\\?("+allBuiltIns+")"
  64. }, {
  65. token : "support.function.deprecated",
  66. regex : "\\?("+deprecatedBuiltIns+")"
  67. }, {
  68. token : "language.variable",
  69. regex : "\\.(?:"+variables+")"
  70. }, {
  71. token : "constant.language",
  72. regex : "\\b("+reserved+")\\b"
  73. }, {
  74. token : "keyword.operator",
  75. regex : "\\b(?:"+operators+")\\b"
  76. }, {
  77. token : "entity.other.attribute-name",
  78. regex : attributes
  79. }, {
  80. token : "string", //
  81. regex : /['"]/,
  82. next : "qstring"
  83. }, {
  84. // Deal with variable names that contains number
  85. // e.g. <#if var42 == 42 >
  86. token : function(value) {
  87. if (value.match("^[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?$")) {
  88. return "constant.numeric";
  89. } else {
  90. return "variable";
  91. }
  92. },
  93. regex : /[\w.+\-]+/
  94. }, {
  95. token : "keyword.operator",
  96. regex : "!|\\.|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^="
  97. }, {
  98. token : "paren.lparen",
  99. regex : "[[({]"
  100. }, {
  101. token : "paren.rparen",
  102. regex : "[\\])}]"
  103. }, {
  104. token : "text",
  105. regex : "\\s+"
  106. }],
  107. "qstring" : [{
  108. token : "constant.character.escape",
  109. regex : '\\\\[nrtvef\\\\"$]'
  110. }, {
  111. token : "string",
  112. regex : /['"]/,
  113. next : "start"
  114. }, {
  115. defaultToken : "string"
  116. }]
  117. };
  118. };
  119. oop.inherits(FtlLangHighlightRules, TextHighlightRules);
  120. var FtlHighlightRules = function() {
  121. HtmlHighlightRules.call(this);
  122. var directives = "assign|attempt|break|case|compress|default|elseif|else|escape|fallback|function|flush|"
  123. + "ftl|global|if|import|include|list|local|lt|macro|nested|noescape|noparse|nt|recover|recurse|return|rt|"
  124. + "setting|stop|switch|t|visit";
  125. var startRules = [
  126. {
  127. token : "comment",
  128. regex : "<#--",
  129. next : "ftl-dcomment"
  130. }, {
  131. token : "string.interpolated",
  132. regex : "\\${",
  133. push : "ftl-start"
  134. }, {
  135. token : "keyword.function",
  136. regex : "</?#("+directives+")",
  137. push : "ftl-start"
  138. }, {
  139. token : "keyword.other",
  140. regex : "</?@[a-zA-Z\\.]+",
  141. push : "ftl-start"
  142. }
  143. ];
  144. var endRules = [
  145. {
  146. token : "keyword",
  147. regex : "/?>",
  148. next : "pop"
  149. }, {
  150. token : "string.interpolated",
  151. regex : "}",
  152. next : "pop"
  153. }
  154. ];
  155. for (var key in this.$rules)
  156. this.$rules[key].unshift.apply(this.$rules[key], startRules);
  157. this.embedRules(FtlLangHighlightRules, "ftl-", endRules, ["start"]);
  158. this.addRules({
  159. "ftl-dcomment" : [{
  160. token : "comment",
  161. regex : ".*?-->",
  162. next : "pop"
  163. }, {
  164. token : "comment",
  165. regex : ".+"
  166. }]
  167. });
  168. this.normalizeRules();
  169. };
  170. oop.inherits(FtlHighlightRules, HtmlHighlightRules);
  171. exports.FtlHighlightRules = FtlHighlightRules;
  172. });