ANTLRInputStream and ANTLRFileStream are deprecated, what are the alternatives?
Asked Answered
S

1

10

If I use

ANTLRFileStream antlrFileStream = new ANTLRFileStream("myfile.testlang");

or

ANTLRInputStream input = new ANTLRInputStream( new FileInputStream("myfile.testlang") );

Compiler shows deprecation error for both the classes what is alternative?

Skydive answered 26/5, 2018 at 9:33 Comment(1)
As for @Mike Lischke's comment about it being unfortunate that Unicode support was not simply folded into the existing ANTLRInputStream rather than creating the new CharStreams class (and breaking existing code, albeit in a (maybe? usually?) "easy-to-fix manner": Could it be that some code writte for ANTLRInputStream depending on it being a non-Unicode stream (e.g. 8 or 16 bit characters only)? I'm new to ANTLR (and picking up rusty Java again after learning it 10 years ago in CS class.), so I don't know how much the incomplete Unicode support for ANTLRInputStream was. The "don't change theGadmon
S
15

You can use the CharStreams class instead of the deprecated classes as below.

CharStream codePointCharStream = CharStreams.fromFileName("myfile.testlang");
TESTLANGLexer lexer = new TESTLANGLexer(codePointCharStream);
TESTLANGParser parser = new TESTLANGParser(new CommonTokenStream(lexer));
parser.addParseListener(new TESTLANGEventListener());

// Start parsing
parser.testlangFile(); 
Skydive answered 26/5, 2018 at 9:33 Comment(1)
It's a bit unfortunate that these classes (which are totally fine) have been deprecated in the Java target (and only there, other targets don't do that). The reason for creating the new function CharStreams.fromFileName() is because of the now complete Unicode support. That could have been folded into the existing classes, but it was decided to not touch them and create a new API instead.Folger

© 2022 - 2024 — McMap. All rights reserved.