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