Java Code Examples for org.mozilla.javascript.Parser#parse()

The following examples show how to use org.mozilla.javascript.Parser#parse() . 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: 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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
Source File: ScriptValuesModDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
  int lineno, Object securityDomain ) {
  // Interpreter compiler = new Interpreter();
  CompilerEnvirons evn = new CompilerEnvirons();
  // evn.setLanguageVersion(Context.VERSION_1_5);
  evn.setOptimizationLevel( -1 );
  evn.setGeneratingSource( true );
  evn.setGenerateDebugInfo( true );
  ErrorReporter errorReporter = new ToolErrorReporter( false );
  Parser p = new Parser( evn, errorReporter );
  ScriptNode tree = p.parse( source, "", 0 ); // IOException
  new NodeTransformer().transform( tree );
  // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  return tree;
}
 
Example 11
Source File: Bug688023Test.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);
}
 
Example 12
Source File: Bug687669Test.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);
}
 
Example 13
Source File: ScriptDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static ScriptNode parseVariables( Context cx, Scriptable scope, String source, String sourceName,
  int lineno, Object securityDomain ) {
  // Interpreter compiler = new Interpreter();
  CompilerEnvirons evn = new CompilerEnvirons();
  // evn.setLanguageVersion(Context.VERSION_1_5);
  evn.setOptimizationLevel( -1 );
  evn.setGeneratingSource( true );
  evn.setGenerateDebugInfo( true );
  ErrorReporter errorReporter = new ToolErrorReporter( false );
  Parser p = new Parser( evn, errorReporter );
  ScriptNode tree = p.parse( source, "", 0 ); // IOException
  new NodeTransformer().transform( tree );
  // Script result = (Script)compiler.compile(scope, evn, tree, p.getEncodedSource(),false, null);
  return tree;
}
 
Example 14
Source File: Bug491621Test.java    From rhino-android with Apache License 2.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 15
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 16
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);
}
 
Example 17
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 18
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression, boolean mode )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( getCompiledExpCacheMap( mode ).containsKey( expression ) )
	{
		return ( (Boolean)  getCompiledExpCacheMap( mode ).get( expression ) ).booleanValue( );
	}
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		getCompiledExpCacheMap( mode ).put( expression,
				Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child, mode );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}
	getCompiledExpCacheMap( mode ).put( expression,
			Boolean.valueOf( isColumn ) );
	return isColumn;
}
 
Example 19
Source File: Bug789277Test.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
private static AstRoot parseStrict(Reader source, ErrorReporter reporter, boolean ide)
        throws IOException {
    Parser parser = new Parser(compilerEnv(reporter, ide));
    return parser.parse(source, SOURCE_NAME, 1);
}
 
Example 20
Source File: Bug789277Test.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
private static AstRoot parseStrict(String source, ErrorReporter reporter, boolean ide)
        throws IOException {
    Parser parser = new Parser(compilerEnv(reporter, ide));
    return parser.parse(source, SOURCE_NAME, 1);
}