mllike.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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('mllike', function(_config, parserConfig) {
  13. var words = {
  14. 'let': 'keyword',
  15. 'rec': 'keyword',
  16. 'in': 'keyword',
  17. 'of': 'keyword',
  18. 'and': 'keyword',
  19. 'if': 'keyword',
  20. 'then': 'keyword',
  21. 'else': 'keyword',
  22. 'for': 'keyword',
  23. 'to': 'keyword',
  24. 'while': 'keyword',
  25. 'do': 'keyword',
  26. 'done': 'keyword',
  27. 'fun': 'keyword',
  28. 'function': 'keyword',
  29. 'val': 'keyword',
  30. 'type': 'keyword',
  31. 'mutable': 'keyword',
  32. 'match': 'keyword',
  33. 'with': 'keyword',
  34. 'try': 'keyword',
  35. 'open': 'builtin',
  36. 'ignore': 'builtin',
  37. 'begin': 'keyword',
  38. 'end': 'keyword'
  39. };
  40. var extraWords = parserConfig.extraWords || {};
  41. for (var prop in extraWords) {
  42. if (extraWords.hasOwnProperty(prop)) {
  43. words[prop] = parserConfig.extraWords[prop];
  44. }
  45. }
  46. function tokenBase(stream, state) {
  47. var ch = stream.next();
  48. if (ch === '"') {
  49. state.tokenize = tokenString;
  50. return state.tokenize(stream, state);
  51. }
  52. if (ch === '(') {
  53. if (stream.eat('*')) {
  54. state.commentLevel++;
  55. state.tokenize = tokenComment;
  56. return state.tokenize(stream, state);
  57. }
  58. }
  59. if (ch === '~') {
  60. stream.eatWhile(/\w/);
  61. return 'variable-2';
  62. }
  63. if (ch === '`') {
  64. stream.eatWhile(/\w/);
  65. return 'quote';
  66. }
  67. if (ch === '/' && parserConfig.slashComments && stream.eat('/')) {
  68. stream.skipToEnd();
  69. return 'comment';
  70. }
  71. if (/\d/.test(ch)) {
  72. stream.eatWhile(/[\d]/);
  73. if (stream.eat('.')) {
  74. stream.eatWhile(/[\d]/);
  75. }
  76. return 'number';
  77. }
  78. if ( /[+\-*&%=<>!?|]/.test(ch)) {
  79. return 'operator';
  80. }
  81. stream.eatWhile(/\w/);
  82. var cur = stream.current();
  83. return words.hasOwnProperty(cur) ? words[cur] : 'variable';
  84. }
  85. function tokenString(stream, state) {
  86. var next, end = false, escaped = false;
  87. while ((next = stream.next()) != null) {
  88. if (next === '"' && !escaped) {
  89. end = true;
  90. break;
  91. }
  92. escaped = !escaped && next === '\\';
  93. }
  94. if (end && !escaped) {
  95. state.tokenize = tokenBase;
  96. }
  97. return 'string';
  98. };
  99. function tokenComment(stream, state) {
  100. var prev, next;
  101. while(state.commentLevel > 0 && (next = stream.next()) != null) {
  102. if (prev === '(' && next === '*') state.commentLevel++;
  103. if (prev === '*' && next === ')') state.commentLevel--;
  104. prev = next;
  105. }
  106. if (state.commentLevel <= 0) {
  107. state.tokenize = tokenBase;
  108. }
  109. return 'comment';
  110. }
  111. return {
  112. startState: function() {return {tokenize: tokenBase, commentLevel: 0};},
  113. token: function(stream, state) {
  114. if (stream.eatSpace()) return null;
  115. return state.tokenize(stream, state);
  116. },
  117. blockCommentStart: "(*",
  118. blockCommentEnd: "*)",
  119. lineComment: parserConfig.slashComments ? "//" : null
  120. };
  121. });
  122. CodeMirror.defineMIME('text/x-ocaml', {
  123. name: 'mllike',
  124. extraWords: {
  125. 'succ': 'keyword',
  126. 'trace': 'builtin',
  127. 'exit': 'builtin',
  128. 'print_string': 'builtin',
  129. 'print_endline': 'builtin',
  130. 'true': 'atom',
  131. 'false': 'atom',
  132. 'raise': 'keyword'
  133. }
  134. });
  135. CodeMirror.defineMIME('text/x-fsharp', {
  136. name: 'mllike',
  137. extraWords: {
  138. 'abstract': 'keyword',
  139. 'as': 'keyword',
  140. 'assert': 'keyword',
  141. 'base': 'keyword',
  142. 'class': 'keyword',
  143. 'default': 'keyword',
  144. 'delegate': 'keyword',
  145. 'downcast': 'keyword',
  146. 'downto': 'keyword',
  147. 'elif': 'keyword',
  148. 'exception': 'keyword',
  149. 'extern': 'keyword',
  150. 'finally': 'keyword',
  151. 'global': 'keyword',
  152. 'inherit': 'keyword',
  153. 'inline': 'keyword',
  154. 'interface': 'keyword',
  155. 'internal': 'keyword',
  156. 'lazy': 'keyword',
  157. 'let!': 'keyword',
  158. 'member' : 'keyword',
  159. 'module': 'keyword',
  160. 'namespace': 'keyword',
  161. 'new': 'keyword',
  162. 'null': 'keyword',
  163. 'override': 'keyword',
  164. 'private': 'keyword',
  165. 'public': 'keyword',
  166. 'return': 'keyword',
  167. 'return!': 'keyword',
  168. 'select': 'keyword',
  169. 'static': 'keyword',
  170. 'struct': 'keyword',
  171. 'upcast': 'keyword',
  172. 'use': 'keyword',
  173. 'use!': 'keyword',
  174. 'val': 'keyword',
  175. 'when': 'keyword',
  176. 'yield': 'keyword',
  177. 'yield!': 'keyword',
  178. 'List': 'builtin',
  179. 'Seq': 'builtin',
  180. 'Map': 'builtin',
  181. 'Set': 'builtin',
  182. 'int': 'builtin',
  183. 'string': 'builtin',
  184. 'raise': 'builtin',
  185. 'failwith': 'builtin',
  186. 'not': 'builtin',
  187. 'true': 'builtin',
  188. 'false': 'builtin'
  189. },
  190. slashComments: true
  191. });
  192. });