~ubuntu-branches/ubuntu/utopic/codemirror-js/utopic

« back to all changes in this revision

Viewing changes to mode/go/go.js

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2012-04-12 12:25:28 UTC
  • Revision ID: package-import@ubuntu.com-20120412122528-8xp5a8frj4h1d3ee
Tags: upstream-2.23
ImportĀ upstreamĀ versionĀ 2.23

Show diffs side-by-side

added added

removed removed

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