Skip to content

Commit fbd4452

Browse files
authored
Js support encoding in file stream + packaging issues (#4112)
* export Recognizer Signed-off-by: Eric Vergnaud <[email protected]> * Fix packaging issue * Bump beta version * support encoding in FileStream sync * fix prototype * fix prototype * more exports * bump beta version number * fix packaging issues * fix failing tests --------- Signed-off-by: Eric Vergnaud <[email protected]>
1 parent c2bfa34 commit fbd4452

File tree

14 files changed

+174
-70
lines changed

14 files changed

+174
-70
lines changed

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.js.stg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import antlr4 from 'file://<runtimePath>/src/antlr4/index.js'
1+
import antlr4 from 'file://<runtimePath>/src/antlr4/index.node.js'
22
import <lexerName> from './<lexerName>.js';
33
<if(parserName)>
44
import <parserName> from './<parserName>.js';
@@ -23,7 +23,7 @@ class TreeShapeListener extends antlr4.tree.ParseTreeListener {
2323
<endif>
2424

2525
function main(argv) {
26-
var input = new antlr4.FileStream(argv[2], true);
26+
var input = new antlr4.FileStream(argv[2], "utf-8", true);
2727
var lexer = new <lexerName>(input);
2828
var stream = new antlr4.CommonTokenStream(lexer);
2929
<if(parserName)>

runtime-testsuite/resources/org/antlr/v4/test/runtime/helpers/Test.ts.stg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class TreeShapeListener extends ParseTreeListener {
3333
<endif>
3434

3535
function main(argv: string[]): void {
36-
const input = new FileStream(argv[2], true);
36+
const input = new FileStream(argv[2], "utf-8", true);
3737
const lexer = new <lexerName>(input);
3838
const stream = new CommonTokenStream(lexer);
3939
<if(parserName)>

runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/NodeRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public String getLanguage() {
3636

3737
private final static String normalizedRuntimePath = getRuntimePath("JavaScript").replace('\\', '/');
3838
private final static String newImportAntlrString =
39-
"import antlr4 from 'file://" + normalizedRuntimePath + "/src/antlr4/index.js'";
39+
"import antlr4 from 'file://" + normalizedRuntimePath + "/src/antlr4/index.node.js'";
4040

4141
@Override
4242
protected CompiledState compile(RunOptions runOptions, GeneratedState generatedState) {

runtime/JavaScript/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "antlr4",
3-
"version": "4.12.0-beta.5",
3+
"version": "4.12.0-beta.10",
44
"type": "module",
55
"description": "JavaScript runtime for ANTLR4",
6-
"main": "dist/antlr4.js",
6+
"browser": "dist/antlr4.web.js",
7+
"main": "dist/antlr4.node.js",
78
"types": "src/antlr4/index.d.ts",
89
"repository": "antlr/antlr4.git",
910
"keywords": [
@@ -43,8 +44,5 @@
4344
},
4445
"engines": {
4546
"node": ">=16"
46-
},
47-
"browser": {
48-
"fs": false
4947
}
5048
}

runtime/JavaScript/src/antlr4/CharStreams.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* can be found in the LICENSE.txt file in the project root.
44
*/
55

6-
import InputStream from './InputStream.js';
7-
import fs from "fs";
6+
import CharStream from "./CharStream.js";
7+
import FileStream from "./FileStream.js";
88

99
/**
1010
* Utility functions to create InputStreams from various sources.
@@ -16,7 +16,7 @@ import fs from "fs";
1616
export default {
1717
// Creates an InputStream from a string.
1818
fromString: function(str) {
19-
return new InputStream(str, true);
19+
return new CharStream(str, true);
2020
},
2121

2222
/**
@@ -30,7 +30,7 @@ export default {
3030
fromBlob: function(blob, encoding, onLoad, onError) {
3131
const reader = new window.FileReader();
3232
reader.onload = function(e) {
33-
const is = new InputStream(e.target.result, true);
33+
const is = new CharStream(e.target.result, true);
3434
onLoad(is);
3535
};
3636
reader.onerror = onError;
@@ -43,7 +43,7 @@ export default {
4343
* encoding is null).
4444
*/
4545
fromBuffer: function(buffer, encoding) {
46-
return new InputStream(buffer.toString(encoding), true);
46+
return new CharStream(buffer.toString(encoding), true);
4747
},
4848

4949
/** Asynchronously creates an InputStream from a file on disk given
@@ -53,13 +53,7 @@ export default {
5353
* Invokes callback(error, result) on completion.
5454
*/
5555
fromPath: function(path, encoding, callback) {
56-
fs.readFile(path, encoding, function(err, data) {
57-
let is = null;
58-
if (data !== null) {
59-
is = new InputStream(data, true);
60-
}
61-
callback(err, is);
62-
});
56+
FileStream.fromPath(path, encoding, callback);
6357
},
6458

6559
/**
@@ -68,7 +62,6 @@ export default {
6862
* 'utf8' if encoding is null).
6963
*/
7064
fromPathSync: function(path, encoding) {
71-
const data = fs.readFileSync(path, encoding);
72-
return new InputStream(data, true);
65+
return new FileStream(path, encoding);
7366
}
7467
};

runtime/JavaScript/src/antlr4/FileStream.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export declare class FileStream extends CharStream {
44

55
fileName: string;
66

7-
constructor(fileName: string);
8-
constructor(fileName: string, decodeToUnicodeCodePoints: boolean);
7+
constructor(fileName: string, encoding?: string, decodeToUnicodeCodePoints?: boolean);
98

109
}

runtime/JavaScript/src/antlr4/FileStream.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,36 @@
44
*/
55

66
import InputStream from './InputStream.js';
7+
import CharStream from './CharStream.js';
78
const isNode =
89
typeof process !== "undefined" &&
910
process.versions != null &&
1011
process.versions.node != null;
11-
// use eval to fool webpack and mocha
12-
const fs = isNode ? await eval("import('fs')") : null;
12+
import fs from 'fs';
1313

1414
/**
1515
* This is an InputStream that is loaded from a file all at once
1616
* when you construct the object.
1717
*/
1818
export default class FileStream extends InputStream {
19-
constructor(fileName, decodeToUnicodeCodePoints) {
19+
20+
static fromPath(path, encoding, callback) {
21+
if(!isNode)
22+
throw new Error("FileStream is only available when running in Node!");
23+
fs.readFile(path, encoding, function(err, data) {
24+
let is = null;
25+
if (data !== null) {
26+
is = new CharStream(data, true);
27+
}
28+
callback(err, is);
29+
});
30+
31+
}
32+
33+
constructor(fileName, encoding, decodeToUnicodeCodePoints) {
2034
if(!isNode)
2135
throw new Error("FileStream is only available when running in Node!");
22-
const data = fs.readFileSync(fileName, "utf8");
36+
const data = fs.readFileSync(fileName, encoding || "utf-8" );
2337
super(data, decodeToUnicodeCodePoints);
2438
this.fileName = fileName;
2539
}

runtime/JavaScript/src/antlr4/Lexer.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export declare class Lexer extends Recognizer<number> {
1818
_type: number;
1919

2020
constructor(input: CharStream);
21-
nextToken() : Token;
22-
emit() : Token;
21+
nextToken(): Token;
22+
emit(): Token;
23+
reset(): void;
2324
}

runtime/JavaScript/src/antlr4/atn/ParserATNSimulator.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export declare class ParserATNSimulator extends ATNSimulator {
1414
decisionToDFA: DFA[];
1515
atn: ATN;
1616
debug?: boolean;
17+
trace_atn_sim?: boolean;
1718

1819
constructor(recog: Recognizer<Token>, atn: ATN, decisionToDFA: DFA[], sharedContextCache: PredictionContextCache);
1920
adaptivePredict(input: TokenStream, decision: number, outerContext: ParserRuleContext) : number;

runtime/JavaScript/src/antlr4/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from "./CharStreams";
55
export * from "./TokenStream";
66
export * from "./BufferedTokenStream";
77
export * from "./CommonTokenStream";
8+
export * from "./Recognizer";
89
export * from "./Lexer";
910
export * from "./Parser";
1011
export * from './Token';

0 commit comments

Comments
 (0)