
import jburg.ProductionTable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class FirstTest
{
    public enum Nonterminal { Int, Short };
    public enum NodeType { ShortLiteral, IntLiteral, Add, Subtract, Multiply };

    public static class Node
    {
        NodeType    type;
        List<Node>  children;

        Node(NodeType type)
        {
            this.type = type;
            this.children = Collections.emptyList();
        }

        Node(NodeType type, Node... children)
        {
            this.type = type;
            this.children = Arrays.asList(children);
        }

        int     stateNumber = -1;

        Object  payload;

        /*
         * ** Nullary Operators **
         */
        public Integer intLiteral()
        {
            return (Integer)payload;
        }

        public Integer shortLiteral()
        {
            return intLiteral();
        }

        /*
         * ** Unary Operators **
         */
        public Integer negate(Integer x)
        {
            return -x;
        }

        public Integer identity(Integer x)
        {
            return x;
        }

        /*
         * ** Binary Operators **
         */
        public Integer add(Integer x, Integer y)
        {
            return x + y;
        }

        public Integer subtract(Integer x, Integer y)
        {
            return x - y;
        }

        public Integer multiply(Integer x, Integer y)
        {
            return x * y;
        }

        public Integer divide(Integer x, Integer y)
        {
            return x / y;
        }
    }

    public static void main(String[] args)
    throws Exception
    {
        ProductionTable<Nonterminal, NodeType> productions = new ProductionTable<Nonterminal, NodeType>();

        productions.addPatternMatch(Nonterminal.Int, NodeType.IntLiteral, Node.class.getDeclaredMethod("intLiteral"));

        productions.addPatternMatch(Nonterminal.Int, NodeType.Add, Node.class.getDeclaredMethod("add", Integer.class, Integer.class), Nonterminal.Int, Nonterminal.Int);
        productions.addPatternMatch(Nonterminal.Int, NodeType.Add, Node.class.getDeclaredMethod("identity", Integer.class), Nonterminal.Int);
        productions.addPatternMatch(Nonterminal.Int, NodeType.Multiply, Node.class.getDeclaredMethod("multiply", Integer.class, Integer.class), Nonterminal.Int, Nonterminal.Int);
        productions.addPatternMatch(Nonterminal.Int, NodeType.Subtract, Node.class.getDeclaredMethod("negate", Integer.class), Nonterminal.Int);
        productions.addPatternMatch(Nonterminal.Int, NodeType.Subtract, Node.class.getDeclaredMethod("subtract", Integer.class, Integer.class), Nonterminal.Int, Nonterminal.Int);

        productions.addPatternMatch(Nonterminal.Short, NodeType.ShortLiteral, Node.class.getDeclaredMethod("shortLiteral"));
        productions.addClosure(Nonterminal.Int, Nonterminal.Short);

        productions.generateStates();
        productions.dump(new java.io.PrintWriter(System.out));
    }

}

@@ -197,16 +197,24 @@
 t, Short
+, String
  %7D;%0A    
@@ -261,16 +261,31 @@
 Literal,
+ StringLiteral,
  Add, Su
@@ -1663,16 +1663,218 @@
        %7D
+%0A%0A        public String stringLiteral()%0A        %7B%0A            return payload.toString();%0A        %7D%0A%0A        public String concat(String lhs, String rhs)%0A        %7B%0A            return lhs + rhs;%0A        %7D
 %0A    %7D%0A%0A
@@ -2474,32 +2474,33 @@
 nterminal.Int);%0A
+%0A
         producti
@@ -2654,32 +2654,33 @@
 nterminal.Int);%0A
+%0A
         producti
@@ -3109,24 +3109,24 @@
 Literal%22));%0A
-
         prod
@@ -3178,24 +3178,332 @@
 al.Short);%0A%0A
+        productions.addPatternMatch(Nonterminal.String, NodeType.StringLiteral, Node.class.getDeclaredMethod(%22stringLiteral%22));%0A        productions.addPatternMatch(Nonterminal.String, NodeType.Add, Node.class.getDeclaredMethod(%22concat%22, String.class, String.class), Nonterminal.String, Nonterminal.String);%0A%0A
         prod

Add String type and productions