Java Code Examples for org.antlr.v4.runtime.ParserRuleContext#getRuleIndex()

The following examples show how to use org.antlr.v4.runtime.ParserRuleContext#getRuleIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static TerminalNode getMatchingLeftSymbol(Corpus corpus,
                                                 InputDocument doc,
                                                 TerminalNode node)
{
	ParserRuleContext parent = (ParserRuleContext)node.getParent();
	int curTokensParentRuleIndex = parent.getRuleIndex();
	Token curToken = node.getSymbol();
	if (corpus.ruleToPairsBag != null) {
		String ruleName = doc.parser.getRuleNames()[curTokensParentRuleIndex];
		RuleAltKey ruleAltKey = new RuleAltKey(ruleName, parent.getAltNumber());
		List<Pair<Integer, Integer>> pairs = corpus.ruleToPairsBag.get(ruleAltKey);
		if ( pairs!=null ) {
			// Find appropriate pair given current token
			// If more than one pair (a,b) with b=current token pick first one
			// or if a common pair like ({,}), then give that one preference.
			// or if b is punctuation, prefer a that is punct
			List<Integer> viableMatchingLeftTokenTypes = viableLeftTokenTypes(parent, curToken, pairs);
			Vocabulary vocab = doc.parser.getVocabulary();
			if ( !viableMatchingLeftTokenTypes.isEmpty() ) {
				int matchingLeftTokenType =
					CollectTokenPairs.getMatchingLeftTokenType(curToken, viableMatchingLeftTokenTypes, vocab);
				List<TerminalNode> matchingLeftNodes = parent.getTokens(matchingLeftTokenType);
				// get matching left node by getting last node to left of current token
				List<TerminalNode> nodesToLeftOfCurrentToken =
					filter(matchingLeftNodes, n -> n.getSymbol().getTokenIndex()<curToken.getTokenIndex());
				TerminalNode matchingLeftNode = nodesToLeftOfCurrentToken.get(nodesToLeftOfCurrentToken.size()-1);
				if (matchingLeftNode == null) {
					System.err.println("can't find matching node for "+node.getSymbol());
				}
				return matchingLeftNode;
			}
		}
	}
	return null;
}
 
Example 2
Source File: CollectTokenPairs.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void enterEveryRule(ParserRuleContext ctx) {
	String ruleName = ruleNames[ctx.getRuleIndex()];
	List<TerminalNode> tnodes = getDirectTerminalChildren(ctx);
	// Find all ordered unique pairs of literals;
	// no (a,a) pairs and only literals like '{', 'begin', '}', ...
	// Add a for (a,a) into ruleToRepeatedTokensSet for later filtering
	RuleAltKey ruleAltKey = new RuleAltKey(ruleName, ctx.getAltNumber());
	for (int i=0; i<tnodes.size(); i++) {
		for (int j = i+1; j<tnodes.size(); j++) {
			TerminalNode a = tnodes.get(i);
			TerminalNode b = tnodes.get(j);
			int atype = a.getSymbol().getType();
			int btype = b.getSymbol().getType();
			// only include literals like '{' and ':' not IDENTIFIER etc...
			if ( vocab.getLiteralName(atype)==null || vocab.getLiteralName(btype)==null ) {
				continue;
			}

			if ( atype==btype ) {
				Set<Integer> repeatedTokensSet = ruleToRepeatedTokensSet.get(ruleAltKey);
				if ( repeatedTokensSet==null ) {
					repeatedTokensSet = new HashSet<>();
					ruleToRepeatedTokensSet.put(ruleAltKey, repeatedTokensSet);
				}
				repeatedTokensSet.add(atype);
			}
			else {
				Pair<Integer, Integer> pair = new Pair<>(atype, btype);
				Set<Pair<Integer, Integer>> pairsBag = ruleToPairsBag.get(ruleAltKey);
				if ( pairsBag==null ) {
					pairsBag = new HashSet<>();
					ruleToPairsBag.put(ruleAltKey, pairsBag);
				}
				pairsBag.add(pair);
			}
		}
	}
}
 
Example 3
Source File: ParentSiblingListKey.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ParentSiblingListKey(ParserRuleContext parent, ParserRuleContext child, int separatorTokenType) {
	parentRuleIndex = parent.getRuleIndex();
	parentRuleAlt = parent.getAltNumber();
	childRuleIndex = child.getRuleIndex();
	childRuleAlt = child.getAltNumber();
	this.separatorTokenType = separatorTokenType;
}
 
Example 4
Source File: Trainer.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/** Get the token type and tree ancestor features. These are computed
 *  the same for both training and formatting.
 */
public static int[] getContextFeatures(Corpus corpus,
                                       Map<Token, TerminalNode> tokenToNodeMap,
                                       InputDocument doc,
                                       int i)
{
	int[] features = new int[NUM_FEATURES];
	CodeBuffTokenStream tokens = doc.tokens;
	TerminalNode node = tokenToNodeMap.get(tokens.get(i));
	if ( node==null ) {
		System.err.println("### No node associated with token "+tokens.get(i));
		return features;
	}
	Token curToken = node.getSymbol();

	// Get context information for previous token
	Token prevToken = tokens.getPreviousRealToken(i);
	TerminalNode prevNode = tokenToNodeMap.get(prevToken);

	ParserRuleContext prevEarliestRightAncestor = earliestAncestorEndingWithToken(prevNode);
	int prevEarliestAncestorRuleIndex = prevEarliestRightAncestor.getRuleIndex();
	int prevEarliestAncestorRuleAltNum = prevEarliestRightAncestor.getAltNumber();

	// Get context information for current token
	ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken(node);

	ParserRuleContext earliestLeftAncestorParent  =
		earliestLeftAncestor!=null ? earliestLeftAncestor.getParent() : null;

	ParserRuleContext earliestLeftAncestorParent2 =
		earliestLeftAncestorParent!=null ? earliestLeftAncestorParent.getParent() : null;

	ParserRuleContext earliestLeftAncestorParent3 =
		earliestLeftAncestorParent2!=null ? earliestLeftAncestorParent2.getParent() : null;

	ParserRuleContext earliestLeftAncestorParent4 =
		earliestLeftAncestorParent3!=null ? earliestLeftAncestorParent3.getParent() : null;

	ParserRuleContext earliestLeftAncestorParent5 =
		earliestLeftAncestorParent4!=null ? earliestLeftAncestorParent4.getParent() : null;

	features[INDEX_PREV_TYPE]                     = prevToken.getType();
	features[INDEX_PREV_EARLIEST_RIGHT_ANCESTOR]  = rulealt(prevEarliestAncestorRuleIndex,prevEarliestAncestorRuleAltNum);
	features[INDEX_CUR_TOKEN_TYPE]                = curToken.getType();
	features[INDEX_CUR_TOKEN_CHILD_INDEX]         = getChildIndexOrListMembership(node);
	features[INDEX_EARLIEST_LEFT_ANCESTOR]        = rulealt(earliestLeftAncestor);
	features[INDEX_ANCESTORS_CHILD_INDEX]         = getChildIndexOrListMembership(earliestLeftAncestor);
	features[INDEX_ANCESTORS_PARENT_RULE]         = earliestLeftAncestorParent!=null ? rulealt(earliestLeftAncestorParent) : -1;
	features[INDEX_ANCESTORS_PARENT_CHILD_INDEX]  = getChildIndexOrListMembership(earliestLeftAncestorParent);
	features[INDEX_ANCESTORS_PARENT2_RULE]        = earliestLeftAncestorParent2!=null ? rulealt(earliestLeftAncestorParent2) : -1;
	features[INDEX_ANCESTORS_PARENT2_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestorParent2);
	features[INDEX_ANCESTORS_PARENT3_RULE]        = earliestLeftAncestorParent3!=null ? rulealt(earliestLeftAncestorParent3) : -1;
	features[INDEX_ANCESTORS_PARENT3_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestorParent3);
	features[INDEX_ANCESTORS_PARENT4_RULE]        = earliestLeftAncestorParent4!=null ? rulealt(earliestLeftAncestorParent4) : -1;
	features[INDEX_ANCESTORS_PARENT4_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestorParent4);
	features[INDEX_ANCESTORS_PARENT5_RULE]        = earliestLeftAncestorParent5!=null ? rulealt(earliestLeftAncestorParent5) : -1;
	features[INDEX_ANCESTORS_PARENT5_CHILD_INDEX] = getChildIndexOrListMembership(earliestLeftAncestorParent5);

	features[INDEX_MATCHING_TOKEN_STARTS_LINE] = getMatchingSymbolStartsLine(corpus, doc, node);
	features[INDEX_MATCHING_TOKEN_ENDS_LINE]   = getMatchingSymbolEndsLine(corpus, doc, node);

	features[INDEX_INFO_FILE]    = 0; // dummy; _toString() dumps filename w/o this value; placeholder for col in printout
	features[INDEX_INFO_LINE]    = curToken.getLine();
	features[INDEX_INFO_CHARPOS] = curToken.getCharPositionInLine();

	return features;
}