CodeMirror: JavaScript mode

1
// Demo code (the actual new parser character stream implementation)
2
 
3
function StringStream(string) {
4
  this.pos = 0;
5
  this.string = string;
6
}
7
 
8
StringStream.prototype = {
9
  done: function() {return this.pos >= this.string.length;},
10
  peek: function() {return this.string.charAt(this.pos);},
11
  next: function() {
12
    if (this.pos < this.string.length)
13
      return this.string.charAt(this.pos++);
14
  },
15
  eat: function(match) {
16
    var ch = this.string.charAt(this.pos);
17
    if (typeof match == "string") var ok = ch == match;
18
    else var ok = ch && match.test ? match.test(ch) : match(ch);
19
    if (ok) {this.pos++; return ch;}
20
  },
21
  eatWhile: function(match) {
22
    var start = this.pos;
23
    while (this.eat(match));
24
    if (this.pos > start) return this.string.slice(start, this.pos);
25
  },
26
  backUp: function(n) {this.pos -= n;},
27
  column: function() {return this.pos;},
28
  eatSpace: function() {
29
    var start = this.pos;
30
    while (/\s/.test(this.string.charAt(this.pos))) this.pos++;
31
    return this.pos - start;
32
  },
33
  match: function(pattern, consume, caseInsensitive) {
34
    if (typeof pattern == "string") {
35
      function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
36
      if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
37
        if (consume !== false) this.pos += str.length;
38
        return true;
39
      }
40
    }
41
    else {
42
      var match = this.string.slice(this.pos).match(pattern);
43
      if (match && consume !== false) this.pos += match[0].length;
44
      return match;
45
    }
46
  }
47
};
48
 
 
 

JavaScript mode supports a two configuration options:

MIME types defined: text/javascript, application/json, text/typescript, application/typescript.