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.

359 lines
8.5 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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. 'as': 'keyword',
  15. 'do': 'keyword',
  16. 'else': 'keyword',
  17. 'end': 'keyword',
  18. 'exception': 'keyword',
  19. 'fun': 'keyword',
  20. 'functor': 'keyword',
  21. 'if': 'keyword',
  22. 'in': 'keyword',
  23. 'include': 'keyword',
  24. 'let': 'keyword',
  25. 'of': 'keyword',
  26. 'open': 'keyword',
  27. 'rec': 'keyword',
  28. 'struct': 'keyword',
  29. 'then': 'keyword',
  30. 'type': 'keyword',
  31. 'val': 'keyword',
  32. 'while': 'keyword',
  33. 'with': 'keyword'
  34. };
  35. var extraWords = parserConfig.extraWords || {};
  36. for (var prop in extraWords) {
  37. if (extraWords.hasOwnProperty(prop)) {
  38. words[prop] = parserConfig.extraWords[prop];
  39. }
  40. }
  41. var hintWords = [];
  42. for (var k in words) { hintWords.push(k); }
  43. CodeMirror.registerHelper("hintWords", "mllike", hintWords);
  44. function tokenBase(stream, state) {
  45. var ch = stream.next();
  46. if (ch === '"') {
  47. state.tokenize = tokenString;
  48. return state.tokenize(stream, state);
  49. }
  50. if (ch === '{') {
  51. if (stream.eat('|')) {
  52. state.longString = true;
  53. state.tokenize = tokenLongString;
  54. return state.tokenize(stream, state);
  55. }
  56. }
  57. if (ch === '(') {
  58. if (stream.match(/^\*(?!\))/)) {
  59. state.commentLevel++;
  60. state.tokenize = tokenComment;
  61. return state.tokenize(stream, state);
  62. }
  63. }
  64. if (ch === '~' || ch === '?') {
  65. stream.eatWhile(/\w/);
  66. return 'variable-2';
  67. }
  68. if (ch === '`') {
  69. stream.eatWhile(/\w/);
  70. return 'quote';
  71. }
  72. if (ch === '/' && parserConfig.slashComments && stream.eat('/')) {
  73. stream.skipToEnd();
  74. return 'comment';
  75. }
  76. if (/\d/.test(ch)) {
  77. if (ch === '0' && stream.eat(/[bB]/)) {
  78. stream.eatWhile(/[01]/);
  79. } if (ch === '0' && stream.eat(/[xX]/)) {
  80. stream.eatWhile(/[0-9a-fA-F]/)
  81. } if (ch === '0' && stream.eat(/[oO]/)) {
  82. stream.eatWhile(/[0-7]/);
  83. } else {
  84. stream.eatWhile(/[\d_]/);
  85. if (stream.eat('.')) {
  86. stream.eatWhile(/[\d]/);
  87. }
  88. if (stream.eat(/[eE]/)) {
  89. stream.eatWhile(/[\d\-+]/);
  90. }
  91. }
  92. return 'number';
  93. }
  94. if ( /[+\-*&%=<>!?|@\.~:]/.test(ch)) {
  95. return 'operator';
  96. }
  97. if (/[\w\xa1-\uffff]/.test(ch)) {
  98. stream.eatWhile(/[\w\xa1-\uffff]/);
  99. var cur = stream.current();
  100. return words.hasOwnProperty(cur) ? words[cur] : 'variable';
  101. }
  102. return null
  103. }
  104. function tokenString(stream, state) {
  105. var next, end = false, escaped = false;
  106. while ((next = stream.next()) != null) {
  107. if (next === '"' && !escaped) {
  108. end = true;
  109. break;
  110. }
  111. escaped = !escaped && next === '\\';
  112. }
  113. if (end && !escaped) {
  114. state.tokenize = tokenBase;
  115. }
  116. return 'string';
  117. };
  118. function tokenComment(stream, state) {
  119. var prev, next;
  120. while(state.commentLevel > 0 && (next = stream.next()) != null) {
  121. if (prev === '(' && next === '*') state.commentLevel++;
  122. if (prev === '*' && next === ')') state.commentLevel--;
  123. prev = next;
  124. }
  125. if (state.commentLevel <= 0) {
  126. state.tokenize = tokenBase;
  127. }
  128. return 'comment';
  129. }
  130. function tokenLongString(stream, state) {
  131. var prev, next;
  132. while (state.longString && (next = stream.next()) != null) {
  133. if (prev === '|' && next === '}') state.longString = false;
  134. prev = next;
  135. }
  136. if (!state.longString) {
  137. state.tokenize = tokenBase;
  138. }
  139. return 'string';
  140. }
  141. return {
  142. startState: function() {return {tokenize: tokenBase, commentLevel: 0, longString: false};},
  143. token: function(stream, state) {
  144. if (stream.eatSpace()) return null;
  145. return state.tokenize(stream, state);
  146. },
  147. blockCommentStart: "(*",
  148. blockCommentEnd: "*)",
  149. lineComment: parserConfig.slashComments ? "//" : null
  150. };
  151. });
  152. CodeMirror.defineMIME('text/x-ocaml', {
  153. name: 'mllike',
  154. extraWords: {
  155. 'and': 'keyword',
  156. 'assert': 'keyword',
  157. 'begin': 'keyword',
  158. 'class': 'keyword',
  159. 'constraint': 'keyword',
  160. 'done': 'keyword',
  161. 'downto': 'keyword',
  162. 'external': 'keyword',
  163. 'function': 'keyword',
  164. 'initializer': 'keyword',
  165. 'lazy': 'keyword',
  166. 'match': 'keyword',
  167. 'method': 'keyword',
  168. 'module': 'keyword',
  169. 'mutable': 'keyword',
  170. 'new': 'keyword',
  171. 'nonrec': 'keyword',
  172. 'object': 'keyword',
  173. 'private': 'keyword',
  174. 'sig': 'keyword',
  175. 'to': 'keyword',
  176. 'try': 'keyword',
  177. 'value': 'keyword',
  178. 'virtual': 'keyword',
  179. 'when': 'keyword',
  180. // builtins
  181. 'raise': 'builtin',
  182. 'failwith': 'builtin',
  183. 'true': 'builtin',
  184. 'false': 'builtin',
  185. // Pervasives builtins
  186. 'asr': 'builtin',
  187. 'land': 'builtin',
  188. 'lor': 'builtin',
  189. 'lsl': 'builtin',
  190. 'lsr': 'builtin',
  191. 'lxor': 'builtin',
  192. 'mod': 'builtin',
  193. 'or': 'builtin',
  194. // More Pervasives
  195. 'raise_notrace': 'builtin',
  196. 'trace': 'builtin',
  197. 'exit': 'builtin',
  198. 'print_string': 'builtin',
  199. 'print_endline': 'builtin',
  200. 'int': 'type',
  201. 'float': 'type',
  202. 'bool': 'type',
  203. 'char': 'type',
  204. 'string': 'type',
  205. 'unit': 'type',
  206. // Modules
  207. 'List': 'builtin'
  208. }
  209. });
  210. CodeMirror.defineMIME('text/x-fsharp', {
  211. name: 'mllike',
  212. extraWords: {
  213. 'abstract': 'keyword',
  214. 'assert': 'keyword',
  215. 'base': 'keyword',
  216. 'begin': 'keyword',
  217. 'class': 'keyword',
  218. 'default': 'keyword',
  219. 'delegate': 'keyword',
  220. 'do!': 'keyword',
  221. 'done': 'keyword',
  222. 'downcast': 'keyword',
  223. 'downto': 'keyword',
  224. 'elif': 'keyword',
  225. 'extern': 'keyword',
  226. 'finally': 'keyword',
  227. 'for': 'keyword',
  228. 'function': 'keyword',
  229. 'global': 'keyword',
  230. 'inherit': 'keyword',
  231. 'inline': 'keyword',
  232. 'interface': 'keyword',
  233. 'internal': 'keyword',
  234. 'lazy': 'keyword',
  235. 'let!': 'keyword',
  236. 'match': 'keyword',
  237. 'member': 'keyword',
  238. 'module': 'keyword',
  239. 'mutable': 'keyword',
  240. 'namespace': 'keyword',
  241. 'new': 'keyword',
  242. 'null': 'keyword',
  243. 'override': 'keyword',
  244. 'private': 'keyword',
  245. 'public': 'keyword',
  246. 'return!': 'keyword',
  247. 'return': 'keyword',
  248. 'select': 'keyword',
  249. 'static': 'keyword',
  250. 'to': 'keyword',
  251. 'try': 'keyword',
  252. 'upcast': 'keyword',
  253. 'use!': 'keyword',
  254. 'use': 'keyword',
  255. 'void': 'keyword',
  256. 'when': 'keyword',
  257. 'yield!': 'keyword',
  258. 'yield': 'keyword',
  259. // Reserved words
  260. 'atomic': 'keyword',
  261. 'break': 'keyword',
  262. 'checked': 'keyword',
  263. 'component': 'keyword',
  264. 'const': 'keyword',
  265. 'constraint': 'keyword',
  266. 'constructor': 'keyword',
  267. 'continue': 'keyword',
  268. 'eager': 'keyword',
  269. 'event': 'keyword',
  270. 'external': 'keyword',
  271. 'fixed': 'keyword',
  272. 'method': 'keyword',
  273. 'mixin': 'keyword',
  274. 'object': 'keyword',
  275. 'parallel': 'keyword',
  276. 'process': 'keyword',
  277. 'protected': 'keyword',
  278. 'pure': 'keyword',
  279. 'sealed': 'keyword',
  280. 'tailcall': 'keyword',
  281. 'trait': 'keyword',
  282. 'virtual': 'keyword',
  283. 'volatile': 'keyword',
  284. // builtins
  285. 'List': 'builtin',
  286. 'Seq': 'builtin',
  287. 'Map': 'builtin',
  288. 'Set': 'builtin',
  289. 'Option': 'builtin',
  290. 'int': 'builtin',
  291. 'string': 'builtin',
  292. 'not': 'builtin',
  293. 'true': 'builtin',
  294. 'false': 'builtin',
  295. 'raise': 'builtin',
  296. 'failwith': 'builtin'
  297. },
  298. slashComments: true
  299. });
  300. CodeMirror.defineMIME('text/x-sml', {
  301. name: 'mllike',
  302. extraWords: {
  303. 'abstype': 'keyword',
  304. 'and': 'keyword',
  305. 'andalso': 'keyword',
  306. 'case': 'keyword',
  307. 'datatype': 'keyword',
  308. 'fn': 'keyword',
  309. 'handle': 'keyword',
  310. 'infix': 'keyword',
  311. 'infixr': 'keyword',
  312. 'local': 'keyword',
  313. 'nonfix': 'keyword',
  314. 'op': 'keyword',
  315. 'orelse': 'keyword',
  316. 'raise': 'keyword',
  317. 'withtype': 'keyword',
  318. 'eqtype': 'keyword',
  319. 'sharing': 'keyword',
  320. 'sig': 'keyword',
  321. 'signature': 'keyword',
  322. 'structure': 'keyword',
  323. 'where': 'keyword',
  324. 'true': 'keyword',
  325. 'false': 'keyword',
  326. // types
  327. 'int': 'builtin',
  328. 'real': 'builtin',
  329. 'string': 'builtin',
  330. 'char': 'builtin',
  331. 'bool': 'builtin'
  332. },
  333. slashComments: true
  334. });
  335. });