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;
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 = /[+\-*\/^<>=!&|~$:]/;
14
function tokenBase(stream, state) {
16
var ch = stream.next();
20
} else if (ch == "0" && stream.eat("x")) {
21
stream.eatWhile(/[\da-f]/i);
23
} else if (ch == "." && stream.eat(/\d/)) {
24
stream.match(/\d*(?:e[+\-]?\d+)?/);
26
} else if (/\d/.test(ch)) {
27
stream.match(/\d*(?:\.\d+)?(?:e[+\-]\d+)?L?/);
29
} else if (ch == "'" || ch == '"') {
30
state.tokenize = tokenString(ch);
32
} else if (ch == "." && stream.match(/.[.\d]+/)) {
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
if (blockkeywords.propertyIsEnumerable(word)) curPunc = "block";
42
if (builtins.propertyIsEnumerable(word)) return "builtin";
44
} else if (ch == "%") {
45
if (stream.skipTo("%")) stream.next();
47
} else if (ch == "<" && stream.eat("-")) {
49
} else if (ch == "=" && state.ctx.argList) {
51
} else if (opChars.test(ch)) {
52
if (ch == "$") return "dollar";
53
stream.eatWhile(opChars);
55
} else if (/[\(\){}\[\];]/.test(ch)) {
57
if (ch == ";") return "semi";
64
function tokenString(quote) {
65
return function(stream, state) {
66
if (stream.eat("\\")) {
67
var ch = stream.next();
68
if (ch == "x") stream.match(/^[a-f0-9]{2}/i);
69
else if ((ch == "u" || ch == "U") && stream.eat("{") && stream.skipTo("}")) stream.next();
70
else if (ch == "u") stream.match(/^[a-f0-9]{4}/i);
71
else if (ch == "U") stream.match(/^[a-f0-9]{8}/i);
72
else if (/[0-7]/.test(ch)) stream.match(/^[0-7]{1,2}/);
76
while ((next = stream.next()) != null) {
77
if (next == quote) { state.tokenize = tokenBase; break; }
78
if (next == "\\") { stream.backUp(1); break; }
85
function push(state, type, stream) {
86
state.ctx = {type: type,
89
column: stream.column(),
93
state.indent = state.ctx.indent;
94
state.ctx = state.ctx.prev;
98
startState: function(base) {
99
return {tokenize: tokenBase,
101
indent: -config.indentUnit,
107
token: function(stream, state) {
109
if (state.ctx.align == null) state.ctx.align = false;
110
state.indent = stream.indentation();
112
if (stream.eatSpace()) return null;
113
var style = state.tokenize(stream, state);
114
if (style != "comment" && state.ctx.align == null) state.ctx.align = true;
116
var ctype = state.ctx.type;
117
if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && ctype == "block") pop(state);
118
if (curPunc == "{") push(state, "}", stream);
119
else if (curPunc == "(") {
120
push(state, ")", stream);
121
if (state.afterIdent) state.ctx.argList = true;
123
else if (curPunc == "[") push(state, "]", stream);
124
else if (curPunc == "block") push(state, "block", stream);
125
else if (curPunc == ctype) pop(state);
126
state.afterIdent = style == "variable" || style == "keyword";
130
indent: function(state, textAfter) {
131
if (state.tokenize != tokenBase) return 0;
132
var firstChar = textAfter && textAfter.charAt(0), ctx = state.ctx,
133
closing = firstChar == ctx.type;
134
if (ctx.type == "block") return ctx.indent + (firstChar == "{" ? 0 : config.indentUnit);
135
else if (ctx.align) return ctx.column + (closing ? 0 : 1);
136
else return ctx.indent + (closing ? 0 : config.indentUnit);
141
CodeMirror.defineMIME("text/x-rsrc", "r");