MCPcopy Index your code
hub / github.com/antlr/codebuff / getMatchingLeftSymbol

Method getMatchingLeftSymbol

src/org/antlr/codebuff/Trainer.java:686–720  ·  view source on GitHub ↗
(Corpus corpus,
	                                                 InputDocument doc,
	                                                 TerminalNode node)

Source from the content-addressed store, hash-verified

684 }
685
686 public static TerminalNode getMatchingLeftSymbol(Corpus corpus,
687 InputDocument doc,
688 TerminalNode node)
689 {
690 ParserRuleContext parent = (ParserRuleContext)node.getParent();
691 int curTokensParentRuleIndex = parent.getRuleIndex();
692 Token curToken = node.getSymbol();
693 if (corpus.ruleToPairsBag != null) {
694 String ruleName = doc.parser.getRuleNames()[curTokensParentRuleIndex];
695 RuleAltKey ruleAltKey = new RuleAltKey(ruleName, parent.getAltNumber());
696 List<Pair<Integer, Integer>> pairs = corpus.ruleToPairsBag.get(ruleAltKey);
697 if ( pairs!=null ) {
698 // Find appropriate pair given current token
699 // If more than one pair (a,b) with b=current token pick first one
700 // or if a common pair like ({,}), then give that one preference.
701 // or if b is punctuation, prefer a that is punct
702 List<Integer> viableMatchingLeftTokenTypes = viableLeftTokenTypes(parent, curToken, pairs);
703 Vocabulary vocab = doc.parser.getVocabulary();
704 if ( !viableMatchingLeftTokenTypes.isEmpty() ) {
705 int matchingLeftTokenType =
706 CollectTokenPairs.getMatchingLeftTokenType(curToken, viableMatchingLeftTokenTypes, vocab);
707 List<TerminalNode> matchingLeftNodes = parent.getTokens(matchingLeftTokenType);
708 // get matching left node by getting last node to left of current token
709 List<TerminalNode> nodesToLeftOfCurrentToken =
710 filter(matchingLeftNodes, n -> n.getSymbol().getTokenIndex()<curToken.getTokenIndex());
711 TerminalNode matchingLeftNode = nodesToLeftOfCurrentToken.get(nodesToLeftOfCurrentToken.size()-1);
712 if (matchingLeftNode == null) {
713 System.err.println("can't find matching node for "+node.getSymbol());
714 }
715 return matchingLeftNode;
716 }
717 }
718 }
719 return null;
720 }
721
722 public static List<Integer> viableLeftTokenTypes(ParserRuleContext node,
723 Token curToken,

Callers 2

Calls 11

viableLeftTokenTypesMethod · 0.95
getRuleNamesMethod · 0.80
getAltNumberMethod · 0.80
getVocabularyMethod · 0.80
getTokenIndexMethod · 0.80
getMethod · 0.65
isEmptyMethod · 0.65
sizeMethod · 0.65
getParentMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected