(Corpus corpus, InputDocument doc, TerminalNode node)
| 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, |
no test coverage detected