package com.redhat.ceylon.compiler.util;

import com.redhat.ceylon.compiler.tree.Node;
import com.redhat.ceylon.compiler.tree.Tree;
import com.redhat.ceylon.compiler.tree.Visitor;

public class AssertionVisitor extends Visitor {
    
    boolean expectingError = false;
    boolean foundError = false;

    @Override
    public void visit(Tree.TypedDeclaration that) {
        for (Tree.CompilerAnnotation c: that.getCompilerAnnotations()) {
            if (c.getIdentifier().getText().equals("type")) {
                String expectedType = c.getStringLiteral().getText();
                String actualType = that.getTypeOrSubtype().getTypeModel().getProducedTypeName();
                if ( !actualType.equals(expectedType.substring(1,expectedType.length()-1)) )
                    System.err.println(
                        c.getIdentifier().getText()  + 
                        " is not of type " + expectedType + 
                        " at "+ that.getAntlrTreeNode().getLine() + ":" +
                        that.getAntlrTreeNode().getCharPositionInLine());
            }
        }
        super.visit(that);
    }
    
    @Override
    public void visit(Tree.Statement that) {
        boolean b = expectingError;
        boolean f = foundError;
        expectingError = false;
        foundError = false;
        for (Tree.CompilerAnnotation c: that.getCompilerAnnotations()) {
            if (c.getIdentifier().getText().equals("error")) {
                expectingError = true;
            }
        }
        super.visit(that);
        if (expectingError!=foundError)
            System.err.println(
                (expectingError ? "no " : "") + "error encountered at " + 
                that.getAntlrTreeNode().getLine() + ":" +
                that.getAntlrTreeNode().getCharPositionInLine());
        expectingError = b;
        foundError = f;
    }
    
    @Override
    public void visitAny(Node that) {
        if ( !that.getErrors().isEmpty() )
            foundError = true;
        super.visitAny(that);
    }
    
}

@@ -35,16 +35,62 @@
 .util;%0A%0A
+import com.redhat.ceylon.compiler.model.Type;%0A
 import c
@@ -126,16 +126,16 @@
 e.Node;%0A
-
 import c
@@ -400,32 +400,359 @@
 aration that) %7B%0A
+        checkType(that, that.getTypeOrSubtype().getTypeModel());%0A        super.visit(that);%0A    %7D%0A%0A    @Override%0A    public void visit(Tree.ExpressionStatement that) %7B%0A        checkType(that, that.getExpression().getTypeModel());%0A        super.visit(that);%0A    %7D%0A    %0A    private void checkType(Tree.Statement that, Type tm) %7B%0A
         for (Tre
@@ -866,24 +866,86 @@
 (%22type%22)) %7B%0A
+                String actualType = tm.getProducedTypeName();%0A
             
@@ -1006,106 +1006,8 @@
 ();%0A
-                String actualType = that.getTypeOrSubtype().getTypeModel().getProducedTypeName();%0A
     
@@ -1139,64 +1139,8 @@
 ln(%0A
-                        c.getIdentifier().getText()  + %0A
     
@@ -1164,12 +1164,8 @@
    %22
- is 
 not 
@@ -1346,32 +1346,32 @@
 ;%0A            %7D%0A
+
         %7D%0A      
@@ -1368,35 +1368,8 @@
   %7D%0A
-        super.visit(that);%0A
     

fix type assertions