public String toStringScopeNames() { StringBuilder sb = new StringBuilder(); sb.append("Scopes:\n"); int i = 0; for (Scope scope = this; scope != null; scope = scope.getParent()) sb.append("\t" + (i++) + ": " + scope.getScopeName() + "\n"); return sb.toString(); }
/** Find all functions whose names are 'functionName' */ public List<ScopeSymbol> getFunctions(String functionName) { List<ScopeSymbol> funcs = new ArrayList<ScopeSymbol>(); for (Scope scope = this; scope != null; scope = scope.parent) { List<ScopeSymbol> fs = scope.getFunctionsLocal(functionName); if (fs != null) funcs.addAll(fs); } return funcs; }
/** Copy symbols from other scope */ void copy(Scope oscope) { if (oscope == null) return; // Note: Symbols are added at variable declaration time (i.e. when we evaluate a // 'VarDeclaration') // So we should not copy them here (otherwise we'd get 'Duplicate symbol error') // Copy functions if (oscope.hasFunctions()) { for (ScopeSymbol ss : oscope.getFunctions()) add(ss); } }
public String toString(boolean showFunc) { // Show parents StringBuilder sb = new StringBuilder(); if (parent != null) { String parentStr = parent.toString(showFunc); if (!parentStr.isEmpty()) sb.append(parentStr); } // Show scope symbols StringBuilder sbThis = new StringBuilder(); ArrayList<ScopeSymbol> ssyms = new ArrayList<ScopeSymbol>(); ssyms.addAll(symbols.values()); Collections.sort(ssyms); for (ScopeSymbol ss : ssyms) sbThis.append(ss + "\n"); // Show scope functions if (showFunc && functions != null) { for (String fname : functions.keySet()) for (ScopeSymbol ss : functions.get(fname)) sbThis.append(ss + "\n"); } // Show header if (sbThis.length() > 0) sb.append("\n---------- Scope " + getScopeName() + " ----------\n" + sbThis.toString()); return sb.toString(); }
/** Get symbol on this scope (or any parent scope) */ public ScopeSymbol getSymbol(String symbol) { // Find symbol on this or any parent scope for (Scope scope = this; scope != null; scope = scope.parent) { // Try to find a symbol ScopeSymbol ssym = scope.getSymbolLocal(symbol); if (ssym != null) return ssym; // Try a function List<ScopeSymbol> fs = scope.getFunctionsLocal(symbol); // Since we are only matching by name, there has to be one // and only one function with that name // Note, this is limiting and very naive. A better approach is needed if (fs != null && fs.size() == 1) return fs.get(0); } // Nothing found return null; }
@Override public String serializeSave(BdsSerializer serializer) { StringBuilder out = new StringBuilder(); out.append("Scope"); out.append("\t" + serializer.serializeSaveValue(id)); out.append("\t" + serializer.serializeSaveValue(parent != null ? parent.getNodeId() : "")); out.append("\t" + serializer.serializeSaveValue(node)); out.append("\n"); for (ScopeSymbol ss : symbols.values()) { if (ss.getType().isNative()) {; // Do not save native functions } else out.append(serializer.serializeSave(ss)); } if (parent != null) out.append(serializer.serializeSave(parent)); return out.toString(); }