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.

julia.js 11KB

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