org.mozilla.javascript.ast.AstRoot Java Examples

The following examples show how to use org.mozilla.javascript.ast.AstRoot. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BugGetterSetterTest.java    From rhino-android with Apache License 2.0 7 votes vote down vote up
@Test
public void testNodeReplacementInWhileLoopWithBrackets() throws IOException {
    String script = "var o = {\n" +
            "  _x: 123, \n" +
            "  get x() {\n" +
            "    return this._x;\n" +
            "  }\n" +
            ", \n" +
            "  set x(value) {\n" +
            "    this._x = value;\n" +
            "  }\n" +
            "};\n";

    Parser parser = new Parser(environment);
    AstRoot astRoot = parser.parse(new StringReader(script), null, 1);
    assertEquals(script, astRoot.toSource());
}
 
Example #2
Source File: Bug708801Test.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
/**
 * Compiles {@code source} and returns the transformed and optimized
 * {@link ScriptNode}
 */
protected ScriptNode compile(CharSequence source) {
    final String mainMethodClassName = "Main";
    final String scriptClassName = "Main";

    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    ErrorReporter compilationErrorReporter = compilerEnv
            .getErrorReporter();
    Parser p = new Parser(compilerEnv, compilationErrorReporter);
    AstRoot ast = p.parse(source.toString(), "<eval>", 1);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    Codegen codegen = new Codegen();
    codegen.setMainMethodClass(mainMethodClassName);
    codegen.compileToClassFile(compilerEnv, scriptClassName, tree,
            tree.getEncodedSource(), false);

    return tree;
}
 
Example #3
Source File: CubeQueryUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 
 * @param expr
 * @param objectName
 * @return
 */
private static String getReferencedScriptObject( String expr,
		String objectName )
{
	if ( expr == null )
		return null;
	try
	{
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		IRFactory ir = new IRFactory( ce );
		ScriptNode script = ir.transformTree( tree );
		return getScriptObjectName( script, objectName );
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #4
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 
 * @param expr
 * @param objectName
 * @return
 */
public static String getReferencedScriptObject( String expr,
		String objectName )
{
	if ( expr == null )
		return null;
	try
	{
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		Node root = new IRFactory( ce).transformTree(tree);

		return getScriptObjectName( root, objectName );
	}
	catch( Exception ex )
	{
		return null;
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #5
Source File: Bug782363Test.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
/**
 * Compiles {@code source} and returns the transformed and optimized
 * {@link ScriptNode}
 */
protected ScriptNode compile(CharSequence source) {
    final String mainMethodClassName = "Main";
    final String scriptClassName = "Main";

    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    compilerEnv.initFromContext(cx);
    Parser p = new Parser(compilerEnv);
    AstRoot ast = p.parse(source.toString(), "<eval>", 1);
    IRFactory irf = new IRFactory(compilerEnv);
    ScriptNode tree = irf.transformTree(ast);

    Codegen codegen = new Codegen();
    codegen.setMainMethodClass(mainMethodClassName);
    codegen.compileToClassFile(compilerEnv, scriptClassName, tree, tree.getEncodedSource(),
            false);

    return tree;
}
 
Example #6
Source File: ExplanationsTest.java    From SJS with Apache License 2.0 6 votes vote down vote up
private static String explainErrors(JSEnvironment env, String sourceCode) {
    AstRoot root = new Parser().parse(sourceCode, "", 1);
    SatSolver sat = new Sat4J();
    SJSTypeTheory theory = new SJSTypeTheory(env, null, root);
    List<Integer> hard = new ArrayList<>();
    List<Integer> soft = new ArrayList<>();
    List<ITypeConstraint> constraints = theory.getConstraints();
    for (int i = 0; i < constraints.size(); ++i) {
        (theory.hackyGenerator().hasExplanation(constraints.get(i)) ? soft : hard).add(i);
    }
    Pair<TypeAssignment, Collection<Integer>> result =
            TheorySolver.solve(
                theory, new SatFixingSetFinder<>(sat),
                hard, soft);
    ConstraintGenerator g = theory.hackyGenerator();
    StringBuilder buf = new StringBuilder();
    for (int broken : result.getRight()) {
        ITypeConstraint c = theory.hackyConstraintAccess().get(broken);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        g.explainFailure(c, result.getLeft()).prettyprint(new PrintStream(stream));
        buf.append(stream.toString());
    }
    return buf.toString();
}
 
Example #7
Source File: ClassDefScanner.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Scan the given file for class definitions and accumulate dependencies.
 */
private void scan(final File source) throws IOException {
  log.debug("Scanning: " + source);

  ErrorReporter errorReporter = new LogErrorReporter(log);

  CompilerEnvirons env = new CompilerEnvirons();
  env.setErrorReporter(errorReporter);

  Parser parser = new Parser(env, errorReporter);
  Reader reader = new BufferedReader(new FileReader(source));
  try {
    AstRoot root = parser.parse(reader, source.getAbsolutePath(), 0);
    DependencyAccumulator visitor = new DependencyAccumulator(source);
    root.visit(visitor);

    // complain if no def was found in this source
    if (visitor.current == null) {
      log.warn("No class definition was found while processing: " + source);
    }
  }
  finally {
    reader.close();
  }
}
 
Example #8
Source File: NodeTransformer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void transformCompilationUnit(ScriptNode tree)
{
    loops = new ObjArray();
    loopEnds = new ObjArray();

    // to save against upchecks if no finally blocks are used.
    hasFinally = false;

    // Flatten all only if we are not using scope objects for block scope
    boolean createScopeObjects = tree.getType() != Token.FUNCTION ||
                              ((FunctionNode)tree).requiresActivation();
    tree.flattenSymbolTable(!createScopeObjects);

    //uncomment to print tree before transformation
    if (Token.printTrees) System.out.println(tree.toStringTree(tree));
    boolean inStrictMode = tree instanceof AstRoot &&
                           ((AstRoot)tree).isInStrictMode();
    transformCompilationUnit_r(tree, tree, tree, createScopeObjects,
                               inStrictMode);
}
 
Example #9
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Transforms the tree into a lower-level IR suitable for codegen.
 * Optionally generates the encoded source.
 */
public ScriptNode transformTree(AstRoot root) {
    currentScriptOrFn = root;
    this.inUseStrictDirective = root.isInStrictMode();
    int sourceStartOffset = decompiler.getCurrentOffset();

    if (Token.printTrees) {
        System.out.println("IRFactory.transformTree");
        System.out.println(root.debugPrint());
    }
    ScriptNode script = (ScriptNode)transform(root);

    int sourceEndOffset = decompiler.getCurrentOffset();
    script.setEncodedSourceBounds(sourceStartOffset,
                                  sourceEndOffset);

    if (compilerEnv.isGeneratingSource()) {
        script.setEncodedSource(decompiler.getEncodedSource());
    }

    decompiler = null;
    return script;
}
 
Example #10
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Builds a parse tree from the given sourcereader.
 * @see #parse(String,String,int)
 * @throws IOException if the {@link Reader} encounters an error
 */
public AstRoot parse(Reader sourceReader, String sourceURI, int lineno)
    throws IOException
{
    if (parseFinished) throw new IllegalStateException("parser reused");
    if (compilerEnv.isIdeMode()) {
        return parse(readFully(sourceReader), sourceURI, lineno);
    }
    try {
        this.sourceURI = sourceURI;
        ts = new TokenStream(this, sourceReader, null, lineno);
        return parse();
    } finally {
        parseFinished = true;
    }
}
 
Example #11
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Builds a parse tree from the given source string.
 *
 * @return an {@link AstRoot} object representing the parsed program.  If
 * the parse fails, {@code null} will be returned.  (The parse failure will
 * result in a call to the {@link ErrorReporter} from
 * {@link CompilerEnvirons}.)
 */
public AstRoot parse(String sourceString, String sourceURI, int lineno)
{
    if (parseFinished) throw new IllegalStateException("parser reused");
    this.sourceURI = sourceURI;
    if (compilerEnv.isIdeMode()) {
        this.sourceChars = sourceString.toCharArray();
    }
    this.ts = new TokenStream(this, null, sourceString, lineno);
    try {
        return parse();
    } catch (IOException iox) {
        // Should never happen
        throw new IllegalStateException();
    } finally {
        parseFinished = true;
    }
}
 
Example #12
Source File: CodeGenerator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public InterpreterData compile(CompilerEnvirons compilerEnv,
                               ScriptNode tree,
                               String encodedSource,
                               boolean returnFunction)
{
    this.compilerEnv = compilerEnv;

    if (Token.printTrees) {
        System.out.println("before transform:");
        System.out.println(tree.toStringTree(tree));
    }

    new NodeTransformer().transform(tree);

    if (Token.printTrees) {
        System.out.println("after transform:");
        System.out.println(tree.toStringTree(tree));
    }

    if (returnFunction) {
        scriptOrFn = tree.getFunctionNode(0);
    } else {
        scriptOrFn = tree;
    }
    itsData = new InterpreterData(compilerEnv.getLanguageVersion(),
                                  scriptOrFn.getSourceName(),
                                  encodedSource,
                                  ((AstRoot)tree).isInStrictMode());
    itsData.topLevel = true;

    if (returnFunction) {
        generateFunctionICode();
    } else {
        generateICodeFromTree(scriptOrFn);
    }
    return itsData;
}
 
Example #13
Source File: Bug689314Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #14
Source File: FileRepository.java    From testability-explorer with Apache License 2.0 5 votes vote down vote up
public List<AstRoot> getAsts() throws IOException {
  List<AstRoot> asts = new LinkedList<AstRoot>();
  for (Map.Entry<String, Reader> file : files.entrySet()) {
    Parser parser = new Parser();
    asts.add(parser.parse(file.getValue(), file.getKey(), 1));
  }
  return asts;
}
 
Example #15
Source File: Bug688021Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #16
Source File: Bug491621Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Asserts that the value returned by {@link AstRoot#toSource()} after
 * the given input source was parsed equals the specified expected output source.
 *
 * @param source the JavaScript source to be parsed
 * @param expectedOutput the JavaScript source that is expected to be
 *                       returned by {@link AstRoot#toSource()}
 */
private void assertSource(String source, String expectedOutput)
{
    CompilerEnvirons env = new CompilerEnvirons();
    env.setLanguageVersion(Context.VERSION_1_7);
    Parser parser = new Parser(env);
    AstRoot root = parser.parse(source, null, 0);
    Assert.assertEquals(expectedOutput, root.toSource());
}
 
Example #17
Source File: JavascriptParsingTest.java    From testability-explorer with Apache License 2.0 5 votes vote down vote up
public void testParseComplexStuff() throws Exception {
  Reader source = new InputStreamReader(
      this.getClass().getResourceAsStream("browser_debug.js"));
  Parser parser = new Parser();
  AstRoot ast = parser.parse(source, "browser_debug.js", 1);
  String debugString = ast.debugPrint();
  assertTrue(debugString.contains("getRequiresAndProvides"));
  assertTrue(debugString.contains("ARRAYLIT"));
}
 
Example #18
Source File: Bug688018Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #19
Source File: Bug687669Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #20
Source File: Bug689308Test.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #21
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param expr
 * @param bindings
 * @param onlyFromDirectReferenceExpr
 * @return
 * @throws DataException
 */
private static Set getReferencedDimLevel( IScriptExpression expr,
		List bindings, boolean onlyFromDirectReferenceExpr )
		throws DataException
{
	if ( expr == null || expr.getText( ) == null || expr.getText( ).length( ) == 0 || BaseExpression.constantId.equals( expr.getScriptId( ) ) )
		return new HashSet( );
	try
	{
		Set result = new HashSet( );
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr.getText( ), null, 0 );
		Node root = new IRFactory( ce).transformTree(tree);

		populateDimLevels( null,
				root,
				result,
				bindings,
				onlyFromDirectReferenceExpr );
		return result;
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #22
Source File: ExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param context
 * @param aggregateExpression
 * @param callNode
 * @throws DataException
 */
private void extractArguments( Context context,
		AggregateExpression aggregateExpression, Node callNode )
		throws DataException
{
	Node arg = callNode.getFirstChild().getNext();
	
	while( arg != null )
	{
		// need to hold on to the next argument because the tree extraction 
		// will cause us to lose the reference otherwise
		Node nextArg = arg.getNext();
		
		CompiledExpression expr = processChild( context,
				false,
				callNode,
				arg,
				null );
		if( ! ( expr instanceof BytecodeExpression ) )
		{
			aggregateExpression.addArgument( expr );
			arg = nextArg;
			continue;
		}
		
		AstRoot tree = new AstRoot( Token.SCRIPT );
		Node exprNode = new Node( Token.EXPR_RESULT);
		exprNode.addChildToFront( arg );
		tree.addChildrenToFront( exprNode );
		compileForBytecodeExpr( context, tree, expr );
		aggregateExpression.addArgument( expr );
		
		arg = nextArg;
	}
}
 
Example #23
Source File: AbstractExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * parse the expression to script tree
 * 
 * @param expression
 * @param cx
 * @return
 * @throws DataException
 */
protected ScriptNode parse( String expression, Context cx )
		throws DataException
{
	if ( expression == null )
		throw new DataException( ResourceConstants.EXPRESSION_CANNOT_BE_NULL_OR_BLANK );

	CompilerEnvirons compilerEnv = getCompilerEnv( cx );
	Parser p = new Parser( compilerEnv, cx.getErrorReporter( ) );
	AstRoot root = p.parse( expression, null, 0 );
	IRFactory ir = new IRFactory(compilerEnv );
	ScriptNode script = ir.transformTree(root);
	return script;
}
 
Example #24
Source File: MultiPassExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * parse the aggregate expression's arguments
 * 
 * @param context
 * @param aggregateExpression
 * @param callNode
 * @throws DataException
 */
private void extractArguments( Context context,
		AggregateExpression aggregateExpression, Node callNode )
		throws DataException
{
	Node arg = callNode.getFirstChild( ).getNext( );
					
	while ( arg != null )
	{
		// need to hold on to the next argument because the tree extraction
		// will cause us to lose the reference otherwise
		Node nextArg = arg.getNext( );
		CompiledExpression expr = processChild( context,
				true,
				callNode,
				arg,
				null );
		if ( !( expr instanceof BytecodeExpression ) )
		{
			aggregateExpression.addArgument( expr );
			arg = nextArg;
			continue;
		}

		AstRoot tree = new AstRoot( Token.SCRIPT );
		Node exprNode = new Node( Token.EXPR_RESULT );
		exprNode.addChildToFront( arg );
		tree.addChildrenToFront( exprNode );
		if ( expr instanceof AggregateExpression )
		{
			int registry = getRegisterId( new AggregateObject( (AggregateExpression) expr ) );
			if ( registry >= 0 )
				replaceAggregateNode( registry, exprNode, arg );
		}
		
		compileForBytecodeExpr( context, tree, expr );
		aggregateExpression.addArgument( expr );
		arg = nextArg;
	}
}
 
Example #25
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * parse the expression into a script tree
 * 
 * @param expression
 * @param cx
 * @return
 */
private ScriptNode parse( String expression, Context cx )
{
	CompilerEnvirons compilerEnv = new CompilerEnvirons( );
	Parser p = new Parser( compilerEnv, cx.getErrorReporter( ) );
	AstRoot root = p.parse( expression, null, 0 );
	IRFactory ir = new IRFactory(compilerEnv);
	return ir.transformTree(root);
}
 
Example #26
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param expr
 * @param objectName
 * @return
 */
public static Set<String> getReferencedMeasure( String expr )
{
	if ( expr == null )
		return Collections.emptySet( );
	try
	{
		Set<String> result = new LinkedHashSet<String>( );
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		IRFactory ir = new IRFactory( ce );
		ScriptNode script = ir.transformTree( tree );
		getScriptObjectName( script, "measure", result ); //$NON-NLS-1$
		
		return result;
	}
	catch( Exception e )
	{
		return Collections.emptySet( );
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #27
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param expr
 * @param bindings
 * @param onlyFromDirectReferenceExpr
 * @return
 * @throws DataException
 */
public static Set<IDimLevel> getReferencedDimLevel( String expr )
		throws CoreException
{
	if ( expr == null  )
		return new HashSet<IDimLevel>( );
	try
	{
		Set<IDimLevel> result = new HashSet<IDimLevel>( );
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		IRFactory ir = new IRFactory( ce );
		ScriptNode script = ir.transformTree( tree );
		populateDimLevels( null, script, result );
		return result;
	}
	catch( Exception e )
	{
		return Collections.emptySet( );
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #28
Source File: JavascriptTestability.java    From testability-explorer with Apache License 2.0 5 votes vote down vote up
public int calculateCost() throws IOException {
  FunctionCountNodeVisitor v = new FunctionCountNodeVisitor();
  for (AstRoot astRoot : repository.getAsts()) {
    astRoot.visit(v);
  }
  return v.getCount();
}
 
Example #29
Source File: ConstraintGenerator.java    From SJS with Apache License 2.0 5 votes vote down vote up
/**
	 * generate constraints from JavaScript code
	 * @param code the AST to generate constraints for
	 */
	public void generateConstraints(AstRoot code) {
//        System.out.println(code.toSource(0));
        code.visit(constraintVisitor);
		List<SyntaxError> errors = constraintVisitor.getErrors();
		if (!errors.isEmpty()) {
			StringBuilder errmsg = new StringBuilder();
			errmsg.append("Found ").append(errors.size()).append(" syntax errors:");
			for (SyntaxError e : errors) {
				errmsg.append("\n  ").append(e.getMessage())
						.append(" (at line ").append(e.getNode().getLineno()).append(')');
			}
			throw new SolverException(errmsg.toString());
		}
	}
 
Example #30
Source File: Bug689314Test.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
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);
}