
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of SableCC.                             *
 * See the file "LICENSE" for copyright information and the  *
 * terms and conditions for copying, distribution and        *
 * modification of SableCC.                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// 0: xxx.tool
// 1: xxx

Macro:TreeBuilder
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

/**/
import $1$.node.Node;
import $1$.lexer.Lexer;
import $1$.lexer.LexerException;
import $1$.parser.Parser;
import $1$.parser.ParserException;
import java.io.*;

/**
 * Generic parser utility API, Node builder. This class is not really a
 * builder (in terms of Builder pattern), while the result can be
 * collected directly from it. This builder will parse the input (string, stream) 
 * and create Abstract Syntax Tree, via set of utility methods.
 *@author Mariusz Nowostawski
 */
public class TreeBuilder {

  public static Node getNode(final Reader r)
    throws IOException, LexerException, ParserException {
    return getNode(new PushbackReader(r));
  }
  
  public static Node getNode(final StringBuffer text)
    throws IOException, LexerException, ParserException {
    return getNode(text.toString());
  }

  public synchronized static Node getNode(final String text)
    throws IOException, LexerException, ParserException {
    return getNode(new PushbackReader
      (new BufferedReader
        (new StringReader(text))));
  }
  
  public synchronized static Node getNode(final PushbackReader r)
    throws IOException, LexerException, ParserException {
    Lexer lexer = new Lexer(r);
    Parser parser = new Parser(lexer);
    Node ast = parser.parse();
    return ast;
  }
  
} //TreeBuilder

$

Macro:PrintWalker
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

/**/
import $1$.analysis.DepthFirstAdapter;
import $1$.node.*;

/**
 * Simple AST walker. This is simple Abstract Syntax Tree walker
 * which will visit each of the nodes and print on standard output the
 * name of particular node visited. This class can be very usefull while
 * testing the tree structure for a given input. Check PrintTree class.
 *@author Mariusz Nowostawski
 */
public class PrintWalker extends DepthFirstAdapter {

  int indent = 0;

  void indent(){
    String s = new String ("");
    for(int i=0; i < indent; i++) s += " "; 
    System.out.print(s);
  }
    
  public void defaultIn(Node node) {
    indent();
    System.out.println(node.getClass().getName().substring("$0$".length()+1));
    indent++;
  }
  
  public void defaultOut(Node node) {
    indent--;
  }
  
  public void defaultCase(Node node) {
    indent();
    System.out.println(node.getClass().getName().substring("$0$".length()+1)+": " + ((Token) node).getText());
  }
  
}


$

Macro:PrintTree
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

/**/
import $1$.node.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;


/**
 * Prints the tree of the given AST on standard output or inside JFrame.
 * This is a toy class which demonstrates the use of PrintWalker adapter 
 * for traversing the Abstract Syntax Tree and printing each node.
 *@author Mariusz Nowostawski
 */
public class PrintTree {


  public static void main(String[] arguments) {
    System.out.print(Version.banner());

    if(arguments.length < 1 || arguments[0].equals("-h") || arguments[0].equals("--help")) {
      System.out.println("Usage:");
      System.out.println("  java nzdis.lang.kif.tool.PrintTree [-g|--gui] filename");
      System.exit(1);
    }
 
    String input;
    if(arguments.length == 2) input = arguments[1];
    else input = arguments[0];

    try {
      Node ast = TreeBuilder.getNode(new PushbackReader(
                                                        new BufferedReader(
                                                        new FileReader(input)), 1024));
      PrintTree app = new PrintTree(); 
      if(arguments[0].equals("--gui") || arguments[0].equals("-g")) 
        app.doGUITree(ast);
      else
        app.doTextTree(ast);
    }catch(Exception ex){
      ex.printStackTrace();
    }
  }


  private JPopupMenu popup; 
  private JFrame frame;
  private JTree table;


  private void doGUITree(Node ast) {
        DisplayTree display = new DisplayTree();
        ast.apply(display);
        popup = createPopupMenuBar();
        frame = display.getTreeFrame();
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
              System.exit(0);
            }    
          });

        table = ((DisplayTree.TreeFrame)frame).getJTree();
        table.addMouseListener(new PopupListener());

        frame.setVisible(true);
  }


  private void doTextTree(Node ast) {
    ast.apply(new PrintWalker());
  }


  private JPopupMenu createPopupMenuBar() {
    JPopupMenu menu = new JPopupMenu();
    JMenuItem LAFnative = new JMenuItem("Set Native Look&Feel");
    LAFnative.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          try { 
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          } catch(Exception ex) {
            System.out.println("Error setting Native LAF: " + ex);
          }
          SwingUtilities.updateComponentTreeUI(frame);
        }
      });
    JMenuItem LAFjava = new JMenuItem("Set Java Look&Feel");
    LAFjava.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          try { 
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
          } catch(Exception ex) {
            System.out.println("Error setting Java LAF: " + ex);
          }
          SwingUtilities.updateComponentTreeUI(frame);
        }
      });
    JMenuItem LAFmotif = new JMenuItem("Set Motif Look&Feel");
    LAFmotif.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          try { 
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); 
          } catch(Exception ex) {
            System.out.println("Error setting Motif LAF: " + ex);
          }
          SwingUtilities.updateComponentTreeUI(frame);
        }
      });
    JMenuItem quit = new JMenuItem("Quit");
    quit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.exit(0);
        }
      });
    menu.add(LAFnative);
    menu.add(LAFjava);
    menu.add(LAFmotif);
    menu.addSeparator();
    menu.add(quit);
    return menu;
  }

  private class PopupListener extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
      maybeShowPopup(e);
    }
    public void mouseReleased(MouseEvent e) {
      maybeShowPopup(e);
    }
    private void maybeShowPopup(MouseEvent e) {
      if (e.isPopupTrigger()) {
        popup.show(e.getComponent(),
                   e.getX(), e.getY());
      }
    }
  }
}

$

Macro:DisplayTreeHeader
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

/**/
import $1$.analysis.*;
import $1$.node.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;


/** 
 * GUI-based display of AST.
 * This is a simple utility that uses Java's JTree class to provide a
 * rudimentary hierarchical display of an AST built by a parser that is 
 * generated by SableCC.  See also PrintTree class.
 *@author Jeffrey Van Baalen
 *@author Mariusz Nowostawski
 */
public class DisplayTree extends DepthFirstAdapter
{

    private Hashtable table = new Hashtable();
    private DefaultMutableTreeNode root = null;

    private String stripName(Node node){
         return node.getClass().getName().substring("$1$".length()+6);
    }

    // Default node creation.
    public void defaultIn(Node node) 
    {   
        table.put(node, new DefaultMutableTreeNode(stripName(node)));
    }

$

Macro:DisplayTreeNodeHeader

    public void out$0$($0$ node)
    {
        root =  (DefaultMutableTreeNode)table.get(node);
        DefaultMutableTreeNode dnode = root;

$

Macro:DisplayTreeCaseBodyNode
        if(node.get$0$() != null) 
        {
            DefaultMutableTreeNode n = (DefaultMutableTreeNode)table.get(node.get$0$());
            if(n == null )
            {
                dnode.add(new DefaultMutableTreeNode(stripName(node.get$0$())+": "+node.get$0$().toString()));
            } 
            else 
            {
                dnode.add(n);
            }
        }

$

Macro:DisplayTreeCaseBodyList
        {
            Object temp[] = node.get$0$().toArray();
            for(int i = 0; i < temp.length; i++) 
            {
                if(temp[i]!=null) dnode.add((DefaultMutableTreeNode)table.get(($1$) temp[i]));
            }
        }

$

Macro:DisplayTreeNodeFooter
    }

$

Macro:DisplayTreeFooter

    public TreeFrame getTreeFrame() 
    {
        return new TreeFrame("Abstract Syntax Tree", root);
    }

    public TreeInternalFrame getTreeInternalFrame() 
    {
        return new TreeInternalFrame("Abstract Syntax Tree", root);
    }

    public static class TreeFrame extends JFrame
    {
        private JTree jtree;

        public TreeFrame(String title, DefaultMutableTreeNode root)
        {
            super(title);
            //WindowUtilities.setNativeLookAndFeel();
            Container content = getContentPane();
            jtree = new JTree(root);
            content.add(new JScrollPane(jtree), BorderLayout.CENTER);
            setSize(275, 300);
        }

        public JTree getJTree()
        {
            return this.jtree;
        }

    }//TreeFrame


    public static class TreeInternalFrame extends JInternalFrame 
    {

        private JTree jtree;

        public TreeInternalFrame(String title, DefaultMutableTreeNode root)
        {
            super(title);
            Container content = getContentPane();
            jtree = new JTree(root);
            content.add(new JScrollPane(jtree), BorderLayout.CENTER);
            setSize(275, 300);
         }

        public JTree getJTree()
        {
            return this.jtree;
        }

    }//TreeInternalFrame

}//DisplayTree

$

Macro:Version
/* This file was generated by SableCC (http://www.sablecc.org/). */

package $0$;

public class Version
{  
  public static final String COPYRIGHT = "@copyright@";
  public static final String VERSION = "@version@";
  public static final String NAME = "@name@";
  public static final String FULLNAME = "@Name@";

  public static String banner(){
    return FULLNAME + "  ("+NAME+")"+"    version "+VERSION+"\n"+COPYRIGHT+"\nAll Rights Reserved.\n\n";
  }
}

$

