julia.js 7.9 KB

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