

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.tree.CommonTree;

import com.redhat.ceylon.compiler.parser.CeylonLexer;
import com.redhat.ceylon.compiler.parser.CeylonParser;
import com.redhat.ceylon.compiler.tree.Builder;
import com.redhat.ceylon.compiler.tree.Node;
import com.redhat.ceylon.compiler.tree.Tree.CompilationUnit;
import com.redhat.ceylon.compiler.tree.Visitor;
import com.redhat.ceylon.compiler.tree.Walker;

public class Main {

	public static void main(String[] args) throws Exception {
		String path;
		if ( args.length==0 ) {
			path = "corpus";
		}
		else {
			path = args[0];
		}
		fileOrDir( new File(path) );
	}
	
	private static void file(File file) throws Exception {
		if ( file.getName().endsWith(".ceylon") ) {
		
		System.out.println("Parsing " + file.getName());
        InputStream is = new FileInputStream( file );
        ANTLRInputStream input = new ANTLRInputStream(is);
        CeylonLexer lexer = new CeylonLexer(input);

        CommonTokenStream tokens = new CommonTokenStream(lexer);

        CeylonParser parser = new CeylonParser(tokens);
        CeylonParser.compilationUnit_return r = parser.compilationUnit();

        CommonTree t = (CommonTree)r.getTree();
        
        CompilationUnit cu = new Builder().buildCompilationUnit(t);
        Visitor v = new Visitor() {
            int depth=0;

            void print(String str) {
            	System.out.print(str);
            }

            void newline() {
                print("\n");
            }

            void indent() {
                for (int i = 0; i < depth; i++)
                    print("|  ");
            }
            
            @Override
            public void visitAny(Node node) {
                if (depth>0) newline();
                indent();
                print("+ ");
                print(node.getText());
                depth++;
                super.visitAny(node);
                depth--;
                if (depth==0) newline();
            }
        };
        Walker.walkCompilationUnit(v, cu);

        if (lexer.getNumberOfSyntaxErrors() != 0) {
            System.out.println("Lexer failed");
        }
        else if (parser.getNumberOfSyntaxErrors() != 0) {
        	System.out.println("Parser failed");
        }
        else {
        	System.out.println("Parser succeeded");
        }
        
		}
        
	}
	
	private static void fileOrDir(File file) throws Exception {
		if (file.isDirectory()) 
			dir(file);
		else
			file(file);
	}

	private static void dir(File dir) throws Exception {
		for (File file: dir.listFiles()) 
			fileOrDir(file);
	}

}

@@ -1483,16 +1483,52 @@
 depth=0;
+%0A            //boolean inType=false;
 %0A%0A      
@@ -1811,32 +1811,817 @@
  %7D%0A            %0A
+            /*@Override%0A            public void visit(Type node) %7B%0A            %09if (!inType) %7B%0A                    if (depth%3E0) newline();%0A                    indent();%0A                    print(%22+ %22);%0A            %09%7D%0A                if (node.getTypeName()!=null) %7B%0A                %09print(node.getTypeName().getText());%0A                %09boolean oldInType = inType;%0A                %09inType=true;%0A                %09if (node.getTypeArgumentList()!=null)%0A                        visit(node.getTypeArgumentList());%0A                %09inType=oldInType;%0A                %7D%0A            %7D%0A            %0A            @Override%0A            public void visit(TypeArgumentList node) %7B%0A            %09print(%22%3C%22);%0A                super.visitAny(node);%0A                print(%22%3E%22);%0A            %7D*/%0A            %0A
             @Ove
@@ -2759,32 +2759,32 @@
    print(%22+ %22);%0A
-
                 
@@ -2795,32 +2795,128 @@
 t(node.getText()
+ + %22 (%22 + node.getTreeNode().getLine() + %22:%22 + node.getTreeNode().getCharPositionInLine()  + %22)%22
 );%0A             

print line numbers