| 1 |
var keywords = { |
| 2 |
"break":true, "case":true, "chan":true, "const":true, "continue":true, |
| 3 |
"default":true, "defer":true, "else":true, "fallthrough":true, "for":true, |
| 4 |
"func":true, "go":true, "goto":true, "if":true, "import":true, |
| 5 |
"interface":true, "map":true, "package":true, "range":true, "return":true, |
| 6 |
"select":true, "struct":true, "switch":true, "type":true, "var":true, |
| 7 |
"bool":true, "byte":true, "complex64":true, "complex128":true, |
| 8 |
"float32":true, "float64":true, "int8":true, "int16":true, "int32":true, |
| 9 |
"int64":true, "string":true, "uint8":true, "uint16":true, "uint32":true, |
| 10 |
"uint64":true, "int":true, "uint":true, "uintptr":true, "error": true, |
| 11 |
"rune":true, "any":true, "comparable":true |
| 12 |
}; |
| 13 |
|
| 14 |
var atoms = { |
| 15 |
"true":true, "false":true, "iota":true, "nil":true, "append":true, |
| 16 |
"cap":true, "close":true, "complex":true, "copy":true, "delete":true, "imag":true, |
| 17 |
"len":true, "make":true, "new":true, "panic":true, "print":true, |
| 18 |
"println":true, "real":true, "recover":true |
| 19 |
}; |
| 20 |
|
| 21 |
var isOperatorChar = /[+\-*&^%:=<>!|\/]/; |
| 22 |
|
| 23 |
var curPunc; |
| 24 |
|
| 25 |
function tokenBase(stream, state) { |
| 26 |
var ch = stream.next(); |
| 27 |
if (ch == '"' || ch == "'" || ch == "`") { |
| 28 |
state.tokenize = tokenString(ch); |
| 29 |
return state.tokenize(stream, state); |
| 30 |
} |
| 31 |
if (/[\d\.]/.test(ch)) { |
| 32 |
if (ch == ".") { |
| 33 |
stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/); |
| 34 |
} else if (ch == "0") { |
| 35 |
stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/); |
| 36 |
} else { |
| 37 |
stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/); |
| 38 |
} |
| 39 |
return "number"; |
| 40 |
} |
| 41 |
if (/[\[\]{}\(\),;\:\.]/.test(ch)) { |
| 42 |
curPunc = ch; |
| 43 |
return null; |
| 44 |
} |
| 45 |
if (ch == "/") { |
| 46 |
if (stream.eat("*")) { |
| 47 |
state.tokenize = tokenComment; |
| 48 |
return tokenComment(stream, state); |
| 49 |
} |
| 50 |
if (stream.eat("/")) { |
| 51 |
stream.skipToEnd(); |
| 52 |
return "comment"; |
| 53 |
} |
| 54 |
} |
| 55 |
if (isOperatorChar.test(ch)) { |
| 56 |
stream.eatWhile(isOperatorChar); |
| 57 |
return "operator"; |
| 58 |
} |
| 59 |
stream.eatWhile(/[\w\$_\xa1-\uffff]/); |
| 60 |
var cur = stream.current(); |
| 61 |
if (keywords.propertyIsEnumerable(cur)) { |
| 62 |
if (cur == "case" || cur == "default") curPunc = "case"; |
| 63 |
return "keyword"; |
| 64 |
} |
| 65 |
if (atoms.propertyIsEnumerable(cur)) return "atom"; |
| 66 |
return "variable"; |
| 67 |
} |
| 68 |
|
| 69 |
function tokenString(quote) { |
| 70 |
return function(stream, state) { |
| 71 |
var escaped = false, next, end = false; |
| 72 |
while ((next = stream.next()) != null) { |
| 73 |
if (next == quote && !escaped) {end = true; break;} |
| 74 |
escaped = !escaped && quote != "`" && next == "\\"; |
| 75 |
} |
| 76 |
if (end || !(escaped || quote == "`")) |
| 77 |
state.tokenize = tokenBase; |
| 78 |
return "string"; |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
function tokenComment(stream, state) { |
| 83 |
var maybeEnd = false, ch; |
| 84 |
while (ch = stream.next()) { |
| 85 |
if (ch == "/" && maybeEnd) { |
| 86 |
state.tokenize = tokenBase; |
| 87 |
break; |
| 88 |
} |
| 89 |
maybeEnd = (ch == "*"); |
| 90 |
} |
| 91 |
return "comment"; |
| 92 |
} |
| 93 |
|
| 94 |
function Context(indented, column, type, align, prev) { |
| 95 |
this.indented = indented; |
| 96 |
this.column = column; |
| 97 |
this.type = type; |
| 98 |
this.align = align; |
| 99 |
this.prev = prev; |
| 100 |
} |
| 101 |
function pushContext(state, col, type) { |
| 102 |
return state.context = new Context(state.indented, col, type, null, state.context); |
| 103 |
} |
| 104 |
function popContext(state) { |
| 105 |
if (!state.context.prev) return; |
| 106 |
var t = state.context.type; |
| 107 |
if (t == ")" || t == "]" || t == "}") |
| 108 |
state.indented = state.context.indented; |
| 109 |
return state.context = state.context.prev; |
| 110 |
} |
| 111 |
|
| 112 |
// Interface |
| 113 |
|
| 114 |
export const go = { |
| 115 |
name: "go", |
| 116 |
startState: function(indentUnit) { |
| 117 |
return { |
| 118 |
tokenize: null, |
| 119 |
context: new Context(-indentUnit, 0, "top", false), |
| 120 |
indented: 0, |
| 121 |
startOfLine: true |
| 122 |
}; |
| 123 |
}, |
| 124 |
|
| 125 |
token: function(stream, state) { |
| 126 |
var ctx = state.context; |
| 127 |
if (stream.sol()) { |
| 128 |
if (ctx.align == null) ctx.align = false; |
| 129 |
state.indented = stream.indentation(); |
| 130 |
state.startOfLine = true; |
| 131 |
if (ctx.type == "case") ctx.type = "}"; |
| 132 |
} |
| 133 |
if (stream.eatSpace()) return null; |
| 134 |
curPunc = null; |
| 135 |
var style = (state.tokenize || tokenBase)(stream, state); |
| 136 |
if (style == "comment") return style; |
| 137 |
if (ctx.align == null) ctx.align = true; |
| 138 |
|
| 139 |
if (curPunc == "{") pushContext(state, stream.column(), "}"); |
| 140 |
else if (curPunc == "[") pushContext(state, stream.column(), "]"); |
| 141 |
else if (curPunc == "(") pushContext(state, stream.column(), ")"); |
| 142 |
else if (curPunc == "case") ctx.type = "case"; |
| 143 |
else if (curPunc == "}" && ctx.type == "}") popContext(state); |
| 144 |
else if (curPunc == ctx.type) popContext(state); |
| 145 |
state.startOfLine = false; |
| 146 |
return style; |
| 147 |
}, |
| 148 |
|
| 149 |
indent: function(state, textAfter, cx) { |
| 150 |
if (state.tokenize != tokenBase && state.tokenize != null) return null; |
| 151 |
var ctx = state.context, firstChar = textAfter && textAfter.charAt(0); |
| 152 |
if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) return ctx.indented; |
| 153 |
var closing = firstChar == ctx.type; |
| 154 |
if (ctx.align) return ctx.column + (closing ? 0 : 1); |
| 155 |
else return ctx.indented + (closing ? 0 : cx.unit); |
| 156 |
}, |
| 157 |
|
| 158 |
languageData: { |
| 159 |
indentOnInput: /^\s([{}]|case |default\s*:)$/, |
| 160 |
commentTokens: {line: "//", block: {open: "/*", close: "*/"}} |
| 161 |
} |
| 162 |
}; |
| 163 |
|