00001
00014 #include "jbltools/sfh/JBLToken.h"
00015
00016 #include <iostream>
00017 #include <sstream>
00018
00019
00020
00021
00022
00023
00024 using namespace std;
00025
00026
00027 static const char *ident="@(#)$Id: JBLToken.C,v 1.8 2005/07/08 15:01:34 blist Exp $";
00028
00029 JBLToken::JBLToken()
00030 {}
00031
00032 bool JBLToken::isoperator (char c) {
00033 return (c=='=' || c=='+' || c=='-' || c=='*' || c=='/' ||
00034 c=='<' || c=='>' || c=='!' || c=='^' ||
00035 c=='|' || c=='&' || c=='~');
00036 }
00037
00038 bool JBLToken::issimpleoperator (char c) {
00039 return (c==',');
00040 }
00041
00042 bool JBLToken::isparanthesis (char c) {
00043
00044 return (c=='(' || c==')' || c=='{' || c=='}');
00045 }
00046
00047 bool JBLToken::input(istream& is) {
00048 type = EMPTY;
00049 stringvalue = "";
00050 char c;
00051
00052 while (isblank(is.peek())) is.get();
00053
00054 c = is.peek();
00055 if (c == EOF) return false;
00056 if (isalpha(c)) {
00057 type = IDENTIFIER;
00058
00059 while (isalnum(c) || c=='_' || c=='[' || c==']' || c=='.' || c==':') {
00060 stringvalue+=is.get();
00061 c = is.peek();
00062 }
00063 if (stringvalue == "true") {
00064 type = BOOL;
00065 boolvalue = true;
00066 } else if (stringvalue == "false") {
00067 type = BOOL;
00068 boolvalue = false;
00069 }
00070 }
00071 else if (isdigit(c) || c=='.') {
00072 type = INT;
00073 bool pointallowed = true;
00074 bool signallowed = false;
00075 bool expallowed = true;
00076 while (isdigit(c) ||
00077 (pointallowed && c=='.') ||
00078 (expallowed && (c=='e' || c=='E')) ||
00079 (signallowed && (c=='+' || c=='-'))) {
00080 if (!isdigit(c)) type = FLOAT;
00081 if (c=='.') pointallowed = false;
00082 if (c=='e' || c=='E')
00083 {expallowed = false; signallowed = true;}
00084 else
00085 {signallowed = false;}
00086 stringvalue+=is.get();
00087 c = is.peek();
00088 }
00089 if ((type == INT) && (1 == sscanf (stringvalue.c_str(), "%d", &intvalue))) {
00090 floatvalue = intvalue;
00091
00092 }
00093 else if (1 == sscanf (stringvalue.c_str(), "%f", &floatvalue)) {
00094 type = FLOAT;
00095
00096 }
00097 else {
00098 type = UNKNOWN;
00099 cerr << "JBLToken::input: '" << stringvalue << "' is an ill-formatted number!\n";
00100 }
00101 }
00102 else if (issimpleoperator(c)) {
00103 type = OPERATOR;
00104 stringvalue+=is.get();
00105 }
00106 else if (isoperator(c)) {
00107 type = OPERATOR;
00108 while (isoperator(c)) {
00109 stringvalue+=is.get();
00110 c = is.peek();
00111 }
00112 }
00113 else if (isparanthesis(c)) {
00114 type = PARANTHESIS;
00115 stringvalue = is.get();
00116 }
00117 else {
00118 type = UNKNOWN;
00119 stringvalue = is.get();
00120 cerr << "token.input: illegal character '" << stringvalue << "'!\n";
00121 }
00122
00123 #if DEBUG>0
00124 cout << "JBLToken::input: " << *this << endl;
00125 #endif
00126 return stringvalue != "";
00127 }
00128
00129 std::ostream& JBLToken::print(std::ostream& os) const {
00130 if (isEmpty()) os << "EMPTY";
00131 else if (isUnknown()) os << "UNKNOWN";
00132 else if (isIdentifier()) os << "IDENTIFIER '" << getStringValue() << "'";
00133 else if (isInt()) os << "INT " << getIntValue();
00134 else if (isFloat()) os << "FLOAT " << getFloatValue();
00135 else if (isBool()) os << "BOOL " << (getBoolValue() ? "true" : "false");
00136 else if (isOperator()) os << "IDENTIFIER '" << getStringValue() << "'";
00137 else if (isParanthesis()) os << "PARANTHESIS '" << getStringValue() << "'";
00138
00139 return os;
00140 }
00141
00142 istream& operator>> (istream& is, JBLToken& token) {
00143 token.input (is);
00144 return is;
00145 }
00146
00147 ostream& operator<< (ostream& os, const JBLToken& token) {
00148 return token.print (os);
00149 }