PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/bsh/SimpleNode.java

#
Java | 224 lines | 106 code | 25 blank | 93 comment | 17 complexity | 9822e3325c9753e241ffa374b58b9b2a MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. package bsh;
  34. /*
  35. Note: great care (and lots of typing) were taken to insure that the
  36. namespace and interpreter references are passed on the stack and not
  37. (as they were erroneously before) installed in instance variables...
  38. Each of these node objects must be re-entrable to allow for recursive
  39. situations.
  40. The only data which should really be stored in instance vars here should
  41. be parse tree data... features of the node which should never change (e.g.
  42. the number of arguments, etc.)
  43. Exceptions would be public fields of simple classes that just publish
  44. data produced by the last eval()... data that is used immediately. We'll
  45. try to remember to mark these as transient to highlight them.
  46. */
  47. class SimpleNode implements Node
  48. {
  49. public static SimpleNode JAVACODE =
  50. new SimpleNode( -1 ) {
  51. public String getSourceFile() {
  52. return "<Called from Java Code>";
  53. }
  54. public int getLineNumber() {
  55. return -1;
  56. }
  57. public String getText() {
  58. return "<Compiled Java Code>";
  59. }
  60. };
  61. protected Node parent;
  62. protected Node[] children;
  63. protected int id;
  64. Token firstToken, lastToken;
  65. /** the source of the text from which this was parsed */
  66. String sourceFile;
  67. public SimpleNode(int i) {
  68. id = i;
  69. }
  70. public void jjtOpen() { }
  71. public void jjtClose() { }
  72. public void jjtSetParent(Node n) { parent = n; }
  73. public Node jjtGetParent() { return parent; }
  74. //public SimpleNode getParent() { return (SimpleNode)parent; }
  75. public void jjtAddChild(Node n, int i)
  76. {
  77. if (children == null)
  78. children = new Node[i + 1];
  79. else
  80. if (i >= children.length)
  81. {
  82. Node c[] = new Node[i + 1];
  83. System.arraycopy(children, 0, c, 0, children.length);
  84. children = c;
  85. }
  86. children[i] = n;
  87. }
  88. public Node jjtGetChild(int i) {
  89. return children[i];
  90. }
  91. public SimpleNode getChild( int i ) {
  92. return (SimpleNode)jjtGetChild(i);
  93. }
  94. public int jjtGetNumChildren() {
  95. return (children == null) ? 0 : children.length;
  96. }
  97. /*
  98. You can override these two methods in subclasses of SimpleNode to
  99. customize the way the node appears when the tree is dumped. If
  100. your output uses more than one line you should override
  101. toString(String), otherwise overriding toString() is probably all
  102. you need to do.
  103. */
  104. public String toString() { return ParserTreeConstants.jjtNodeName[id]; }
  105. public String toString(String prefix) { return prefix + toString(); }
  106. /*
  107. Override this method if you want to customize how the node dumps
  108. out its children.
  109. */
  110. public void dump(String prefix)
  111. {
  112. System.out.println(toString(prefix));
  113. if(children != null)
  114. {
  115. for(int i = 0; i < children.length; ++i)
  116. {
  117. SimpleNode n = (SimpleNode)children[i];
  118. if (n != null)
  119. {
  120. n.dump(prefix + " ");
  121. }
  122. }
  123. }
  124. }
  125. // ---- BeanShell specific stuff hereafter ---- //
  126. /**
  127. Detach this node from its parent.
  128. This is primarily useful in node serialization.
  129. (see BSHMethodDeclaration)
  130. */
  131. public void prune() {
  132. jjtSetParent( null );
  133. }
  134. /**
  135. This is the general signature for evaluation of a node.
  136. */
  137. public Object eval( CallStack callstack, Interpreter interpreter )
  138. throws EvalError
  139. {
  140. throw new InterpreterError(
  141. "Unimplemented or inappropriate for " + getClass().getName() );
  142. }
  143. /**
  144. Set the name of the source file (or more generally source) of
  145. the text from which this node was parsed.
  146. */
  147. public void setSourceFile( String sourceFile ) {
  148. this.sourceFile = sourceFile;
  149. }
  150. /**
  151. Get the name of the source file (or more generally source) of
  152. the text from which this node was parsed.
  153. This will recursively search up the chain of parent nodes until
  154. a source is found or return a string indicating that the source
  155. is unknown.
  156. */
  157. public String getSourceFile() {
  158. if ( sourceFile == null )
  159. if ( parent != null )
  160. return ((SimpleNode)parent).getSourceFile();
  161. else
  162. return "<unknown file>";
  163. else
  164. return sourceFile;
  165. }
  166. /**
  167. Get the line number of the starting token
  168. */
  169. public int getLineNumber() {
  170. return firstToken.beginLine;
  171. }
  172. /**
  173. Get the ending line number of the starting token
  174. public int getEndLineNumber() {
  175. return lastToken.endLine;
  176. }
  177. */
  178. /**
  179. Get the text of the tokens comprising this node.
  180. */
  181. public String getText()
  182. {
  183. StringBuffer text = new StringBuffer();
  184. Token t = firstToken;
  185. while ( t!=null ) {
  186. text.append(t.image);
  187. if ( !t.image.equals(".") )
  188. text.append(" ");
  189. if ( t==lastToken ||
  190. t.image.equals("{") || t.image.equals(";") )
  191. break;
  192. t=t.next;
  193. }
  194. return text.toString();
  195. }
  196. }