JavaScript mode
function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}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
},JavaScript mode supports several configuration options:
jsonwhich will set the mode to expect JSON data rather than a JavaScript program.jsonldwhich will set the mode to expect JSON-LD linked data rather than a JavaScript program (demo).typescriptwhich will activate additional syntax highlighting and some other things for TypeScript code (demo).statementIndentwhich (given a number) will determine the amount of indentation to use for statements continued on a new line.
MIME types defined: text/javascript, application/json, application/ld+json, text/typescript, application/typescript.
