import java.util.*;

public class Matcher
{
    public Matcher (Rule[] rules, boolean debug)
    {
        _rules = rules;
        _debug = debug;
    }

    public Message[] matchRule (int ruleNumber, Message[] messages)
    {
        if (true)
        {
            String str = "";

            System.out.println("Got "+expandRule(ruleNumber));

            return null;
        }

        Rule ruleToMatch = _rules[ruleNumber];
        Vector<Message> toMatch = new Vector<Message>();

        //if (_debug)
            System.out.println("Trying to match rule: "+ruleToMatch);

        String current = getMatchingString(ruleToMatch, "");

        System.out.println("Match using first rules: "+current);

        toMatch.add(new Message(current));

        return null;
    }

    public String expandRule (int ruleNumber)
    {
        Rule ruleToMatch = _rules[ruleNumber];
        String str = "";

        if (ruleToMatch.isCharacterRule())
            str = ""+ruleToMatch.getMatch();
        else
        {
            int[] leftRules = ruleToMatch.leftRules();

            str += " ( ";

            for (int i = 0; i < leftRules.length; i++)
            {
                str += expandRule(leftRules[i]);

                System.out.println("Expanded one left rule: "+str);
            }

            int[] rightRules = ruleToMatch.rightRules();

            if (rightRules != null)
            {
                System.out.println("Have OR rule");

                str += " | ";

                for (int j = 0; j < rightRules.length; j++)
                {
                    str += expandRule(rightRules[j]);
                }
            }

            str += " ) ";

            System.out.println("At this stage have: "+str);
        }

        System.out.println("Done and returning: "+str);

        return str;
    }

    public String[] expandedRules (int ruleNumber)
    {
        Vector<String> toReturn = new Vector<String>();

        Rule ruleToMatch = _rules[ruleNumber];
        String str = "";

        if (ruleToMatch.isCharacterRule())
            str = ""+ruleToMatch.getMatch();
        else
        {
            int[] leftRules = ruleToMatch.leftRules();

            str += " ( ";

            for (int i = 0; i < leftRules.length; i++)
            {
                str += expandRule(leftRules[i]);

                System.out.println("Expanded one left rule: "+str);
            }

            int[] rightRules = ruleToMatch.rightRules();

            if (rightRules != null)
            {
                str += " | ";

                for (int j = 0; j < rightRules.length; j++)
                {
                    str += expandRule(rightRules[j]);
                }
            }

            str += " ) ";

            System.out.println("At this stage have: "+str);
        }
        
        System.out.println("Have "+str);

        return (String[]) toReturn.toArray();
    }

    // try first rule set only.
    
    private String getMatchingString (Rule theRule, String current)
    {
        String str = current;

        System.out.println("Looking to match "+theRule.getNumber());

        if (!theRule.isCharacterRule())
        {
            int[] leftRules = theRule.leftRules();

            for (int i = 0; i < leftRules.length; i++)
            {
                str = getMatchingString(_rules[leftRules[i]], str);

                //if (_debug)
                    System.out.println("First rules string: "+str);
            }
        }
        else
            str += theRule.getMatch();

        System.out.println("\n**returning "+str);

        return str;
    }

    private Rule[] _rules;
    private boolean _debug;
}
@@ -225,40 +225,8 @@
   %7B%0A
-        if (true)%0A        %7B%0A    
     
@@ -243,28 +243,24 @@
  str = %22%22;%0A%0A
-    
         Syst
@@ -311,414 +311,8 @@
 );%0A%0A
-            return null;%0A        %7D%0A%0A        Rule ruleToMatch = _rules%5BruleNumber%5D;%0A        Vector%3CMessage%3E toMatch = new Vector%3CMessage%3E();%0A%0A        //if (_debug)%0A            System.out.println(%22Trying to match rule: %22+ruleToMatch);%0A%0A        String current = getMatchingString(ruleToMatch, %22%22);%0A%0A        System.out.println(%22Match using first rules: %22+current);%0A%0A        toMatch.add(new Message(current));%0A%0A
     
@@ -1399,1801 +1399,8 @@
  %7D%0A%0A
-    public String%5B%5D expandedRules (int ruleNumber)%0A    %7B%0A        Vector%3CString%3E toReturn = new Vector%3CString%3E();%0A%0A        Rule ruleToMatch = _rules%5BruleNumber%5D;%0A        String str = %22%22;%0A%0A        if (ruleToMatch.isCharacterRule())%0A            str = %22%22+ruleToMatch.getMatch();%0A        else%0A        %7B%0A            int%5B%5D leftRules = ruleToMatch.leftRules();%0A%0A            str += %22 ( %22;%0A%0A            for (int i = 0; i %3C leftRules.length; i++)%0A            %7B%0A                str += expandRule(leftRules%5Bi%5D);%0A%0A                System.out.println(%22Expanded one left rule: %22+str);%0A            %7D%0A%0A            int%5B%5D rightRules = ruleToMatch.rightRules();%0A%0A            if (rightRules != null)%0A            %7B%0A                str += %22 %7C %22;%0A%0A                for (int j = 0; j %3C rightRules.length; j++)%0A                %7B%0A                    str += expandRule(rightRules%5Bj%5D);%0A                %7D%0A            %7D%0A%0A            str += %22 ) %22;%0A%0A            System.out.println(%22At this stage have: %22+str);%0A        %7D%0A        %0A        System.out.println(%22Have %22+str);%0A%0A        return (String%5B%5D) toReturn.toArray();%0A    %7D%0A%0A    // try first rule set only.%0A    %0A    private String getMatchingString (Rule theRule, String current)%0A    %7B%0A        String str = current;%0A%0A        System.out.println(%22Looking to match %22+theRule.getNumber());%0A%0A        if (!theRule.isCharacterRule())%0A        %7B%0A            int%5B%5D leftRules = theRule.leftRules();%0A%0A            for (int i = 0; i %3C leftRules.length; i++)%0A            %7B%0A                str = getMatchingString(_rules%5BleftRules%5Bi%5D%5D, str);%0A%0A                //if (_debug)%0A                    System.out.println(%22First rules string: %22+str);%0A            %7D%0A        %7D%0A        else%0A            str += theRule.getMatch();%0A%0A        System.out.println(%22%5Cn**returning %22+str);%0A%0A        return str;%0A    %7D%0A%0A
     

Update Matcher.java