Nuthatch

Tree-Walking for Peeps

Example: Term Printer

Convert a tree to its string representation (Printer.java) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package nuthatch.examples;

import nuthatch.examples.xmpllang.Expr;
import nuthatch.examples.xmpllang.XmplNode;
import nuthatch.examples.xmpllang.full.XmplActionFactory;
import nuthatch.examples.xmpllang.full.XmplWalker;
import nuthatch.library.Action;
import nuthatch.library.BaseAction;

public class Printer {
  public static Action<XmplWalker> printAction = new BaseAction<XmplWalker>(){
      @Override
      public int step(XmplWalker walker) {
          System.out.println(Printer.toTerm(walker.getData()));
          return PROCEED;
      }
  };


  public static void main(String[] args) {
      for(Expr e : new Expr[] { ExampleExpr.expr1, ExampleExpr.expr2, ExampleExpr.expr3, ExampleExpr.expr4 }) {
          System.out.println(toTerm(e));
      }
  }

  public static String toTerm(XmplNode expr) {
      // Walk which outputs the tree in a term-like representation. The result are accumulated in the
      // stringbuffer 's'.
      XmplActionFactory af = XmplActionFactory.getInstance();

      final StringBuilder t = new StringBuilder();

      Action<XmplWalker> appendData = new BaseAction<XmplWalker>() {
          @Override
          public int step(XmplWalker walker) {
              t.append(walker.getData());
              return NEXT;
          }
      };
      Action<XmplWalker> appendName = new BaseAction<XmplWalker>() {
          @Override
          public int step(XmplWalker walker) {
              t.append(walker.getName() + "(");
              return NEXT;
          }
      };
      Action<XmplWalker> appendEnd = new BaseAction<XmplWalker>() {
          @Override
          public int step(XmplWalker walker) {
              t.append(")");
              return NEXT;
          }
      };
      Action<XmplWalker> appendComma = new BaseAction<XmplWalker>() {
          @Override
          public int step(XmplWalker walker) {
              t.append(", ");
              return NEXT;
          }
      };

      Action<XmplWalker> toTerm = af.seq(af.atLeaf(appendData), af.down(appendName), af.afterChild((af.beforeChild(appendComma))), af.up(appendEnd));

      XmplWalker toTermWalker = new XmplWalker(expr, af.walk(toTerm));
      toTermWalker.start();
      return t.toString();
  }

}