ANTLR4 如何设置 sourceName?
ANTLR4 How can I set sourceName?
我使用 ANTLR4 创建了一个解析器,它可以很好地处理文件。
现在,当我尝试从 URLs 直接解析 Token 的 getSourceName() 方法 returns“未知”时。这是错误消息中的一个问题,我希望尽可能清楚。
所以我想使用从中检索输入的 URL 作为源名称,但我看不到在哪里可以设置该值。这是我的代码的样子:
private parseDocument(URL u) {
URLConnection conn = u.openConnection();
CharStream charStream = CharStreams.fromStream(conn.getInputStream());
MyLexer lexer = new MyLexer(charStream);
CommonTokenStream tokenstream = new CommonTokenStream(lexer);
MyParser parser = new MyParser(tokenstream);
MyParser.ParseContext pc = parser.parse();
...
}
是否有类似 setSourceName(String) 方法的东西,无论是在 CharStream 上还是在词法分析器上?或者提供此信息的预期方式是什么?
如果你看 source code of CharStreams:
public static CharStream fromStream(InputStream is) throws IOException {
return fromStream(is, StandardCharsets.UTF_8);
}
public static CharStream fromStream(InputStream is, Charset charset) throws IOException {
return fromStream(is, charset, -1);
}
public static CharStream fromStream(InputStream is, Charset charset, long inputSize) throws IOException {
try (ReadableByteChannel channel = Channels.newChannel(is)) {
return fromChannel(
channel,
charset,
DEFAULT_BUFFER_SIZE,
CodingErrorAction.REPLACE,
IntStream.UNKNOWN_SOURCE_NAME,
inputSize);
}
}
...
public static CodePointCharStream fromChannel(
ReadableByteChannel channel,
Charset charset,
int bufferSize,
CodingErrorAction decodingErrorAction,
String sourceName,
long inputSize) throws IOException
{
...
}
您会看到所有 fromStream(...)
调用最终都变成了 fromChannel(...)
调用,即 public
所以可以像这样使用(虽然未经测试):
URL u = ...
URLConnection conn = u.openConnection();
ReadableByteChannel channel = Channels.newChannel(conn.getInputStream());
CharStream charStream = CharStreams.fromChannel(
channel,
StandardCharsets.UTF_8,
4096,
CodingErrorAction.REPLACE,
u.getPath(),
-1);
我使用 ANTLR4 创建了一个解析器,它可以很好地处理文件。 现在,当我尝试从 URLs 直接解析 Token 的 getSourceName() 方法 returns“未知”时。这是错误消息中的一个问题,我希望尽可能清楚。
所以我想使用从中检索输入的 URL 作为源名称,但我看不到在哪里可以设置该值。这是我的代码的样子:
private parseDocument(URL u) {
URLConnection conn = u.openConnection();
CharStream charStream = CharStreams.fromStream(conn.getInputStream());
MyLexer lexer = new MyLexer(charStream);
CommonTokenStream tokenstream = new CommonTokenStream(lexer);
MyParser parser = new MyParser(tokenstream);
MyParser.ParseContext pc = parser.parse();
...
}
是否有类似 setSourceName(String) 方法的东西,无论是在 CharStream 上还是在词法分析器上?或者提供此信息的预期方式是什么?
如果你看 source code of CharStreams:
public static CharStream fromStream(InputStream is) throws IOException {
return fromStream(is, StandardCharsets.UTF_8);
}
public static CharStream fromStream(InputStream is, Charset charset) throws IOException {
return fromStream(is, charset, -1);
}
public static CharStream fromStream(InputStream is, Charset charset, long inputSize) throws IOException {
try (ReadableByteChannel channel = Channels.newChannel(is)) {
return fromChannel(
channel,
charset,
DEFAULT_BUFFER_SIZE,
CodingErrorAction.REPLACE,
IntStream.UNKNOWN_SOURCE_NAME,
inputSize);
}
}
...
public static CodePointCharStream fromChannel(
ReadableByteChannel channel,
Charset charset,
int bufferSize,
CodingErrorAction decodingErrorAction,
String sourceName,
long inputSize) throws IOException
{
...
}
您会看到所有 fromStream(...)
调用最终都变成了 fromChannel(...)
调用,即 public
所以可以像这样使用(虽然未经测试):
URL u = ...
URLConnection conn = u.openConnection();
ReadableByteChannel channel = Channels.newChannel(conn.getInputStream());
CharStream charStream = CharStreams.fromChannel(
channel,
StandardCharsets.UTF_8,
4096,
CodingErrorAction.REPLACE,
u.getPath(),
-1);