all repos — NoPaste @ 4d5201f1a9fd36bf5ab4e04de97fc70c12081b9a

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/go/go.js (view raw)

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