julia.js 12 KB

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