-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCharStreams.js
More file actions
66 lines (60 loc) · 2.02 KB
/
CharStreams.js
File metadata and controls
66 lines (60 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Copyright (c) 2012-2022 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.
*/
import CharStream from "./CharStream.js";
/**
* 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).
*/
export default {
// Creates an InputStream from a string.
fromString: function(str) {
return new CharStream(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.
*/
fromBlob: function(blob, encoding, onLoad, onError) {
let reader = new window.FileReader();
reader.onload = function(e) {
let is = new CharStream(e.target.result, true);
onLoad(is);
};
reader.onerror = onError;
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).
*/
fromBuffer: function(buffer, encoding) {
return new CharStream(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.
*/
fromPath: function(path, encoding, callback) {
throw new Error("fromPath is not available in browser environment!");
},
/**
* 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) {
throw new Error("fromPathSync is not available in browser environment!");
}
};