PageRenderTime 168ms CodeModel.GetById 135ms app.highlight 11ms RepoModel.GetById 19ms 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
  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
 34
 35package bsh;
 36/*
 37	Note: great care (and lots of typing) were taken to insure that the
 38	namespace and interpreter references are passed on the stack and not 
 39	(as they were erroneously before) installed in instance variables...
 40	Each of these node objects must be re-entrable to allow for recursive 
 41	situations.
 42
 43	The only data which should really be stored in instance vars here should 
 44	be parse tree data... features of the node which should never change (e.g.
 45	the number of arguments, etc.)
 46	
 47	Exceptions would be public fields of simple classes that just publish
 48	data produced by the last eval()... data that is used immediately. We'll
 49	try to remember to mark these as transient to highlight them.
 50
 51*/
 52class SimpleNode implements Node 
 53{
 54	public static SimpleNode JAVACODE =
 55		new SimpleNode( -1 ) {
 56			public String getSourceFile() {
 57				return "<Called from Java Code>";
 58			}
 59
 60			public int getLineNumber() {
 61				return -1;
 62			}
 63
 64			public String getText()  {
 65				return "<Compiled Java Code>";
 66			}
 67		};
 68
 69	protected Node parent;
 70	protected Node[] children;
 71	protected int id;
 72	Token firstToken, lastToken;
 73
 74	/** the source of the text from which this was parsed */
 75	String sourceFile;
 76
 77	public SimpleNode(int i) {
 78		id = i;
 79	}
 80
 81	public void jjtOpen() { }
 82	public void jjtClose() { }
 83
 84	public void jjtSetParent(Node n) { parent = n; }
 85	public Node jjtGetParent() { return parent; }
 86	//public SimpleNode getParent() { return (SimpleNode)parent; }
 87
 88	public void jjtAddChild(Node n, int i)
 89	{
 90		if (children == null)
 91			children = new Node[i + 1];
 92		else
 93			if (i >= children.length)
 94			{
 95				Node c[] = new Node[i + 1];
 96				System.arraycopy(children, 0, c, 0, children.length);
 97				children = c;
 98			}
 99
100		children[i] = n;
101	}
102
103	public Node jjtGetChild(int i) { 
104		return children[i]; 
105	}
106	public SimpleNode getChild( int i ) {
107		return (SimpleNode)jjtGetChild(i);
108	}
109
110	public int jjtGetNumChildren() {
111		return (children == null) ? 0 : children.length;
112	}
113
114	/*
115		You can override these two methods in subclasses of SimpleNode to
116		customize the way the node appears when the tree is dumped.  If
117		your output uses more than one line you should override
118		toString(String), otherwise overriding toString() is probably all
119		you need to do.
120	*/
121	public String toString() { return ParserTreeConstants.jjtNodeName[id]; }
122	public String toString(String prefix) { return prefix + toString(); }
123
124	/*
125		Override this method if you want to customize how the node dumps
126		out its children.
127	*/
128	public void dump(String prefix)
129	{
130		System.out.println(toString(prefix));
131		if(children != null)
132		{
133			for(int i = 0; i < children.length; ++i)
134			{
135				SimpleNode n = (SimpleNode)children[i];
136				if (n != null)
137				{
138					n.dump(prefix + " ");
139				}
140			}
141		}
142	}
143
144	//  ---- BeanShell specific stuff hereafter ----  //
145
146	/**
147		Detach this node from its parent.
148		This is primarily useful in node serialization.
149		(see BSHMethodDeclaration)
150	*/
151	public void prune() {
152		jjtSetParent( null );
153	}
154
155	/**
156		This is the general signature for evaluation of a node.
157	*/
158	public Object eval( CallStack callstack, Interpreter interpreter ) 
159		throws EvalError
160	{
161		throw new InterpreterError(
162			"Unimplemented or inappropriate for " + getClass().getName() );
163	}
164
165	/**
166		Set the name of the source file (or more generally source) of
167		the text from which this node was parsed.
168	*/
169	public void setSourceFile( String sourceFile ) {
170		this.sourceFile = sourceFile;
171	}
172
173	/**
174		Get the name of the source file (or more generally source) of
175		the text from which this node was parsed.
176		This will recursively search up the chain of parent nodes until
177		a source is found or return a string indicating that the source
178		is unknown.
179	*/
180	public String getSourceFile() {
181		if ( sourceFile == null )
182			if ( parent != null )
183				return ((SimpleNode)parent).getSourceFile();
184			else
185				return "<unknown file>";
186		else
187			return sourceFile;
188	}
189
190	/**
191		Get the line number of the starting token
192	*/
193	public int getLineNumber() {
194		return firstToken.beginLine;
195	}
196
197	/**
198		Get the ending line number of the starting token
199	public int getEndLineNumber() {
200		return lastToken.endLine;
201	}
202	*/
203
204	/**
205		Get the text of the tokens comprising this node.
206	*/
207	public String getText() 
208	{
209		StringBuffer text = new StringBuffer();
210		Token t = firstToken;
211		while ( t!=null ) {
212			text.append(t.image);
213			if ( !t.image.equals(".") )
214				text.append(" ");
215			if ( t==lastToken ||
216				t.image.equals("{") || t.image.equals(";") )
217				break;
218			t=t.next;
219		}
220			
221		return text.toString();
222	}
223}
224