Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
5 lars 1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: http://codemirror.net/LICENSE
3
 
4
// Swift mode created by Michael Kaminsky https://github.com/mkaminsky11
5
 
6
(function(mod) {
7
  if (typeof exports == "object" && typeof module == "object")
8
    mod(require("../../lib/codemirror"))
9
  else if (typeof define == "function" && define.amd)
10
    define(["../../lib/codemirror"], mod)
11
  else
12
    mod(CodeMirror)
13
})(function(CodeMirror) {
14
  "use strict"
15
 
16
  function trim(str) { return /^\s*(.*?)\s*$/.exec(str)[1] }
17
 
18
  var separators = [" ","\\\+","\\\-","\\\(","\\\)","\\\*","/",":","\\\?","\\\<","\\\>"," ","\\\."]
19
  var tokens = new RegExp(separators.join("|"),"g")
20
 
21
  function getWord(string, pos) {
22
    var index = -1, count = 1
23
    var words = string.split(tokens)
24
    for (var i = 0; i < words.length; i++) {
25
      for(var j = 1; j <= words[i].length; j++) {
26
        if (count==pos) index = i
27
        count++
28
      }
29
      count++
30
    }
31
    var ret = ["", ""]
32
    if (pos == 0) {
33
      ret[1] = words[0]
34
      ret[0] = null
35
    } else {
36
      ret[1] = words[index]
37
      ret[0] = words[index-1]
38
    }
39
    return ret
40
  }
41
 
42
  CodeMirror.defineMode("swift", function() {
43
    var keywords=["var","let","class","deinit","enum","extension","func","import","init","let","protocol","static","struct","subscript","typealias","var","as","dynamicType","is","new","super","self","Self","Type","__COLUMN__","__FILE__","__FUNCTION__","__LINE__","break","case","continue","default","do","else","fallthrough","if","in","for","return","switch","where","while","associativity","didSet","get","infix","inout","left","mutating","none","nonmutating","operator","override","postfix","precedence","prefix","right","set","unowned","unowned(safe)","unowned(unsafe)","weak","willSet"]
44
    var commonConstants=["Infinity","NaN","undefined","null","true","false","on","off","yes","no","nil","null","this","super"]
45
    var types=["String","bool","int","string","double","Double","Int","Float","float","public","private","extension"]
46
    var numbers=["0","1","2","3","4","5","6","7","8","9"]
47
    var operators=["+","-","/","*","%","=","|","&","<",">"]
48
    var punc=[";",",",".","(",")","{","}","[","]"]
49
    var delimiters=/^(?:[()\[\]{},:`=;]|\.\.?\.?)/
50
    var identifiers=/^[_A-Za-z$][_A-Za-z$0-9]*/
51
    var properties=/^(@|this\.)[_A-Za-z$][_A-Za-z$0-9]*/
52
    var regexPrefixes=/^(\/{3}|\/)/
53
 
54
    return {
55
      startState: function() {
56
        return {
57
          prev: false,
58
          string: false,
59
          escape: false,
60
          inner: false,
61
          comment: false,
62
          num_left: 0,
63
          num_right: 0,
64
          doubleString: false,
65
          singleString: false
66
        }
67
      },
68
      token: function(stream, state) {
69
        if (stream.eatSpace()) return null
70
 
71
        var ch = stream.next()
72
        if (state.string) {
73
          if (state.escape) {
74
            state.escape = false
75
            return "string"
76
          } else {
77
            if ((ch == "\"" && (state.doubleString && !state.singleString) ||
78
                 (ch == "'" && (!state.doubleString && state.singleString))) &&
79
                !state.escape) {
80
              state.string = false
81
              state.doubleString = false
82
              state.singleString = false
83
              return "string"
84
            } else if (ch == "\\" && stream.peek() == "(") {
85
              state.inner = true
86
              state.string = false
87
              return "keyword"
88
            } else if (ch == "\\" && stream.peek() != "(") {
89
              state.escape = true
90
              state.string = true
91
              return "string"
92
            } else {
93
              return "string"
94
            }
95
          }
96
        } else if (state.comment) {
97
          if (ch == "*" && stream.peek() == "/") {
98
            state.prev = "*"
99
            return "comment"
100
          } else if (ch == "/" && state.prev == "*") {
101
            state.prev = false
102
            state.comment = false
103
            return "comment"
104
          }
105
          return "comment"
106
        } else {
107
          if (ch == "/") {
108
            if (stream.peek() == "/") {
109
              stream.skipToEnd()
110
              return "comment"
111
            }
112
            if (stream.peek() == "*") {
113
              state.comment = true
114
              return "comment"
115
            }
116
          }
117
          if (ch == "(" && state.inner) {
118
            state.num_left++
119
            return null
120
          }
121
          if (ch == ")" && state.inner) {
122
            state.num_right++
123
            if (state.num_left == state.num_right) {
124
              state.inner=false
125
              state.string=true
126
            }
127
            return null
128
          }
129
 
130
          var ret = getWord(stream.string, stream.pos)
131
          var the_word = ret[1]
132
          var prev_word = ret[0]
133
 
134
          if (operators.indexOf(ch + "") > -1) return "operator"
135
          if (punc.indexOf(ch) > -1) return "punctuation"
136
 
137
          if (typeof the_word != "undefined") {
138
            the_word = trim(the_word)
139
            if (typeof prev_word != "undefined") prev_word = trim(prev_word)
140
            if (the_word.charAt(0) == "#") return null
141
 
142
            if (types.indexOf(the_word) > -1) return "def"
143
            if (commonConstants.indexOf(the_word) > -1) return "atom"
144
            if (numbers.indexOf(the_word) > -1) return "number"
145
 
146
            if ((numbers.indexOf(the_word.charAt(0) + "") > -1 ||
147
                 operators.indexOf(the_word.charAt(0) + "") > -1) &&
148
                numbers.indexOf(ch) > -1) {
149
              return "number"
150
            }
151
 
152
            if (keywords.indexOf(the_word) > -1 ||
153
                keywords.indexOf(the_word.split(tokens)[0]) > -1)
154
              return "keyword"
155
            if (keywords.indexOf(prev_word) > -1) return "def"
156
          }
157
          if (ch == '"' && !state.doubleString) {
158
            state.string = true
159
            state.doubleString = true
160
            return "string"
161
          }
162
          if (ch == "'" && !state.singleString) {
163
            state.string = true
164
            state.singleString = true
165
            return "string"
166
          }
167
          if (ch == "(" && state.inner)
168
            state.num_left++
169
          if (ch == ")" && state.inner) {
170
            state.num_right++
171
            if (state.num_left == state.num_right) {
172
              state.inner = false
173
              state.string = true
174
            }
175
            return null
176
          }
177
          if (stream.match(/^-?[0-9\.]/, false)) {
178
            if (stream.match(/^-?\d*\.\d+(e[\+\-]?\d+)?/i) ||
179
                stream.match(/^-?\d+\.\d*/) ||
180
                stream.match(/^-?\.\d+/)) {
181
              if (stream.peek() == ".") stream.backUp(1)
182
              return "number"
183
            }
184
            if (stream.match(/^-?0x[0-9a-f]+/i) ||
185
                stream.match(/^-?[1-9]\d*(e[\+\-]?\d+)?/) ||
186
                stream.match(/^-?0(?![\dx])/i))
187
              return "number"
188
          }
189
          if (stream.match(regexPrefixes)) {
190
            if (stream.current()!="/" || stream.match(/^.*\//,false)) return "string"
191
            else stream.backUp(1)
192
          }
193
          if (stream.match(delimiters)) return "punctuation"
194
          if (stream.match(identifiers)) return "variable"
195
          if (stream.match(properties)) return "property"
196
          return "variable"
197
        }
198
      }
199
    }
200
  })
201
 
202
  CodeMirror.defineMIME("text/x-swift","swift")
203
})