Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/mode/groovy/groovy.js
1294 views
CodeMirror.defineMode("groovy", function(config) {1function words(str) {2var obj = {}, words = str.split(" ");3for (var i = 0; i < words.length; ++i) obj[words[i]] = true;4return obj;5}6var 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 transient " +11"try void volatile while");12var blockKeywords = words("catch class do else finally for if switch try while enum interface def");13var atoms = words("null true false this");1415var curPunc;16function tokenBase(stream, state) {17var ch = stream.next();18if (ch == '"' || ch == "'") {19return startString(ch, stream, state);20}21if (/[\[\]{}\(\),;\:\.]/.test(ch)) {22curPunc = ch;23return null;24}25if (/\d/.test(ch)) {26stream.eatWhile(/[\w\.]/);27if (stream.eat(/eE/)) { stream.eat(/\+\-/); stream.eatWhile(/\d/); }28return "number";29}30if (ch == "/") {31if (stream.eat("*")) {32state.tokenize.push(tokenComment);33return tokenComment(stream, state);34}35if (stream.eat("/")) {36stream.skipToEnd();37return "comment";38}39if (expectExpression(state.lastToken)) {40return startString(ch, stream, state);41}42}43if (ch == "-" && stream.eat(">")) {44curPunc = "->";45return null;46}47if (/[+\-*&%=<>!?|\/~]/.test(ch)) {48stream.eatWhile(/[+\-*&%=<>|~]/);49return "operator";50}51stream.eatWhile(/[\w\$_]/);52if (ch == "@") { stream.eatWhile(/[\w\$_\.]/); return "meta"; }53if (state.lastToken == ".") return "property";54if (stream.eat(":")) { curPunc = "proplabel"; return "property"; }55var cur = stream.current();56if (atoms.propertyIsEnumerable(cur)) { return "atom"; }57if (keywords.propertyIsEnumerable(cur)) {58if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";59return "keyword";60}61return "variable";62}63tokenBase.isBase = true;6465function startString(quote, stream, state) {66var tripleQuoted = false;67if (quote != "/" && stream.eat(quote)) {68if (stream.eat(quote)) tripleQuoted = true;69else return "string";70}71function t(stream, state) {72var escaped = false, next, end = !tripleQuoted;73while ((next = stream.next()) != null) {74if (next == quote && !escaped) {75if (!tripleQuoted) { break; }76if (stream.match(quote + quote)) { end = true; break; }77}78if (quote == '"' && next == "$" && !escaped && stream.eat("{")) {79state.tokenize.push(tokenBaseUntilBrace());80return "string";81}82escaped = !escaped && next == "\\";83}84if (end) state.tokenize.pop();85return "string";86}87state.tokenize.push(t);88return t(stream, state);89}9091function tokenBaseUntilBrace() {92var depth = 1;93function t(stream, state) {94if (stream.peek() == "}") {95depth--;96if (depth == 0) {97state.tokenize.pop();98return state.tokenize[state.tokenize.length-1](stream, state);99}100} else if (stream.peek() == "{") {101depth++;102}103return tokenBase(stream, state);104}105t.isBase = true;106return t;107}108109function tokenComment(stream, state) {110var maybeEnd = false, ch;111while (ch = stream.next()) {112if (ch == "/" && maybeEnd) {113state.tokenize.pop();114break;115}116maybeEnd = (ch == "*");117}118return "comment";119}120121function expectExpression(last) {122return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||123last == "newstatement" || last == "keyword" || last == "proplabel";124}125126function Context(indented, column, type, align, prev) {127this.indented = indented;128this.column = column;129this.type = type;130this.align = align;131this.prev = prev;132}133function pushContext(state, col, type) {134return state.context = new Context(state.indented, col, type, null, state.context);135}136function popContext(state) {137var t = state.context.type;138if (t == ")" || t == "]" || t == "}")139state.indented = state.context.indented;140return state.context = state.context.prev;141}142143// Interface144145return {146startState: function(basecolumn) {147return {148tokenize: [tokenBase],149context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),150indented: 0,151startOfLine: true,152lastToken: null153};154},155156token: function(stream, state) {157var ctx = state.context;158if (stream.sol()) {159if (ctx.align == null) ctx.align = false;160state.indented = stream.indentation();161state.startOfLine = true;162// Automatic semicolon insertion163if (ctx.type == "statement" && !expectExpression(state.lastToken)) {164popContext(state); ctx = state.context;165}166}167if (stream.eatSpace()) return null;168curPunc = null;169var style = state.tokenize[state.tokenize.length-1](stream, state);170if (style == "comment") return style;171if (ctx.align == null) ctx.align = true;172173if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);174// Handle indentation for {x -> \n ... }175else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") {176popContext(state);177state.context.align = false;178}179else if (curPunc == "{") pushContext(state, stream.column(), "}");180else if (curPunc == "[") pushContext(state, stream.column(), "]");181else if (curPunc == "(") pushContext(state, stream.column(), ")");182else if (curPunc == "}") {183while (ctx.type == "statement") ctx = popContext(state);184if (ctx.type == "}") ctx = popContext(state);185while (ctx.type == "statement") ctx = popContext(state);186}187else if (curPunc == ctx.type) popContext(state);188else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))189pushContext(state, stream.column(), "statement");190state.startOfLine = false;191state.lastToken = curPunc || style;192return style;193},194195indent: function(state, textAfter) {196if (!state.tokenize[state.tokenize.length-1].isBase) return 0;197var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;198if (ctx.type == "statement" && !expectExpression(state.lastToken)) ctx = ctx.prev;199var closing = firstChar == ctx.type;200if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);201else if (ctx.align) return ctx.column + (closing ? 0 : 1);202else return ctx.indented + (closing ? 0 : config.indentUnit);203},204205electricChars: "{}",206fold: "brace"207};208});209210CodeMirror.defineMIME("text/x-groovy", "groovy");211212213