julia.js 11 KB

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