📄 symtabast.java
字号:
// Transmogrify License
//
// Copyright (c) 2001, ThoughtWorks, Inc.
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the ThoughtWorks, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
import java.io.File;
import antlr.collections.AST;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* an extension of <code>antlr.CommonAST</code> that includes
* extra information about the AST's location. This information
* is the file and line number where the AST was created.
*
* To use this AST node in your tree structure, assuming your
* antlr.TreeParser is called parser, use
*
* parser.setASTNOdeCLass(SymTabAST.class.getName());
*
* make sure you also call setTokenObjectClass for the lexer as well
*
*
* @see SymTabToken
*/
//TODO: Should be an adapter of DetailAST
public class SymTabAST
extends antlr.CommonASTWithHiddenTokens
{
private Scope _scope;
private IDefinition _definition = null;
private boolean _isMeaningful = true;
private File _file;
private int _line;
private int _column;
// parent is not used by Checkstyle
// private SymTabAST parent;
private Span _span;
/** original syntax tree node */
private DetailAST detailNode;
// /**
// * gets parent of this node
// * @return <code>SymTabAST</code>
// */
// public SymTabAST getParent() {
// return parent;
// }
// /**
// * gets previous sibling of this node
// * @return <code>SymTabAST</code>
// */
// public SymTabAST getPreviousSibling() {
// return prevSibling;
// }
/**
* sets parent of this node
* @param parent
* @return <code>void</code>
*/
public void setParent(SymTabAST parent) {
//parent is not used by Checkstyle
// this.parent = parent;
}
/**
* gets the scope of this node
* @return <code>Scope</code>
*/
public Scope getScope() {
return _scope;
}
/**
* sets the scope of this node
* @param scope
* @return <code>void</code>
*/
public void setScope(Scope scope) {
_scope = scope;
}
/**
* sets <code>Definition</code> for this node
* @param definition
* @param scope
* @return <code>void</code>
* @see #setDefinition(IDefinition, Scope, boolean)
*/
public void setDefinition(IDefinition definition, Scope scope) {
setDefinition(definition, scope, true);
}
/**
* sets <code>Definition</code> for this node and adds <code>Reference</code>
* to the <code>_definition</code> and <code>scope</code>
* @param definition
* @param scope
* @param createReference
* @return <code>void</code>
* @see net.sourceforge.transmogrify.symtab.Reference
*/
public void setDefinition(
IDefinition definition,
Scope scope,
boolean createReference) {
_definition = definition;
Reference reference = new Reference(this);
if (scope != null) {
scope.addReferenceInScope(reference);
}
if (definition.isSourced() && createReference) {
_definition.addReference(reference);
}
}
/**
* gets <code>_definitin</code>
* @return <code>IDefinition</code>
*/
public IDefinition getDefinition() {
return _definition;
}
/**
* tests if this node is meaningful or should be ignored
* @return <code>boolean</code>
*/
public boolean isMeaningful() {
return _isMeaningful;
}
/**
* sets <code>_isMeaningful</code> member
* @param isMeaningful
* @return <code>void</code>
*/
public void setMeaningfulness(boolean isMeaningful) {
_isMeaningful = isMeaningful;
}
/**
* sets meaningfulness for this node and its children
* @return <code>void</code>
* @see #setMeaningfulness(boolean)
*/
public void ignoreChildren() {
if (getType() == TokenTypes.IDENT) {
setMeaningfulness(false);
}
SymTabAST child = (SymTabAST) getFirstChild();
while (child != null) {
child.ignoreChildren();
child = (SymTabAST) child.getNextSibling();
}
}
/**
* sets file where this node belong to
* @param file
* @return <code>void</code>
*/
public void setFile(File file) {
_file = file;
}
/**
* finishes process for adding node to its parent
* @param file file where this node belongs to
* @param parent parent of this node
* @param previousSibling previous sibling of this node
* @return <code>Span</code> the span of this node
* @see #setFile(File)
* @see #setParent(SymTabAST)
* @see #setPreviousSibling(SymTabAST)
* @see #finishChildren(File)
* @see #setSpan(Span)
*/
public Span finishDefinition(
File file,
SymTabAST parent) {
setFile(file);
setParent(parent);
Span result = finishChildren(file);
if (getLineNo() != 0) {
result.compose(
new Span(
getLineNo(),
getColumnNo(),
getLineNo(),
getColumnNo()
+ ((getText() == null) ? 0 : getText().length() - 1)));
}
setSpan(result);
return result;
}
/**
* finishes children of this node definition process
* @param file file where this node belongs to
* @return <code>Span</code>
* @see #finishDefinition(File, SymTabAST, SymTabAST)
*/
public Span finishChildren(File file) {
Span result = null;
SymTabAST current = (SymTabAST) getFirstChild();
if (current == null) {
result = getSpan();
}
else {
while (current != null) {
Span childSpan =
current.finishDefinition(file, this);
if (childSpan != null) {
if (result == null) {
result = new Span(childSpan);
}
else {
result.compose(childSpan);
}
}
current = (SymTabAST) current.getNextSibling();
}
}
return result;
}
/**
* gets file where this node belongs to
* @return <code>File</code>
*/
public File getFile() {
return _file;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -