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.

187 lines
5.9 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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, "any":true, "comparable":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-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. });