PageRenderTime 85ms CodeModel.GetById 78ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 216 lines | 98 code | 22 blank | 96 comment | 17 complexity | 2c28b2b46ad4fb12c2ffe23ac5e5d363 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	protected Node parent;
 54	protected Node[] children;
 55	protected int id;
 56	Token firstToken, lastToken;
 57
 58	/** the source of the text from which this was parsed */
 59	String sourceFile;
 60
 61	public SimpleNode(int i) {
 62		id = i;
 63	}
 64
 65	public void jjtOpen() { }
 66	public void jjtClose() { }
 67
 68	public void jjtSetParent(Node n) { parent = n; }
 69	public Node jjtGetParent() { return parent; }
 70	//public SimpleNode getParent() { return (SimpleNode)parent; }
 71
 72	public void jjtAddChild(Node n, int i)
 73	{
 74		if (children == null)
 75			children = new Node[i + 1];
 76		else
 77			if (i >= children.length)
 78			{
 79				Node c[] = new Node[i + 1];
 80				System.arraycopy(children, 0, c, 0, children.length);
 81				children = c;
 82			}
 83
 84		children[i] = n;
 85	}
 86
 87	public Node jjtGetChild(int i) { 
 88		return children[i]; 
 89	}
 90	public SimpleNode getChild( int i ) {
 91		return (SimpleNode)jjtGetChild(i);
 92	}
 93
 94	public int jjtGetNumChildren() {
 95		return (children == null) ? 0 : children.length;
 96	}
 97
 98	/*
 99		You can override these two methods in subclasses of SimpleNode to
100		customize the way the node appears when the tree is dumped.  If
101		your output uses more than one line you should override
102		toString(String), otherwise overriding toString() is probably all
103		you need to do.
104	*/
105	public String toString() { return ParserTreeConstants.jjtNodeName[id]; }
106	public String toString(String prefix) { return prefix + toString(); }
107
108	/*
109		Override this method if you want to customize how the node dumps
110		out its children.
111	*/
112	public void dump(String prefix)
113	{
114		System.out.println(toString(prefix));
115		if(children != null)
116		{
117			for(int i = 0; i < children.length; ++i)
118			{
119				SimpleNode n = (SimpleNode)children[i];
120				if (n != null)
121				{
122					n.dump(prefix + " ");
123				}
124			}
125		}
126	}
127
128	//  ---- BeanShell specific stuff hereafter ----  //
129
130	/**
131		Detach this node from its parent.
132		This is primarily useful in node serialization.
133		(see BSHMethodDeclaration)
134	*/
135	public void prune() {
136		jjtSetParent( null );
137	}
138
139	/**
140		Some nodes require only a namespace for evaluation.
141	*/
142	public Object eval( NameSpace namespace ) 
143		throws EvalError
144	{
145		throw new EvalError(
146			"Unimplemented or inappropriate for " + getClass().getName());
147	}
148
149	/**
150		This is the general signature for evaluation of a node.
151	*/
152	public Object eval( CallStack callstack, Interpreter interpreter ) 
153		throws EvalError
154	{
155		/*
156			Try to call the simpler signature
157			I don't ilke this, but it's the price we pay for being able to
158			eval() any simplenode and still limit the signature on those
159			that only need namespace.
160		*/
161		return eval( callstack.top() );
162	}
163
164	/**
165		Set the name of the source file (or more generally source) of
166		the text from which this node was parsed.
167	*/
168	public void setSourceFile( String sourceFile ) {
169		this.sourceFile = sourceFile;
170	}
171
172	/**
173		Get the name of the source file (or more generally source) of
174		the text from which this node was parsed.
175		This will recursively search up the chain of parent nodes until
176		a source is found or return a string indicating that the source
177		is unknown.
178	*/
179	public String getSourceFile() {
180		if ( sourceFile == null )
181			if ( parent != null )
182				return ((SimpleNode)parent).getSourceFile();
183			else
184				return "<unknown file>";
185		else
186			return sourceFile;
187	}
188
189	/**
190		Get the line number of the starting token
191	*/
192	public int getLineNumber() {
193		return firstToken.beginLine;
194	}
195
196	/**
197		Get the text of the tokens comprising this node.
198	*/
199	public String getText() 
200	{
201		StringBuffer text = new StringBuffer();
202		Token t = firstToken;
203		while ( t!=null ) {
204			text.append(t.image);
205			if ( !t.image.equals(".") )
206				text.append(" ");
207			if ( t==lastToken ||
208				t.image.equals("{") || t.image.equals(";") )
209				break;
210			t=t.next;
211		}
212			
213		return text.toString();
214	}
215}
216