go.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("go", function(config) {
  13. var indentUnit = config.indentUnit;
  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, "error": true
  24. };
  25. var atoms = {
  26. "true":true, "false":true, "iota":true, "nil":true, "append":true,
  27. "cap":true, "close":true, "complex":true, "copy":true, "imag":true,
  28. "len":true, "make":true, "new":true, "panic":true, "print":true,
  29. "println":true, "real":true, "recover":true
  30. };
  31. var isOperatorChar = /[+\-*&^%:=<>!|\/]/;
  32. var curPunc;
  33. function tokenBase(stream, state) {
  34. var ch = stream.next();
  35. if (ch == '"' || ch == "'" || ch == "`") {
  36. state.tokenize = tokenString(ch);
  37. return state.tokenize(stream, state);
  38. }
  39. if (/[\d\.]/.test(ch)) {
  40. if (ch == ".") {
  41. stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/);
  42. } else if (ch == "0") {
  43. stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/);
  44. } else {
  45. stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/);
  46. }
  47. return "number";
  48. }
  49. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  50. curPunc = ch;
  51. return null;
  52. }
  53. if (ch == "/") {
  54. if (stream.eat("*")) {
  55. state.tokenize = tokenComment;
  56. return tokenComment(stream, state);
  57. }
  58. if (stream.eat("/")) {
  59. stream.skipToEnd();
  60. return "comment";
  61. }
  62. }
  63. if (isOperatorChar.test(ch)) {
  64. stream.eatWhile(isOperatorChar);
  65. return "operator";
  66. }
  67. stream.eatWhile(/[\w\$_\xa1-\uffff]/);
  68. var cur = stream.current();
  69. if (keywords.propertyIsEnumerable(cur)) {
  70. if (cur == "case" || cur == "default") curPunc = "case";
  71. return "keyword";
  72. }
  73. if (atoms.propertyIsEnumerable(cur)) return "atom";
  74. return "variable";
  75. }
  76. function tokenString(quote) {
  77. return function(stream, state) {
  78. var escaped = false, next, end = false;
  79. while ((next = stream.next()) != null) {
  80. if (next == quote && !escaped) {end = true; break;}
  81. escaped = !escaped && quote != "`" && next == "\\";
  82. }
  83. if (end || !(escaped || quote == "`"))
  84. state.tokenize = tokenBase;
  85. return "string";
  86. };
  87. }
  88. function tokenComment(stream, state) {
  89. var maybeEnd = false, ch;
  90. while (ch = stream.next()) {
  91. if (ch == "/" && maybeEnd) {
  92. state.tokenize = tokenBase;
  93. break;
  94. }
  95. maybeEnd = (ch == "*");
  96. }
  97. return "comment";
  98. }
  99. function Context(indented, column, type, align, prev) {
  100. this.indented = indented;
  101. this.column = column;
  102. this.type = type;
  103. this.align = align;
  104. this.prev = prev;
  105. }
  106. function pushContext(state, col, type) {
  107. return state.context = new Context(state.indented, col, type, null, state.context);
  108. }
  109. function popContext(state) {
  110. if (!state.context.prev) return;
  111. var t = state.context.type;
  112. if (t == ")" || t == "]" || t == "}")
  113. state.indented = state.context.indented;
  114. return state.context = state.context.prev;
  115. }
  116. // Interface
  117. return {
  118. startState: function(basecolumn) {
  119. return {
  120. tokenize: null,
  121. context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
  122. indented: 0,
  123. startOfLine: true
  124. };
  125. },
  126. token: function(stream, state) {
  127. var ctx = state.context;
  128. if (stream.sol()) {
  129. if (ctx.align == null) ctx.align = false;
  130. state.indented = stream.indentation();
  131. state.startOfLine = true;
  132. if (ctx.type == "case") ctx.type = "}";
  133. }
  134. if (stream.eatSpace()) return null;
  135. curPunc = null;
  136. var style = (state.tokenize || tokenBase)(stream, state);
  137. if (style == "comment") return style;
  138. if (ctx.align == null) ctx.align = true;
  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 == "}") ctx = popContext(state);
  144. else if (curPunc == ctx.type) popContext(state);
  145. state.startOfLine = false;
  146. return style;
  147. },
  148. indent: function(state, textAfter) {
  149. if (state.tokenize != tokenBase && state.tokenize != null) return 0;
  150. var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
  151. if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) {
  152. state.context.type = "}";
  153. return ctx.indented;
  154. }
  155. var closing = firstChar == ctx.type;
  156. if (ctx.align) return ctx.column + (closing ? 0 : 1);
  157. else return ctx.indented + (closing ? 0 : indentUnit);
  158. },
  159. electricChars: "{}):",
  160. fold: "brace",
  161. blockCommentStart: "/*",
  162. blockCommentEnd: "*/",
  163. lineComment: "//"
  164. };
  165. });
  166. CodeMirror.defineMIME("text/x-go", "go");
  167. });