julia.js 12 KB

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