Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/mode/r/r.js
1294 views
CodeMirror.defineMode("r", function(config) {1function wordObj(str) {2var words = str.split(" "), res = {};3for (var i = 0; i < words.length; ++i) res[words[i]] = true;4return res;5}6var atoms = wordObj("NULL NA Inf NaN NA_integer_ NA_real_ NA_complex_ NA_character_");7var builtins = wordObj("list quote bquote eval return call parse deparse");8var keywords = wordObj("if else repeat while function for in next break");9var blockkeywords = wordObj("if else repeat while function for");10var opChars = /[+\-*\/^<>=!&|~$:]/;11var curPunc;1213function tokenBase(stream, state) {14curPunc = null;15var ch = stream.next();16if (ch == "#") {17stream.skipToEnd();18return "comment";19} else if (ch == "0" && stream.eat("x")) {20stream.eatWhile(/[\da-f]/i);21return "number";22} else if (ch == "." && stream.eat(/\d/)) {23stream.match(/\d*(?:e[+\-]?\d+)?/);24return "number";25} else if (/\d/.test(ch)) {26stream.match(/\d*(?:\.\d+)?(?:e[+\-]\d+)?L?/);27return "number";28} else if (ch == "'" || ch == '"') {29state.tokenize = tokenString(ch);30return "string";31} else if (ch == "." && stream.match(/.[.\d]+/)) {32return "keyword";33} else if (/[\w\.]/.test(ch) && ch != "_") {34stream.eatWhile(/[\w\.]/);35var word = stream.current();36if (atoms.propertyIsEnumerable(word)) return "atom";37if (keywords.propertyIsEnumerable(word)) {38// Block keywords start new blocks, except 'else if', which only starts39// one new block for the 'if', no block for the 'else'.40if (blockkeywords.propertyIsEnumerable(word) &&41!stream.match(/\s*if(\s+|$)/, false))42curPunc = "block";43return "keyword";44}45if (builtins.propertyIsEnumerable(word)) return "builtin";46return "variable";47} else if (ch == "%") {48if (stream.skipTo("%")) stream.next();49return "variable-2";50} else if (ch == "<" && stream.eat("-")) {51return "arrow";52} else if (ch == "=" && state.ctx.argList) {53return "arg-is";54} else if (opChars.test(ch)) {55if (ch == "$") return "dollar";56stream.eatWhile(opChars);57return "operator";58} else if (/[\(\){}\[\];]/.test(ch)) {59curPunc = ch;60if (ch == ";") return "semi";61return null;62} else {63return null;64}65}6667function tokenString(quote) {68return function(stream, state) {69if (stream.eat("\\")) {70var ch = stream.next();71if (ch == "x") stream.match(/^[a-f0-9]{2}/i);72else if ((ch == "u" || ch == "U") && stream.eat("{") && stream.skipTo("}")) stream.next();73else if (ch == "u") stream.match(/^[a-f0-9]{4}/i);74else if (ch == "U") stream.match(/^[a-f0-9]{8}/i);75else if (/[0-7]/.test(ch)) stream.match(/^[0-7]{1,2}/);76return "string-2";77} else {78var next;79while ((next = stream.next()) != null) {80if (next == quote) { state.tokenize = tokenBase; break; }81if (next == "\\") { stream.backUp(1); break; }82}83return "string";84}85};86}8788function push(state, type, stream) {89state.ctx = {type: type,90indent: state.indent,91align: null,92column: stream.column(),93prev: state.ctx};94}95function pop(state) {96state.indent = state.ctx.indent;97state.ctx = state.ctx.prev;98}99100return {101startState: function() {102return {tokenize: tokenBase,103ctx: {type: "top",104indent: -config.indentUnit,105align: false},106indent: 0,107afterIdent: false};108},109110token: function(stream, state) {111if (stream.sol()) {112if (state.ctx.align == null) state.ctx.align = false;113state.indent = stream.indentation();114}115if (stream.eatSpace()) return null;116var style = state.tokenize(stream, state);117if (style != "comment" && state.ctx.align == null) state.ctx.align = true;118119var ctype = state.ctx.type;120if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && ctype == "block") pop(state);121if (curPunc == "{") push(state, "}", stream);122else if (curPunc == "(") {123push(state, ")", stream);124if (state.afterIdent) state.ctx.argList = true;125}126else if (curPunc == "[") push(state, "]", stream);127else if (curPunc == "block") push(state, "block", stream);128else if (curPunc == ctype) pop(state);129state.afterIdent = style == "variable" || style == "keyword";130return style;131},132133indent: function(state, textAfter) {134if (state.tokenize != tokenBase) return 0;135var firstChar = textAfter && textAfter.charAt(0), ctx = state.ctx,136closing = firstChar == ctx.type;137if (ctx.type == "block") return ctx.indent + (firstChar == "{" ? 0 : config.indentUnit);138else if (ctx.align) return ctx.column + (closing ? 0 : 1);139else return ctx.indent + (closing ? 0 : config.indentUnit);140}141};142});143144CodeMirror.defineMIME("text/x-rsrc", "r");145146147