1
0

go.js 5.3 KB

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