From 8d2975fc9994e24621ff0cfd5fbd6c5c4fc3564f Mon Sep 17 00:00:00 2001 From: Andrew Brampton Date: Wed, 29 Nov 2017 21:56:05 -0800 Subject: [PATCH] [Javascript] Add a case insensitive input stream. --- .../src/antlr4/CaseInsensitiveInputStream.js | 54 +++++++++++++++++++ runtime/JavaScript/src/antlr4/CharStreams.js | 9 ++++ 2 files changed, 63 insertions(+) create mode 100644 runtime/JavaScript/src/antlr4/CaseInsensitiveInputStream.js diff --git a/runtime/JavaScript/src/antlr4/CaseInsensitiveInputStream.js b/runtime/JavaScript/src/antlr4/CaseInsensitiveInputStream.js new file mode 100644 index 000000000..5ec762de9 --- /dev/null +++ b/runtime/JavaScript/src/antlr4/CaseInsensitiveInputStream.js @@ -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; diff --git a/runtime/JavaScript/src/antlr4/CharStreams.js b/runtime/JavaScript/src/antlr4/CharStreams.js index 71c507616..75f777ba8 100644 --- a/runtime/JavaScript/src/antlr4/CharStreams.js +++ b/runtime/JavaScript/src/antlr4/CharStreams.js @@ -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); } };