GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
CSymbolTable.java
Go to the documentation of this file.
1package com.jogamp.gluegen.cgram;
2
3import java.util.Vector;
4import java.util.Hashtable;
5import java.util.Enumeration;
6
7
8public class CSymbolTable {
9
10 /** holds list of scopes */
11 private final Vector<String> scopeStack;
12
13 /** table where all defined names are mapped to TNode tree nodes */
14 private final Hashtable<String, TNode> symTable;
15
16 public CSymbolTable() {
17 scopeStack = new Vector<String>(10);
18 symTable = new Hashtable<String, TNode>(533);
19 }
20
21
22 /** push a new scope onto the scope stack.
23 */
24 public void pushScope(final String s) {
25 //System.out.println("push scope:" + s);
26 scopeStack.addElement(s);
27 }
28
29 /** pop the last scope off the scope stack.
30 */
31 public void popScope() {
32 //System.out.println("pop scope");
33 final int size = scopeStack.size();
34 if(size > 0)
35 scopeStack.removeElementAt(size - 1);
36 }
37
38 /** return the current scope as a string
39 */
40 public String currentScopeAsString() {
41 final StringBuilder buf = new StringBuilder(100);
42 boolean first = true;
43 final Enumeration<String> e = scopeStack.elements();
44 while(e.hasMoreElements()) {
45 if(first)
46 first = false;
47 else
48 buf.append("::");
49 buf.append(e.nextElement().toString());
50 }
51 return buf.toString();
52 }
53
54 /** given a name for a type, append it with the
55 current scope.
56 */
57 public String addCurrentScopeToName(final String name) {
58 final String currScope = currentScopeAsString();
59 return addScopeToName(currScope, name);
60 }
61
62 /** given a name for a type, append it with the
63 given scope. MBZ
64 */
65 public String addScopeToName(final String scope, final String name) {
66 if(scope == null || scope.length() > 0)
67 return scope + "::" + name;
68 else
69 return name;
70 }
71
72 /** remove one level of scope from name MBZ*/
73 public String removeOneLevelScope(final String scopeName) {
74 final int index = scopeName.lastIndexOf("::");
75 if (index > 0) {
76 return scopeName.substring(0,index);
77 }
78 if (scopeName.length() > 0) {
79 return "";
80 }
81 return null;
82 }
83
84 /** add a node to the table with it's key as
85 the current scope and the name */
86 public TNode add(final String name, final TNode node) {
87 return symTable.put(addCurrentScopeToName(name),node);
88 }
89
90
91 /** lookup a fully scoped name in the symbol table */
92 public TNode lookupScopedName(final String scopedName) {
93 return symTable.get(scopedName);
94 }
95
96 /** lookup an unscoped name in the table by prepending
97 the current scope.
98 MBZ -- if not found, pop scopes and look again
99 */
100 public TNode lookupNameInCurrentScope(final String name) {
101 String scope = currentScopeAsString();
102 String scopedName;
103 TNode tnode = null;
104
105 //System.out.println( "\n"+ this.toString() );
106
107 while (tnode == null && scope != null) {
108 scopedName = addScopeToName(scope, name);
109 //System.out.println("lookup trying " + scopedName);
110 tnode = symTable.get(scopedName);
111 scope = removeOneLevelScope(scope);
112 }
113 return tnode;
114 }
115
116 /** convert this table to a string */
117 @Override
118 public String toString() {
119 final StringBuilder buff = new StringBuilder(300);
120 buff.append("CSymbolTable { \nCurrentScope: " + currentScopeAsString() +
121 "\nDefinedSymbols:\n");
122 final Enumeration<String> ke = symTable.keys();
123 final Enumeration<TNode> ve = symTable.elements();
124 while(ke.hasMoreElements()) {
125 buff.append(ke.nextElement().toString());
126 buff.append(" (").append(TNode.getNameForType(ve.nextElement().getType())).append(")\n");
127 }
128 buff.append("}\n");
129 return buff.toString();
130 }
131
132}
String addScopeToName(final String scope, final String name)
given a name for a type, append it with the given scope.
void popScope()
pop the last scope off the scope stack.
TNode add(final String name, final TNode node)
add a node to the table with it's key as the current scope and the name
TNode lookupNameInCurrentScope(final String name)
lookup an unscoped name in the table by prepending the current scope.
String addCurrentScopeToName(final String name)
given a name for a type, append it with the current scope.
TNode lookupScopedName(final String scopedName)
lookup a fully scoped name in the symbol table
String removeOneLevelScope(final String scopeName)
remove one level of scope from name MBZ
void pushScope(final String s)
push a new scope onto the scope stack.
String toString()
convert this table to a string
String currentScopeAsString()
return the current scope as a string
Class TNode is an implementation of the AST interface and adds many useful features:
Definition: TNode.java:38
static String getNameForType(final int t)
converts an int tree token type to a name.
Definition: TNode.java:481