go.js 5.7 KB

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