package jltools.frontend;

import jltools.ast.*;
import jltools.util.*;

/** A pass which runs a visitor. */
public class VisitorPass extends AbstractPass
{
    Job job;
    NodeVisitor v;

    public VisitorPass(Job job) {
	this(job, null);
    }

    public VisitorPass(Job job, NodeVisitor v) {
	this.job = job;
	this.v = v;
    }

    public void visitor(NodeVisitor v) {
	this.v = v;
    }

    public NodeVisitor visitor() {
	return v;
    }

    public boolean run() {
	Node ast = job.ast();

	if (ast == null) {
	    throw new InternalCompilerError("Null AST: did the parser run?");
	}

        ErrorQueue q = job.compiler().errorQueue();
        int nErrsBefore = q.errorCount();

	ast = ast.visit(v);
        v.finish();

        int nErrsAfter = q.errorCount();

	job.ast(ast);

        return (nErrsBefore == nErrsAfter);
        // because, if they're equal, no new errors occured,
        // so the run was successful.
    }

    public String toString() {
	return v.getClass().getName() + "(" + job + ")";
    }
}

@@ -588,16 +588,45 @@
 %22);%0A%09%7D%0A%0A
+        if (v.begin()) %7B%0A    
         
@@ -669,32 +669,36 @@
 ueue();%0A        
+    
 int nErrsBefore 
@@ -716,17 +716,28 @@
 unt();%0A%0A
-%09
+            
 ast = as
@@ -756,16 +756,20 @@
         
+   
+ 
 v.finish
@@ -769,24 +769,28 @@
 .finish();%0A%0A
+    
         int 
@@ -819,17 +819,28 @@
 unt();%0A%0A
-%09
+            
 job.ast(
@@ -854,16 +854,20 @@
         
+   
+ 
 return (
@@ -894,16 +894,20 @@
 After);%0A
+    
         
@@ -967,16 +967,20 @@
         
+  
+  
 // so th
@@ -1001,16 +1001,49 @@
 essful.%0A
+        %7D%0A%0A        return false;%0A
     %7D%0A%0A 

Call NodeVisitor.begin() before visiting ast.