Skip to content

Commit a261d9e

Browse files
committed
add child index for immediate left ancestor. Made big difference when distinguishing lists of stuff in grammar rules of ANTLRv4Parser.g4. Both java and grammars seem good with these features.
1 parent 740ecbd commit a261d9e

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

java/src/org/antlr/codebuff/CollectFeatures.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,21 @@ public class CollectFeatures {
8181
public static final int INDEX_MATCHING_TOKEN_DIFF_LINE = 3;
8282
public static final int INDEX_FIRST_ON_LINE = 4; // a \n right before this token?
8383
public static final int INDEX_EARLIEST_LEFT_ANCESTOR = 5;
84-
public static final int INDEX_ANCESTORS_PARENT_RULE = 6;
85-
public static final int INDEX_ANCESTORS_PARENT_CHILD_INDEX = 7;
86-
public static final int INDEX_ANCESTORS_PARENT2_RULE = 8;
87-
public static final int INDEX_ANCESTORS_PARENT2_CHILD_INDEX = 9;
88-
public static final int INDEX_ANCESTORS_PARENT3_RULE = 10;
89-
public static final int INDEX_ANCESTORS_PARENT3_CHILD_INDEX = 11;
90-
public static final int INDEX_ANCESTORS_PARENT4_RULE = 12;
91-
public static final int INDEX_ANCESTORS_PARENT4_CHILD_INDEX = 13;
92-
93-
public static final int INDEX_INFO_FILE = 14;
94-
public static final int INDEX_INFO_LINE = 15;
95-
public static final int INDEX_INFO_CHARPOS = 16;
96-
97-
public static final int NUM_FEATURES = 17;
84+
public static final int INDEX_ANCESTORS_CHILD_INDEX = 6;
85+
public static final int INDEX_ANCESTORS_PARENT_RULE = 7;
86+
public static final int INDEX_ANCESTORS_PARENT_CHILD_INDEX = 8;
87+
public static final int INDEX_ANCESTORS_PARENT2_RULE = 9;
88+
public static final int INDEX_ANCESTORS_PARENT2_CHILD_INDEX = 10;
89+
public static final int INDEX_ANCESTORS_PARENT3_RULE = 11;
90+
public static final int INDEX_ANCESTORS_PARENT3_CHILD_INDEX = 12;
91+
public static final int INDEX_ANCESTORS_PARENT4_RULE = 13;
92+
public static final int INDEX_ANCESTORS_PARENT4_CHILD_INDEX = 14;
93+
94+
public static final int INDEX_INFO_FILE = 15;
95+
public static final int INDEX_INFO_LINE = 16;
96+
public static final int INDEX_INFO_CHARPOS = 17;
97+
98+
public static final int NUM_FEATURES = 18;
9899

99100
// public static final int INDEX_RULE = 8; // what rule are we in?
100101
// public static final int INDEX_EARLIEST_RIGHT_ANCESTOR = 9;
@@ -117,6 +118,7 @@ public class CollectFeatures {
117118
FeatureMetaData.UNUSED,
118119
new FeatureMetaData(FeatureType.RULE, new String[] {"LT(1)", "left ancestor"}, 1),
119120
// these previous 5 features seem to predict newline really well. whitespace ok too
121+
new FeatureMetaData(FeatureType.INT, new String[] {"ancestor", "child index"}, 1),
120122
FeatureMetaData.UNUSED,
121123
FeatureMetaData.UNUSED,
122124
FeatureMetaData.UNUSED,
@@ -137,6 +139,7 @@ public class CollectFeatures {
137139
new FeatureMetaData(FeatureType.BOOL, new String[] {"Pair", "dif\\n"}, 1),
138140
new FeatureMetaData(FeatureType.BOOL, new String[] {"Strt", "line"}, 1),
139141
new FeatureMetaData(FeatureType.RULE, new String[] {"LT(1)", "left ancestor"}, 1),
142+
new FeatureMetaData(FeatureType.INT, new String[] {"ancestor", "child index"}, 1),
140143
new FeatureMetaData(FeatureType.RULE, new String[] {"", "parent"}, 1),
141144
new FeatureMetaData(FeatureType.INT, new String[] {"parent", "child index"}, 1),
142145
new FeatureMetaData(FeatureType.RULE, new String[] {"", "parent^2"}, 1),
@@ -157,6 +160,7 @@ public class CollectFeatures {
157160
new FeatureMetaData(FeatureType.BOOL, new String[] {"Pair", "dif\\n"}, 1),
158161
new FeatureMetaData(FeatureType.BOOL, new String[] {"Strt", "line"}, 1),
159162
new FeatureMetaData(FeatureType.RULE, new String[] {"LT(1)", "left ancestor"}, 1),
163+
new FeatureMetaData(FeatureType.INT, new String[] {"ancestor", "child index"}, 1),
160164
new FeatureMetaData(FeatureType.RULE, new String[] {"", "parent"}, 1),
161165
new FeatureMetaData(FeatureType.INT, new String[] {"parent", "child index"}, 1),
162166
new FeatureMetaData(FeatureType.RULE, new String[] {"", "parent^2"}, 1),
@@ -490,6 +494,7 @@ public static int[] getNodeFeatures(Map<Token, TerminalNode> tokenToNodeMap,
490494
matchingSymbolOnDiffLine,
491495
curTokenStartsNewLine ? 1 : 0,
492496
rulealt(earliestLeftAncestor.getRuleIndex(),earliestLeftAncestor.getAltNumber()),
497+
getChildIndex(node),
493498
earliestLeftAncestorParent!=null ? rulealt(earliestLeftAncestorParent.getRuleIndex(), earliestLeftAncestorParent.getAltNumber()) : -1,
494499
getChildIndex(earliestLeftAncestor),
495500
earliestLeftAncestorParent2!=null ? rulealt(earliestLeftAncestorParent2.getRuleIndex(), earliestLeftAncestorParent2.getAltNumber()) : -1,
@@ -761,17 +766,19 @@ public static ParserRuleContext getParent(TerminalNode p) {
761766
return parentClosure((ParserRuleContext)p.getParent());
762767
}
763768

764-
public static int getChildIndex(ParserRuleContext t) {
769+
public static int getChildIndex(ParseTree t) {
765770
if ( t==null ) return -1;
766-
ParserRuleContext parent = t.getParent();
771+
ParseTree parent = t.getParent();
767772
if ( parent==null ) {
768773
return -1;
769774
}
770775
// we know we have a parent now
771776
// check to see if we are 2nd or beyond element in a sibling list
772-
List<ParserRuleContext> siblings = parent.getRuleContexts(t.getClass());
773-
if ( siblings.size()>1 && siblings.indexOf(t)>0 ) {
774-
return CHILD_INDEX_LIST_ELEMENT;
777+
if ( t instanceof ParserRuleContext ) {
778+
List<ParserRuleContext> siblings = ((ParserRuleContext)parent).getRuleContexts(((ParserRuleContext)t).getClass());
779+
if ( siblings.size()>1 && siblings.indexOf(t)>0 ) {
780+
return CHILD_INDEX_LIST_ELEMENT;
781+
}
775782
}
776783
// Either first of sibling list or not in a list.
777784
// Figure out which child index t is of parent

0 commit comments

Comments
 (0)