001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.contract.lang.parser;
028:
029: import java.io.*;
030: import java.util.*;
031:
032: import org.cougaar.lib.contract.lang.*;
033: import org.cougaar.lib.contract.lang.op.OpBuilder;
034: import org.cougaar.lib.contract.lang.type.TypeImpl;
035: import org.cougaar.lib.contract.lang.type.TypeListImpl;
036:
037: /**
038: * Implementation of <code>OpParser</code>, which parses an
039: * <code>Op</code>.
040: */
041: public class OpParserImpl implements OpParser {
042:
043: public static final boolean DEBUG = false;
044:
045: protected TypeList currentTypeList;
046:
047: protected VisitTokenizer visTokenizer;
048:
049: public void parse(VisitTokenizer visTokenizer) throws Exception {
050: this .visTokenizer = visTokenizer;
051: if (DEBUG) {
052: System.out.println("parsed: ");
053: System.out.println(ParenParser.toString(visTokenizer));
054: }
055: // set default instance assumption to "is:Object"
056: setTypeList(Object.class);
057: }
058:
059: public Op nextOp() throws ParseException {
060: int tok = visTokenizer.nextToken();
061: switch (tok) {
062: case VisitTokenizer.TT_END:
063: if (DEBUG) {
064: System.out.println("@@ end");
065: }
066: return null;
067: case VisitTokenizer.TT_END_OF_TREE:
068: return null;
069: case VisitTokenizer.TT_WORD: {
070: String sval = visTokenizer.getWord();
071: if (DEBUG) {
072: System.out.println("@@ word " + sval);
073: }
074: Op newOp = OpBuilder.create(sval);
075: if (DEBUG) {
076: System.out
077: .println("@@ created " + sval + " = " + newOp);
078: }
079: try {
080: // attempt parse
081: newOp = newOp.parse(this );
082: } catch (ParseException pe) {
083: // add trace and re-throw
084: pe.addTrace(sval);
085: throw pe;
086: } catch (Exception e) {
087: // create trace
088: ParseException npe = new ParseException(e);
089: npe.addTrace(sval);
090: throw npe;
091: }
092: if (DEBUG) {
093: System.out.println("@@ parsed " + sval + " = " + newOp);
094: }
095: return newOp;
096: }
097: case VisitTokenizer.TT_CONSTANT: {
098: String consttype = visTokenizer.getConstantType();
099: String constvalue = visTokenizer.getConstantValue();
100: if (DEBUG) {
101: System.out.println("@@ const " + consttype + " "
102: + constvalue);
103: }
104: Op newOp = OpBuilder
105: .createConstantOp(consttype, constvalue);
106: if (DEBUG) {
107: System.out.println("@@ const " + consttype + " "
108: + constvalue + " = " + newOp);
109: }
110: return newOp;
111: }
112: default:
113: throw new InternalError("No such token: " + tok);
114: }
115: }
116:
117: /**
118: * @see org.cougaar.lib.contract.lang.OpParser
119: */
120: public void setTypeList(final TypeList tl) {
121: if (tl == null) {
122: throw new IllegalArgumentException(
123: "Parser given null TypeList");
124: }
125: currentTypeList = tl;
126: }
127:
128: public void setTypeList(final Class cl) {
129: currentTypeList = new TypeListImpl(TypeImpl.getInstance(false,
130: cl));
131: }
132:
133: public int addType(final Type type) {
134: return currentTypeList.add(type);
135: }
136:
137: public TypeList getTypeList() {
138: return currentTypeList;
139: }
140:
141: public TypeList cloneTypeList() {
142: return (TypeList) currentTypeList.clone();
143: }
144: }
|