parent
4752ecf521
commit
2e139f8d08
|
@ -1,30 +1,32 @@
|
|||
//
|
||||
/* 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.
|
||||
*/
|
||||
//
|
||||
|
||||
const {InputStream} = require('./InputStream');
|
||||
const fs = require("fs");
|
||||
|
||||
// Utility functions to create InputStreams from various sources.
|
||||
//
|
||||
// All returned InputStreams support the full range of Unicode
|
||||
// up to U+10FFFF (the default behavior of InputStream only supports
|
||||
// code points up to U+FFFF).
|
||||
/**
|
||||
* Utility functions to create InputStreams from various sources.
|
||||
*
|
||||
* All returned InputStreams support the full range of Unicode
|
||||
* up to U+10FFFF (the default behavior of InputStream only supports
|
||||
* code points up to U+FFFF).
|
||||
*/
|
||||
const CharStreams = {
|
||||
// Creates an InputStream from a string.
|
||||
fromString: function(str) {
|
||||
return new InputStream(str, true);
|
||||
},
|
||||
|
||||
// Asynchronously creates an InputStream from a blob given the
|
||||
// encoding of the bytes in that blob (defaults to 'utf8' if
|
||||
// encoding is null).
|
||||
//
|
||||
// Invokes onLoad(result) on success, onError(error) on
|
||||
// failure.
|
||||
/**
|
||||
* Asynchronously creates an InputStream from a blob given the
|
||||
* encoding of the bytes in that blob (defaults to 'utf8' if
|
||||
* encoding is null).
|
||||
*
|
||||
* Invokes onLoad(result) on success, onError(error) on
|
||||
* failure.
|
||||
*/
|
||||
fromBlob: function(blob, encoding, onLoad, onError) {
|
||||
const reader = new window.FileReader();
|
||||
reader.onload = function(e) {
|
||||
|
@ -35,18 +37,21 @@ const CharStreams = {
|
|||
reader.readAsText(blob, encoding);
|
||||
},
|
||||
|
||||
// Creates an InputStream from a Buffer given the
|
||||
// encoding of the bytes in that buffer (defaults to 'utf8' if
|
||||
// encoding is null).
|
||||
/**
|
||||
* Creates an InputStream from a Buffer given the
|
||||
* encoding of the bytes in that buffer (defaults to 'utf8' if
|
||||
* encoding is null).
|
||||
*/
|
||||
fromBuffer: function(buffer, encoding) {
|
||||
return new InputStream(buffer.toString(encoding), true);
|
||||
},
|
||||
|
||||
// Asynchronously creates an InputStream from a file on disk given
|
||||
// the encoding of the bytes in that file (defaults to 'utf8' if
|
||||
// encoding is null).
|
||||
//
|
||||
// Invokes callback(error, result) on completion.
|
||||
/** Asynchronously creates an InputStream from a file on disk given
|
||||
* the encoding of the bytes in that file (defaults to 'utf8' if
|
||||
* encoding is null).
|
||||
*
|
||||
* Invokes callback(error, result) on completion.
|
||||
*/
|
||||
fromPath: function(path, encoding, callback) {
|
||||
fs.readFile(path, encoding, function(err, data) {
|
||||
let is = null;
|
||||
|
@ -57,13 +62,15 @@ const CharStreams = {
|
|||
});
|
||||
},
|
||||
|
||||
// Synchronously creates an InputStream given a path to a file
|
||||
// on disk and the encoding of the bytes in that file (defaults to
|
||||
// 'utf8' if encoding is null).
|
||||
/**
|
||||
* Synchronously creates an InputStream given a path to a file
|
||||
* on disk and the encoding of the bytes in that file (defaults to
|
||||
* 'utf8' if encoding is null).
|
||||
*/
|
||||
fromPathSync: function(path, encoding) {
|
||||
const data = fs.readFileSync(path, encoding);
|
||||
return new InputStream(data, true);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { CharStreams }
|
||||
module.exports = CharStreams
|
||||
|
|
|
@ -9,7 +9,7 @@ exports.fromcodepoint = require('./polyfills/fromcodepoint');
|
|||
exports.tree = require('./tree/index');
|
||||
exports.error = require('./error/index');
|
||||
exports.Token = require('./Token').Token;
|
||||
exports.CharStreams = require('./CharStreams').CharStreams;
|
||||
exports.CharStreams = require('./CharStreams');
|
||||
exports.CommonToken = require('./Token').CommonToken;
|
||||
exports.InputStream = require('./InputStream').InputStream;
|
||||
exports.FileStream = require('./FileStream').FileStream;
|
||||
|
|
Loading…
Reference in New Issue