| 1 | package com.hammurapi.extract.java; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import antlr.CommonAST; |
| 7 | import antlr.Token; |
| 8 | import antlr.collections.AST; |
| 9 | |
| 10 | /** |
| 11 | * Custom AST to create canonical parse tree. |
| 12 | * @author Pavel Vlasov |
| 13 | * |
| 14 | */ |
| 15 | public class ParamsAST extends CommonAST { |
| 16 | |
| 17 | int parameterIndex = -1; |
| 18 | |
| 19 | void setParameterIndex(int parameterIndex) { |
| 20 | this.parameterIndex = parameterIndex; |
| 21 | } |
| 22 | |
| 23 | public ParamsAST() { |
| 24 | |
| 25 | } |
| 26 | |
| 27 | public ParamsAST(Token token) { |
| 28 | super(token); |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public boolean equals(AST ast) { |
| 33 | if (!(ast instanceof ParamsAST)) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | ParamsAST past = (ParamsAST) ast; |
| 38 | if (past.getType()!=getType()) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | if (parameterIndex!=past.parameterIndex) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | if (past.parameterIndex==-1 && !strcmp(getText(), past.getText())) { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | if (getNumberOfChildren()!=past.getNumberOfChildren()) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | for (AST child=getFirstChild(),otherChild=past.getFirstChild(); child!=null; child=child.getNextSibling(), otherChild=otherChild.getNextSibling()) { |
| 55 | if (!child.equals(otherChild)) { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | private boolean strcmp(String s1, String s2) { |
| 64 | return s1==s2 || (s1!=null && s1.equals(s2)); |
| 65 | } |
| 66 | |
| 67 | List<Object> getIdentity() { |
| 68 | List<Object> ret = new ArrayList<Object>(); |
| 69 | ret.add(getType()); |
| 70 | if (parameterIndex==-1) { |
| 71 | ret.add(getText()); |
| 72 | } else { |
| 73 | ret.add(parameterIndex); |
| 74 | } |
| 75 | |
| 76 | if (getFirstChild()!=null) { |
| 77 | List<List<Object>> children = new ArrayList<List<Object>>(); |
| 78 | ret.add(children); |
| 79 | for (AST child=getFirstChild(); child!=null; child=child.getNextSibling()) { |
| 80 | children.add(((ParamsAST) child).getIdentity()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return ret; |
| 85 | } |
| 86 | } |