private AstRoot parse(CharSequence cs) {
   CompilerEnvirons compilerEnv = new CompilerEnvirons();
   compilerEnv.initFromContext(cx);
   ErrorReporter compilationErrorReporter = compilerEnv.getErrorReporter();
   Parser p = new Parser(compilerEnv, compilationErrorReporter);
   return p.parse(cs.toString(), "<eval>", 1);
 }
示例#2
0
  private AstRoot parse(
      String string, final String[] errors, final String[] warnings, boolean jsdoc) {
    TestErrorReporter testErrorReporter =
        new TestErrorReporter(errors, warnings) {
          @Override
          public EvaluatorException runtimeError(
              String message, String sourceName, int line, String lineSource, int lineOffset) {
            if (errors == null) {
              throw new UnsupportedOperationException();
            }
            return new EvaluatorException(message, sourceName, line, lineSource, lineOffset);
          }
        };
    environment.setErrorReporter(testErrorReporter);

    environment.setRecordingComments(true);
    environment.setRecordingLocalJsDocComments(jsdoc);

    Parser p = new Parser(environment, testErrorReporter);
    AstRoot script = null;
    try {
      script = p.parse(string, null, 0);
    } catch (EvaluatorException e) {
      if (errors == null) {
        // EvaluationExceptions should not occur when we aren't expecting
        // errors.
        throw e;
      }
    }

    assertTrue(testErrorReporter.hasEncounteredAllErrors());
    assertTrue(testErrorReporter.hasEncounteredAllWarnings());

    return script;
  }
示例#3
0
  public void testParseObjectLiteral3() {
    environment.setLanguageVersion(Context.VERSION_1_8);
    environment.setReservedKeywordAsIdentifier(true);
    parse("var {get} = {get:1};");

    environment.setReservedKeywordAsIdentifier(false);
    parse("var {get} = {get:1};");
    expectParseErrors("var {get} = {if:1};", new String[] {"invalid property id"});
  }
示例#4
0
  public void testParseObjectLiteral2() {
    // keywords, fail
    environment.setReservedKeywordAsIdentifier(false);
    expectParseErrors("({function:1});", new String[] {"invalid property id"});

    environment.setReservedKeywordAsIdentifier(true);

    // keywords ok
    parse("({function:1});");
  }
  // 得到的tokens包含源代码的由词法分析器分析出的所有单词,但不包含注释
  private static ArrayList<JavaScriptToken> parse(
      Reader in, ErrorReporter reporter) // 返回tokens(保存的是源文件中出现的javascript关键字和NAME,REGEXP,STRING等类型)
      throws IOException, EvaluatorException {

    CompilerEnvirons env = new CompilerEnvirons(); // 创建编译环境对象
    env.setLanguageVersion(Context.VERSION_1_7); // 设置语言版本
    Parser parser = new Parser(env, reporter); // 创建解释器对象
    parser.parse(in, null, 1); // 解释输入流
    String source = parser.getEncodedSource(); // 获得已编码的源码(词法分析阶段通常是把从源程序中识别出的各个单词的词文
    // 转换为某种内部表示
    int offset = 0;
    int length = source.length();
    ArrayList<JavaScriptToken> tokens = new ArrayList<JavaScriptToken>();
    StringBuffer sb = new StringBuffer();

    while (offset < length) {
      int tt = source.charAt(offset++); // 获取特定位置上的字符,并转化为ASCII编码
      switch (tt) {
        case Token.CONDCOMMENT: // 条件注释
        case Token.KEEPCOMMENT: // 注释
        case Token.NAME: //
        case Token.REGEXP: // 正则表达式类型
        case Token.STRING: // String类型,js程序中双引号或单引号括起来的字符串
          sb.setLength(0);
          offset = printSourceString(source, offset, sb);
          tokens.add(new JavaScriptToken(tt, sb.toString()));
          break;

        case Token.NUMBER: // Number类型
          sb.setLength(0);
          offset = printSourceNumber(source, offset, sb);
          tokens.add(new JavaScriptToken(tt, sb.toString()));
          break;

        default:
          String literal = literals.get(new Integer(tt));
          if (literal != null) { // 若不为空,说明哈希表literals中含有键new
            // Integer(tt)所对应的值
            tokens.add(new JavaScriptToken(tt, literal)); // 将此关键字保存到数组列表tokens中
          }
          break;
      }
    }

    /*
     * //begin Iterator<JavaScriptToken> iterator = tokens.iterator();
     * JavaScriptToken token; while(iterator.hasNext()) { token =
     * iterator.next();
     * System.out.println(token.getType()+"\t"+token.getValue()); } //end
     */
    return tokens;
  }
示例#6
0
  private AstRoot parseAsReader(String string) throws IOException {
    TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
    environment.setErrorReporter(testErrorReporter);

    environment.setRecordingComments(true);
    environment.setRecordingLocalJsDocComments(true);

    Parser p = new Parser(environment, testErrorReporter);
    AstRoot script = p.parse(new StringReader(string), null, 0);

    assertTrue(testErrorReporter.hasEncounteredAllErrors());
    assertTrue(testErrorReporter.hasEncounteredAllWarnings());

    return script;
  }
示例#7
0
  protected void parse() throws EvaluatorException {
    // Parse script source
    CompilerEnvirons ce = new CompilerEnvirons();
    ScriptParserErrorReporter errorReporter = new ScriptParserErrorReporter();

    ce.setGenerateDebugInfo(true);
    ce.initFromContext(ContextFactory.getGlobal().enterContext());
    ce.setErrorReporter(errorReporter);

    Parser p = new Parser(ce, errorReporter);
    ScriptOrFnNode ast = p.parse(this.scriptSource, "script", 0);

    recursiveParse(ast);

    this.scriptParsed = true;
  }
示例#8
0
 public CacheManager(CompilerEnvirons compilerEnv) {
   if (compilerEnv == null) {
     compilerEnv = new CompilerEnvirons();
     compilerEnv.setErrorReporter(new ToolErrorReporter(true));
   }
   this.compiler = new ClassCompiler(compilerEnv);
 }
示例#9
0
  public void testParseKeywordPropertyAccess() {
    environment.setReservedKeywordAsIdentifier(true);

    // keywords ok
    parse("({function:1}).function;");

    // reserved words ok.
    parse("({import:1}).import;");
  }
  @Override
  public void filter(Reader reader, Writer writer) {
    try {
      CompilerEnvirons config = new CompilerEnvirons();
      // 'float' is otherwise considered a reserved keyword (usage: element.style.float = 'left').
      config.setReservedKeywordAsIdentifier(true);
      // Force the parser to build the parent scope chain.
      config.setIdeMode(true);
      Parser parser = new Parser(config);
      AstRoot root = parser.parse(reader, null, 0);

      // Filter the AST.
      for (NodeVisitor filter : filters) {
        root.visit(filter);
      }

      // Back to source.
      writer.write(root.toSource());
    } catch (IOException e) {
      LOGGER.error("Failed to rewrite JavaScript code.", e);
    }
  }
示例#11
0
  public void testParseObjectLiteral1() {
    environment.setReservedKeywordAsIdentifier(true);

    parse("({a:1});");
    parse("({'a':1});");
    parse("({0:1});");

    // property getter and setter definitions accept string and number
    parse("({get a() {return 1}});");
    parse("({get 'a'() {return 1}});");
    parse("({get 0() {return 1}});");

    parse("({set a(a) {return 1}});");
    parse("({set 'a'(a) {return 1}});");
    parse("({set 0(a) {return 1}});");

    // keywords ok
    parse("({function:1});");
    // reserved words ok
    parse("({float:1});");
  }