本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

430 lines
12KB

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