Skip to content

Commit 87590aa

Browse files
committed
cleanup
1 parent a901a00 commit 87590aa

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,29 @@
2424
import java.util.Map;
2525

2626
public class CollectFeatures {
27+
public static final double MAX_CONTEXT_DIFF_THRESHOLD = 0.6;
28+
2729
// Feature values for pair on diff lines feature
2830
public static final int NOT_PAIR = -1;
2931
public static final int PAIR_ON_SAME_LINE = 0;
3032
public static final int PAIR_ON_DIFF_LINE = 1;
3133

3234
// Categories for alignment/indentation
3335
public static final int CAT_NO_ALIGNMENT = 0;
34-
public static final int CAT_INDENT = 1;
35-
public static final int CAT_ALIGN_WITH_ANCESTOR_FIRST_TOKEN = 2;
36-
public static final int CAT_ALIGN_WITH_ANCESTORS_PARENT_FIRST_TOKEN = 3;
37-
public static final int CAT_ALIGN_WITH_LIST_FIRST_ELEMENT = 4;
38-
public static final int CAT_ALIGN_WITH_PAIR = 5;
36+
public static final int CAT_ALIGN_WITH_ANCESTOR_FIRST_TOKEN = 1;
37+
public static final int CAT_ALIGN_WITH_ANCESTORS_PARENT_FIRST_TOKEN = 2;
38+
public static final int CAT_ALIGN_WITH_LIST_FIRST_ELEMENT = 3;
39+
public static final int CAT_ALIGN_WITH_PAIR = 4;
3940

40-
public static final double MAX_CONTEXT_DIFF_THRESHOLD = 0.6;
41+
public static final int CAT_INDENT = 100;
4142

4243
public static final int INDEX_PREV2_TYPE = 0;
4344
public static final int INDEX_PREV_TYPE = 1;
4445
public static final int INDEX_PREV_RULE = 2; // what rule is prev token in?
4546
public static final int INDEX_PREV_END_COLUMN = 3;
4647
public static final int INDEX_PREV_EARLIEST_ANCESTOR = 4;
4748
public static final int INDEX_TYPE = 5;
48-
public static final int INDEX_FIRST_EL_OF_LIST = 6;
49+
public static final int INDEX_FIRST_EL_OF_LIST = 6; // TODO: I don't think we can detect first element of list
4950
public static final int INDEX_MATCHING_TOKEN_DIFF_LINE = 7;
5051
public static final int INDEX_FIRST_ON_LINE = 8; // a \n right before this token?
5152
public static final int INDEX_RULE = 9; // what rule are we in?
@@ -475,6 +476,7 @@ public static int[] getNodeFeatures(Map<Token, TerminalNode> tokenToNodeMap,
475476
sumEndColAndAncestorWidth = prevTokenEndCharPos+earliestAncestorWidth;
476477
}
477478

479+
// TODO: I don't think we can detect first element of list
478480
boolean startOfList = isFirstSiblingOfList(tokenToNodeMap, curToken);
479481

480482
boolean curTokenStartsNewLine = window.get(2).getLine()>window.get(1).getLine();
@@ -779,4 +781,18 @@ public static List<Token> getRealTokens(CommonTokenStream tokens) {
779781
}
780782
return real;
781783
}
784+
785+
/** Same as p.getParent() except we scan through chain rule nodes */
786+
public static ParserRuleContext getParent(ParserRuleContext p) {
787+
if ( p==null ) return null;
788+
ParserRuleContext lastValidParent = p.getParent();
789+
// now try to walk chain rules starting with the parent of the usual parent
790+
ParserRuleContext q = lastValidParent.getParent();
791+
while ( q!=null && q.getChildCount()==1 ) { // while is a chain rule
792+
lastValidParent = q;
793+
q = q.getParent();
794+
}
795+
return lastValidParent;
796+
}
797+
782798
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class T {
2+
public TokenPositionAnalysis getTokenAnalysis(int[] features,
3+
int indexIntoRealTokens,
4+
int tokenIndexInStream,
5+
int injectNewline,
6+
int alignWithPrevious,
7+
int indent,
8+
int ws)
9+
{
10+
CommonToken curToken = foo;
11+
CommonToken prevToken = 1;
12+
if ( ws==0 && cannotJoin(realTokens.get(indexIntoRealTokens-1), curToken) ) {
13+
ws = 1;
14+
failsafeTriggered = true;
15+
}
16+
}
17+
public static boolean cannotJoin(Token prevToken, Token curToken) {
18+
String prevTokenText = prevToken.getText();
19+
char prevLastChar = prevTokenText.charAt(prevTokenText.length()-1);
20+
String curTokenText = curToken.getText();
21+
char curFirstChar = curTokenText.charAt(0);
22+
return Character.isLetterOrDigit(prevLastChar) &&
23+
Character.isLetterOrDigit(curFirstChar);
24+
}
25+
}

0 commit comments

Comments
 (0)