| 1 |
function words(str) { |
| 2 |
var obj = {}, words = str.split(" "); |
| 3 |
for (var i = 0; i < words.length; ++i) obj[words[i]] = true; |
| 4 |
return obj; |
| 5 |
} |
| 6 |
var keywords = words( |
| 7 |
"abstract as assert boolean break byte case catch char class const continue def default " + |
| 8 |
"do double else enum extends final finally float for goto if implements import in " + |
| 9 |
"instanceof int interface long native new package private protected public return " + |
| 10 |
"short static strictfp super switch synchronized threadsafe throw throws trait transient " + |
| 11 |
"try void volatile while"); |
| 12 |
var blockKeywords = words("catch class def do else enum finally for if interface switch trait try while"); |
| 13 |
var standaloneKeywords = words("return break continue"); |
| 14 |
var atoms = words("null true false this"); |
| 15 |
|
| 16 |
var curPunc; |
| 17 |
function tokenBase(stream, state) { |
| 18 |
var ch = stream.next(); |
| 19 |
if (ch == '"' || ch == "'") { |
| 20 |
return startString(ch, stream, state); |
| 21 |
} |
| 22 |
if (/[\[\]{}\(\),;\:\.]/.test(ch)) { |
| 23 |
curPunc = ch; |
| 24 |
return null; |
| 25 |
} |
| 26 |
if (/\d/.test(ch)) { |
| 27 |
stream.eatWhile(/[\w\.]/); |
| 28 |
if (stream.eat(/eE/)) { stream.eat(/\+\-/); stream.eatWhile(/\d/); } |
| 29 |
return "number"; |
| 30 |
} |
| 31 |
if (ch == "/") { |
| 32 |
if (stream.eat("*")) { |
| 33 |
state.tokenize.push(tokenComment); |
| 34 |
return tokenComment(stream, state); |
| 35 |
} |
| 36 |
if (stream.eat("/")) { |
| 37 |
stream.skipToEnd(); |
| 38 |
return "comment"; |
| 39 |
} |
| 40 |
if (expectExpression(state.lastToken, false)) { |
| 41 |
return startString(ch, stream, state); |
| 42 |
} |
| 43 |
} |
| 44 |
if (ch == "-" && stream.eat(">")) { |
| 45 |
curPunc = "->"; |
| 46 |
return null; |
| 47 |
} |
| 48 |
if (/[+\-*&%=<>!?|\/~]/.test(ch)) { |
| 49 |
stream.eatWhile(/[+\-*&%=<>|~]/); |
| 50 |
return "operator"; |
| 51 |
} |
| 52 |
stream.eatWhile(/[\w\$_]/); |
| 53 |
if (ch == "@") { stream.eatWhile(/[\w\$_\.]/); return "meta"; } |
| 54 |
if (state.lastToken == ".") return "property"; |
| 55 |
if (stream.eat(":")) { curPunc = "proplabel"; return "property"; } |
| 56 |
var cur = stream.current(); |
| 57 |
if (atoms.propertyIsEnumerable(cur)) { return "atom"; } |
| 58 |
if (keywords.propertyIsEnumerable(cur)) { |
| 59 |
if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement"; |
| 60 |
else if (standaloneKeywords.propertyIsEnumerable(cur)) curPunc = "standalone"; |
| 61 |
return "keyword"; |
| 62 |
} |
| 63 |
return "variable"; |
| 64 |
} |
| 65 |
tokenBase.isBase = true; |
| 66 |
|
| 67 |
function startString(quote, stream, state) { |
| 68 |
var tripleQuoted = false; |
| 69 |
if (quote != "/" && stream.eat(quote)) { |
| 70 |
if (stream.eat(quote)) tripleQuoted = true; |
| 71 |
else return "string"; |
| 72 |
} |
| 73 |
function t(stream, state) { |
| 74 |
var escaped = false, next, end = !tripleQuoted; |
| 75 |
while ((next = stream.next()) != null) { |
| 76 |
if (next == quote && !escaped) { |
| 77 |
if (!tripleQuoted) { break; } |
| 78 |
if (stream.match(quote + quote)) { end = true; break; } |
| 79 |
} |
| 80 |
if (quote == '"' && next == "$" && !escaped) { |
| 81 |
if (stream.eat("{")) { |
| 82 |
state.tokenize.push(tokenBaseUntilBrace()); |
| 83 |
return "string"; |
| 84 |
} else if (stream.match(/^\w/, false)) { |
| 85 |
state.tokenize.push(tokenVariableDeref); |
| 86 |
return "string"; |
| 87 |
} |
| 88 |
} |
| 89 |
escaped = !escaped && next == "\\"; |
| 90 |
} |
| 91 |
if (end) state.tokenize.pop(); |
| 92 |
return "string"; |
| 93 |
} |
| 94 |
state.tokenize.push(t); |
| 95 |
return t(stream, state); |
| 96 |
} |
| 97 |
|
| 98 |
function tokenBaseUntilBrace() { |
| 99 |
var depth = 1; |
| 100 |
function t(stream, state) { |
| 101 |
if (stream.peek() == "}") { |
| 102 |
depth--; |
| 103 |
if (depth == 0) { |
| 104 |
state.tokenize.pop(); |
| 105 |
return state.tokenize[state.tokenize.length-1](stream, state); |
| 106 |
} |
| 107 |
} else if (stream.peek() == "{") { |
| 108 |
depth++; |
| 109 |
} |
| 110 |
return tokenBase(stream, state); |
| 111 |
} |
| 112 |
t.isBase = true; |
| 113 |
return t; |
| 114 |
} |
| 115 |
|
| 116 |
function tokenVariableDeref(stream, state) { |
| 117 |
var next = stream.match(/^(\.|[\w\$_]+)/) |
| 118 |
if (!next || !stream.match(next[0] == "." ? /^[\w$_]/ : /^\./)) state.tokenize.pop() |
| 119 |
if (!next) return state.tokenize[state.tokenize.length-1](stream, state) |
| 120 |
return next[0] == "." ? null : "variable" |
| 121 |
} |
| 122 |
|
| 123 |
function tokenComment(stream, state) { |
| 124 |
var maybeEnd = false, ch; |
| 125 |
while (ch = stream.next()) { |
| 126 |
if (ch == "/" && maybeEnd) { |
| 127 |
state.tokenize.pop(); |
| 128 |
break; |
| 129 |
} |
| 130 |
maybeEnd = (ch == "*"); |
| 131 |
} |
| 132 |
return "comment"; |
| 133 |
} |
| 134 |
|
| 135 |
function expectExpression(last, newline) { |
| 136 |
return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) || |
| 137 |
last == "newstatement" || last == "keyword" || last == "proplabel" || |
| 138 |
(last == "standalone" && !newline); |
| 139 |
} |
| 140 |
|
| 141 |
function Context(indented, column, type, align, prev) { |
| 142 |
this.indented = indented; |
| 143 |
this.column = column; |
| 144 |
this.type = type; |
| 145 |
this.align = align; |
| 146 |
this.prev = prev; |
| 147 |
} |
| 148 |
function pushContext(state, col, type) { |
| 149 |
return state.context = new Context(state.indented, col, type, null, state.context); |
| 150 |
} |
| 151 |
function popContext(state) { |
| 152 |
var t = state.context.type; |
| 153 |
if (t == ")" || t == "]" || t == "}") |
| 154 |
state.indented = state.context.indented; |
| 155 |
return state.context = state.context.prev; |
| 156 |
} |
| 157 |
|
| 158 |
// Interface |
| 159 |
|
| 160 |
export const groovy = { |
| 161 |
name: "groovy", |
| 162 |
startState: function(indentUnit) { |
| 163 |
return { |
| 164 |
tokenize: [tokenBase], |
| 165 |
context: new Context(-indentUnit, 0, "top", false), |
| 166 |
indented: 0, |
| 167 |
startOfLine: true, |
| 168 |
lastToken: null |
| 169 |
}; |
| 170 |
}, |
| 171 |
|
| 172 |
token: function(stream, state) { |
| 173 |
var ctx = state.context; |
| 174 |
if (stream.sol()) { |
| 175 |
if (ctx.align == null) ctx.align = false; |
| 176 |
state.indented = stream.indentation(); |
| 177 |
state.startOfLine = true; |
| 178 |
// Automatic semicolon insertion |
| 179 |
if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) { |
| 180 |
popContext(state); ctx = state.context; |
| 181 |
} |
| 182 |
} |
| 183 |
if (stream.eatSpace()) return null; |
| 184 |
curPunc = null; |
| 185 |
var style = state.tokenize[state.tokenize.length-1](stream, state); |
| 186 |
if (style == "comment") return style; |
| 187 |
if (ctx.align == null) ctx.align = true; |
| 188 |
|
| 189 |
if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state); |
| 190 |
// Handle indentation for {x -> \n ... } |
| 191 |
else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") { |
| 192 |
popContext(state); |
| 193 |
state.context.align = false; |
| 194 |
} |
| 195 |
else if (curPunc == "{") pushContext(state, stream.column(), "}"); |
| 196 |
else if (curPunc == "[") pushContext(state, stream.column(), "]"); |
| 197 |
else if (curPunc == "(") pushContext(state, stream.column(), ")"); |
| 198 |
else if (curPunc == "}") { |
| 199 |
while (ctx.type == "statement") ctx = popContext(state); |
| 200 |
if (ctx.type == "}") ctx = popContext(state); |
| 201 |
while (ctx.type == "statement") ctx = popContext(state); |
| 202 |
} |
| 203 |
else if (curPunc == ctx.type) popContext(state); |
| 204 |
else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement")) |
| 205 |
pushContext(state, stream.column(), "statement"); |
| 206 |
state.startOfLine = false; |
| 207 |
state.lastToken = curPunc || style; |
| 208 |
return style; |
| 209 |
}, |
| 210 |
|
| 211 |
indent: function(state, textAfter, cx) { |
| 212 |
if (!state.tokenize[state.tokenize.length-1].isBase) return null; |
| 213 |
var firstChar = textAfter && textAfter.charAt(0), ctx = state.context; |
| 214 |
if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) ctx = ctx.prev; |
| 215 |
var closing = firstChar == ctx.type; |
| 216 |
if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : cx.unit); |
| 217 |
else if (ctx.align) return ctx.column + (closing ? 0 : 1); |
| 218 |
else return ctx.indented + (closing ? 0 : cx.unit); |
| 219 |
}, |
| 220 |
|
| 221 |
languageData: { |
| 222 |
indentOnInput: /^\s*[{}]$/, |
| 223 |
commentTokens: {line: "//", block: {open: "/*", close: "*/"}}, |
| 224 |
closeBrackets: {brackets: ["(", "[", "{", "'", '"', "'''", '"""']} |
| 225 |
} |
| 226 |
}; |