Back to index

wims  3.65+svn20090927
DeclarationParser.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007-2008 Mihai Preda.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 package org.javia.arity;
00018 
00019 import java.util.Vector;
00020 
00021 class DeclarationParser extends TokenConsumer {
00022     static final String NO_ARGS[] = new String[0];
00023     static final int UNKNOWN_ARITY = -2;
00024     static final int MAX_ARITY = 5;
00025         
00026     String name;
00027     int arity = UNKNOWN_ARITY;
00028     Vector args = new Vector();
00029 
00030     private SyntaxException exception;
00031 
00032     DeclarationParser(SyntaxException e) {
00033         this.exception = e;
00034     }
00035 
00036     //@Override
00037     void start() {
00038         name = null;
00039         args.setSize(0);
00040     }
00041 
00042     //@Override
00043     void push(Token token) throws SyntaxException {
00044         switch (token.id) {
00045         case Lexer.CALL:
00046             if (name == null) {
00047                 name = token.name;
00048                 arity = 0;
00049             } else {
00050                 throw exception.set("repeated CALL in declaration", token.position);
00051             }
00052             break;
00053 
00054         case Lexer.CONST:
00055             if (name == null) {
00056                 name = token.name;
00057                 arity = UNKNOWN_ARITY;
00058             } else if (arity >= 0) {
00059                 args.addElement(token.name);
00060                 ++arity;
00061                 if (arity > MAX_ARITY) {
00062                     throw exception.set("Arity too large " + arity, token.position);
00063                 }
00064             } else {
00065                 throw exception.set("Invalid declaration", token.position);
00066             }
00067             break;
00068 
00069         case Lexer.RPAREN:            
00070         case Lexer.COMMA:
00071         case Lexer.END:
00072             break;
00073 
00074         default:
00075             throw exception.set("invalid token in declaration", token.position);
00076         }
00077     }
00078     
00079     String[] argNames() {
00080         if (arity > 0) {
00081             String argNames[] = new String[arity];
00082             args.copyInto(argNames);
00083             return argNames;
00084         } else {
00085             return NO_ARGS;
00086         }
00087     }
00088 }