使用Rhino的Javascript解析器,如何获取评论? [英] Using Rhino's Javascript parser, how to get the comments?

查看:254
本文介绍了使用Rhino的Javascript解析器,如何获取评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些javascript文件,并使用Rhino的JavaScript解析器解析。



但我无法获得评论。



我如何获得评论?



这里是我的代码的一部分。



这个代码,comment变量有null。
,同时运行astRoot.toSource();,它只显示javascript代码。没有评论。它消失了!

[java代码]

  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();
块块=(块)bNode;
visitBody(block);
}

System.out.println(astRoot.toSource());
SortedSet< Comment> comment = astRoot.getComments();
if(comment == null)
System.out.println(comment is null);
}


解决方案

http://www.jarvana.com/jarvana/view/org/mozilla/rhino/1.7R3/rhino-1.7R3-javadoc.jar!/org/mozilla/javascript/CompilerEnvirons.html\">CompilerEnvirons 并使用 AstRoot.visitAll(NodeVisitor)

  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 {
类打印机实现NodeVisitor {
@Override public boolean visit ){
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 = 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();
}
}
}

Rhino 1.7R4


I have some javascript files and parse it using Rhino's javascript parser.

but I can't get the comments.

How can I get the comments?

here's a part of my code.

run this code, "comment" variable has null. also, while running "astRoot.toSource();", it shows only javascript code. no comment included. it disappeared!

[java code]

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");
}

解决方案

Configure your CompilerEnvirons and use AstRoot.visitAll(NodeVisitor):

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();
    }
  }
}

Java 6; Rhino 1.7R4

这篇关于使用Rhino的Javascript解析器,如何获取评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆