julia.js 12 KB

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