You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
6.5 KiB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("oz", function (conf) {
  13. function wordRegexp(words) {
  14. return new RegExp("^((" + words.join(")|(") + "))\\b");
  15. }
  16. var singleOperators = /[\^@!\|<>#~\.\*\-\+\\/,=]/;
  17. var doubleOperators = /(<-)|(:=)|(=<)|(>=)|(<=)|(<:)|(>:)|(=:)|(\\=)|(\\=:)|(!!)|(==)|(::)/;
  18. var tripleOperators = /(:::)|(\.\.\.)|(=<:)|(>=:)/;
  19. var middle = ["in", "then", "else", "of", "elseof", "elsecase", "elseif", "catch",
  20. "finally", "with", "require", "prepare", "import", "export", "define", "do"];
  21. var end = ["end"];
  22. var atoms = wordRegexp(["true", "false", "nil", "unit"]);
  23. var commonKeywords = wordRegexp(["andthen", "at", "attr", "declare", "feat", "from", "lex",
  24. "mod", "mode", "orelse", "parser", "prod", "prop", "scanner", "self", "syn", "token"]);
  25. var openingKeywords = wordRegexp(["local", "proc", "fun", "case", "class", "if", "cond", "or", "dis",
  26. "choice", "not", "thread", "try", "raise", "lock", "for", "suchthat", "meth", "functor"]);
  27. var middleKeywords = wordRegexp(middle);
  28. var endKeywords = wordRegexp(end);
  29. // Tokenizers
  30. function tokenBase(stream, state) {
  31. if (stream.eatSpace()) {
  32. return null;
  33. }
  34. // Brackets
  35. if(stream.match(/[{}]/)) {
  36. return "bracket";
  37. }
  38. // Special [] keyword
  39. if (stream.match(/(\[])/)) {
  40. return "keyword"
  41. }
  42. // Operators
  43. if (stream.match(tripleOperators) || stream.match(doubleOperators)) {
  44. return "operator";
  45. }
  46. // Atoms
  47. if(stream.match(atoms)) {
  48. return 'atom';
  49. }
  50. // Opening keywords
  51. var matched = stream.match(openingKeywords);
  52. if (matched) {
  53. if (!state.doInCurrentLine)
  54. state.currentIndent++;
  55. else
  56. state.doInCurrentLine = false;
  57. // Special matching for signatures
  58. if(matched[0] == "proc" || matched[0] == "fun")
  59. state.tokenize = tokenFunProc;
  60. else if(matched[0] == "class")
  61. state.tokenize = tokenClass;
  62. else if(matched[0] == "meth")
  63. state.tokenize = tokenMeth;
  64. return 'keyword';
  65. }
  66. // Middle and other keywords
  67. if (stream.match(middleKeywords) || stream.match(commonKeywords)) {
  68. return "keyword"
  69. }
  70. // End keywords
  71. if (stream.match(endKeywords)) {
  72. state.currentIndent--;
  73. return 'keyword';
  74. }
  75. // Eat the next char for next comparisons
  76. var ch = stream.next();
  77. // Strings
  78. if (ch == '"' || ch == "'") {
  79. state.tokenize = tokenString(ch);
  80. return state.tokenize(stream, state);
  81. }
  82. // Numbers
  83. if (/[~\d]/.test(ch)) {
  84. if (ch == "~") {
  85. if(! /^[0-9]/.test(stream.peek()))
  86. return null;
  87. else if (( stream.next() == "0" && stream.match(/^[xX][0-9a-fA-F]+/)) || stream.match(/^[0-9]*(\.[0-9]+)?([eE][~+]?[0-9]+)?/))
  88. return "number";
  89. }
  90. if ((ch == "0" && stream.match(/^[xX][0-9a-fA-F]+/)) || stream.match(/^[0-9]*(\.[0-9]+)?([eE][~+]?[0-9]+)?/))
  91. return "number";
  92. return null;
  93. }
  94. // Comments
  95. if (ch == "%") {
  96. stream.skipToEnd();
  97. return 'comment';
  98. }
  99. else if (ch == "/") {
  100. if (stream.eat("*")) {
  101. state.tokenize = tokenComment;
  102. return tokenComment(stream, state);
  103. }
  104. }
  105. // Single operators
  106. if(singleOperators.test(ch)) {
  107. return "operator";
  108. }
  109. // If nothing match, we skip the entire alphanumerical block
  110. stream.eatWhile(/\w/);
  111. return "variable";
  112. }
  113. function tokenClass(stream, state) {
  114. if (stream.eatSpace()) {
  115. return null;
  116. }
  117. stream.match(/([A-Z][A-Za-z0-9_]*)|(`.+`)/);
  118. state.tokenize = tokenBase;
  119. return "variable-3"
  120. }
  121. function tokenMeth(stream, state) {
  122. if (stream.eatSpace()) {
  123. return null;
  124. }
  125. stream.match(/([a-zA-Z][A-Za-z0-9_]*)|(`.+`)/);
  126. state.tokenize = tokenBase;
  127. return "def"
  128. }
  129. function tokenFunProc(stream, state) {
  130. if (stream.eatSpace()) {
  131. return null;
  132. }
  133. if(!state.hasPassedFirstStage && stream.eat("{")) {
  134. state.hasPassedFirstStage = true;
  135. return "bracket";
  136. }
  137. else if(state.hasPassedFirstStage) {
  138. stream.match(/([A-Z][A-Za-z0-9_]*)|(`.+`)|\$/);
  139. state.hasPassedFirstStage = false;
  140. state.tokenize = tokenBase;
  141. return "def"
  142. }
  143. else {
  144. state.tokenize = tokenBase;
  145. return null;
  146. }
  147. }
  148. function tokenComment(stream, state) {
  149. var maybeEnd = false, ch;
  150. while (ch = stream.next()) {
  151. if (ch == "/" && maybeEnd) {
  152. state.tokenize = tokenBase;
  153. break;
  154. }
  155. maybeEnd = (ch == "*");
  156. }
  157. return "comment";
  158. }
  159. function tokenString(quote) {
  160. return function (stream, state) {
  161. var escaped = false, next, end = false;
  162. while ((next = stream.next()) != null) {
  163. if (next == quote && !escaped) {
  164. end = true;
  165. break;
  166. }
  167. escaped = !escaped && next == "\\";
  168. }
  169. if (end || !escaped)
  170. state.tokenize = tokenBase;
  171. return "string";
  172. };
  173. }
  174. function buildElectricInputRegEx() {
  175. // Reindentation should occur on [] or on a match of any of
  176. // the block closing keywords, at the end of a line.
  177. var allClosings = middle.concat(end);
  178. return new RegExp("[\\[\\]]|(" + allClosings.join("|") + ")$");
  179. }
  180. return {
  181. startState: function () {
  182. return {
  183. tokenize: tokenBase,
  184. currentIndent: 0,
  185. doInCurrentLine: false,
  186. hasPassedFirstStage: false
  187. };
  188. },
  189. token: function (stream, state) {
  190. if (stream.sol())
  191. state.doInCurrentLine = 0;
  192. return state.tokenize(stream, state);
  193. },
  194. indent: function (state, textAfter) {
  195. var trueText = textAfter.replace(/^\s+|\s+$/g, '');
  196. if (trueText.match(endKeywords) || trueText.match(middleKeywords) || trueText.match(/(\[])/))
  197. return conf.indentUnit * (state.currentIndent - 1);
  198. if (state.currentIndent < 0)
  199. return 0;
  200. return state.currentIndent * conf.indentUnit;
  201. },
  202. fold: "indent",
  203. electricInput: buildElectricInputRegEx(),
  204. lineComment: "%",
  205. blockCommentStart: "/*",
  206. blockCommentEnd: "*/"
  207. };
  208. });
  209. CodeMirror.defineMIME("text/x-oz", "oz");
  210. });