Back to index

wims  3.65+svn20090927
Token.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 class Token {
00020     //kind
00021     static final int 
00022         PREFIX = 1,
00023         LEFT   = 2,
00024         RIGHT  = 3,
00025         SUFIX  = 4;
00026 
00027     final int priority;
00028     final int assoc;
00029     final int id;
00030     final byte vmop;
00031 
00032     double value = 0;    //for NUMBER only
00033     String name  = null; //for CONST & CALL
00034     int arity;
00035     int position;        //pos inside expression
00036 
00037     Token(int id, int priority, int assoc, int vmop) {
00038         this.id = id;
00039         this.priority = priority;
00040         this.assoc = assoc;
00041         this.vmop = (byte)vmop;
00042         arity = id == Lexer.CALL ? 1 : Symbol.CONST_ARITY;
00043     }
00044     
00045     Token setPos(int pos) {
00046         this.position = pos;
00047         return this;
00048     }
00049 
00050     Token setValue(double value) {
00051         this.value = value;
00052         return this;
00053     }
00054 
00055     Token setAlpha(String alpha) {
00056         name = alpha;
00057         return this;
00058     }
00059 
00060     public String toString() {
00061         switch (id) {
00062         case Lexer.NUMBER:
00063             return "" + value;
00064         case Lexer.CALL:
00065             return name + '(' + arity + ')';
00066         case Lexer.CONST:
00067             return name;
00068         }
00069         return "" + id;
00070     }
00071 }