Java Code Examples for org.antlr.v4.runtime.tree.TerminalNode#getParent()
The following examples show how to use
org.antlr.v4.runtime.tree.TerminalNode#getParent() .
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: InputPanel.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void setCursorToGrammarRule(Project project, PreviewState previewState, int offset) { Token tokenUnderCursor = ParsingUtils.getTokenUnderCursor(previewState, offset); if ( tokenUnderCursor==null ) { return; } ParseTree tree = previewState.parsingResult.tree; TerminalNode nodeWithToken = (TerminalNode) ParsingUtils.getParseTreeNodeWithToken(tree, tokenUnderCursor); if ( nodeWithToken==null ) { // hidden token return; } ParserRuleContext parent = (ParserRuleContext) nodeWithToken.getParent(); int ruleIndex = parent.getRuleIndex(); Rule rule = previewState.g.getRule(ruleIndex); GrammarAST ruleNameNode = (GrammarAST) rule.ast.getChild(0); int start = ((CommonToken) ruleNameNode.getToken()).getStartIndex(); jumpToGrammarPosition(project, start); }
Example 2
Source File: Trainer.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** Walk upwards from node while p.start == token; return immediate parent * if there is no ancestor starting at token. This is the earliest * left ancestor. E.g, for '{' of a block, return parent up the chain from * block starting with '{'. For '}' of block, return just block as nothing * starts with '}'. (block stops with it). */ public static ParserRuleContext earliestAncestorStartingWithToken(TerminalNode node) { Token token = node.getSymbol(); ParserRuleContext p = (ParserRuleContext)node.getParent(); ParserRuleContext prev = null; while (p!=null && p.getStart()==token) { prev = p; p = p.getParent(); } if ( prev==null ) { return (ParserRuleContext)node.getParent(); } return prev; }
Example 3
Source File: Trainer.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** Walk upwards from node while p.stop == token; return immediate parent * if there is no ancestor stopping at token. This is the earliest * right ancestor. E.g, for '}' of a block, return parent up the chain from * block stopping with '}'. For '{' of block, return just block as nothing * stops with '{'. (block starts with it). */ public static ParserRuleContext earliestAncestorEndingWithToken(TerminalNode node) { Token token = node.getSymbol(); ParserRuleContext p = (ParserRuleContext)node.getParent(); ParserRuleContext prev = null; while (p!=null && p.getStop()==token) { prev = p; p = p.getParent(); } if ( prev==null ) { return (ParserRuleContext)node.getParent(); } return prev; }
Example 4
Source File: Trainer.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
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 5
Source File: InputPanel.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Show tokens/region associated with parse tree parent of this token * if the alt-key is down and mouse movement occurs. */ public void showParseRegion(EditorMouseEvent event, Editor editor, PreviewState previewState, int offset) { Token tokenUnderCursor = ParsingUtils.getTokenUnderCursor(previewState, offset); if ( tokenUnderCursor==null ) { return; } ParseTree tree = previewState.parsingResult.tree; TerminalNode nodeWithToken = (TerminalNode) ParsingUtils.getParseTreeNodeWithToken(tree, tokenUnderCursor); if ( nodeWithToken==null ) { // hidden token return; } PreviewParser parser = (PreviewParser) previewState.parsingResult.parser; CommonTokenStream tokenStream = (CommonTokenStream) parser.getInputStream(); ParserRuleContext parent = (ParserRuleContext) nodeWithToken.getParent(); Interval tokenInterval = parent.getSourceInterval(); Token startToken = tokenStream.get(tokenInterval.a); Token stopToken = tokenStream.get(tokenInterval.b); Interval sourceInterval = Interval.of(startToken.getStartIndex(), stopToken.getStopIndex()+1); List<String> stack = parser.getRuleInvocationStack(parent); Collections.reverse(stack); if ( stack.size()>MAX_STACK_DISPLAY ) { // collapse contiguous dups to handle left-recursive stacks List<Pair<String, Integer>> smaller = new ArrayList<>(); int last = 0; smaller.add(new Pair<>(stack.get(0), 1)); // init to having first element, count of 1 for (int i = 1; i<stack.size(); i++) { String s = stack.get(i); if ( smaller.get(last).a.equals(s) ) { smaller.set(last, new Pair<>(s, smaller.get(last).b + 1)); } else { smaller.add(new Pair<>(s, 1)); last++; } } stack = new ArrayList<>(); for ( Pair<String, Integer> pair : smaller ) { if ( pair.b>1 ) { stack.add(pair.a + "^" + pair.b); } else { stack.add(pair.a); } } } String stackS = Utils.join(stack.toArray(), "\n"); highlightAndOfferHint(editor, offset, sourceInterval, JBColor.BLUE, EffectType.ROUNDED_BOX, stackS); // Code for a balloon. // JBPopupFactory popupFactory = JBPopupFactory.getInstance(); // BalloonBuilder builder = // popupFactory.createHtmlTextBalloonBuilder(Utils.join(stack.toArray(), "<br>"), // MessageType.INFO, null); // builder.setHideOnClickOutside(true); // Balloon balloon = builder.createBalloon(); // MouseEvent mouseEvent = event.getMouseEvent(); // Point point = mouseEvent.getPoint(); // point.translate(10, -15); // RelativePoint where = new RelativePoint(mouseEvent.getComponent(), point); // balloon.show(where, Balloon.Position.above); }