使用Rhino';s的Javascript解析器,如何获取注释?
我有一些javascript文件,并使用Rhino的javascript解析器对其进行解析 但我无法得到评论 我怎样才能得到评论 这是我代码的一部分 运行此代码,“comment”变量为null。 此外,在运行“astRoot.toSource();”时,它只显示javascript代码。没有评论包括在内。它消失了 [java代码]使用Rhino';s的Javascript解析器,如何获取注释?,java,javascript,parsing,comments,rhino,Java,Javascript,Parsing,Comments,Rhino,我有一些javascript文件,并使用Rhino的javascript解析器对其进行解析 但我无法得到评论 我怎样才能得到评论 这是我代码的一部分 运行此代码,“comment”变量为null。 此外,在运行“astRoot.toSource();”时,它只显示javascript代码。没有评论包括在内。它消失了 [java代码] public void parser() { AstRoot astRoot = new Parser().parse(this.jsString, thi
public void parser() {
AstRoot astRoot = new Parser().parse(this.jsString, this.uri, 1);
List<AstNode> statList = astRoot.getStatements();
for(Iterator<AstNode> iter = statList.iterator(); iter.hasNext();) {
FunctionNode fNode = (FunctionNode)iter.next();
System.out.println("*** function Name : " + fNode.getName() + ", paramCount : " + fNode.getParamCount() + ", depth : " + fNode.depth());
AstNode bNode = fNode.getBody();
Block block = (Block)bNode;
visitBody(block);
}
System.out.println(astRoot.toSource());
SortedSet<Comment> comment = astRoot.getComments();
if(comment == null)
System.out.println("comment is null");
}
public void解析器(){
AstRoot AstRoot=newparser().parse(this.jsString,this.uri,1);
List statList=astRoot.getStatements();
for(迭代器iter=statList.Iterator();iter.hasNext();){
FunctionNode fNode=(FunctionNode)iter.next();
System.out.println(“***函数名:”+fNode.getName()+”,paramCount:“+fNode.getParamCount()+”,深度:“+fNode.depth());
AstNode bNode=fNode.getBody();
Block Block=(Block)bNode;
访问体(块);
}
System.out.println(astRoot.toSource());
SortedSet comment=astRoot.getComments();
if(注释==null)
System.out.println(“注释为空”);
}
配置并使用:
爪哇6;Rhino 1.7R4您使用的Rhino版本是什么?使用1.7R4,但现在我解决了问题!谢谢谢谢,麦克道尔。这是一个病毒缺失的问题!现在,工作完美!
import java.io.*;
import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.Parser;
import org.mozilla.javascript.ast.*;
public class PrintNodes {
public static void main(String[] args) throws IOException {
class Printer implements NodeVisitor {
@Override public boolean visit(AstNode node) {
String indent = "%1$Xs".replace("X", String.valueOf(node.depth() + 1));
System.out.format(indent, "").println(node.getClass());
return true;
}
}
String file = "foo.js";
Reader reader = new FileReader(file);
try {
CompilerEnvirons env = new CompilerEnvirons();
env.setRecordingLocalJsDocComments(true);
env.setAllowSharpComments(true);
env.setRecordingComments(true);
AstRoot node = new Parser(env).parse(reader, file, 1);
node.visitAll(new Printer());
} finally {
reader.close();
}
}
}