001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: * Contributor(s): Ivan Soleimanipour.
042: */
043:
044: package org.netbeans.lib.terminalemulator;
045:
046: public abstract class AbstractInterp implements Interp {
047:
048: protected interface Actor {
049: public String action(AbstractInterp interp, char c);
050: }
051:
052: protected static class State {
053:
054: // some generic actors
055: Actor act_error = new Actor() {
056: public String action(AbstractInterp ai, char c) {
057: return "generic error"; // NOI18N
058: }
059: };
060:
061: public String name() {
062: return name;
063: }
064:
065: private String name;
066:
067: class Action {
068: public State new_state = null;
069: public Actor actor = act_error;
070: };
071:
072: private Action action[] = new Action[128];
073: private Action action_regular = new Action();
074:
075: public State(String name) {
076: this .name = name;
077: for (int i = 0; i < action.length; i++)
078: action[i] = new Action();
079: action_regular.actor = null;
080: action_regular.new_state = null;
081: }
082:
083: /*
084: * Specify the state action_regular will transition to.
085: */
086: public void setRegular(State new_state, Actor actor) {
087: action_regular.actor = actor;
088: action_regular.new_state = new_state;
089: }
090:
091: public void setAction(char c, State new_state, Actor actor) {
092: if ((int) c > 127)
093: return;
094: action[c].actor = actor;
095: action[c].new_state = new_state;
096: }
097:
098: public Action getAction(char c) {
099: if ((int) c > 127)
100: return action_regular;
101: return action[c];
102: }
103: };
104:
105: // Why make these be public and not protected?
106: // Someone might inherit from us in a package other than org.netbeans
107: // and while the inherited Interp will see these if they are protected,
108: // the corresponding InterpType won't.
109:
110: /*
111: */
112: public Ops ops;
113: public State state; // current state
114:
115: /*
116: protected Ops ops;
117: protected State state; // current state
118: */
119:
120: protected AbstractInterp(Ops ops) {
121: this .ops = ops;
122: }
123:
124: public void reset() {
125: ;
126: }
127:
128: /*
129: * Management of number parsing
130: */
131:
132: private static final int max_numbers = 5;
133: private int numberx = 0;
134: private String number[] = new String[max_numbers];
135:
136: protected void resetNumber() {
137: for (int x = 0; x < max_numbers; x++) {
138: number[x] = "";
139: }
140: numberx = 0;
141: }
142:
143: protected void remember_digit(char c) {
144: number[numberx] += c;
145: }
146:
147: protected boolean pushNumber() {
148: numberx++;
149: return (numberx < max_numbers);
150: }
151:
152: protected boolean noNumber() {
153: return number[0].equals(""); // NOI18N
154: }
155:
156: protected int numberAt(int position) {
157: if (position > numberx) {
158: return 1;
159: }
160: return Integer.parseInt(number[position]);
161: }
162:
163: protected int nNumbers() {
164: return numberx;
165: }
166:
167: }
|