go.js 5.6 KB

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