LaravelTest
390 строк · 11.1 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14CodeMirror.defineMode("julia", function(config, parserConf) {15function wordRegexp(words, end, pre) {16if (typeof pre === "undefined") { pre = ""; }17if (typeof end === "undefined") { end = "\\b"; }18return new RegExp("^" + pre + "((" + words.join(")|(") + "))" + end);19}20
21var octChar = "\\\\[0-7]{1,3}";22var hexChar = "\\\\x[A-Fa-f0-9]{1,2}";23var sChar = "\\\\[abefnrtv0%?'\"\\\\]";24var uChar = "([^\\u0027\\u005C\\uD800-\\uDFFF]|[\\uD800-\\uDFFF][\\uDC00-\\uDFFF])";25
26var asciiOperatorsList = [27"[<>]:", "[<>=]=", "<<=?", ">>>?=?", "=>", "--?>", "<--[->]?", "\\/\\/",28"\\.{2,3}", "[\\.\\\\%*+\\-<>!\\/^|&]=?", "\\?", "\\$", "~", ":"29];30var operators = parserConf.operators || wordRegexp([31"[<>]:", "[<>=]=", "<<=?", ">>>?=?", "=>", "--?>", "<--[->]?", "\\/\\/",32"[\\\\%*+\\-<>!\\/^|&\\u00F7\\u22BB]=?", "\\?", "\\$", "~", ":",33"\\u00D7", "\\u2208", "\\u2209", "\\u220B", "\\u220C", "\\u2218",34"\\u221A", "\\u221B", "\\u2229", "\\u222A", "\\u2260", "\\u2264",35"\\u2265", "\\u2286", "\\u2288", "\\u228A", "\\u22C5",36"\\b(in|isa)\\b(?!\.?\\()"37], "");38var delimiters = parserConf.delimiters || /^[;,()[\]{}]/;39var identifiers = parserConf.identifiers ||40/^[_A-Za-z\u00A1-\u2217\u2219-\uFFFF][\w\u00A1-\u2217\u2219-\uFFFF]*!*/;41
42var chars = wordRegexp([octChar, hexChar, sChar, uChar], "'");43
44var openersList = ["begin", "function", "type", "struct", "immutable", "let",45"macro", "for", "while", "quote", "if", "else", "elseif", "try",46"finally", "catch", "do"];47
48var closersList = ["end", "else", "elseif", "catch", "finally"];49
50var keywordsList = ["if", "else", "elseif", "while", "for", "begin", "let",51"end", "do", "try", "catch", "finally", "return", "break", "continue",52"global", "local", "const", "export", "import", "importall", "using",53"function", "where", "macro", "module", "baremodule", "struct", "type",54"mutable", "immutable", "quote", "typealias", "abstract", "primitive",55"bitstype"];56
57var builtinsList = ["true", "false", "nothing", "NaN", "Inf"];58
59CodeMirror.registerHelper("hintWords", "julia", keywordsList.concat(builtinsList));60
61var openers = wordRegexp(openersList);62var closers = wordRegexp(closersList);63var keywords = wordRegexp(keywordsList);64var builtins = wordRegexp(builtinsList);65
66var macro = /^@[_A-Za-z\u00A1-\uFFFF][\w\u00A1-\uFFFF]*!*/;67var symbol = /^:[_A-Za-z\u00A1-\uFFFF][\w\u00A1-\uFFFF]*!*/;68var stringPrefixes = /^(`|([_A-Za-z\u00A1-\uFFFF]*"("")?))/;69
70var macroOperators = wordRegexp(asciiOperatorsList, "", "@");71var symbolOperators = wordRegexp(asciiOperatorsList, "", ":");72
73function inArray(state) {74return (state.nestedArrays > 0);75}76
77function inGenerator(state) {78return (state.nestedGenerators > 0);79}80
81function currentScope(state, n) {82if (typeof(n) === "undefined") { n = 0; }83if (state.scopes.length <= n) {84return null;85}86return state.scopes[state.scopes.length - (n + 1)];87}88
89// tokenizers90function tokenBase(stream, state) {91// Handle multiline comments92if (stream.match('#=', false)) {93state.tokenize = tokenComment;94return state.tokenize(stream, state);95}96
97// Handle scope changes98var leavingExpr = state.leavingExpr;99if (stream.sol()) {100leavingExpr = false;101}102state.leavingExpr = false;103
104if (leavingExpr) {105if (stream.match(/^'+/)) {106return "operator";107}108}109
110if (stream.match(/\.{4,}/)) {111return "error";112} else if (stream.match(/\.{1,3}/)) {113return "operator";114}115
116if (stream.eatSpace()) {117return null;118}119
120var ch = stream.peek();121
122// Handle single line comments123if (ch === '#') {124stream.skipToEnd();125return "comment";126}127
128if (ch === '[') {129state.scopes.push('[');130state.nestedArrays++;131}132
133if (ch === '(') {134state.scopes.push('(');135state.nestedGenerators++;136}137
138if (inArray(state) && ch === ']') {139while (state.scopes.length && currentScope(state) !== "[") { state.scopes.pop(); }140state.scopes.pop();141state.nestedArrays--;142state.leavingExpr = true;143}144
145if (inGenerator(state) && ch === ')') {146while (state.scopes.length && currentScope(state) !== "(") { state.scopes.pop(); }147state.scopes.pop();148state.nestedGenerators--;149state.leavingExpr = true;150}151
152if (inArray(state)) {153if (state.lastToken == "end" && stream.match(':')) {154return "operator";155}156if (stream.match('end')) {157return "number";158}159}160
161var match;162if (match = stream.match(openers, false)) {163state.scopes.push(match[0]);164}165
166if (stream.match(closers, false)) {167state.scopes.pop();168}169
170// Handle type annotations171if (stream.match(/^::(?![:\$])/)) {172state.tokenize = tokenAnnotation;173return state.tokenize(stream, state);174}175
176// Handle symbols177if (!leavingExpr && (stream.match(symbol) || stream.match(symbolOperators))) {178return "builtin";179}180
181// Handle parametric types182//if (stream.match(/^{[^}]*}(?=\()/)) {183// return "builtin";184//}185
186// Handle operators and Delimiters187if (stream.match(operators)) {188return "operator";189}190
191// Handle Number Literals192if (stream.match(/^\.?\d/, false)) {193var imMatcher = RegExp(/^im\b/);194var numberLiteral = false;195if (stream.match(/^0x\.[0-9a-f_]+p[\+\-]?[_\d]+/i)) { numberLiteral = true; }196// Integers197if (stream.match(/^0x[0-9a-f_]+/i)) { numberLiteral = true; } // Hex198if (stream.match(/^0b[01_]+/i)) { numberLiteral = true; } // Binary199if (stream.match(/^0o[0-7_]+/i)) { numberLiteral = true; } // Octal200// Floats201if (stream.match(/^(?:(?:\d[_\d]*)?\.(?!\.)(?:\d[_\d]*)?|\d[_\d]*\.(?!\.)(?:\d[_\d]*))?([Eef][\+\-]?[_\d]+)?/i)) { numberLiteral = true; }202if (stream.match(/^\d[_\d]*(e[\+\-]?\d+)?/i)) { numberLiteral = true; } // Decimal203if (numberLiteral) {204// Integer literals may be "long"205stream.match(imMatcher);206state.leavingExpr = true;207return "number";208}209}210
211// Handle Chars212if (stream.match('\'')) {213state.tokenize = tokenChar;214return state.tokenize(stream, state);215}216
217// Handle Strings218if (stream.match(stringPrefixes)) {219state.tokenize = tokenStringFactory(stream.current());220return state.tokenize(stream, state);221}222
223if (stream.match(macro) || stream.match(macroOperators)) {224return "meta";225}226
227if (stream.match(delimiters)) {228return null;229}230
231if (stream.match(keywords)) {232return "keyword";233}234
235if (stream.match(builtins)) {236return "builtin";237}238
239var isDefinition = state.isDefinition || state.lastToken == "function" ||240state.lastToken == "macro" || state.lastToken == "type" ||241state.lastToken == "struct" || state.lastToken == "immutable";242
243if (stream.match(identifiers)) {244if (isDefinition) {245if (stream.peek() === '.') {246state.isDefinition = true;247return "variable";248}249state.isDefinition = false;250return "def";251}252state.leavingExpr = true;253return "variable";254}255
256// Handle non-detected items257stream.next();258return "error";259}260
261function tokenAnnotation(stream, state) {262stream.match(/.*?(?=[,;{}()=\s]|$)/);263if (stream.match('{')) {264state.nestedParameters++;265} else if (stream.match('}') && state.nestedParameters > 0) {266state.nestedParameters--;267}268if (state.nestedParameters > 0) {269stream.match(/.*?(?={|})/) || stream.next();270} else if (state.nestedParameters == 0) {271state.tokenize = tokenBase;272}273return "builtin";274}275
276function tokenComment(stream, state) {277if (stream.match('#=')) {278state.nestedComments++;279}280if (!stream.match(/.*?(?=(#=|=#))/)) {281stream.skipToEnd();282}283if (stream.match('=#')) {284state.nestedComments--;285if (state.nestedComments == 0)286state.tokenize = tokenBase;287}288return "comment";289}290
291function tokenChar(stream, state) {292var isChar = false, match;293if (stream.match(chars)) {294isChar = true;295} else if (match = stream.match(/\\u([a-f0-9]{1,4})(?=')/i)) {296var value = parseInt(match[1], 16);297if (value <= 55295 || value >= 57344) { // (U+0,U+D7FF), (U+E000,U+FFFF)298isChar = true;299stream.next();300}301} else if (match = stream.match(/\\U([A-Fa-f0-9]{5,8})(?=')/)) {302var value = parseInt(match[1], 16);303if (value <= 1114111) { // U+10FFFF304isChar = true;305stream.next();306}307}308if (isChar) {309state.leavingExpr = true;310state.tokenize = tokenBase;311return "string";312}313if (!stream.match(/^[^']+(?=')/)) { stream.skipToEnd(); }314if (stream.match('\'')) { state.tokenize = tokenBase; }315return "error";316}317
318function tokenStringFactory(delimiter) {319if (delimiter.substr(-3) === '"""') {320delimiter = '"""';321} else if (delimiter.substr(-1) === '"') {322delimiter = '"';323}324function tokenString(stream, state) {325if (stream.eat('\\')) {326stream.next();327} else if (stream.match(delimiter)) {328state.tokenize = tokenBase;329state.leavingExpr = true;330return "string";331} else {332stream.eat(/[`"]/);333}334stream.eatWhile(/[^\\`"]/);335return "string";336}337return tokenString;338}339
340var external = {341startState: function() {342return {343tokenize: tokenBase,344scopes: [],345lastToken: null,346leavingExpr: false,347isDefinition: false,348nestedArrays: 0,349nestedComments: 0,350nestedGenerators: 0,351nestedParameters: 0,352firstParenPos: -1353};354},355
356token: function(stream, state) {357var style = state.tokenize(stream, state);358var current = stream.current();359
360if (current && style) {361state.lastToken = current;362}363
364return style;365},366
367indent: function(state, textAfter) {368var delta = 0;369if ( textAfter === ']' || textAfter === ')' || /^end\b/.test(textAfter) ||370/^else/.test(textAfter) || /^catch\b/.test(textAfter) || /^elseif\b/.test(textAfter) ||371/^finally/.test(textAfter) ) {372delta = -1;373}374return (state.scopes.length + delta) * config.indentUnit;375},376
377electricInput: /\b(end|else|catch|finally)\b/,378blockCommentStart: "#=",379blockCommentEnd: "=#",380lineComment: "#",381closeBrackets: "()[]{}\"\"",382fold: "indent"383};384return external;385});386
387
388CodeMirror.defineMIME("text/x-julia", "julia");389
390});391