[Javascript] Add a case insensitive input stream.

This commit is contained in:
Andrew Brampton 2017-11-29 21:56:05 -08:00 committed by parrt
parent d3a7fe37ff
commit 8d2975fc99
2 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,54 @@
//
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
//
function CaseInsensitiveInputStream(stream, upper) {
this._stream = stream;
this._case = upper ? String.toUpperCase : String.toLowerCase;
return this;
}
CaseInsensitiveInputStream.prototype.LA = function (offset) {
c = this._stream.LA(i);
if (c <= 0) {
return c;
}
return this._case.call(String.fromCodePoint(c))
};
CaseInsensitiveInputStream.prototype.reset = function() {
return this._stream.reset();
};
CaseInsensitiveInputStream.prototype.consume = function() {
return this._stream.consume();
};
CaseInsensitiveInputStream.prototype.LT = function(offset) {
return this._stream.LT(offset);
};
CaseInsensitiveInputStream.prototype.mark = function() {
return this._stream.mark();
};
CaseInsensitiveInputStream.prototype.release = function(marker) {
return this._stream.release(marker);
};
CaseInsensitiveInputStream.prototype.seek = function(_index) {
return this._stream.getText(start, stop);
};
CaseInsensitiveInputStream.prototype.getText = function(start, stop) {
return this._stream.getText(start, stop);
};
CaseInsensitiveInputStream.prototype.toString = function() {
return this._stream.toString();
};
exports.CaseInsensitiveInputStream = CaseInsensitiveInputStream;

View File

@ -5,6 +5,7 @@
*/
//
var CaseInsensitiveInputStream = require('./CaseInsensitiveInputStream').CaseInsensitiveInputStream;
var InputStream = require('./InputStream').InputStream;
var isNodeJs = typeof window === 'undefined' && typeof importScripts === 'undefined';
@ -65,6 +66,14 @@ var CharStreams = {
fromPathSync: function(path, encoding) {
var data = fs.readFileSync(path, encoding);
return new InputStream(data, true);
},
toUpper: function(stream) {
return new CaseInsensitiveInputStream(stream, true);
},
toLower: function(stream) {
return new CaseInsensitiveInputStream(stream, false);
}
};