github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/assets/muted/lib/go.js (about) 1 (function(mod) { 2 if (typeof exports == "object" && typeof module == "object") // CommonJS 3 mod(require("../../lib/codemirror")); 4 else if (typeof define == "function" && define.amd) // AMD 5 define(["../../lib/codemirror"], mod); 6 else // Plain browser env 7 mod(CodeMirror); 8 })(function(CodeMirror) { 9 "use strict"; 10 11 CodeMirror.defineMode("go", function(config) { 12 var indentUnit = config.indentUnit; 13 14 var keywords = { 15 "break":true, "case":true, "chan":true, "const":true, "continue":true, 16 "default":true, "defer":true, "else":true, "fallthrough":true, "for":true, 17 "func":true, "go":true, "goto":true, "if":true, "import":true, 18 "interface":true, "map":true, "package":true, "range":true, "return":true, 19 "select":true, "struct":true, "switch":true, "type":true, "var":true, 20 "bool":true, "byte":true, "complex64":true, "complex128":true, 21 "float32":true, "float64":true, "int8":true, "int16":true, "int32":true, 22 "int64":true, "string":true, "uint8":true, "uint16":true, "uint32":true, 23 "uint64":true, "int":true, "uint":true, "uintptr":true, "big": true, 24 "main": true, "init": true, "this":true 25 }; 26 27 var atoms = { 28 "true":true, "false":true, "iota":true, "nil":true, "append":true, 29 "cap":true, "close":true, "complex":true, "copy":true, "imag":true, 30 "len":true, "make":true, "new":true, "panic":true, "print":true, 31 "println":true, "real":true, "recover":true, 32 }; 33 34 var isOperatorChar = /[+\-*&^%:=<>!|\/]/; 35 36 var curPunc; 37 38 function tokenBase(stream, state) { 39 var ch = stream.next(); 40 if (ch == '"' || ch == "'" || ch == "`") { 41 state.tokenize = tokenString(ch); 42 return state.tokenize(stream, state); 43 } 44 if (/[\d\.]/.test(ch)) { 45 if (ch == ".") { 46 stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/); 47 } else if (ch == "0") { 48 stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/); 49 } else { 50 stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/); 51 } 52 return "number"; 53 } 54 if (/[\[\]{}\(\),;\:\.]/.test(ch)) { 55 curPunc = ch; 56 return null; 57 } 58 if (ch == "/") { 59 if (stream.eat("*")) { 60 state.tokenize = tokenComment; 61 return tokenComment(stream, state); 62 } 63 if (stream.eat("/")) { 64 stream.skipToEnd(); 65 return "comment"; 66 } 67 } 68 if (isOperatorChar.test(ch)) { 69 stream.eatWhile(isOperatorChar); 70 return "operator"; 71 } 72 stream.eatWhile(/[\w\$_]/); 73 var cur = stream.current(); 74 if (keywords.propertyIsEnumerable(cur)) { 75 if (cur == "case" || cur == "default") curPunc = "case"; 76 return "keyword"; 77 } 78 if (atoms.propertyIsEnumerable(cur)) return "atom"; 79 return "variable"; 80 } 81 82 function tokenString(quote) { 83 return function(stream, state) { 84 var escaped = false, next, end = false; 85 while ((next = stream.next()) != null) { 86 if (next == quote && !escaped) {end = true; break;} 87 escaped = !escaped && next == "\\"; 88 } 89 if (end || !(escaped || quote == "`")) 90 state.tokenize = tokenBase; 91 return "string"; 92 }; 93 } 94 95 function tokenComment(stream, state) { 96 var maybeEnd = false, ch; 97 while (ch = stream.next()) { 98 if (ch == "/" && maybeEnd) { 99 state.tokenize = tokenBase; 100 break; 101 } 102 maybeEnd = (ch == "*"); 103 } 104 return "comment"; 105 } 106 107 function Context(indented, column, type, align, prev) { 108 this.indented = indented; 109 this.column = column; 110 this.type = type; 111 this.align = align; 112 this.prev = prev; 113 } 114 function pushContext(state, col, type) { 115 return state.context = new Context(state.indented, col, type, null, state.context); 116 } 117 function popContext(state) { 118 var t = state.context.type; 119 if (t == ")" || t == "]" || t == "}") 120 state.indented = state.context.indented; 121 return state.context = state.context.prev; 122 } 123 124 // Interface 125 126 return { 127 startState: function(basecolumn) { 128 return { 129 tokenize: null, 130 context: new Context((basecolumn || 0) - indentUnit, 0, "top", false), 131 indented: 0, 132 startOfLine: true 133 }; 134 }, 135 136 token: function(stream, state) { 137 var ctx = state.context; 138 if (stream.sol()) { 139 if (ctx.align == null) ctx.align = false; 140 state.indented = stream.indentation(); 141 state.startOfLine = true; 142 if (ctx.type == "case") ctx.type = "}"; 143 } 144 if (stream.eatSpace()) return null; 145 curPunc = null; 146 var style = (state.tokenize || tokenBase)(stream, state); 147 if (style == "comment") return style; 148 if (ctx.align == null) ctx.align = true; 149 150 if (curPunc == "{") pushContext(state, stream.column(), "}"); 151 else if (curPunc == "[") pushContext(state, stream.column(), "]"); 152 else if (curPunc == "(") pushContext(state, stream.column(), ")"); 153 else if (curPunc == "case") ctx.type = "case"; 154 else if (curPunc == "}" && ctx.type == "}") ctx = popContext(state); 155 else if (curPunc == ctx.type) popContext(state); 156 state.startOfLine = false; 157 return style; 158 }, 159 160 indent: function(state, textAfter) { 161 if (state.tokenize != tokenBase && state.tokenize != null) return 0; 162 var ctx = state.context, firstChar = textAfter && textAfter.charAt(0); 163 if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) { 164 state.context.type = "}"; 165 return ctx.indented; 166 } 167 var closing = firstChar == ctx.type; 168 if (ctx.align) return ctx.column + (closing ? 0 : 1); 169 else return ctx.indented + (closing ? 0 : indentUnit); 170 }, 171 172 electricChars: "{}):", 173 fold: "brace", 174 blockCommentStart: "/*", 175 blockCommentEnd: "*/", 176 lineComment: "//" 177 }; 178 }); 179 180 CodeMirror.defineMIME("text/x-go", "go"); 181 182 });