Theme Inspinia
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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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
  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 && 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. var t = state.context.type;
  111. if (t == ")" || t == "]" || t == "}")
  112. state.indented = state.context.indented;
  113. return state.context = state.context.prev;
  114. }
  115. // Interface
  116. return {
  117. startState: function(basecolumn) {
  118. return {
  119. tokenize: null,
  120. context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
  121. indented: 0,
  122. startOfLine: true
  123. };
  124. },
  125. token: function(stream, state) {
  126. var ctx = state.context;
  127. if (stream.sol()) {
  128. if (ctx.align == null) ctx.align = false;
  129. state.indented = stream.indentation();
  130. state.startOfLine = true;
  131. if (ctx.type == "case") ctx.type = "}";
  132. }
  133. if (stream.eatSpace()) return null;
  134. curPunc = null;
  135. var style = (state.tokenize || tokenBase)(stream, state);
  136. if (style == "comment") return style;
  137. if (ctx.align == null) ctx.align = true;
  138. if (curPunc == "{") pushContext(state, stream.column(), "}");
  139. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  140. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  141. else if (curPunc == "case") ctx.type = "case";
  142. else if (curPunc == "}" && ctx.type == "}") ctx = popContext(state);
  143. else if (curPunc == ctx.type) popContext(state);
  144. state.startOfLine = false;
  145. return style;
  146. },
  147. indent: function(state, textAfter) {
  148. if (state.tokenize != tokenBase && state.tokenize != null) return 0;
  149. var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
  150. if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) {
  151. state.context.type = "}";
  152. return ctx.indented;
  153. }
  154. var closing = firstChar == ctx.type;
  155. if (ctx.align) return ctx.column + (closing ? 0 : 1);
  156. else return ctx.indented + (closing ? 0 : indentUnit);
  157. },
  158. electricChars: "{}):",
  159. fold: "brace",
  160. blockCommentStart: "/*",
  161. blockCommentEnd: "*/",
  162. lineComment: "//"
  163. };
  164. });
  165. CodeMirror.defineMIME("text/x-go", "go");
  166. });