1 19 package org.netbeans.modules.ruby; 20 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.jruby.ast.ArrayNode; 26 import org.jruby.ast.BignumNode; 27 import org.jruby.ast.CallNode; 28 import org.jruby.ast.ClassVarAsgnNode; 29 import org.jruby.ast.Colon2Node; 30 import org.jruby.ast.FalseNode; 31 import org.jruby.ast.FixnumNode; 32 import org.jruby.ast.FloatNode; 33 import org.jruby.ast.GlobalAsgnNode; 34 import org.jruby.ast.HashNode; 35 import org.jruby.ast.InstAsgnNode; 36 import org.jruby.ast.LocalAsgnNode; 37 import org.jruby.ast.NilNode; 38 import org.jruby.ast.Node; 39 import org.jruby.ast.RegexpNode; 40 import org.jruby.ast.StrNode; 41 import org.jruby.ast.SymbolNode; 42 import org.jruby.ast.TrueNode; 43 import org.jruby.ast.types.INameNode; 44 45 46 52 public class TypeAnalyzer { 53 54 private Map <String , String > types; 55 private int offset; 56 private Node root; 57 58 60 public TypeAnalyzer(Node root, int offset) { 61 this.root = root; 62 this.offset = offset; 63 } 64 65 69 private void analyze(Node node) { 70 if (node.getPosition().getStartOffset() > offset) { 76 return; 77 } 78 79 if (node instanceof LocalAsgnNode || node instanceof InstAsgnNode || 82 node instanceof GlobalAsgnNode || node instanceof ClassVarAsgnNode) { 83 String type = expressionType(node); 84 85 if (type != null) { 86 String symbol = ((INameNode)node).getName(); 87 types.put(symbol, type); 88 } 89 } 90 91 @SuppressWarnings ("unchecked") 92 List <Node> list = node.childNodes(); 93 94 for (Node child : list) { 95 analyze(child); 96 } 97 } 98 99 100 private String expressionType(Node node) { 101 @SuppressWarnings ("unchecked") 108 List <Node> list = node.childNodes(); 109 110 if (list.size() != 1) { 111 return null; 112 } 113 114 Node child = list.get(0); 115 116 if (child instanceof CallNode) { 117 CallNode call = (CallNode)child; 118 119 if ("new".equals(call.getName())) { 122 Node receiver = call.getReceiverNode(); 123 124 if (receiver instanceof Colon2Node) { 125 return AstUtilities.getFqn((Colon2Node)receiver); 126 } else if (receiver instanceof INameNode) { 127 return ((INameNode)receiver).getName(); 129 } 130 } 131 } else if (child instanceof ArrayNode) { 132 return "Array"; 133 } else if (child instanceof StrNode) { 134 return "String"; 135 } else if (child instanceof FixnumNode) { 136 return "Fixnum"; 137 } else if (child instanceof BignumNode) { 138 return "Bignum"; 139 } else if (child instanceof HashNode) { 140 return "Hash"; 141 } else if (child instanceof RegexpNode) { 142 return "Regexp"; 143 } else if (child instanceof SymbolNode) { 144 return "Symbol"; 145 } else if (child instanceof FloatNode) { 146 return "Float"; 147 } else if (child instanceof NilNode) { 148 return "NilClass"; 149 } else if (child instanceof TrueNode) { 150 return "TrueClass"; 151 } else if (child instanceof FalseNode) { 152 return "FalseClass"; 153 154 } 157 158 return null; 159 } 160 161 public String getType(String symbol) { 162 if (types == null) { 163 types = new HashMap <String , String >(); 164 analyze(root); 165 } 166 167 return types.get(symbol); 168 } 169 } 170 | Popular Tags |