You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

go.js 5.9KB

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