5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineMode("go", function(config) {
15
var indentUnit = config.indentUnit;
18
"break":true, "case":true, "chan":true, "const":true, "continue":true,
19
"default":true, "defer":true, "else":true, "fallthrough":true, "for":true,
20
"func":true, "go":true, "goto":true, "if":true, "import":true,
21
"interface":true, "map":true, "package":true, "range":true, "return":true,
22
"select":true, "struct":true, "switch":true, "type":true, "var":true,
23
"bool":true, "byte":true, "complex64":true, "complex128":true,
24
"float32":true, "float64":true, "int8":true, "int16":true, "int32":true,
25
"int64":true, "string":true, "uint8":true, "uint16":true, "uint32":true,
26
"uint64":true, "int":true, "uint":true, "uintptr":true, "error": true,
31
"true":true, "false":true, "iota":true, "nil":true, "append":true,
32
"cap":true, "close":true, "complex":true, "copy":true, "delete":true, "imag":true,
33
"len":true, "make":true, "new":true, "panic":true, "print":true,
34
"println":true, "real":true, "recover":true
37
var isOperatorChar = /[+\-*&^%:=<>!|\/]/;
41
function tokenBase(stream, state) {
42
var ch = stream.next();
43
if (ch == '"' || ch == "'" || ch == "`") {
44
state.tokenize = tokenString(ch);
45
return state.tokenize(stream, state);
47
if (/[\d\.]/.test(ch)) {
49
stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/);
50
} else if (ch == "0") {
51
stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/);
53
stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/);
57
if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
62
if (stream.eat("*")) {
63
state.tokenize = tokenComment;
64
return tokenComment(stream, state);
66
if (stream.eat("/")) {
71
if (isOperatorChar.test(ch)) {
72
stream.eatWhile(isOperatorChar);
75
stream.eatWhile(/[\w\$_\xa1-\uffff]/);
76
var cur = stream.current();
77
if (keywords.propertyIsEnumerable(cur)) {
78
if (cur == "case" || cur == "default") curPunc = "case";
81
if (atoms.propertyIsEnumerable(cur)) return "atom";
85
function tokenString(quote) {
86
return function(stream, state) {
87
var escaped = false, next, end = false;
88
while ((next = stream.next()) != null) {
89
if (next == quote && !escaped) {end = true; break;}
90
escaped = !escaped && quote != "`" && next == "\\";
92
if (end || !(escaped || quote == "`"))
93
state.tokenize = tokenBase;
98
function tokenComment(stream, state) {
99
var maybeEnd = false, ch;
100
while (ch = stream.next()) {
101
if (ch == "/" && maybeEnd) {
102
state.tokenize = tokenBase;
105
maybeEnd = (ch == "*");
110
function Context(indented, column, type, align, prev) {
111
this.indented = indented;
112
this.column = column;
117
function pushContext(state, col, type) {
118
return state.context = new Context(state.indented, col, type, null, state.context);
120
function popContext(state) {
121
if (!state.context.prev) return;
122
var t = state.context.type;
123
if (t == ")" || t == "]" || t == "}")
124
state.indented = state.context.indented;
125
return state.context = state.context.prev;
131
startState: function(basecolumn) {
134
context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
140
token: function(stream, state) {
141
var ctx = state.context;
143
if (ctx.align == null) ctx.align = false;
144
state.indented = stream.indentation();
145
state.startOfLine = true;
146
if (ctx.type == "case") ctx.type = "}";
148
if (stream.eatSpace()) return null;
150
var style = (state.tokenize || tokenBase)(stream, state);
151
if (style == "comment") return style;
152
if (ctx.align == null) ctx.align = true;
154
if (curPunc == "{") pushContext(state, stream.column(), "}");
155
else if (curPunc == "[") pushContext(state, stream.column(), "]");
156
else if (curPunc == "(") pushContext(state, stream.column(), ")");
157
else if (curPunc == "case") ctx.type = "case";
158
else if (curPunc == "}" && ctx.type == "}") popContext(state);
159
else if (curPunc == ctx.type) popContext(state);
160
state.startOfLine = false;
164
indent: function(state, textAfter) {
165
if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
166
var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
167
if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) {
168
state.context.type = "}";
171
var closing = firstChar == ctx.type;
172
if (ctx.align) return ctx.column + (closing ? 0 : 1);
173
else return ctx.indented + (closing ? 0 : indentUnit);
176
electricChars: "{}):",
177
closeBrackets: "()[]{}''\"\"``",
179
blockCommentStart: "/*",
180
blockCommentEnd: "*/",
185
CodeMirror.defineMIME("text/x-go", "go");