11package org .antlr .codebuff ;
22
3+ import org .antlr .codebuff .misc .HashBag ;
34import org .antlr .v4 .runtime .CommonToken ;
45import org .antlr .v4 .runtime .CommonTokenStream ;
56import org .antlr .v4 .runtime .ParserRuleContext ;
2324import static org .antlr .codebuff .CollectFeatures .INDEX_FIRST_ON_LINE ;
2425import static org .antlr .codebuff .CollectFeatures .INDEX_PREV_END_COLUMN ;
2526import static org .antlr .codebuff .CollectFeatures .MAX_CONTEXT_DIFF_THRESHOLD ;
27+ import static org .antlr .codebuff .CollectFeatures .MAX_CONTEXT_DIFF_THRESHOLD2 ;
2628import static org .antlr .codebuff .CollectFeatures .earliestAncestorStartingWithToken ;
2729import static org .antlr .codebuff .CollectFeatures .getNodeFeatures ;
2830import static org .antlr .codebuff .CollectFeatures .getRealTokens ;
@@ -124,7 +126,14 @@ public void processToken(int indexIntoRealTokens, int tokenIndexInStream) {
124126 features [INDEX_FIRST_ON_LINE ] = injectNewline ; // use \n prediction to match exemplars for alignment
125127
126128 int align = alignClassifier .classify (k , features , corpus .align , MAX_CONTEXT_DIFF_THRESHOLD );
127- //indentClassifier.classify(k, features, corpus.indent, CollectFeatures.MAX_CONTEXT_DIFF_THRESHOLD);
129+ if ( align ==CAT_NO_ALIGNMENT ) {
130+ HashBag <Integer > votes = alignClassifier .votes (k , features , corpus .align , MAX_CONTEXT_DIFF_THRESHOLD );
131+ if ( votes .size ()==0 ) {
132+ // try with less strict match threshold to get some indication of alignment
133+ align = alignClassifier .classify (k , features , corpus .align , MAX_CONTEXT_DIFF_THRESHOLD2 );
134+ }
135+ }
136+
128137 int ws = wsClassifier .classify (k , features , corpus .injectWS , MAX_CONTEXT_DIFF_THRESHOLD );
129138
130139 TokenPositionAnalysis tokenPositionAnalysis =
@@ -150,57 +159,51 @@ public void processToken(int indexIntoRealTokens, int tokenIndexInStream) {
150159 TerminalNode node = tokenToNodeMap .get (curToken );
151160 ParserRuleContext parent = (ParserRuleContext )node .getParent ();
152161
153- switch ( align ) {
154- case CAT_INDENT :
155- if ( firstTokenOnPrevLine !=null ) { // if not on first line, we can indent indent
156- int indentedCol = firstTokenOnPrevLine .getCharPositionInLine () + INDENT_LEVEL ;
157- charPosInLine = indentedCol ;
158- output .append (Tool .spaces (indentedCol ));
159- }
160- break ;
161- case CAT_NO_ALIGNMENT :
162- break ;
163-
164- default :
165- if ( (align &0xFF )==CAT_ALIGN_WITH_ANCESTOR_CHILD ) {
166- int [] deltaChild = CollectFeatures .unaligncat (align );
167- int deltaFromAncestor = deltaChild [0 ];
168- int childIndex = deltaChild [1 ];
169- ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (parent , curToken );
170- if ( earliestLeftAncestor ==null ) {
171- earliestLeftAncestor = parent ;
172- }
173- ParserRuleContext ancestor = CollectFeatures .getAncestor (earliestLeftAncestor , deltaFromAncestor );
174- ParseTree child = ancestor .getChild (childIndex );
175- Token start = null ;
176- if ( child instanceof ParserRuleContext ) {
177- start = ((ParserRuleContext ) child ).getStart ();
178- }
179- else if ( child instanceof TerminalNode ){
180- start = ((TerminalNode )child ).getSymbol ();
181- }
182- else {
183- // uh oh.
184- System .err .println ("Whoops. Tried access invalid child" );
185- }
186- if ( start !=null ) {
187- int indentCol = start .getCharPositionInLine ();
188- charPosInLine = indentCol ;
189- output .append (Tool .spaces (indentCol ));
190- }
191- }
192- else if ( (align &0xFF )==CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ) {
193- int deltaFromAncestor = CollectFeatures .unindentcat (align );
194- ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (parent , curToken );
195- if ( earliestLeftAncestor ==null ) {
196- earliestLeftAncestor = parent ;
197- }
198- ParserRuleContext ancestor = CollectFeatures .getAncestor (earliestLeftAncestor , deltaFromAncestor );
199- Token start = ancestor .getStart ();
200- int indentCol = start .getCharPositionInLine () + INDENT_LEVEL ;
201- charPosInLine = indentCol ;
202- output .append (Tool .spaces (indentCol ));
203- }
162+ if ( align ==CAT_INDENT ) {
163+ if ( firstTokenOnPrevLine !=null ) { // if not on first line, we can indent indent
164+ int indentedCol = firstTokenOnPrevLine .getCharPositionInLine ()+INDENT_LEVEL ;
165+ charPosInLine = indentedCol ;
166+ output .append (Tool .spaces (indentedCol ));
167+ }
168+ }
169+ else if ( (align &0xFF )==CAT_ALIGN_WITH_ANCESTOR_CHILD ) {
170+ int [] deltaChild = CollectFeatures .unaligncat (align );
171+ int deltaFromAncestor = deltaChild [0 ];
172+ int childIndex = deltaChild [1 ];
173+ ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (parent , curToken );
174+ if ( earliestLeftAncestor ==null ) {
175+ earliestLeftAncestor = parent ;
176+ }
177+ ParserRuleContext ancestor = CollectFeatures .getAncestor (earliestLeftAncestor , deltaFromAncestor );
178+ ParseTree child = ancestor .getChild (childIndex );
179+ Token start = null ;
180+ if ( child instanceof ParserRuleContext ) {
181+ start = ((ParserRuleContext ) child ).getStart ();
182+ }
183+ else if ( child instanceof TerminalNode ){
184+ start = ((TerminalNode )child ).getSymbol ();
185+ }
186+ else {
187+ // uh oh.
188+ System .err .println ("Whoops. Tried access invalid child" );
189+ }
190+ if ( start !=null ) {
191+ int indentCol = start .getCharPositionInLine ();
192+ charPosInLine = indentCol ;
193+ output .append (Tool .spaces (indentCol ));
194+ }
195+ }
196+ else if ( (align &0xFF )==CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ) {
197+ int deltaFromAncestor = CollectFeatures .unindentcat (align );
198+ ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (parent , curToken );
199+ if ( earliestLeftAncestor ==null ) {
200+ earliestLeftAncestor = parent ;
201+ }
202+ ParserRuleContext ancestor = CollectFeatures .getAncestor (earliestLeftAncestor , deltaFromAncestor );
203+ Token start = ancestor .getStart ();
204+ int indentCol = start .getCharPositionInLine () + INDENT_LEVEL ;
205+ charPosInLine = indentCol ;
206+ output .append (Tool .spaces (indentCol ));
204207 }
205208 }
206209 else {
0 commit comments