julia.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 commonOpeners = ["begin", "function", "type", "struct", "immutable",
  33. "let", "macro", "for", "while", "quote", "if", "else", "elseif", "try",
  34. "finally", "catch", "do"];
  35. var commonClosers = ["end", "else", "elseif", "catch", "finally"];
  36. var commonKeywords = ["if", "else", "elseif", "while", "for", "begin",
  37. "let", "end", "do", "try", "catch", "finally", "return", "break",
  38. "continue", "global", "local", "const", "export", "import", "importall",
  39. "using", "function", "where", "macro", "module", "baremodule", "struct",
  40. "type", "mutable", "immutable", "quote", "typealias", "abstract",
  41. "primitive", "bitstype"];
  42. var commonBuiltins = ["true", "false", "nothing", "NaN", "Inf"];
  43. CodeMirror.registerHelper("hintWords", "julia", commonKeywords.concat(commonBuiltins));
  44. var openers = wordRegexp(commonOpeners);
  45. var closers = wordRegexp(commonClosers);
  46. var keywords = wordRegexp(commonKeywords);
  47. var builtins = wordRegexp(commonBuiltins);
  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 inGenerator(state, '[')
  53. }
  54. function inGenerator(state, bracket, depth) {
  55. if (typeof(bracket) === "undefined") { bracket = '('; }
  56. if (typeof(depth) === "undefined") { depth = 0; }
  57. var scope = currentScope(state, depth);
  58. if ((depth == 0 && scope === "if" && inGenerator(state, bracket, depth + 1)) ||
  59. (scope === "for" && inGenerator(state, bracket, depth + 1)) ||
  60. (scope === bracket)) {
  61. return true;
  62. }
  63. return false;
  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. }
  107. if (ch === '(') {
  108. state.scopes.push('(');
  109. }
  110. if (inArray(state) && ch === ']') {
  111. if (currentScope(state) === "if") { state.scopes.pop(); }
  112. while (currentScope(state) === "for") { state.scopes.pop(); }
  113. state.scopes.pop();
  114. state.leavingExpr = true;
  115. }
  116. if (inGenerator(state) && ch === ')') {
  117. if (currentScope(state) === "if") { state.scopes.pop(); }
  118. while (currentScope(state) === "for") { state.scopes.pop(); }
  119. state.scopes.pop();
  120. state.leavingExpr = true;
  121. }
  122. if (inArray(state)) {
  123. if (state.lastToken == "end" && stream.match(/^:/)) {
  124. return "operator";
  125. }
  126. if (stream.match(/^end/)) {
  127. return "number";
  128. }
  129. }
  130. var match;
  131. if (match = stream.match(openers)) {
  132. state.scopes.push(match[0]);
  133. return "keyword";
  134. }
  135. if (stream.match(closers)) {
  136. state.scopes.pop();
  137. return "keyword";
  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) ||
  146. stream.match(/:([<>]:|<<=?|>>>?=?|->|\/\/|\.{2,3}|[\.\\%*+\-<>!\/^|&]=?|[~\?\$])/)) {
  147. return "builtin";
  148. }
  149. // Handle parametric types
  150. //if (stream.match(/^{[^}]*}(?=\()/)) {
  151. // return "builtin";
  152. //}
  153. // Handle operators and Delimiters
  154. if (stream.match(operators)) {
  155. return "operator";
  156. }
  157. // Handle Number Literals
  158. if (stream.match(/^\.?\d/, false)) {
  159. var imMatcher = RegExp(/^im\b/);
  160. var numberLiteral = false;
  161. // Floats
  162. if (stream.match(/^\d*\.(?!\.)\d*([Eef][\+\-]?\d+)?/i)) { numberLiteral = true; }
  163. if (stream.match(/^\d+\.(?!\.)\d*/)) { numberLiteral = true; }
  164. if (stream.match(/^\.\d+/)) { numberLiteral = true; }
  165. if (stream.match(/^0x\.[0-9a-f]+p[\+\-]?\d+/i)) { numberLiteral = true; }
  166. // Integers
  167. if (stream.match(/^0x[0-9a-f]+/i)) { numberLiteral = true; } // Hex
  168. if (stream.match(/^0b[01]+/i)) { numberLiteral = true; } // Binary
  169. if (stream.match(/^0o[0-7]+/i)) { numberLiteral = true; } // Octal
  170. if (stream.match(/^[1-9]\d*(e[\+\-]?\d+)?/)) { numberLiteral = true; } // Decimal
  171. // Zero by itself with no other piece of number.
  172. if (stream.match(/^0(?![\dx])/i)) { numberLiteral = true; }
  173. if (numberLiteral) {
  174. // Integer literals may be "long"
  175. stream.match(imMatcher);
  176. state.leavingExpr = true;
  177. return "number";
  178. }
  179. }
  180. // Handle Chars
  181. if (stream.match(/^'/)) {
  182. state.tokenize = tokenChar;
  183. return state.tokenize(stream, state);
  184. }
  185. // Handle Strings
  186. if (stream.match(stringPrefixes)) {
  187. state.tokenize = tokenStringFactory(stream.current());
  188. return state.tokenize(stream, state);
  189. }
  190. if (stream.match(macro)) {
  191. return "meta";
  192. }
  193. if (stream.match(delimiters)) {
  194. return null;
  195. }
  196. if (stream.match(keywords)) {
  197. return "keyword";
  198. }
  199. if (stream.match(builtins)) {
  200. return "builtin";
  201. }
  202. var isDefinition = state.isDefinition || state.lastToken == "function" ||
  203. state.lastToken == "macro" || state.lastToken == "type" ||
  204. state.lastToken == "struct" || state.lastToken == "immutable";
  205. if (stream.match(identifiers)) {
  206. if (isDefinition) {
  207. if (stream.peek() === '.') {
  208. state.isDefinition = true;
  209. return "variable";
  210. }
  211. state.isDefinition = false;
  212. return "def";
  213. }
  214. if (stream.match(/^({[^}]*})*\(/, false)) {
  215. state.tokenize = tokenCallOrDef;
  216. return state.tokenize(stream, state);
  217. }
  218. state.leavingExpr = true;
  219. return "variable";
  220. }
  221. // Handle non-detected items
  222. stream.next();
  223. return "error";
  224. }
  225. function tokenCallOrDef(stream, state) {
  226. var match = stream.match(/^(\(\s*)/);
  227. if (match) {
  228. if (state.firstParenPos < 0)
  229. state.firstParenPos = state.scopes.length;
  230. state.scopes.push('(');
  231. state.charsAdvanced += match[1].length;
  232. }
  233. if (currentScope(state) == '(' && stream.match(/^\)/)) {
  234. state.scopes.pop();
  235. state.charsAdvanced += 1;
  236. if (state.scopes.length <= state.firstParenPos) {
  237. var isDefinition = stream.match(/^(\s*where\s+[^\s=]+)*\s*?=(?!=)/, false);
  238. stream.backUp(state.charsAdvanced);
  239. state.firstParenPos = -1;
  240. state.charsAdvanced = 0;
  241. state.tokenize = tokenBase;
  242. if (isDefinition)
  243. return "def";
  244. return "builtin";
  245. }
  246. }
  247. // Unfortunately javascript does not support multiline strings, so we have
  248. // to undo anything done upto here if a function call or definition splits
  249. // over two or more lines.
  250. if (stream.match(/^$/g, false)) {
  251. stream.backUp(state.charsAdvanced);
  252. while (state.scopes.length > state.firstParenPos)
  253. state.scopes.pop();
  254. state.firstParenPos = -1;
  255. state.charsAdvanced = 0;
  256. state.tokenize = tokenBase;
  257. return "builtin";
  258. }
  259. state.charsAdvanced += stream.match(/^([^()]*)/)[1].length;
  260. return state.tokenize(stream, state);
  261. }
  262. function tokenAnnotation(stream, state) {
  263. stream.match(/.*?(?=,|;|{|}|\(|\)|=|$|\s)/);
  264. if (stream.match(/^{/)) {
  265. state.nestedLevels++;
  266. } else if (stream.match(/^}/)) {
  267. state.nestedLevels--;
  268. }
  269. if (state.nestedLevels > 0) {
  270. stream.match(/.*?(?={|})/) || stream.next();
  271. } else if (state.nestedLevels == 0) {
  272. state.tokenize = tokenBase;
  273. }
  274. return "builtin";
  275. }
  276. function tokenComment(stream, state) {
  277. if (stream.match(/^#=/)) {
  278. state.nestedLevels++;
  279. }
  280. if (!stream.match(/.*?(?=(#=|=#))/)) {
  281. stream.skipToEnd();
  282. }
  283. if (stream.match(/^=#/)) {
  284. state.nestedLevels--;
  285. if (state.nestedLevels == 0)
  286. state.tokenize = tokenBase;
  287. }
  288. return "comment";
  289. }
  290. function tokenChar(stream, state) {
  291. var isChar = false, match;
  292. if (stream.match(chars)) {
  293. isChar = true;
  294. } else if (match = stream.match(/\\u([a-f0-9]{1,4})(?=')/i)) {
  295. var value = parseInt(match[1], 16);
  296. if (value <= 55295 || value >= 57344) { // (U+0,U+D7FF), (U+E000,U+FFFF)
  297. isChar = true;
  298. stream.next();
  299. }
  300. } else if (match = stream.match(/\\U([A-Fa-f0-9]{5,8})(?=')/)) {
  301. var value = parseInt(match[1], 16);
  302. if (value <= 1114111) { // U+10FFFF
  303. isChar = true;
  304. stream.next();
  305. }
  306. }
  307. if (isChar) {
  308. state.leavingExpr = true;
  309. state.tokenize = tokenBase;
  310. return "string";
  311. }
  312. if (!stream.match(/^[^']+(?=')/)) { stream.skipToEnd(); }
  313. if (stream.match(/^'/)) { state.tokenize = tokenBase; }
  314. return "error";
  315. }
  316. function tokenStringFactory(delimiter) {
  317. if (delimiter.substr(-3) === '"""') {
  318. delimiter = '"""';
  319. } else if (delimiter.substr(-1) === '"') {
  320. delimiter = '"';
  321. }
  322. function tokenString(stream, state) {
  323. if (stream.eat('\\')) {
  324. stream.next();
  325. } else if (stream.match(delimiter)) {
  326. state.tokenize = tokenBase;
  327. state.leavingExpr = true;
  328. return "string";
  329. } else {
  330. stream.eat(/[`"]/);
  331. }
  332. stream.eatWhile(/[^\\`"]/);
  333. return "string";
  334. }
  335. return tokenString;
  336. }
  337. var external = {
  338. startState: function() {
  339. return {
  340. tokenize: tokenBase,
  341. scopes: [],
  342. lastToken: null,
  343. leavingExpr: false,
  344. isDefinition: false,
  345. nestedLevels: 0,
  346. charsAdvanced: 0,
  347. firstParenPos: -1
  348. };
  349. },
  350. token: function(stream, state) {
  351. var style = state.tokenize(stream, state);
  352. var current = stream.current();
  353. if (current && style) {
  354. state.lastToken = current;
  355. }
  356. return style;
  357. },
  358. indent: function(state, textAfter) {
  359. var delta = 0;
  360. if ( textAfter === ']' || textAfter === ')' || textAfter === "end" ||
  361. textAfter === "else" || textAfter === "catch" || textAfter === "elseif" ||
  362. textAfter === "finally" ) {
  363. delta = -1;
  364. }
  365. return (state.scopes.length + delta) * config.indentUnit;
  366. },
  367. electricInput: /\b(end|else|catch|finally)\b/,
  368. blockCommentStart: "#=",
  369. blockCommentEnd: "=#",
  370. lineComment: "#",
  371. fold: "indent"
  372. };
  373. return external;
  374. });
  375. CodeMirror.defineMIME("text/x-julia", "julia");
  376. });