home *** CD-ROM | disk | FTP | other *** search
Java Source | 1995-08-11 | 3.1 KB | 110 lines |
- /*
- * @(#)ActiveFunction.java 1.1 95/05/10
- *
- * Copyright (c) 1995 Sun Microsystems, Inc. All Rights reserved Permission to
- * use, copy, modify, and distribute this software and its documentation for
- * NON-COMMERCIAL purposes and without fee is hereby granted provided that
- * this copyright notice appears in all copies. Please refer to the file
- * copyright.html for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
- * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
- * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
- * ITS DERIVATIVES.
- */
-
- package java.tools.jsc;
- import java.io.StreamTokenizer;
-
- /**
- * A class to implement one command in a script.
- * @author James Gosling
- */
-
- class ActiveFunction {
- Object args[];
- String command;
- ActiveFunction next;
- static final String immediate_cmd = "[";
- static final String later_cmd = "(";
- ScriptCommand resolution;
- private Object parsearg(StreamTokenizer st) {
- Object ret;
- switch (st.nextToken()) {
- case ']':
- case ')':
- case '}':
- st.pushBack();
- case ';':
- case StreamTokenizer.TT_EOF:
- case '\n':
- return null;
- case '{':
- return parsecmds(st, '}');
- case '[':
- return parsecmds(st, ']');
- case '(':
- return parsecmds(st, ')');
- case StreamTokenizer.TT_WORD:{
- char c = st.sval.charAt(0);
- if ('0' <= c && c <= '9' || c == '.' || c == '-') {
- try {
- return Double.valueOf(st.sval);
- } catch(Exception e) {
- }
- }
- }
- default:
- if (st.sval == null)
- throw new MalformedScriptException("Argument expected");
- return st.sval;
- }
- }
- private void parseargs(StreamTokenizer st, int slot) {
- Object arg = parsearg(st);
- if (arg != null) {
- parseargs(st, slot + 1);
- args[slot] = arg;
- } else if (slot > 0)
- args = new Object[slot];
- }
- ActiveFunction (StreamTokenizer st, String firstword) {
- command = firstword.toLowerCase();
- parseargs(st, 0);
- }
- private ActiveFunction parsecmds(StreamTokenizer st, int term) {
- ActiveFunction cmd = ScriptContext.parse(st);
- if (cmd != null)
- cmd.next = parsecmds(st, term);
- if (term != 0 && term != st.ttype)
- throw new MalformedScriptException(term+" expected");
- return cmd;
- }
- private String thisString() {
- String ret = null;
- if (command == immediate_cmd) {
- for (int i = 0; i < args.length; i++)
- ret = (ret == null ? "[" : ret + "; ") + args[i];
- ret = ret = ret + "]";
- } else if (command == later_cmd) {
- for (int i = 0; i < args.length; i++)
- ret = (ret == null ? "{" : ret + ";\n\t") + args[i];
- ret = ret = ret + "}";
- } else {
- ret = command;
- if (args != null)
- for (int i = 0; i < args.length; i++)
- ret = ret + " " + args[i];
- }
- return ret;
- }
- public String toString() {
- String s = thisString();
- for (ActiveFunction p = next; p != null; p = p.next)
- s = s + "; " + p.thisString();
- return "(" + s + ")";
- }
- }
-