View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.ecmascript.ast;
5   
6   import net.sourceforge.pmd.lang.ast.AbstractNode;
7   
8   import org.mozilla.javascript.ast.AstNode;
9   
10  public abstract class AbstractEcmascriptNode<T extends AstNode> extends AbstractNode implements EcmascriptNode<T> {
11  
12      protected final T node;
13  
14      public AbstractEcmascriptNode(T node) {
15  	super(node.getType());
16  	this.node = node;
17      }
18  
19      /* package private */
20      void calculateLineNumbers(SourceCodePositioner positioner) {
21  	int startOffset = node.getAbsolutePosition();
22  	int endOffset = startOffset + node.getLength();
23  
24  	this.beginLine = positioner.lineNumberFromOffset(startOffset);
25  	this.beginColumn = positioner.columnFromOffset(startOffset);
26  	this.endLine = positioner.lineNumberFromOffset(endOffset);
27  	this.endColumn = positioner.columnFromOffset(endOffset) - 1; // end column is inclusive
28  	if (this.endColumn < 0) {
29  	    this.endColumn = 0;
30  	}
31      }
32  
33      /**
34       * Accept the visitor. *
35       */
36      public Object jjtAccept(EcmascriptParserVisitor visitor, Object data) {
37  	return visitor.visit(this, data);
38      }
39  
40      /**
41       * Accept the visitor. *
42       */
43      public Object childrenAccept(EcmascriptParserVisitor visitor, Object data) {
44  	if (children != null) {
45  	    for (int i = 0; i < children.length; ++i) {
46  		@SuppressWarnings("unchecked") // we know that the children here are all EcmascriptNodes
47  		EcmascriptNode<T> ecmascriptNode = (EcmascriptNode<T>) children[i];
48  		ecmascriptNode.jjtAccept(visitor, data);
49  	    }
50  	}
51  	return data;
52      }
53      
54      public T getNode() {
55  	return node;
56      }
57  
58      public String getJsDoc() {
59  	return node.getJsDoc();
60      }
61  
62      public boolean hasSideEffects() {
63  	return node.hasSideEffects();
64      }
65  
66      @Override
67      public String toString() {
68  	return node.shortName();
69      }
70  }