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