public Token getLastToken() { if (!this.hasToken()) { return null; } AstNode currentNode = this; while (currentNode.hasChildren()) { for (int i = currentNode.children.size() - 1; i >= 0; i--) { AstNode child = currentNode.children.get(i); if (child.hasToken()) { currentNode = child; break; } } } return currentNode.getToken(); }
public Token getLastToken() { if (!this.hasToken()) { return null; } AstNode currentNode = this; while (currentNode.hasChildren()) { for (int i = currentNode.children.size() - 1; i >= 0; i--) { AstNode child = currentNode.children.get(i); if (child.hasToken()) { currentNode = child; break; } } } return currentNode.getToken(); }
public Token getLastToken() { if (!this.hasToken()) { return null; } AstNode currentNode = this; while (currentNode.hasChildren()) { for (int i = currentNode.children.size() - 1; i >= 0; i--) { AstNode child = currentNode.children.get(i); if (child.hasToken()) { currentNode = child; break; } } } return currentNode.getToken(); }
@Nullable private static AstNode getLastTokenAstNode(AstNode astNode) { if (!astNode.hasToken()) { return null; } AstNode currentNode = astNode; while (currentNode.hasChildren()) { for (int i = currentNode.getChildren().size() - 1; i >= 0; i--) { AstNode child = currentNode.getChildren().get(i); if (child.hasToken()) { currentNode = child; break; } } } return currentNode; } }
private static Token getPreviousToken(AstNode node) { AstNode result = node.getPreviousAstNode(); while (result != null && !result.hasToken()) { while (result != null && !result.hasToken()) { result = result.getPreviousAstNode(); } while (result != null && result.getLastChild() != null) { result = result.getLastChild(); } } return result == null ? null : result.getToken(); }
private void appendNodecontent(AstNode node) throws IOException { writer.append(node.getName()); if (node.getTokenValue() != null) { writer.append(" tokenValue=\"" + node.getTokenValue() + "\""); } if (node.hasToken()) { writer.append(" tokenLine=\"" + node.getTokenLine() + "\" tokenColumn=\"" + node.getToken().getColumn() + "\""); } }
private void appendNodecontent(AstNode node) throws IOException { writer.append(node.getName()); if (node.getTokenValue() != null) { writer.append(" tokenValue=\"" + node.getTokenValue() + "\""); } if (node.hasToken()) { writer.append(" tokenLine=\"" + node.getTokenLine() + "\" tokenColumn=\"" + node.getToken().getColumn() + "\""); } }
private void appendNodecontent(AstNode node) throws IOException { writer.append(node.getName()); if (node.getTokenValue() != null) { writer.append(" tokenValue=\"" + node.getTokenValue() + "\""); } if (node.hasToken()) { writer.append(" tokenLine=\"" + node.getTokenLine() + "\" tokenColumn=\"" + node.getToken().getColumn() + "\""); } }
@Override public Iterator getAttributeAxisIterator(Object contextNode) { if (isElement(contextNode)) { AstNode astNode = (AstNode) contextNode; if (!astNode.hasToken()) { return EMPTY_ITERATOR; } else { return Iterators.forArray(new Attribute("tokenLine", astNode), new Attribute("tokenColumn", astNode), new Attribute("tokenValue", astNode)); } } else if (isAttribute(contextNode)) { return EMPTY_ITERATOR; } else { throw new UnsupportedOperationException("Unsupported context object type for attribute axis \"" + contextNode.getClass().getSimpleName() + "\": " + contextNode); } }
public static InternalSyntaxToken create(AstNode astNode) { Preconditions.checkArgument(astNode.hasToken(), "has no token"); Preconditions.checkArgument(astNode.getToken() == astNode.getLastToken(), "has several tokens"); return new InternalSyntaxToken(astNode.getType(), astNode.getToken(), astNode.getFromIndex(), astNode.getToIndex()); }
public static InternalSyntaxToken create(AstNode astNode) { Preconditions.checkArgument(astNode.hasToken(), "has no token"); Preconditions.checkArgument(astNode.getToken() == astNode.getLastToken(), "has several tokens"); return new InternalSyntaxToken(astNode.getType(), astNode.getToken(), astNode.getFromIndex(), astNode.getToIndex()); }
@Override public AstNode createNonTerminal(GrammarRuleKey ruleKey, Rule rule, List<Object> children, int startIndex, int endIndex) { Token token = null; for (Object child : children) { if (child instanceof AstNode && ((AstNode) child).hasToken()) { token = ((AstNode) child).getToken(); break; } } AstNode astNode = new AstNode(rule, ruleKey.toString(), token); for (Object child : children) { astNode.addChild((AstNode) child); } astNode.setFromIndex(startIndex); astNode.setToIndex(endIndex); return astNode; }
@Override public AstNode createNonTerminal(GrammarRuleKey ruleKey, Rule rule, List<Object> children, int startIndex, int endIndex) { Token token = null; for (Object child : children) { if (child instanceof AstNode && ((AstNode) child).hasToken()) { token = ((AstNode) child).getToken(); break; } } AstNode astNode = new AstNode(rule, ruleKey.toString(), token); for (Object child : children) { astNode.addChild((AstNode) child); } astNode.setFromIndex(startIndex); astNode.setToIndex(endIndex); return astNode; }
@Override public void scrollSourceCodeTo(@Nullable AstNode astNode) { if (astNode != null && astNode.hasToken()) { int visibleLines = sourceCodeEditorPane.getVisibleRect().height / sourceCodeEditorPane.getFontMetrics(sourceCodeEditorPane.getFont()).getHeight(); int line = astNode.getToken().getLine() + visibleLines / 2; try { sourceCodeEditorPane.scrollRectToVisible(sourceCodeEditorPane.modelToView(0)); sourceCodeEditorPane.scrollRectToVisible(sourceCodeEditorPane.modelToView(lineOffsets.getOffset(line, 0))); } catch (BadLocationException e) { throw new RuntimeException(e); } } }
private DefaultMutableTreeNode getTreeNode(AstNode astNode) { DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(astNode); if (astNode.hasChildren()) { for (AstNode childAstNode : astNode.getChildren()) { treeNode.add(getTreeNode(childAstNode)); } } else if (astNode.hasToken() && astNode.getToken().hasTrivia()) { for (Trivia trivia : astNode.getToken().getTrivia()) { DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(trivia); if (trivia.hasPreprocessingDirective()) { childTreeNode.add(getTreeNode(trivia.getPreprocessingDirective().getAst())); } treeNode.add(childTreeNode); } } return treeNode; }
@Override public void scrollSourceCodeTo(@Nullable AstNode astNode) { if (astNode != null && astNode.hasToken()) { int visibleLines = sourceCodeEditorPane.getVisibleRect().height / sourceCodeEditorPane.getFontMetrics(sourceCodeEditorPane.getFont()).getHeight(); int line = astNode.getToken().getLine() + visibleLines / 2; try { sourceCodeEditorPane.scrollRectToVisible(sourceCodeEditorPane.modelToView(0)); sourceCodeEditorPane.scrollRectToVisible(sourceCodeEditorPane.modelToView(lineOffsets.getOffset(line, 0))); } catch (BadLocationException e) { throw Throwables.propagate(e); } } }
@Override public void scrollSourceCodeTo(@Nullable AstNode astNode) { if (astNode != null && astNode.hasToken()) { int visibleLines = sourceCodeEditorPane.getVisibleRect().height / sourceCodeEditorPane.getFontMetrics(sourceCodeEditorPane.getFont()).getHeight(); int line = astNode.getToken().getLine() + visibleLines / 2; try { sourceCodeEditorPane.scrollRectToVisible(sourceCodeEditorPane.modelToView(0)); sourceCodeEditorPane.scrollRectToVisible(sourceCodeEditorPane.modelToView(lineOffsets.getOffset(line, 0))); } catch (BadLocationException e) { throw new RuntimeException(e); } } }
@Override public void highlightSourceCode(AstNode astNode) { checkNotNull(astNode); if (!astNode.hasToken()) { return; } Token startToken = astNode.getToken(); Token endToken = astNode.getLastToken(); int startOffset = getValidDocumentOffsetFromSourceCodeOffset(lineOffsets.getStartOffset(startToken)); int endOffset = getValidDocumentOffsetFromSourceCodeOffset(lineOffsets.getEndOffset(endToken)); try { sourceCodeEditorPane.getHighlighter().addHighlight(startOffset, endOffset, highlighter); } catch (BadLocationException e) { throw Throwables.propagate(e); } }
@Override public void highlightSourceCode(AstNode astNode) { Objects.requireNonNull(astNode); if (!astNode.hasToken()) { return; } Token startToken = astNode.getToken(); Token endToken = astNode.getLastToken(); int startOffset = getValidDocumentOffsetFromSourceCodeOffset(lineOffsets.getStartOffset(startToken)); int endOffset = getValidDocumentOffsetFromSourceCodeOffset(lineOffsets.getEndOffset(endToken)); try { sourceCodeEditorPane.getHighlighter().addHighlight(startOffset, endOffset, highlighter); } catch (BadLocationException e) { throw new RuntimeException(e); } }
@Override public void highlightSourceCode(AstNode astNode) { Objects.requireNonNull(astNode); if (!astNode.hasToken()) { return; } Token startToken = astNode.getToken(); Token endToken = astNode.getLastToken(); int startOffset = getValidDocumentOffsetFromSourceCodeOffset(lineOffsets.getStartOffset(startToken)); int endOffset = getValidDocumentOffsetFromSourceCodeOffset(lineOffsets.getEndOffset(endToken)); try { sourceCodeEditorPane.getHighlighter().addHighlight(startOffset, endOffset, highlighter); } catch (BadLocationException e) { throw new RuntimeException(e); } }