all repos — NoPaste @ 3e3c433f026c49e04d0efa0f0f39fc688ed32bb1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/julia/julia.js (view raw)

  1// CodeMirror, copyright (c) by Marijn Haverbeke and others
  2// Distributed under an MIT license: https://codemirror.net/LICENSE
  3
  4(function(mod) {
  5  if (typeof exports == "object" && typeof module == "object") // CommonJS
  6    mod(require("../../lib/codemirror"));
  7  else if (typeof define == "function" && define.amd) // AMD
  8    define(["../../lib/codemirror"], mod);
  9  else // Plain browser env
 10    mod(CodeMirror);
 11})(function(CodeMirror) {
 12"use strict";
 13
 14CodeMirror.defineMode("julia", function(config, parserConf) {
 15  function wordRegexp(words, end) {
 16    if (typeof end === "undefined") { end = "\\b"; }
 17    return new RegExp("^((" + words.join(")|(") + "))" + end);
 18  }
 19
 20  var octChar = "\\\\[0-7]{1,3}";
 21  var hexChar = "\\\\x[A-Fa-f0-9]{1,2}";
 22  var sChar = "\\\\[abefnrtv0%?'\"\\\\]";
 23  var uChar = "([^\\u0027\\u005C\\uD800-\\uDFFF]|[\\uD800-\\uDFFF][\\uDC00-\\uDFFF])";
 24
 25  var operators = parserConf.operators || wordRegexp([
 26        "[<>]:", "[<>=]=", "<<=?", ">>>?=?", "=>", "->", "\\/\\/",
 27        "[\\\\%*+\\-<>!=\\/^|&\\u00F7\\u22BB]=?", "\\?", "\\$", "~", ":",
 28        "\\u00D7", "\\u2208", "\\u2209", "\\u220B", "\\u220C", "\\u2218",
 29        "\\u221A", "\\u221B", "\\u2229", "\\u222A", "\\u2260", "\\u2264",
 30        "\\u2265", "\\u2286", "\\u2288", "\\u228A", "\\u22C5",
 31        "\\b(in|isa)\\b(?!\.?\\()"], "");
 32  var delimiters = parserConf.delimiters || /^[;,()[\]{}]/;
 33  var identifiers = parserConf.identifiers ||
 34        /^[_A-Za-z\u00A1-\u2217\u2219-\uFFFF][\w\u00A1-\u2217\u2219-\uFFFF]*!*/;
 35
 36  var chars = wordRegexp([octChar, hexChar, sChar, uChar], "'");
 37
 38  var openersList = ["begin", "function", "type", "struct", "immutable", "let",
 39        "macro", "for", "while", "quote", "if", "else", "elseif", "try",
 40        "finally", "catch", "do"];
 41
 42  var closersList = ["end", "else", "elseif", "catch", "finally"];
 43
 44  var keywordsList = ["if", "else", "elseif", "while", "for", "begin", "let",
 45        "end", "do", "try", "catch", "finally", "return", "break", "continue",
 46        "global", "local", "const", "export", "import", "importall", "using",
 47        "function", "where", "macro", "module", "baremodule", "struct", "type",
 48        "mutable", "immutable", "quote", "typealias", "abstract", "primitive",
 49        "bitstype"];
 50
 51  var builtinsList = ["true", "false", "nothing", "NaN", "Inf"];
 52
 53  CodeMirror.registerHelper("hintWords", "julia", keywordsList.concat(builtinsList));
 54
 55  var openers = wordRegexp(openersList);
 56  var closers = wordRegexp(closersList);
 57  var keywords = wordRegexp(keywordsList);
 58  var builtins = wordRegexp(builtinsList);
 59
 60  var macro = /^@[_A-Za-z][\w]*/;
 61  var symbol = /^:[_A-Za-z\u00A1-\uFFFF][\w\u00A1-\uFFFF]*!*/;
 62  var stringPrefixes = /^(`|([_A-Za-z\u00A1-\uFFFF]*"("")?))/;
 63
 64  function inArray(state) {
 65    return (state.nestedArrays > 0);
 66  }
 67
 68  function inGenerator(state) {
 69    return (state.nestedGenerators > 0);
 70  }
 71
 72  function currentScope(state, n) {
 73    if (typeof(n) === "undefined") { n = 0; }
 74    if (state.scopes.length <= n) {
 75      return null;
 76    }
 77    return state.scopes[state.scopes.length - (n + 1)];
 78  }
 79
 80  // tokenizers
 81  function tokenBase(stream, state) {
 82    // Handle multiline comments
 83    if (stream.match(/^#=/, false)) {
 84      state.tokenize = tokenComment;
 85      return state.tokenize(stream, state);
 86    }
 87
 88    // Handle scope changes
 89    var leavingExpr = state.leavingExpr;
 90    if (stream.sol()) {
 91      leavingExpr = false;
 92    }
 93    state.leavingExpr = false;
 94
 95    if (leavingExpr) {
 96      if (stream.match(/^'+/)) {
 97        return "operator";
 98      }
 99    }
100
101    if (stream.match(/\.{4,}/)) {
102      return "error";
103    } else if (stream.match(/\.{1,3}/)) {
104      return "operator";
105    }
106
107    if (stream.eatSpace()) {
108      return null;
109    }
110
111    var ch = stream.peek();
112
113    // Handle single line comments
114    if (ch === '#') {
115      stream.skipToEnd();
116      return "comment";
117    }
118
119    if (ch === '[') {
120      state.scopes.push('[');
121      state.nestedArrays++;
122    }
123
124    if (ch === '(') {
125      state.scopes.push('(');
126      state.nestedGenerators++;
127    }
128
129    if (inArray(state) && ch === ']') {
130      while (state.scopes.length && currentScope(state) !== "[") { state.scopes.pop(); }
131      state.scopes.pop();
132      state.nestedArrays--;
133      state.leavingExpr = true;
134    }
135
136    if (inGenerator(state) && ch === ')') {
137      while (state.scopes.length && currentScope(state) !== "(") { state.scopes.pop(); }
138      state.scopes.pop();
139      state.nestedGenerators--;
140      state.leavingExpr = true;
141    }
142
143    if (inArray(state)) {
144      if (state.lastToken == "end" && stream.match(/^:/)) {
145        return "operator";
146      }
147      if (stream.match(/^end/)) {
148        return "number";
149      }
150    }
151
152    var match;
153    if (match = stream.match(openers, false)) {
154      state.scopes.push(match[0]);
155    }
156
157    if (stream.match(closers, false)) {
158      state.scopes.pop();
159    }
160
161    // Handle type annotations
162    if (stream.match(/^::(?![:\$])/)) {
163      state.tokenize = tokenAnnotation;
164      return state.tokenize(stream, state);
165    }
166
167    // Handle symbols
168    if (!leavingExpr && stream.match(symbol) ||
169        stream.match(/:([<>]:|<<=?|>>>?=?|->|\/\/|\.{2,3}|[\.\\%*+\-<>!\/^|&]=?|[~\?\$])/)) {
170      return "builtin";
171    }
172
173    // Handle parametric types
174    //if (stream.match(/^{[^}]*}(?=\()/)) {
175    //  return "builtin";
176    //}
177
178    // Handle operators and Delimiters
179    if (stream.match(operators)) {
180      return "operator";
181    }
182
183    // Handle Number Literals
184    if (stream.match(/^\.?\d/, false)) {
185      var imMatcher = RegExp(/^im\b/);
186      var numberLiteral = false;
187      if (stream.match(/^0x\.[0-9a-f_]+p[\+\-]?[_\d]+/i)) { numberLiteral = true; }
188      // Integers
189      if (stream.match(/^0x[0-9a-f_]+/i)) { numberLiteral = true; } // Hex
190      if (stream.match(/^0b[01_]+/i)) { numberLiteral = true; } // Binary
191      if (stream.match(/^0o[0-7_]+/i)) { numberLiteral = true; } // Octal
192      // Floats
193      if (stream.match(/^(?:(?:\d[_\d]*)?\.(?!\.)(?:\d[_\d]*)?|\d[_\d]*\.(?!\.)(?:\d[_\d]*))?([Eef][\+\-]?[_\d]+)?/i)) { numberLiteral = true; }
194      if (stream.match(/^\d[_\d]*(e[\+\-]?\d+)?/i)) { numberLiteral = true; } // Decimal
195      if (numberLiteral) {
196          // Integer literals may be "long"
197          stream.match(imMatcher);
198          state.leavingExpr = true;
199          return "number";
200      }
201    }
202
203    // Handle Chars
204    if (stream.match(/^'/)) {
205      state.tokenize = tokenChar;
206      return state.tokenize(stream, state);
207    }
208
209    // Handle Strings
210    if (stream.match(stringPrefixes)) {
211      state.tokenize = tokenStringFactory(stream.current());
212      return state.tokenize(stream, state);
213    }
214
215    if (stream.match(macro)) {
216      return "meta";
217    }
218
219    if (stream.match(delimiters)) {
220      return null;
221    }
222
223    if (stream.match(keywords)) {
224      return "keyword";
225    }
226
227    if (stream.match(builtins)) {
228      return "builtin";
229    }
230
231    var isDefinition = state.isDefinition || state.lastToken == "function" ||
232                       state.lastToken == "macro" || state.lastToken == "type" ||
233                       state.lastToken == "struct" || state.lastToken == "immutable";
234
235    if (stream.match(identifiers)) {
236      if (isDefinition) {
237        if (stream.peek() === '.') {
238          state.isDefinition = true;
239          return "variable";
240        }
241        state.isDefinition = false;
242        return "def";
243      }
244      if (stream.match(/^({[^}]*})*\(/, false)) {
245        state.tokenize = tokenCallOrDef;
246        return state.tokenize(stream, state);
247      }
248      state.leavingExpr = true;
249      return "variable";
250    }
251
252    // Handle non-detected items
253    stream.next();
254    return "error";
255  }
256
257  function tokenCallOrDef(stream, state) {
258    var match = stream.match(/^(\(\s*)/);
259    if (match) {
260      if (state.firstParenPos < 0)
261        state.firstParenPos = state.scopes.length;
262      state.scopes.push('(');
263      state.charsAdvanced += match[1].length;
264    }
265    if (currentScope(state) == '(' && stream.match(/^\)/)) {
266      state.scopes.pop();
267      state.charsAdvanced += 1;
268      if (state.scopes.length <= state.firstParenPos) {
269        var isDefinition = stream.match(/^(\s*where\s+[^\s=]+)*\s*?=(?!=)/, false);
270        stream.backUp(state.charsAdvanced);
271        state.firstParenPos = -1;
272        state.charsAdvanced = 0;
273        state.tokenize = tokenBase;
274        if (isDefinition)
275          return "def";
276        return "builtin";
277      }
278    }
279    // Unfortunately javascript does not support multiline strings, so we have
280    // to undo anything done upto here if a function call or definition splits
281    // over two or more lines.
282    if (stream.match(/^$/g, false)) {
283      stream.backUp(state.charsAdvanced);
284      while (state.scopes.length > state.firstParenPos)
285        state.scopes.pop();
286      state.firstParenPos = -1;
287      state.charsAdvanced = 0;
288      state.tokenize = tokenBase;
289      return "builtin";
290    }
291    state.charsAdvanced += stream.match(/^([^()]*)/)[1].length;
292    return state.tokenize(stream, state);
293  }
294
295  function tokenAnnotation(stream, state) {
296    stream.match(/.*?(?=,|;|{|}|\(|\)|=|$|\s)/);
297    if (stream.match(/^{/)) {
298      state.nestedParameters++;
299    } else if (stream.match(/^}/) && state.nestedParameters > 0) {
300      state.nestedParameters--;
301    }
302    if (state.nestedParameters > 0) {
303      stream.match(/.*?(?={|})/) || stream.next();
304    } else if (state.nestedParameters == 0) {
305      state.tokenize = tokenBase;
306    }
307    return "builtin";
308  }
309
310  function tokenComment(stream, state) {
311    if (stream.match(/^#=/)) {
312      state.nestedComments++;
313    }
314    if (!stream.match(/.*?(?=(#=|=#))/)) {
315      stream.skipToEnd();
316    }
317    if (stream.match(/^=#/)) {
318      state.nestedComments--;
319      if (state.nestedComments == 0)
320        state.tokenize = tokenBase;
321    }
322    return "comment";
323  }
324
325  function tokenChar(stream, state) {
326    var isChar = false, match;
327    if (stream.match(chars)) {
328      isChar = true;
329    } else if (match = stream.match(/\\u([a-f0-9]{1,4})(?=')/i)) {
330      var value = parseInt(match[1], 16);
331      if (value <= 55295 || value >= 57344) { // (U+0,U+D7FF), (U+E000,U+FFFF)
332        isChar = true;
333        stream.next();
334      }
335    } else if (match = stream.match(/\\U([A-Fa-f0-9]{5,8})(?=')/)) {
336      var value = parseInt(match[1], 16);
337      if (value <= 1114111) { // U+10FFFF
338        isChar = true;
339        stream.next();
340      }
341    }
342    if (isChar) {
343      state.leavingExpr = true;
344      state.tokenize = tokenBase;
345      return "string";
346    }
347    if (!stream.match(/^[^']+(?=')/)) { stream.skipToEnd(); }
348    if (stream.match(/^'/)) { state.tokenize = tokenBase; }
349    return "error";
350  }
351
352  function tokenStringFactory(delimiter) {
353    if (delimiter.substr(-3) === '"""') {
354      delimiter = '"""';
355    } else if (delimiter.substr(-1) === '"') {
356      delimiter = '"';
357    }
358    function tokenString(stream, state) {
359      if (stream.eat('\\')) {
360        stream.next();
361      } else if (stream.match(delimiter)) {
362        state.tokenize = tokenBase;
363        state.leavingExpr = true;
364        return "string";
365      } else {
366        stream.eat(/[`"]/);
367      }
368      stream.eatWhile(/[^\\`"]/);
369      return "string";
370    }
371    return tokenString;
372  }
373
374  var external = {
375    startState: function() {
376      return {
377        tokenize: tokenBase,
378        scopes: [],
379        lastToken: null,
380        leavingExpr: false,
381        isDefinition: false,
382        nestedArrays: 0,
383        nestedComments: 0,
384        nestedGenerators: 0,
385        nestedParameters: 0,
386        charsAdvanced: 0,
387        firstParenPos: -1
388      };
389    },
390
391    token: function(stream, state) {
392      var style = state.tokenize(stream, state);
393      var current = stream.current();
394
395      if (current && style) {
396        state.lastToken = current;
397      }
398
399      return style;
400    },
401
402    indent: function(state, textAfter) {
403      var delta = 0;
404      if ( textAfter === ']' || textAfter === ')' || /^end\b/.test(textAfter) ||
405           /^else/.test(textAfter) || /^catch\b/.test(textAfter) || /^elseif\b/.test(textAfter) ||
406           /^finally/.test(textAfter) ) {
407        delta = -1;
408      }
409      return (state.scopes.length + delta) * config.indentUnit;
410    },
411
412    electricInput: /\b(end|else|catch|finally)\b/,
413    blockCommentStart: "#=",
414    blockCommentEnd: "=#",
415    lineComment: "#",
416    closeBrackets: "()[]{}\"\"",
417    fold: "indent"
418  };
419  return external;
420});
421
422
423CodeMirror.defineMIME("text/x-julia", "julia");
424
425});