/plugins/XML/tags/release-2-5/sidekick/ecmascript/parser/SimpleNode.java

# · Java · 488 lines · 361 code · 104 blank · 23 comment · 100 complexity · dc1912b621dd370568c86d0d7c90c4f9 MD5 · raw file

  1. /* Generated By:JJTree: Do not edit this line. SimpleNode.java */
  2. package sidekick.ecmascript.parser;
  3. import java.util.*;
  4. import javax.swing.Icon;
  5. import javax.swing.text.Position;
  6. import sidekick.IAsset;
  7. import sidekick.util.*;
  8. import sidekick.ecmascript.parser.ASTDoStatement;
  9. import sidekick.ecmascript.parser.ASTEmptyStatement;
  10. import sidekick.ecmascript.parser.ASTExpressionStatement;
  11. import sidekick.ecmascript.parser.ASTForInStatement;
  12. import sidekick.ecmascript.parser.ASTForStatement;
  13. import sidekick.ecmascript.parser.ASTForVarInStatement;
  14. import sidekick.ecmascript.parser.ASTForVarStatement;
  15. import sidekick.ecmascript.parser.ASTIfStatement;
  16. import sidekick.ecmascript.parser.ASTVariableStatement;
  17. import sidekick.ecmascript.parser.ASTWhileStatement;
  18. import sidekick.ecmascript.parser.ASTWithStatement;
  19. import sidekick.ecmascript.parser.EcmaScript;
  20. import sidekick.ecmascript.parser.EcmaScriptConstants;
  21. import sidekick.ecmascript.parser.EcmaScriptTreeConstants;
  22. import sidekick.ecmascript.parser.EcmaScriptVisitor;
  23. import sidekick.ecmascript.parser.Node;
  24. import sidekick.ecmascript.parser.Token;
  25. public class SimpleNode implements Node, SideKickElement, EcmaScriptConstants,
  26. EcmaScriptTreeConstants {
  27. protected Node parent;
  28. protected Node[] children;
  29. protected int id;
  30. protected EcmaScript parser;
  31. private Location start = null;
  32. private Location end = null;
  33. private Position startPosition = null;
  34. private Position endPosition = null;
  35. private boolean visible = false;
  36. protected Token beginToken;
  37. protected Token endToken;
  38. protected boolean inserted = false;
  39. protected LinkedList<Comment> javadocComments;
  40. public SimpleNode( int i ) {
  41. id = i;
  42. }
  43. public SimpleNode( EcmaScript p, int i ) {
  44. this( i );
  45. parser = p;
  46. }
  47. public void jjtOpen() {}
  48. public void jjtClose() {}
  49. public void jjtSetParent( Node n ) {
  50. parent = n;
  51. if (isVisible() && parent != null) {
  52. System.out.println("visible: " + toString() );
  53. SimpleNode p = (SimpleNode)parent;
  54. while(p != null) {
  55. p.setVisible(true);
  56. p = (SimpleNode)p.jjtGetParent();
  57. }
  58. }
  59. }
  60. public Node jjtGetParent() {
  61. return parent;
  62. }
  63. public void jjtAddChild( Node n, int i ) {
  64. if ( children == null ) {
  65. children = new Node[ i + 1 ];
  66. }
  67. else if ( i >= children.length ) {
  68. Node c[] = new Node[ i + 1 ];
  69. System.arraycopy( children, 0, c, 0, children.length );
  70. children = c;
  71. }
  72. children[ i ] = n;
  73. }
  74. public Node jjtGetChild( int i ) {
  75. if (children == null) {
  76. return null;
  77. }
  78. return children[ i ];
  79. }
  80. public int jjtGetNumChildren() {
  81. return ( children == null ) ? 0 : children.length;
  82. }
  83. public boolean hasChildren() {
  84. return jjtGetNumChildren() > 0;
  85. }
  86. public List getChildren() {
  87. if (children != null)
  88. return Arrays.asList( children );
  89. else
  90. return null;
  91. }
  92. /** Accept the visitor. **/
  93. public Object jjtAccept( EcmaScriptVisitor visitor, Object data ) {
  94. return visitor.visit( this, data );
  95. }
  96. /** Accept the visitor. **/
  97. public Object childrenAccept( EcmaScriptVisitor visitor, Object data ) {
  98. if ( children != null ) {
  99. for ( int i = 0; i < children.length; ++i ) {
  100. children[ i ].jjtAccept( visitor, data );
  101. }
  102. }
  103. return data;
  104. }
  105. // ----------------------------------------------------------------------------------------------
  106. // for SideKickElement
  107. public void setStartLocation( Location loc ) {
  108. start = loc;
  109. }
  110. public Location getStartLocation() {
  111. if ( start == null )
  112. start = new Location();
  113. return start;
  114. }
  115. public void setEndLocation( Location loc ) {
  116. end = loc;
  117. }
  118. public Location getEndLocation() {
  119. if ( end == null )
  120. end = new Location();
  121. return end;
  122. }
  123. public void setStartPosition( Position s ) {
  124. startPosition = s;
  125. }
  126. public Position getStartPosition() {
  127. return startPosition;
  128. }
  129. public void setEndPosition( Position s ) {
  130. endPosition = s;
  131. }
  132. public Position getEndPosition() {
  133. return endPosition;
  134. }
  135. public boolean isVisible() {
  136. boolean b = System.getProperty( "sidekick.ecmascript.general.allNodes", "false" ).startsWith("t");
  137. return b ? b : visible;
  138. }
  139. public void setVisible(boolean b) {
  140. visible = b;
  141. }
  142. // ----------------------------------------------------------------------------------------------
  143. // additional API's uwedeportivo
  144. public void insertChild( Node n, int i ) {
  145. if ( children == null ) {
  146. throw new IllegalStateException(
  147. "no children insertion not possible" );
  148. }
  149. if ( ( i > children.length ) || ( i < 0 ) ) {
  150. throw new IndexOutOfBoundsException();
  151. }
  152. Node c[] = new Node[ children.length + 1 ];
  153. if ( i > 0 ) {
  154. System.arraycopy( children, 0, c, 0, i );
  155. }
  156. c[ i ] = n;
  157. if ( i < children.length ) {
  158. System.arraycopy( children, i, c, i + 1, children.length - i );
  159. }
  160. children = c;
  161. }
  162. public Node getPrevSibling() {
  163. if ( parent == null ) {
  164. return null;
  165. }
  166. SimpleNode parentSNode = ( SimpleNode ) parent;
  167. int i = 0;
  168. for ( i = 0; i < parentSNode.children.length; i++ ) {
  169. if ( parentSNode.children[ i ] == this ) {
  170. break;
  171. }
  172. }
  173. if ( i == 0 ) {
  174. return null;
  175. }
  176. else {
  177. return parentSNode.children[ i - 1 ];
  178. }
  179. }
  180. public Node getNextSibling() {
  181. if ( parent == null ) {
  182. return null;
  183. }
  184. SimpleNode parentSNode = ( SimpleNode ) parent;
  185. int i = 0;
  186. for ( i = 0; i < parentSNode.children.length; i++ ) {
  187. if ( parentSNode.children[ i ] == this ) {
  188. break;
  189. }
  190. }
  191. if ( i == parentSNode.children.length - 1 ) {
  192. return null;
  193. }
  194. else {
  195. return parentSNode.children[ i + 1 ];
  196. }
  197. }
  198. public void setChild( Node n, int i ) {
  199. children[ i ] = n;
  200. n.jjtSetParent( this );
  201. }
  202. public int getChildIndex( Node n ) {
  203. int i = 0;
  204. for ( i = 0; i < children.length; i++ ) {
  205. if ( children[ i ] == n ) {
  206. break;
  207. }
  208. }
  209. return ( i == children.length ) ? -1 : i;
  210. }
  211. public void remove() {
  212. if ( parent == null ) {
  213. return ;
  214. }
  215. SimpleNode parentSNode = ( SimpleNode ) parent;
  216. int i = 0;
  217. for ( i = 0; i < parentSNode.children.length; i++ ) {
  218. if ( parentSNode.children[ i ] == this ) {
  219. break;
  220. }
  221. }
  222. Node c[] = new Node[ parentSNode.children.length - 1 ];
  223. if ( i == 0 ) {
  224. System.arraycopy( parentSNode.children, 1, c, 0,
  225. parentSNode.children.length - 1 );
  226. }
  227. else if ( i == parentSNode.children.length - 1 ) {
  228. System.arraycopy( parentSNode.children, 0, c, 0,
  229. parentSNode.children.length - 1 );
  230. }
  231. else {
  232. System.arraycopy( parentSNode.children, 0, c, 0, i );
  233. System.arraycopy( parentSNode.children, i + 1, c, i,
  234. parentSNode.children.length - i - 1 );
  235. }
  236. parentSNode.children = c;
  237. }
  238. public void removeSafely() {
  239. this.removeSafely( false );
  240. }
  241. public void removeSafely( boolean ignoreSpecialTokens ) {
  242. // we have to clear up the ast if we're the only child of an if
  243. // statement for example
  244. // we do it simple in that case (we just replace with empty statement)
  245. SimpleNode parentSNode = ( SimpleNode ) parent;
  246. boolean replace = false;
  247. if ( ( this instanceof ASTVariableStatement )
  248. || ( this instanceof ASTExpressionStatement ) ) {
  249. if ( ( parentSNode instanceof ASTIfStatement )
  250. || ( parentSNode instanceof ASTWhileStatement )
  251. || ( parentSNode instanceof ASTDoStatement )
  252. || ( parentSNode instanceof ASTForStatement )
  253. || ( parentSNode instanceof ASTForVarStatement )
  254. || ( parentSNode instanceof ASTForInStatement )
  255. || ( parentSNode instanceof ASTForVarInStatement )
  256. || ( parentSNode instanceof ASTWithStatement ) ) {
  257. replace = true;
  258. }
  259. }
  260. if ( replace ) {
  261. int index = parentSNode.getChildIndex( this );
  262. ASTEmptyStatement replacement = new ASTEmptyStatement(
  263. EcmaScriptTreeConstants.JJTEMPTYSTATEMENT );
  264. Token scToken = new Token();
  265. scToken.kind = EcmaScriptConstants.SEMICOLON;
  266. scToken.image = ";";
  267. // we have to transfer the special tokens of the replaced code to
  268. // the replacement
  269. scToken.specialToken = getBeginToken().specialToken;
  270. replacement.setBeginToken( scToken );
  271. replacement.setEndToken( scToken );
  272. replacement.setInserted( true );
  273. parentSNode.setChild( replacement, index );
  274. }
  275. else {
  276. if ( !ignoreSpecialTokens ) {
  277. // transfer the special tokens first
  278. Token specialTransfer = getBeginToken().specialToken;
  279. if ( specialTransfer != null ) {
  280. Token nextBeginToken = getEndToken().next;
  281. if ( nextBeginToken != null ) {
  282. SimpleNode.transferSpecial( specialTransfer,
  283. nextBeginToken );
  284. }
  285. }
  286. }
  287. remove();
  288. }
  289. }
  290. static public void transferSpecial( Token specialTransfer,
  291. Token nextBeginToken ) {
  292. if ( nextBeginToken.specialToken == null ) {
  293. nextBeginToken.specialToken = specialTransfer;
  294. }
  295. else if ( nextBeginToken.specialToken != specialTransfer ) {
  296. Token endSpecial = nextBeginToken.specialToken;
  297. while ( endSpecial.specialToken != null ) {
  298. endSpecial = endSpecial.specialToken;
  299. }
  300. endSpecial.specialToken = specialTransfer;
  301. }
  302. }
  303. /*
  304. * You can override these two methods in subclasses of SimpleNode to
  305. * customize the way the node appears when the tree is dumped. If your
  306. * output uses more than one line you should override toString(String),
  307. * otherwise overriding toString() is probably all you need to do.
  308. */
  309. @Override
  310. public String toString() {
  311. return EcmaScriptTreeConstants.jjtNodeName[ id ];
  312. }
  313. public String toString( String prefix ) {
  314. return prefix + toString();
  315. }
  316. /*
  317. * Override this method if you want to customize how the node dumps out its
  318. * children.
  319. */
  320. public void dump( String prefix ) {
  321. System.out.println( toString( prefix ) );
  322. if ( children != null ) {
  323. for ( int i = 0; i < children.length; ++i ) {
  324. SimpleNode n = ( SimpleNode ) children[ i ];
  325. if ( n != null ) {
  326. n.dump( prefix + " " );
  327. }
  328. }
  329. }
  330. }
  331. public void setBeginToken( Token token ) {
  332. beginToken = token;
  333. }
  334. public Token getBeginToken() {
  335. return beginToken;
  336. }
  337. public void setEndToken( Token token ) {
  338. endToken = token;
  339. }
  340. public Token getEndToken() {
  341. return endToken;
  342. }
  343. public boolean inserted() {
  344. return inserted;
  345. }
  346. public void setInserted( boolean aFlag ) {
  347. inserted = aFlag;
  348. }
  349. private void findComments() {
  350. javadocComments = new LinkedList<Comment>();
  351. Token token = getBeginToken();
  352. Comment aComment = null;
  353. if ( token != null ) {
  354. Token specialToken = token.specialToken;
  355. while ( specialToken != null ) {
  356. aComment = null;
  357. if ( specialToken.kind == EcmaScriptConstants.MULTI_LINE_COMMENT ) {
  358. try {
  359. aComment = new Comment( specialToken.image );
  360. }
  361. catch ( Exception exc ) {
  362. exc.printStackTrace();
  363. }
  364. if ( ( aComment != null ) && aComment.isJavadoc() ) {
  365. javadocComments.add( aComment );
  366. }
  367. }
  368. specialToken = specialToken.specialToken;
  369. }
  370. }
  371. }
  372. public Comment getComment() {
  373. if ( javadocComments == null ) {
  374. findComments();
  375. }
  376. return javadocComments.size() == 0 ? null : javadocComments.getFirst();
  377. }
  378. public List getComments() {
  379. if ( javadocComments == null ) {
  380. findComments();
  381. }
  382. return javadocComments;
  383. }
  384. }