1
0

sieve.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * See LICENSE in this directory for the license under which this code
  3. * is released.
  4. */
  5. CodeMirror.defineMode("sieve", function(config) {
  6. function words(str) {
  7. var obj = {}, words = str.split(" ");
  8. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  9. return obj;
  10. }
  11. var keywords = words("if elsif else stop require");
  12. var atoms = words("true false not");
  13. var indentUnit = config.indentUnit;
  14. function tokenBase(stream, state) {
  15. var ch = stream.next();
  16. if (ch == "/" && stream.eat("*")) {
  17. state.tokenize = tokenCComment;
  18. return tokenCComment(stream, state);
  19. }
  20. if (ch === '#') {
  21. stream.skipToEnd();
  22. return "comment";
  23. }
  24. if (ch == "\"") {
  25. state.tokenize = tokenString(ch);
  26. return state.tokenize(stream, state);
  27. }
  28. if (ch == "(") {
  29. state._indent.push("(");
  30. // add virtual angel wings so that editor behaves...
  31. // ...more sane incase of broken brackets
  32. state._indent.push("{");
  33. return null;
  34. }
  35. if (ch === "{") {
  36. state._indent.push("{");
  37. return null;
  38. }
  39. if (ch == ")") {
  40. state._indent.pop();
  41. state._indent.pop();
  42. }
  43. if (ch === "}") {
  44. state._indent.pop();
  45. return null;
  46. }
  47. if (ch == ",")
  48. return null;
  49. if (ch == ";")
  50. return null;
  51. if (/[{}\(\),;]/.test(ch))
  52. return null;
  53. // 1*DIGIT "K" / "M" / "G"
  54. if (/\d/.test(ch)) {
  55. stream.eatWhile(/[\d]/);
  56. stream.eat(/[KkMmGg]/);
  57. return "number";
  58. }
  59. // ":" (ALPHA / "_") *(ALPHA / DIGIT / "_")
  60. if (ch == ":") {
  61. stream.eatWhile(/[a-zA-Z_]/);
  62. stream.eatWhile(/[a-zA-Z0-9_]/);
  63. return "operator";
  64. }
  65. stream.eatWhile(/\w/);
  66. var cur = stream.current();
  67. // "text:" *(SP / HTAB) (hash-comment / CRLF)
  68. // *(multiline-literal / multiline-dotstart)
  69. // "." CRLF
  70. if ((cur == "text") && stream.eat(":"))
  71. {
  72. state.tokenize = tokenMultiLineString;
  73. return "string";
  74. }
  75. if (keywords.propertyIsEnumerable(cur))
  76. return "keyword";
  77. if (atoms.propertyIsEnumerable(cur))
  78. return "atom";
  79. return null;
  80. }
  81. function tokenMultiLineString(stream, state)
  82. {
  83. state._multiLineString = true;
  84. // the first line is special it may contain a comment
  85. if (!stream.sol()) {
  86. stream.eatSpace();
  87. if (stream.peek() == "#") {
  88. stream.skipToEnd();
  89. return "comment";
  90. }
  91. stream.skipToEnd();
  92. return "string";
  93. }
  94. if ((stream.next() == ".") && (stream.eol()))
  95. {
  96. state._multiLineString = false;
  97. state.tokenize = tokenBase;
  98. }
  99. return "string";
  100. }
  101. function tokenCComment(stream, state) {
  102. var maybeEnd = false, ch;
  103. while ((ch = stream.next()) != null) {
  104. if (maybeEnd && ch == "/") {
  105. state.tokenize = tokenBase;
  106. break;
  107. }
  108. maybeEnd = (ch == "*");
  109. }
  110. return "comment";
  111. }
  112. function tokenString(quote) {
  113. return function(stream, state) {
  114. var escaped = false, ch;
  115. while ((ch = stream.next()) != null) {
  116. if (ch == quote && !escaped)
  117. break;
  118. escaped = !escaped && ch == "\\";
  119. }
  120. if (!escaped) state.tokenize = tokenBase;
  121. return "string";
  122. };
  123. }
  124. return {
  125. startState: function(base) {
  126. return {tokenize: tokenBase,
  127. baseIndent: base || 0,
  128. _indent: []};
  129. },
  130. token: function(stream, state) {
  131. if (stream.eatSpace())
  132. return null;
  133. return (state.tokenize || tokenBase)(stream, state);;
  134. },
  135. indent: function(state, _textAfter) {
  136. var length = state._indent.length;
  137. if (_textAfter && (_textAfter[0] == "}"))
  138. length--;
  139. if (length <0)
  140. length = 0;
  141. return length * indentUnit;
  142. },
  143. electricChars: "}"
  144. };
  145. });
  146. CodeMirror.defineMIME("application/sieve", "sieve");