| 1 |
function wordObj(words) { |
| 2 |
var res = {}; |
| 3 |
for (var i = 0; i < words.length; ++i) res[words[i]] = true; |
| 4 |
return res; |
| 5 |
} |
| 6 |
var commonAtoms = ["NULL", "NA", "Inf", "NaN", "NA_integer_", "NA_real_", "NA_complex_", "NA_character_", "TRUE", "FALSE"]; |
| 7 |
var commonBuiltins = ["list", "quote", "bquote", "eval", "return", "call", "parse", "deparse"]; |
| 8 |
var commonKeywords = ["if", "else", "repeat", "while", "function", "for", "in", "next", "break"]; |
| 9 |
var commonBlockKeywords = ["if", "else", "repeat", "while", "function", "for"]; |
| 10 |
|
| 11 |
var atoms = wordObj(commonAtoms); |
| 12 |
var builtins = wordObj(commonBuiltins); |
| 13 |
var keywords = wordObj(commonKeywords); |
| 14 |
var blockkeywords = wordObj(commonBlockKeywords); |
| 15 |
var opChars = /[+\-*\/^<>=!&|~$:]/; |
| 16 |
var curPunc; |
| 17 |
|
| 18 |
function tokenBase(stream, state) { |
| 19 |
curPunc = null; |
| 20 |
var ch = stream.next(); |
| 21 |
if (ch == "#") { |
| 22 |
stream.skipToEnd(); |
| 23 |
return "comment"; |
| 24 |
} else if (ch == "0" && stream.eat("x")) { |
| 25 |
stream.eatWhile(/[\da-f]/i); |
| 26 |
return "number"; |
| 27 |
} else if (ch == "." && stream.eat(/\d/)) { |
| 28 |
stream.match(/\d*(?:e[+\-]?\d+)?/); |
| 29 |
return "number"; |
| 30 |
} else if (/\d/.test(ch)) { |
| 31 |
stream.match(/\d*(?:\.\d+)?(?:e[+\-]\d+)?L?/); |
| 32 |
return "number"; |
| 33 |
} else if (ch == "'" || ch == '"') { |
| 34 |
state.tokenize = tokenString(ch); |
| 35 |
return "string"; |
| 36 |
} else if (ch == "`") { |
| 37 |
stream.match(/[^`]+`/); |
| 38 |
return "string.special"; |
| 39 |
} else if (ch == "." && stream.match(/.(?:[.]|\d+)/)) { |
| 40 |
return "keyword"; |
| 41 |
} else if (/[a-zA-Z\.]/.test(ch)) { |
| 42 |
stream.eatWhile(/[\w\.]/); |
| 43 |
var word = stream.current(); |
| 44 |
if (atoms.propertyIsEnumerable(word)) return "atom"; |
| 45 |
if (keywords.propertyIsEnumerable(word)) { |
| 46 |
// Block keywords start new blocks, except 'else if', which only starts |
| 47 |
// one new block for the 'if', no block for the 'else'. |
| 48 |
if (blockkeywords.propertyIsEnumerable(word) && |
| 49 |
!stream.match(/\s*if(\s+|$)/, false)) |
| 50 |
curPunc = "block"; |
| 51 |
return "keyword"; |
| 52 |
} |
| 53 |
if (builtins.propertyIsEnumerable(word)) return "builtin"; |
| 54 |
return "variable"; |
| 55 |
} else if (ch == "%") { |
| 56 |
if (stream.skipTo("%")) stream.next(); |
| 57 |
return "variableName.special"; |
| 58 |
} else if ( |
| 59 |
(ch == "<" && stream.eat("-")) || |
| 60 |
(ch == "<" && stream.match("<-")) || |
| 61 |
(ch == "-" && stream.match(/>>?/)) |
| 62 |
) { |
| 63 |
return "operator"; |
| 64 |
} else if (ch == "=" && state.ctx.argList) { |
| 65 |
return "operator"; |
| 66 |
} else if (opChars.test(ch)) { |
| 67 |
if (ch == "$") return "operator"; |
| 68 |
stream.eatWhile(opChars); |
| 69 |
return "operator"; |
| 70 |
} else if (/[\(\){}\[\];]/.test(ch)) { |
| 71 |
curPunc = ch; |
| 72 |
if (ch == ";") return "punctuation"; |
| 73 |
return null; |
| 74 |
} else { |
| 75 |
return null; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
function tokenString(quote) { |
| 80 |
return function(stream, state) { |
| 81 |
if (stream.eat("\\")) { |
| 82 |
var ch = stream.next(); |
| 83 |
if (ch == "x") stream.match(/^[a-f0-9]{2}/i); |
| 84 |
else if ((ch == "u" || ch == "U") && stream.eat("{") && stream.skipTo("}")) stream.next(); |
| 85 |
else if (ch == "u") stream.match(/^[a-f0-9]{4}/i); |
| 86 |
else if (ch == "U") stream.match(/^[a-f0-9]{8}/i); |
| 87 |
else if (/[0-7]/.test(ch)) stream.match(/^[0-7]{1,2}/); |
| 88 |
return "string.special"; |
| 89 |
} else { |
| 90 |
var next; |
| 91 |
while ((next = stream.next()) != null) { |
| 92 |
if (next == quote) { state.tokenize = tokenBase; break; } |
| 93 |
if (next == "\\") { stream.backUp(1); break; } |
| 94 |
} |
| 95 |
return "string"; |
| 96 |
} |
| 97 |
}; |
| 98 |
} |
| 99 |
|
| 100 |
var ALIGN_YES = 1, ALIGN_NO = 2, BRACELESS = 4 |
| 101 |
|
| 102 |
function push(state, type, stream) { |
| 103 |
state.ctx = {type: type, |
| 104 |
indent: state.indent, |
| 105 |
flags: 0, |
| 106 |
column: stream.column(), |
| 107 |
prev: state.ctx}; |
| 108 |
} |
| 109 |
function setFlag(state, flag) { |
| 110 |
var ctx = state.ctx |
| 111 |
state.ctx = {type: ctx.type, |
| 112 |
indent: ctx.indent, |
| 113 |
flags: ctx.flags | flag, |
| 114 |
column: ctx.column, |
| 115 |
prev: ctx.prev} |
| 116 |
} |
| 117 |
function pop(state) { |
| 118 |
state.indent = state.ctx.indent; |
| 119 |
state.ctx = state.ctx.prev; |
| 120 |
} |
| 121 |
|
| 122 |
export const r = { |
| 123 |
name: "r", |
| 124 |
startState: function(indentUnit) { |
| 125 |
return {tokenize: tokenBase, |
| 126 |
ctx: {type: "top", |
| 127 |
indent: -indentUnit, |
| 128 |
flags: ALIGN_NO}, |
| 129 |
indent: 0, |
| 130 |
afterIdent: false}; |
| 131 |
}, |
| 132 |
|
| 133 |
token: function(stream, state) { |
| 134 |
if (stream.sol()) { |
| 135 |
if ((state.ctx.flags & 3) == 0) state.ctx.flags |= ALIGN_NO |
| 136 |
if (state.ctx.flags & BRACELESS) pop(state) |
| 137 |
state.indent = stream.indentation(); |
| 138 |
} |
| 139 |
if (stream.eatSpace()) return null; |
| 140 |
var style = state.tokenize(stream, state); |
| 141 |
if (style != "comment" && (state.ctx.flags & ALIGN_NO) == 0) setFlag(state, ALIGN_YES) |
| 142 |
|
| 143 |
if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && state.ctx.type == "block") pop(state); |
| 144 |
if (curPunc == "{") push(state, "}", stream); |
| 145 |
else if (curPunc == "(") { |
| 146 |
push(state, ")", stream); |
| 147 |
if (state.afterIdent) state.ctx.argList = true; |
| 148 |
} |
| 149 |
else if (curPunc == "[") push(state, "]", stream); |
| 150 |
else if (curPunc == "block") push(state, "block", stream); |
| 151 |
else if (curPunc == state.ctx.type) pop(state); |
| 152 |
else if (state.ctx.type == "block" && style != "comment") setFlag(state, BRACELESS) |
| 153 |
state.afterIdent = style == "variable" || style == "keyword"; |
| 154 |
return style; |
| 155 |
}, |
| 156 |
|
| 157 |
indent: function(state, textAfter, cx) { |
| 158 |
if (state.tokenize != tokenBase) return 0; |
| 159 |
var firstChar = textAfter && textAfter.charAt(0), ctx = state.ctx, |
| 160 |
closing = firstChar == ctx.type; |
| 161 |
if (ctx.flags & BRACELESS) ctx = ctx.prev |
| 162 |
if (ctx.type == "block") return ctx.indent + (firstChar == "{" ? 0 : cx.unit); |
| 163 |
else if (ctx.flags & ALIGN_YES) return ctx.column + (closing ? 0 : 1); |
| 164 |
else return ctx.indent + (closing ? 0 : cx.unit); |
| 165 |
}, |
| 166 |
|
| 167 |
languageData: { |
| 168 |
wordChars: ".", |
| 169 |
commentTokens: {line: "#"}, |
| 170 |
autocomplete: commonAtoms.concat(commonBuiltins, commonKeywords) |
| 171 |
} |
| 172 |
}; |