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, str));

            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, String current)
    {
        Rule ruleToMatch = _rules[ruleNumber];
        String str = current;

        System.out.println("Expanding rule "+ruleNumber+" and currently have "+current);
        
        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;
}
@@ -338,21 +338,16 @@
 leNumber
-, str
 ));%0A%0A   
@@ -821,32 +821,16 @@
 leNumber
-, String current
 )%0A    %7B%0A
@@ -901,115 +901,12 @@
 r = 
-current;%0A%0A        System.out.println(%22Expanding rule %22+ruleNumber+%22 and currently have %22+current);%0A        
+%22%22;%0A
 %0A   

Update Matcher.java