Return the index 0..n-1 of t as child of t.parent. If t is index 0, always return 0. If t is a repeated subtree root and index within sibling list > 0, return CHILD_INDEX_LIST_ELEMENT. In all other cases, return the actual index of t. That means for a sibling list starting at child index 5, the
(ParseTree t)
| 959 | * 5 but 2nd and beyond in list will return CHILD_INDEX_LIST_ELEMENT. |
| 960 | */ |
| 961 | public static int getChildIndexOrListMembership(ParseTree t) { |
| 962 | if ( t==null ) return -1; |
| 963 | ParseTree parent = t.getParent(); |
| 964 | if ( parent==null ) { |
| 965 | return -1; |
| 966 | } |
| 967 | // we know we have a parent now |
| 968 | // check to see if we are 2nd or beyond element in a sibling list |
| 969 | if ( t instanceof ParserRuleContext ) { |
| 970 | List<ParserRuleContext> siblings = |
| 971 | ((ParserRuleContext)parent).getRuleContexts(((ParserRuleContext)t).getClass()); |
| 972 | if ( siblings.size()>1 && siblings.indexOf(t)>0 ) { |
| 973 | return CHILD_INDEX_REPEATED_ELEMENT; |
| 974 | } |
| 975 | } |
| 976 | // check to see if we are 2nd or beyond repeated token |
| 977 | if ( t instanceof TerminalNode ) { |
| 978 | List<TerminalNode> repeatedTokens = |
| 979 | ((ParserRuleContext) parent).getTokens(((TerminalNode) t).getSymbol().getType()); |
| 980 | if ( repeatedTokens.size()>1 && repeatedTokens.indexOf(t)>0 ) { |
| 981 | return CHILD_INDEX_REPEATED_ELEMENT; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | return getChildIndex(t); |
| 986 | } |
| 987 | |
| 988 | public static int getChildIndex(ParseTree t) { |
| 989 | if ( t==null ) return -1; |
no test coverage detected