elm_highlight_rules.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2012, 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. // TODO check with https://github.com/deadfoxygrandpa/Elm.tmLanguage
  31. define(function(require, exports, module) {
  32. "use strict";
  33. var oop = require("../lib/oop");
  34. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  35. var ElmHighlightRules = function() {
  36. var keywordMapper = this.createKeywordMapper({
  37. "keyword": "as|case|class|data|default|deriving|do|else|export|foreign|" +
  38. "hiding|jsevent|if|import|in|infix|infixl|infixr|instance|let|" +
  39. "module|newtype|of|open|then|type|where|_|port|\u03BB"
  40. }, "identifier");
  41. var escapeRe = /\\(\d+|['"\\&trnbvf])/;
  42. var smallRe = /[a-z_]/.source;
  43. var largeRe = /[A-Z]/.source;
  44. var idRe = /[a-z_A-Z0-9\']/.source;
  45. this.$rules = {
  46. start: [{
  47. token: "string.start",
  48. regex: '"',
  49. next: "string"
  50. }, {
  51. token: "string.character",
  52. regex: "'(?:" + escapeRe.source + "|.)'?"
  53. }, {
  54. regex: /0(?:[xX][0-9A-Fa-f]+|[oO][0-7]+)|\d+(\.\d+)?([eE][-+]?\d*)?/,
  55. token: "constant.numeric"
  56. }, {
  57. token: "comment",
  58. regex: "--.*"
  59. }, {
  60. token : "keyword",
  61. regex : /\.\.|\||:|=|\\|\"|->|<-|\u2192/
  62. }, {
  63. token : "keyword.operator",
  64. regex : /[-!#$%&*+.\/<=>?@\\^|~:\u03BB\u2192]+/
  65. }, {
  66. token : "operator.punctuation",
  67. regex : /[,;`]/
  68. }, {
  69. regex : largeRe + idRe + "+\\.?",
  70. token : function(value) {
  71. if (value[value.length - 1] == ".")
  72. return "entity.name.function";
  73. return "constant.language";
  74. }
  75. }, {
  76. regex : "^" + smallRe + idRe + "+",
  77. token : function(value) {
  78. return "constant.language";
  79. }
  80. }, {
  81. token : keywordMapper,
  82. regex : "[\\w\\xff-\\u218e\\u2455-\\uffff]+\\b"
  83. }, {
  84. regex: "{-#?",
  85. token: "comment.start",
  86. onMatch: function(value, currentState, stack) {
  87. this.next = value.length == 2 ? "blockComment" : "docComment";
  88. return this.token;
  89. }
  90. }, {
  91. token: "variable.language",
  92. regex: /\[markdown\|/,
  93. next: "markdown"
  94. }, {
  95. token: "paren.lparen",
  96. regex: /[\[({]/
  97. }, {
  98. token: "paren.rparen",
  99. regex: /[\])}]/
  100. }, ],
  101. markdown: [{
  102. regex: /\|\]/,
  103. next: "start"
  104. }, {
  105. defaultToken : "string"
  106. }],
  107. blockComment: [{
  108. regex: "{-",
  109. token: "comment.start",
  110. push: "blockComment"
  111. }, {
  112. regex: "-}",
  113. token: "comment.end",
  114. next: "pop"
  115. }, {
  116. defaultToken: "comment"
  117. }],
  118. docComment: [{
  119. regex: "{-",
  120. token: "comment.start",
  121. push: "docComment"
  122. }, {
  123. regex: "-}",
  124. token: "comment.end",
  125. next: "pop"
  126. }, {
  127. defaultToken: "doc.comment"
  128. }],
  129. string: [{
  130. token: "constant.language.escape",
  131. regex: escapeRe,
  132. }, {
  133. token: "text",
  134. regex: /\\(\s|$)/,
  135. next: "stringGap"
  136. }, {
  137. token: "string.end",
  138. regex: '"',
  139. next: "start"
  140. }],
  141. stringGap: [{
  142. token: "text",
  143. regex: /\\/,
  144. next: "string"
  145. }, {
  146. token: "error",
  147. regex: "",
  148. next: "start"
  149. }],
  150. };
  151. this.normalizeRules();
  152. };
  153. oop.inherits(ElmHighlightRules, TextHighlightRules);
  154. exports.ElmHighlightRules = ElmHighlightRules;
  155. });