1package com.jogamp.gluegen.cgram;
3import antlr.collections.AST;
7import java.lang.reflect.*;
8import java.util.Hashtable;
9import java.util.Enumeration;
11import com.jogamp.gluegen.ASTLocusTag;
12import com.jogamp.gluegen.ASTLocusTag.ASTLocusTagProvider;
13import com.jogamp.gluegen.GlueGen;
37@SuppressWarnings(
"serial")
41 protected int lineNum = 0;
45 protected boolean marker =
false;
46 protected Hashtable<String, Object> attributes =
null;
47 static String tokenVocabulary;
58 final Object s = getAttribute(
"source");
60 return new ASTLocusTag(s, getLineNum(), -1, getText());
78 setText(tok.getText());
79 setType(tok.getType());
80 setLineNum(tok.getLine());
120 if(attributes ==
null)
121 attributes =
new Hashtable<String, Object>(7);
128 if(attributes ==
null)
129 attributes =
new Hashtable<String, Object>(7);
130 attributes.put(attrName,value);
137 if(attributes ==
null)
140 return attributes.get(attrName);
152 return ((
TNode)down).getLocalLineNum();
163 return ((
TNode)right).getLocalLineNum();
165 return ((
TNode)down).getLocalLineNum();
183 static class DebugASTVisitor {
185 private String tabs(
final StringBuilder sb) {
187 for (
int i = 0; i < level; i++) {
190 return sb.toString();
192 DebugASTVisitor(
final int level) {
195 void visit(
final AST node) {
196 final StringBuilder sb =
new StringBuilder();
198 for (node2 = node; node2 !=
null; node2 = node2.getNextSibling()) {
199 if (node2.getText() ==
null) {
200 System.err.printf(
"%03d: %snil [%d]%n", level, tabs(sb), node2.getType());
202 System.err.printf(
"%03d: %s%s [%d]%n", level, tabs(sb), node2.getText(), node2.getType());
204 if (node2.getFirstChild() !=
null) {
206 visit(node2.getFirstChild());
221 System.err.println(
"TNode.XXX: "+name);
222 new DebugASTVisitor(1).visit(getFirstChild());
224 final StringBuilder buf =
new StringBuilder();
225 final TNode down = (
TNode) this.getFirstChild();
227 buf.append(this.getText());
229 getAllChildrenText(buf,
this, down);
231 return buf.toString();
233 private static void getAllChildrenText(
final StringBuilder buf,
235 boolean first =
true;
236 while(
null != thisNode ) {
237 final boolean isClosing = HeaderParserTokenTypes.RPAREN == thisNode.
getType();
238 final boolean isGroupStart = HeaderParserTokenTypes.NExpressionGroup == thisNode.
getType();
240 final TNode nextNode = (
TNode) thisNode.getNextSibling();
241 final TNode downNode = (
TNode) thisNode.getFirstChild();
243 (
null == downNode &&
null == nextNode ||
247 buf.append(
" ").append(upNode.
getText());
249 if(
null != downNode ) {
250 if( !isGroupStart ) {
253 getAllChildrenText(buf, thisNode, downNode);
254 if( !isGroupStart ) {
257 }
else if( !isClosing ) {
258 buf.append(
" ").append(thisNode.
getText());
287 final TNode prev = left;
297 return getFirstSibling().
up;
307 if(node ==
null)
return;
312 nodeLastSib.right = next;
314 next.left = nodeLastSib;
321 AST child = getFirstChild();
322 while(child !=
null) {
324 child = child.getNextSibling();
333 final TNode parent = up;
334 final TNode prev = left;
372 copy.lineNum = lineNum;
373 copy.defNode = defNode;
374 if(attributes !=
null)
375 copy.attributes =
new Hashtable<String, Object>(attributes);
377 copy.down = ((
TNode)down).deepCopyWithRightSiblings();
391 copy.lineNum = lineNum;
392 copy.defNode = defNode;
393 if(attributes !=
null)
394 copy.attributes =
new Hashtable<String, Object>(attributes);
396 copy.down = ((
TNode)down).deepCopyWithRightSiblings();
398 copy.right = ((
TNode)right).deepCopyWithRightSiblings();
407 final StringBuilder str =
new StringBuilder( getNameForType(getType()) +
408 "[" + getText() +
", " +
"]");
410 if(this.getLineNum() != 0)
411 str.append(
" line:" + (this.getLineNum() ) );
413 final Enumeration<String> keys = (this.getAttributesTable().keys());
414 while (keys.hasMoreElements()) {
415 final String key = keys.nextElement();
416 str.append(
" " + key +
":" + (this.getAttribute(key)));
419 return str.toString();
425 if (t ==
null)
return;
427 System.out.print(
"\n");
434 child1 = t.getFirstChild();
436 System.out.print(
"\n");
437 for(
int i = 0; i < indent; i++)
438 System.out.print(
" ");
441 System.out.print(
"(");
443 final String s = t.getText();
444 if(s !=
null && s.length() > 0) {
445 System.out.print(getNameForType(t.getType()));
446 System.out.print(
": \"" + s +
"\"");
449 System.out.print(getNameForType(t.getType()));
450 if(((
TNode)t).getLineNum() != 0)
451 System.out.print(
" line:" + ((
TNode)t).getLineNum() );
453 final Enumeration<String> keys = ((
TNode)t).getAttributesTable().keys();
454 while (keys.hasMoreElements()) {
455 final String key = keys.nextElement();
456 System.out.print(
" " + key +
":" + ((
TNode)t).getAttribute(key));
460 System.out.print(
"[" + getNameForType(def.
getType()) +
"]");
464 printASTNode(child1,indent + 1);
466 System.out.print(
"\n");
467 for(
int i = 0; i < indent; i++)
468 System.out.print(
" ");
469 System.out.print(
")");
472 next = t.getNextSibling();
474 printASTNode(next,indent);
483 final Class<?> c = Class.forName(tokenVocabulary);
484 final Field[] fields = c.getDeclaredFields();
485 if(t-2 < fields.length)
487 }
catch (
final Exception e) { System.out.println(e); }
488 return "unfoundtype: " + t;
Glue code generator for C functions and data structures.
Class TNode is an implementation of the AST interface and adds many useful features:
void setDefNode(final TNode n)
set the def node for this node
TNode getParent()
return the parent node of this node
void initialize(final Token token)
TNode deepCopyWithRightSiblings()
return a deep copy of this node, all sub nodes, and right siblings.
int getType()
Get the token type for this node.
void setText(final String text_)
Set the token text for this node.
ASTLocusTag getASTLocusTag()
Returns this instance's ASTLocusTag, if available, otherwise returns null.
TNode parentOfType(final int type)
find first parent of the given type, return null on failure
static void printTree(final AST t)
print given tree to System.out
TNode getLastSibling()
return the last sibling of this node, which is this if the next sibling is null
void initialize(final AST tr)
String getAllChildrenText(final String name)
Returns the text for this node, its children and siblings.
static void setTokenVocabulary(final String s)
Set the token vocabulary to a tokentypes class generated by antlr.
void doubleLink()
set up reverse links between this node and its first child and its first sibling, and link those as w...
TNode firstSiblingOfType(final int type)
find the first sibling of the node of the given type, return null on failure
TNode firstChildOfType(final int type)
find the first child of the node of the given type, return null on failure
int numberOfChildren()
return the number of children of this node
TNode getFirstSibling()
return the first sibling of this node, which is this if the prev sibling is null
int getLineNum()
Get the line number for this node.
void setLineNum(final int lineNum_)
Set the line number for this node.
TNode deepCopy()
return a deep copy of this node, and all sub nodes.
void setAttribute(final String attrName, final Object value)
set an attribute in the attribute table.
String getText()
Get the token text for this node.
static String getNameForType(final int t)
converts an int tree token type to a name.
void removeSelf()
remove this node from the tree, resetting sibling and parent pointers as necessary.
void addSibling(final AST node)
add the new node as a new sibling, inserting it ahead of any existing next sibling.
TNode getDefNode()
return the def node for this node
Hashtable< String, Object > getAttributesTable()
get the hashtable that holds attribute values.
Object getAttribute(final String attrName)
lookup the attribute name in the attribute table.
boolean getMarker()
Get the marker value for this node.
void setType(final int ttype_)
Set the token type for this node.
static void printASTNode(final AST t, final int indent)
protected method that does the work of printing
String toString()
return a short string representation of the node
TNode getLastChild()
return the last child of this node, or null if there is none
void setMarker(final boolean marker_)
Set the marker value for this node.
Represents a field in a struct or union.
String getName()
Name of this field in the containing data structure.
Interface tag for ASTLocusTag provider.