MCPcopy Create free account
hub / github.com/antlr/codebuff / getContextFeatures

Method getContextFeatures

src/org/antlr/codebuff/Trainer.java:546–611  ·  view source on GitHub ↗

Get the token type and tree ancestor features. These are computed the same for both training and formatting.

(Corpus corpus,
	                                       Map<Token, TerminalNode> tokenToNodeMap,
	                                       InputDocument doc,
	                                       int i)

Source from the content-addressed store, hash-verified

544 * the same for both training and formatting.
545 */
546 public static int[] getContextFeatures(Corpus corpus,
547 Map<Token, TerminalNode> tokenToNodeMap,
548 InputDocument doc,
549 int i)
550 {
551 int[] features = new int[NUM_FEATURES];
552 CodeBuffTokenStream tokens = doc.tokens;
553 TerminalNode node = tokenToNodeMap.get(tokens.get(i));
554 if ( node==null ) {
555 System.err.println("### No node associated with token "+tokens.get(i));
556 return features;
557 }
558 Token curToken = node.getSymbol();
559
560 // Get context information for previous token
561 Token prevToken = tokens.getPreviousRealToken(i);
562 TerminalNode prevNode = tokenToNodeMap.get(prevToken);
563
564 ParserRuleContext prevEarliestRightAncestor = earliestAncestorEndingWithToken(prevNode);
565 int prevEarliestAncestorRuleIndex = prevEarliestRightAncestor.getRuleIndex();
566 int prevEarliestAncestorRuleAltNum = prevEarliestRightAncestor.getAltNumber();
567
568 // Get context information for current token
569 ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken(node);
570
571 ParserRuleContext earliestLeftAncestorParent =
572 earliestLeftAncestor!=null ? earliestLeftAncestor.getParent() : null;
573
574 ParserRuleContext earliestLeftAncestorParent2 =
575 earliestLeftAncestorParent!=null ? earliestLeftAncestorParent.getParent() : null;
576
577 ParserRuleContext earliestLeftAncestorParent3 =
578 earliestLeftAncestorParent2!=null ? earliestLeftAncestorParent2.getParent() : null;
579
580 ParserRuleContext earliestLeftAncestorParent4 =
581 earliestLeftAncestorParent3!=null ? earliestLeftAncestorParent3.getParent() : null;
582
583 ParserRuleContext earliestLeftAncestorParent5 =
584 earliestLeftAncestorParent4!=null ? earliestLeftAncestorParent4.getParent() : null;
585
586 features[INDEX_PREV_TYPE] = prevToken.getType();
587 features[INDEX_PREV_EARLIEST_RIGHT_ANCESTOR] = rulealt(prevEarliestAncestorRuleIndex,prevEarliestAncestorRuleAltNum);
588 features[INDEX_CUR_TOKEN_TYPE] = curToken.getType();
589 features[INDEX_CUR_TOKEN_CHILD_INDEX] = getChildIndexOrListMembership(node);
590 features[INDEX_EARLIEST_LEFT_ANCESTOR] = rulealt(earliestLeftAncestor);
591 features[INDEX_ANCESTORS_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestor);
592 features[INDEX_ANCESTORS_PARENT_RULE] = earliestLeftAncestorParent!=null ? rulealt(earliestLeftAncestorParent) : -1;
593 features[INDEX_ANCESTORS_PARENT_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestorParent);
594 features[INDEX_ANCESTORS_PARENT2_RULE] = earliestLeftAncestorParent2!=null ? rulealt(earliestLeftAncestorParent2) : -1;
595 features[INDEX_ANCESTORS_PARENT2_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestorParent2);
596 features[INDEX_ANCESTORS_PARENT3_RULE] = earliestLeftAncestorParent3!=null ? rulealt(earliestLeftAncestorParent3) : -1;
597 features[INDEX_ANCESTORS_PARENT3_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestorParent3);
598 features[INDEX_ANCESTORS_PARENT4_RULE] = earliestLeftAncestorParent4!=null ? rulealt(earliestLeftAncestorParent4) : -1;
599 features[INDEX_ANCESTORS_PARENT4_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestorParent4);
600 features[INDEX_ANCESTORS_PARENT5_RULE] = earliestLeftAncestorParent5!=null ? rulealt(earliestLeftAncestorParent5) : -1;
601 features[INDEX_ANCESTORS_PARENT5_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestorParent5);
602
603 features[INDEX_MATCHING_TOKEN_STARTS_LINE] = getMatchingSymbolStartsLine(corpus, doc, node);

Callers 2

getFeaturesMethod · 0.95
getFeaturesMethod · 0.80

Calls 13

getPreviousRealTokenMethod · 0.95
rulealtMethod · 0.95
getAltNumberMethod · 0.80
getCharPositionInLineMethod · 0.80
getMethod · 0.65
getParentMethod · 0.45
getTypeMethod · 0.45

Tested by

no test coverage detected