1
0

julia.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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) {
  15. return new RegExp("^((" + words.join(")|(") + "))\\b");
  16. }
  17. var operators = parserConf.operators || /^\.?[|&^\\%*+\-<>!=\/]=?|\?|~|:|\$|\.[<>]|<<=?|>>>?=?|\.[<>=]=|->?|\/\/|\bin\b/;
  18. var delimiters = parserConf.delimiters || /^[;,()[\]{}]/;
  19. var identifiers = parserConf.identifiers|| /^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*!*/;
  20. var blockOpeners = ["begin", "function", "type", "immutable", "let", "macro", "for", "while", "quote", "if", "else", "elseif", "try", "finally", "catch", "do"];
  21. var blockClosers = ["end", "else", "elseif", "catch", "finally"];
  22. 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', 'ccall'];
  23. var builtinList = ['true', 'false', 'enumerate', 'open', 'close', 'nothing', 'NaN', 'Inf', 'print', 'println', 'Int', 'Int8', 'Uint8', 'Int16', 'Uint16', 'Int32', 'Uint32', 'Int64', 'Uint64', 'Int128', 'Uint128', 'Bool', 'Char', 'Float16', 'Float32', 'Float64', 'Array', 'Vector', 'Matrix', 'String', 'UTF8String', 'ASCIIString', 'error', 'warn', 'info', '@printf'];
  24. //var stringPrefixes = new RegExp("^[br]?('|\")")
  25. var stringPrefixes = /^(`|'|"{3}|([br]?"))/;
  26. var keywords = wordRegexp(keywordList);
  27. var builtins = wordRegexp(builtinList);
  28. var openers = wordRegexp(blockOpeners);
  29. var closers = wordRegexp(blockClosers);
  30. var macro = /^@[_A-Za-z][_A-Za-z0-9]*/;
  31. var symbol = /^:[_A-Za-z][_A-Za-z0-9]*/;
  32. var indentInfo = null;
  33. function in_array(state) {
  34. var ch = cur_scope(state);
  35. if(ch=="[" || ch=="{") {
  36. return true;
  37. }
  38. else {
  39. return false;
  40. }
  41. }
  42. function cur_scope(state) {
  43. if(state.scopes.length==0) {
  44. return null;
  45. }
  46. return state.scopes[state.scopes.length - 1];
  47. }
  48. // tokenizers
  49. function tokenBase(stream, state) {
  50. // Handle scope changes
  51. var leaving_expr = state.leaving_expr;
  52. if(stream.sol()) {
  53. leaving_expr = false;
  54. }
  55. state.leaving_expr = false;
  56. if(leaving_expr) {
  57. if(stream.match(/^'+/)) {
  58. return 'operator';
  59. }
  60. }
  61. if(stream.match(/^\.{2,3}/)) {
  62. return 'operator';
  63. }
  64. if (stream.eatSpace()) {
  65. return null;
  66. }
  67. var ch = stream.peek();
  68. // Handle Comments
  69. if (ch === '#') {
  70. stream.skipToEnd();
  71. return 'comment';
  72. }
  73. if(ch==='[') {
  74. state.scopes.push("[");
  75. }
  76. if(ch==='{') {
  77. state.scopes.push("{");
  78. }
  79. var scope=cur_scope(state);
  80. if(scope==='[' && ch===']') {
  81. state.scopes.pop();
  82. state.leaving_expr=true;
  83. }
  84. if(scope==='{' && ch==='}') {
  85. state.scopes.pop();
  86. state.leaving_expr=true;
  87. }
  88. if(ch===')') {
  89. state.leaving_expr = true;
  90. }
  91. var match;
  92. if(!in_array(state) && (match=stream.match(openers, false))) {
  93. state.scopes.push(match);
  94. }
  95. if(!in_array(state) && stream.match(closers, false)) {
  96. state.scopes.pop();
  97. }
  98. if(in_array(state)) {
  99. if(stream.match(/^end/)) {
  100. return 'number';
  101. }
  102. }
  103. if(stream.match(/^=>/)) {
  104. return 'operator';
  105. }
  106. // Handle Number Literals
  107. if (stream.match(/^[0-9\.]/, false)) {
  108. var imMatcher = RegExp(/^im\b/);
  109. var floatLiteral = false;
  110. // Floats
  111. if (stream.match(/^\d*\.(?!\.)\d+([ef][\+\-]?\d+)?/i)) { floatLiteral = true; }
  112. if (stream.match(/^\d+\.(?!\.)\d*/)) { floatLiteral = true; }
  113. if (stream.match(/^\.\d+/)) { floatLiteral = true; }
  114. if (floatLiteral) {
  115. // Float literals may be "imaginary"
  116. stream.match(imMatcher);
  117. state.leaving_expr = true;
  118. return 'number';
  119. }
  120. // Integers
  121. var intLiteral = false;
  122. // Hex
  123. if (stream.match(/^0x[0-9a-f]+/i)) { intLiteral = true; }
  124. // Binary
  125. if (stream.match(/^0b[01]+/i)) { intLiteral = true; }
  126. // Octal
  127. if (stream.match(/^0o[0-7]+/i)) { intLiteral = true; }
  128. // Decimal
  129. if (stream.match(/^[1-9]\d*(e[\+\-]?\d+)?/)) {
  130. intLiteral = true;
  131. }
  132. // Zero by itself with no other piece of number.
  133. if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
  134. if (intLiteral) {
  135. // Integer literals may be "long"
  136. stream.match(imMatcher);
  137. state.leaving_expr = true;
  138. return 'number';
  139. }
  140. }
  141. if(stream.match(/^(::)|(<:)/)) {
  142. return 'operator';
  143. }
  144. // Handle symbols
  145. if(!leaving_expr && stream.match(symbol)) {
  146. return 'string';
  147. }
  148. // Handle operators and Delimiters
  149. if (stream.match(operators)) {
  150. return 'operator';
  151. }
  152. // Handle Strings
  153. if (stream.match(stringPrefixes)) {
  154. state.tokenize = tokenStringFactory(stream.current());
  155. return state.tokenize(stream, state);
  156. }
  157. if (stream.match(macro)) {
  158. return 'meta';
  159. }
  160. if (stream.match(delimiters)) {
  161. return null;
  162. }
  163. if (stream.match(keywords)) {
  164. return 'keyword';
  165. }
  166. if (stream.match(builtins)) {
  167. return 'builtin';
  168. }
  169. if (stream.match(identifiers)) {
  170. state.leaving_expr=true;
  171. return 'variable';
  172. }
  173. // Handle non-detected items
  174. stream.next();
  175. return ERRORCLASS;
  176. }
  177. function tokenStringFactory(delimiter) {
  178. while ('rub'.indexOf(delimiter.charAt(0).toLowerCase()) >= 0) {
  179. delimiter = delimiter.substr(1);
  180. }
  181. var singleline = delimiter.length == 1;
  182. var OUTCLASS = 'string';
  183. function tokenString(stream, state) {
  184. while (!stream.eol()) {
  185. stream.eatWhile(/[^'"\\]/);
  186. if (stream.eat('\\')) {
  187. stream.next();
  188. if (singleline && stream.eol()) {
  189. return OUTCLASS;
  190. }
  191. } else if (stream.match(delimiter)) {
  192. state.tokenize = tokenBase;
  193. return OUTCLASS;
  194. } else {
  195. stream.eat(/['"]/);
  196. }
  197. }
  198. if (singleline) {
  199. if (parserConf.singleLineStringErrors) {
  200. return ERRORCLASS;
  201. } else {
  202. state.tokenize = tokenBase;
  203. }
  204. }
  205. return OUTCLASS;
  206. }
  207. tokenString.isString = true;
  208. return tokenString;
  209. }
  210. function tokenLexer(stream, state) {
  211. indentInfo = null;
  212. var style = state.tokenize(stream, state);
  213. var current = stream.current();
  214. // Handle '.' connected identifiers
  215. if (current === '.') {
  216. style = stream.match(identifiers, false) ? null : ERRORCLASS;
  217. if (style === null && state.lastStyle === 'meta') {
  218. // Apply 'meta' style to '.' connected identifiers when
  219. // appropriate.
  220. style = 'meta';
  221. }
  222. return style;
  223. }
  224. return style;
  225. }
  226. var external = {
  227. startState: function() {
  228. return {
  229. tokenize: tokenBase,
  230. scopes: [],
  231. leaving_expr: false
  232. };
  233. },
  234. token: function(stream, state) {
  235. var style = tokenLexer(stream, state);
  236. state.lastStyle = style;
  237. return style;
  238. },
  239. indent: function(state, textAfter) {
  240. var delta = 0;
  241. if(textAfter=="end" || textAfter=="]" || textAfter=="}" || textAfter=="else" || textAfter=="elseif" || textAfter=="catch" || textAfter=="finally") {
  242. delta = -1;
  243. }
  244. return (state.scopes.length + delta) * 4;
  245. },
  246. lineComment: "#",
  247. fold: "indent",
  248. electricChars: "edlsifyh]}"
  249. };
  250. return external;
  251. });
  252. CodeMirror.defineMIME("text/x-julia", "julia");
  253. });