twig_highlight_rules.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2013, 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 lang = require("../lib/lang");
  34. var HtmlHighlightRules = require("./html_highlight_rules").HtmlHighlightRules;
  35. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  36. var TwigHighlightRules = function() {
  37. // inherit from html
  38. HtmlHighlightRules.call(this);
  39. var tags = "autoescape|block|do|embed|extends|filter|flush|for|from|if|import|include|macro|sandbox|set|spaceless|use|verbatim";
  40. tags = tags + "|end" + tags.replace(/\|/g, "|end");
  41. var filters = "abs|batch|capitalize|convert_encoding|date|date_modify|default|e|escape|first|format|join|json_encode|keys|last|length|lower|merge|nl2br|number_format|raw|replace|reverse|slice|sort|split|striptags|title|trim|upper|url_encode";
  42. var functions = "attribute|constant|cycle|date|dump|parent|random|range|template_from_string";
  43. var tests = "constant|divisibleby|sameas|defined|empty|even|iterable|odd";
  44. var constants = "null|none|true|false";
  45. var operators = "b-and|b-xor|b-or|in|is|and|or|not"
  46. var keywordMapper = this.createKeywordMapper({
  47. "keyword.control.twig": tags,
  48. "support.function.twig": [filters, functions, tests].join("|"),
  49. "keyword.operator.twig": operators,
  50. "constant.language.twig": constants
  51. }, "identifier");
  52. // add twig start tags to the HTML start tags
  53. for (var rule in this.$rules) {
  54. this.$rules[rule].unshift({
  55. token : "variable.other.readwrite.local.twig",
  56. regex : "\\{\\{-?",
  57. push : "twig-start"
  58. }, {
  59. token : "meta.tag.twig",
  60. regex : "\\{%-?",
  61. push : "twig-start"
  62. }, {
  63. token : "comment.block.twig",
  64. regex : "\\{#-?",
  65. push : "twig-comment"
  66. });
  67. }
  68. // add twig closing comment to HTML comments
  69. this.$rules["twig-comment"] = [{
  70. token : "comment.block.twig",
  71. regex : ".*-?#\\}",
  72. next : "pop"
  73. }];
  74. this.$rules["twig-start"] = [{
  75. token : "variable.other.readwrite.local.twig",
  76. regex : "-?\\}\\}",
  77. next : "pop"
  78. }, {
  79. token : "meta.tag.twig",
  80. regex : "-?%\\}",
  81. next : "pop"
  82. }, {
  83. token : "string",
  84. regex : "'",
  85. next : "twig-qstring"
  86. }, {
  87. token : "string",
  88. regex : '"',
  89. next : "twig-qqstring"
  90. }, {
  91. token : "constant.numeric", // hex
  92. regex : "0[xX][0-9a-fA-F]+\\b"
  93. }, {
  94. token : "constant.numeric", // float
  95. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  96. }, {
  97. token : "constant.language.boolean",
  98. regex : "(?:true|false)\\b"
  99. }, {
  100. token : keywordMapper,
  101. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  102. }, {
  103. token : "keyword.operator.assignment",
  104. regex : "=|~"
  105. }, {
  106. token : "keyword.operator.comparison",
  107. regex : "==|!=|<|>|>=|<=|==="
  108. }, {
  109. token : "keyword.operator.arithmetic",
  110. regex : "\\+|-|/|%|//|\\*|\\*\\*"
  111. }, {
  112. token : "keyword.operator.other",
  113. regex : "\\.\\.|\\|"
  114. }, {
  115. token : "punctuation.operator",
  116. regex : /\?|\:|\,|\;|\./
  117. }, {
  118. token : "paren.lparen",
  119. regex : /[\[\({]/
  120. }, {
  121. token : "paren.rparen",
  122. regex : /[\])}]/
  123. }, {
  124. token : "text",
  125. regex : "\\s+"
  126. } ];
  127. this.$rules["twig-qqstring"] = [{
  128. token : "constant.language.escape",
  129. regex : /\\[\\"$#ntr]|#{[^"}]*}/
  130. }, {
  131. token : "string",
  132. regex : '"',
  133. next : "twig-start"
  134. }, {
  135. defaultToken : "string"
  136. }
  137. ];
  138. this.$rules["twig-qstring"] = [{
  139. token : "constant.language.escape",
  140. regex : /\\[\\'ntr]}/
  141. }, {
  142. token : "string",
  143. regex : "'",
  144. next : "twig-start"
  145. }, {
  146. defaultToken : "string"
  147. }
  148. ];
  149. this.normalizeRules();
  150. };
  151. oop.inherits(TwigHighlightRules, TextHighlightRules);
  152. exports.TwigHighlightRules = TwigHighlightRules;
  153. });