import org.antlr.runtime.*;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.stringtemplate.*;
import java.lang.reflect.*;

public class RunExpression {
  public static void main(String[] args) throws Exception {
    // create an instance of the lexer
    PowerScriptLexer lexer = new PowerScriptLexer(new ANTLRStringStream(args[1])); //ANTLRNoCaseFileStream
        
    // wrap a token-stream around the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
        
    // create the parser
    PowerScriptParser parser = new PowerScriptParser(tokens);
      
    //~ // print the tokens and display their type 
    for(Object o : tokens.getTokens()) {
      CommonToken t = (CommonToken)o;
      System.out.printf("text: %-7s  type: %s \n", t.getText(), parser.tokenNames[t.getType()]);
    }
    
    String className = args[0];

    Method parseMethod = PowerScriptParser.class.getMethod(className);

    Object result = parseMethod.invoke(parser);
    CommonTree ast = (CommonTree)((ParserRuleReturnScope)result).getTree();
    
    if( ast == null ) {
       System.out.println("resultant tree: is NULL");
    } else {
       System.out.println("resultant tree: " + ast.toStringTree());
    }

  }
}
