aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/gluegen/cgram/TNode.java
blob: a564c54d83edaf5d1d0665a5e3f3882cb146fd47 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package com.jogamp.gluegen.cgram;

import antlr.collections.AST;
import antlr.CommonAST;
import antlr.Token;
import java.lang.reflect.*;
import java.util.Hashtable;
import java.util.Enumeration;

/**
  Class TNode is an implementation of the AST interface
  and adds many useful features:

  It is double-linked for reverse searching.
  (this is currently incomplete, in that method doubleLink() must
  be called after any changes to the tree to maintain the
  reverse links).

  It can store a definition node (defNode), so that nodes such
  as scoped names can refer to the node that defines the name.

  It stores line numbers for nodes.

  Searches for parents and children of a tree can be done
  based on their type.

  The tree can be printed to System.out using a lisp-style syntax.



 */
public class TNode extends CommonAST {
  protected int ttype;
  protected String text;
  protected int lineNum = 0;
  protected TNode defNode;
  protected TNode up;
  protected TNode left;
  protected boolean marker = false;
  protected Hashtable<String, Object> attributes = null;
  static String tokenVocabulary;




  /** Set the token vocabulary to a tokentypes class
      generated by antlr.
  */
  public static void setTokenVocabulary(final String s) {
    tokenVocabulary = s;
  }


@Override
public void initialize(final Token token) {
        final CToken tok = (CToken) token;
        setText(tok.getText());
        setType(tok.getType());
        setLineNum(tok.getLine());
        setAttribute("source", tok.getSource());
        setAttribute("tokenNumber", new Integer(tok.getTokenNumber()));
}
@Override
public void initialize(final AST tr) {
        final TNode t = (TNode) tr;
        setText(t.getText());
        setType(t.getType());
        setLineNum(t.getLineNum());
        setDefNode(t.getDefNode());
        this.attributes = t.getAttributesTable();
}


  /** Get the token type for this node */
  @Override
  public int getType() { return ttype; }

  /** Set the token type for this node */
  @Override
  public void setType(final int ttype_) {
    ttype = ttype_;
  }

  /** Get the marker value for this node.
   This member is a general-use marker.
   */
  public boolean getMarker() { return marker; }

  /** Set the marker value for this node.
   This property is a general-use boolean marker.
   */
  public void setMarker(final boolean marker_) {
    marker = marker_;
  }

  /** get the hashtable that holds attribute values.
   */
  public Hashtable<String, Object> getAttributesTable() {
    if(attributes == null)
      attributes = new Hashtable<String, Object>(7);
    return attributes;
  }

  /** set an attribute in the attribute table.
   */
  public void setAttribute(final String attrName, final Object value) {
    if(attributes == null)
      attributes = new Hashtable<String, Object>(7);
    attributes.put(attrName,value);
  }

  /** lookup the attribute name in the attribute table.
    If the value does not exist, it returns null.
    */
  public Object getAttribute(final String attrName) {
    if(attributes == null)
      return null;
    else
      return attributes.get(attrName);
  }

  /** Get the line number for this node.
   If the line number is 0, search for a non-zero line num among children */
  public int getLineNum() {
    if(lineNum != 0)
      return lineNum;
    else
      if(down == null)
        return lineNum;
      else
        return ((TNode)down).getLocalLineNum();
  }

  public int getLocalLineNum() {
    if(lineNum != 0)
      return lineNum;
    else
      if(down == null)
        if(right == null)
          return lineNum;
        else
          return ((TNode)right).getLocalLineNum();
      else
        return ((TNode)down).getLocalLineNum();
  }

  /** Set the line number for this node */
  public void setLineNum(final int lineNum_) {
    lineNum = lineNum_;
  }

  /** Get the token text for this node */
  @Override
  public String getText() { return text; }

  /** Set the token text for this node */
  @Override
  public void setText(final String text_) {
    text = text_;
  }

  /** Returns the text for this node and all children */
  public String getAllChildrenText() {
    final StringBuilder buf = new StringBuilder();
    buf.append(getText());
    for (TNode node = (TNode) getFirstChild(); node != null; node = (TNode) node.getNextSibling()) {
      buf.append(node.getText());
    }
    return buf.toString();
  }

  /** return the last child of this node, or null if there is none */
  public TNode getLastChild() {
    final TNode down = (TNode)getFirstChild();
    if(down != null)
      return down.getLastSibling();
    else
      return null;
  }

  /** return the last sibling of this node, which is
      this if the next sibling is null */
  public TNode getLastSibling() {
    final TNode next = (TNode)getNextSibling();
    if(next != null)
      return next.getLastSibling();
    else
      return this;
  }

  /** return the first sibling of this node, which is
      this if the prev sibling is null */
  public TNode getFirstSibling() {
    final TNode prev = left;
    if(prev != null)
      return prev.getFirstSibling();
    else
      return this;
  }


  /** return the parent node of this node */
  public TNode getParent() {
    return getFirstSibling().up;
  }


  /** add the new node as a new sibling, inserting it ahead of any
    existing next sibling.  This method maintains double-linking.
    if node is null, nothing happens.  If the node has siblings,
    then they are added in as well.
    */
  public void addSibling(final AST node) {
    if(node == null) return;
    final TNode next = (TNode)right;
    right = (TNode)node;
    ((TNode)node).left = this;
    final TNode nodeLastSib = ((TNode)node).getLastSibling();
    nodeLastSib.right = next;
    if(next != null)
      next.left = nodeLastSib;
  }


  /** return the number of children of this node */
  public int numberOfChildren() {
    int count = 0;
    AST child = getFirstChild();
    while(child != null) {
      count++;
      child = child.getNextSibling();
    }
    return count;
  }


  /** remove this node from the tree, resetting sibling and parent
    pointers as necessary.  This method maintains double-linking */
  public void removeSelf() {
    final TNode parent = up;
    final TNode prev = left;
    final TNode next = (TNode)right;

    if(parent != null) {
      parent.down = next;
      if(next != null) {
        next.up = parent;
        next.left = prev;    // which should be null
      }
    }
    else {
     if(prev != null)
      prev.right = next;
     if(next != null)
       next.left = prev;
    }
  }


  /** return the def node for this node */
  public TNode getDefNode() {
      return defNode;
  }

  /** set the def node for this node */
  public void setDefNode(final TNode n) {
    defNode = n;
  }


  /** return a deep copy of this node, and all sub nodes.
    New tree is doubleLinked, with no parent or siblings.
    Marker value is not copied!
    */
  public TNode deepCopy() {
    final TNode copy = new TNode();
    copy.ttype = ttype;
    copy.text = text;
    copy.lineNum = lineNum;
    copy.defNode = defNode;
    if(attributes != null)
      copy.attributes = new Hashtable<String, Object>(attributes);
    if(down != null)
      copy.down = ((TNode)down).deepCopyWithRightSiblings();
    copy.doubleLink();
    return copy;
  }


  /** return a deep copy of this node, all sub nodes,
    and right siblings.
    New tree is doubleLinked, with no parent or left siblings.
    defNode is not copied  */
  public TNode deepCopyWithRightSiblings() {
    final TNode copy = new TNode();
    copy.ttype = ttype;
    copy.text = text;
    copy.lineNum = lineNum;
    copy.defNode = defNode;
    if(attributes != null)
      copy.attributes = new Hashtable<String, Object>(attributes);
    if(down != null)
      copy.down = ((TNode)down).deepCopyWithRightSiblings();
    if(right != null)
      copy.right = ((TNode)right).deepCopyWithRightSiblings();
    copy.doubleLink();
    return copy;
  }


  /** return a short string representation of the node */
  @Override
  public String toString() {
    final StringBuilder str = new StringBuilder( getNameForType(getType()) +
           "[" + getText() + ", " + "]");

     if(this.getLineNum() != 0)
       str.append(" line:" + (this.getLineNum() ) );

     final Enumeration<String> keys = (this.getAttributesTable().keys());
     while (keys.hasMoreElements()) {
       final String key = keys.nextElement();
       str.append(" " + key + ":" + (this.getAttribute(key)));
     }

    return str.toString();
  }


  /** print given tree to System.out */
  public static void printTree(final AST t) {
       if (t == null) return;
       printASTNode(t,0);
       System.out.print("\n");
  }


  /** protected method that does the work of printing */
  protected static void printASTNode(final AST t, final int indent) {
     AST child1, next;
     child1 = t.getFirstChild();

    System.out.print("\n");
     for(int i = 0; i < indent; i++)
       System.out.print("   ");

     if(child1 != null)
        System.out.print("(");

     final String s = t.getText();
     if(s != null && s.length() > 0) {
       System.out.print(getNameForType(t.getType()));
       System.out.print(": \"" + s + "\"");
     }
     else
       System.out.print(getNameForType(t.getType()));
     if(((TNode)t).getLineNum() != 0)
       System.out.print(" line:" + ((TNode)t).getLineNum() );

     final Enumeration<String> keys = ((TNode)t).getAttributesTable().keys();
     while (keys.hasMoreElements()) {
       final String key = keys.nextElement();
       System.out.print(" " + key + ":" + ((TNode)t).getAttribute(key));
     }
     final TNode def = ((TNode)t).getDefNode();
     if(def != null)
       System.out.print("[" + getNameForType(def.getType()) + "]");


     if(child1 != null) {
        printASTNode(child1,indent + 1);

        System.out.print("\n");
        for(int i = 0; i < indent; i++)
           System.out.print("   ");
        System.out.print(")");
     }

     next = t.getNextSibling();
     if(next != null) {
        printASTNode(next,indent);
     }
  }

  /** converts an int tree token type to a name.
      Does this by reflecting on nsdidl.IDLTreeTokenTypes,
      and is dependent on how ANTLR 2.00 outputs that class. */
  public static String getNameForType(final int t) {
    try{
      final Class<?> c = Class.forName(tokenVocabulary);
      final Field[] fields = c.getDeclaredFields();
      if(t-2 < fields.length)
        return fields[t-2].getName();
    } catch (final Exception e) { System.out.println(e); }
    return "unfoundtype: " + t;
  }


  /** set up reverse links between this node and its first
     child and its first sibling, and link those as well */
  public void doubleLink() {
    final TNode right = (TNode)getNextSibling();
    if(right != null) {
      right.left = this;
      right.doubleLink();
    }
    final TNode down = (TNode)getFirstChild();
    if(down != null) {
      down.up = this;
      down.doubleLink();
    }
  }

  /** find first parent of the given type,
    return null on failure */
  public TNode parentOfType(final int type) {
    if(up == null) {
      if(left == null)
        return null;
      else
        return left.parentOfType(type);
    }
    if(up.getType() == type)
      return up;
    return up.parentOfType(type);
  }

  /** find the first child of the node
    of the given type, return null on failure */
  public TNode firstChildOfType(final int type) {
    final TNode down = (TNode)getFirstChild();
    if(down == null)
      return null;
    if(down.getType() == type)
      return down;
    return down.firstSiblingOfType(type);
  }

  /** find the first sibling of the node
    of the given type, return null on failure */
  public TNode firstSiblingOfType(final int type) {
    final TNode right = (TNode)getNextSibling();
    if(right == null)
      return null;
    if(right.getType() == type)
      return right;
    return right.firstSiblingOfType(type);
  }

}