1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
* TreeLib
* A library for manipulating phylogenetic trees.
* Copyright (C) 2001 Roderic D. M. Page <r.page@bio.gla.ac.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// $Id: Parse.cpp,v 1.9 2002/02/23 12:22:32 rdmp1c Exp $
#include <ctype.h>
#include <string.h>
#include "Parse.h"
// Return the next token in the string
tokentype Parser::NextToken ()
{
tokentype t;
token = "";
if (pos >= text.length())
{
t = ENDOFSTRING;
}
else
{
char ch = text[pos];
if (ch == '\'')
{
// Single quoted token
bool done = false;
pos++;
while (!done)
{
ch = text[pos++];
// Look ahead for double quote
if (ch == '\'')
{
ch = text[pos];
done = (ch != '\'');
}
if (!done && (ch != '\n') && (ch != '\r'))
{
token += ch;
}
}
t = STRING; //classify the token
}
else if (isalpha (ch))
{
while (isalnum (ch) || (ch == '_') || (ch == '.') && (pos < text.length()))
{
if (ch == '_')
token += ' ';
else
token += ch;
pos++;
ch = text[pos];
}
t = STRING; //classify the token
}
else
{
if (isdigit(ch) || (ch == '-'))
{
t = ParseNumber();
// int stop = text.find_first_not_of ("01234567890-.Ee", pos);
// token = text.substr (pos, stop-pos);
// pos = stop;
// t = NUMBER;
}
else
{
token += ch;
pos++;
switch (ch)
{
case '(': t = LPAR; break;
case ')': t = RPAR; break;
case ' ': t = SPACE; break;
case ',': t = COMMA; break;
case ';': t = SEMICOLON; break;
case ':': t = COLON; break;
case '\t': t = TAB; break;
case '\r':
case '\n': t = NEWLINE; break;
default: t = BAD; break;
}
}
}
}
// cout << "token = " << token << endl;
return (t);
}
//------------------------------------------------------------------------------
// Parse a number (integer or real).
tokentype Parser::ParseNumber ()
{
enum {
start = 0x0001, // 0
sign = 0x0002, // 1
digit = 0x0004, // 2
fraction = 0x0008, // 3
expsymbol = 0x0010, // 4
expsign = 0x0020, // 5
exponent = 0x0040, // 6
bad = 0x0080,
done = 0x0100
} state;
tokentype result = BAD;
token = "";
state = start;
char curChar = text[pos];
while (!IsWhiteSpace (curChar)
&& !(IsPunctuation (curChar) && (curChar != '-'))
&& (state != bad)
&& (state != done))
{
if (isdigit (curChar))
{
switch (state)
{
case start:
case sign:
state = digit;
break;
case expsymbol:
case expsign:
state = exponent;
break;
default:
break;
}
}
else if ((curChar == '-') || (curChar == '+'))
{
switch (state)
{
case start:
state = sign; // sign of number
break;
case digit:
state = done; // minus sign is punctuation, such as 6-10
break;
case expsymbol:
state = expsign; // sign of exponent
break;
default:
state = bad; // syntax error
break;
}
}
else if ((curChar == '.') && (state == digit))
state = fraction;
else if (((curChar == 'E') || (curChar == 'e')) && (state & (digit | fraction)))
state = expsymbol;
else
state = bad;
if ((state != bad) && (state != done))
{
token += curChar;
curChar = GetNextChar ();
}
}
int isNumber = state & (digit | fraction | exponent | done);
if (isNumber)
{
// We have a number
result = NUMBER;
if (IsPunctuation (curChar))
{
// PutBack (curChar);
// if (!atEOL)
// filecol--;
}
}
else
{
// Not a number, but a string that starts with numbers, such as "00BW0762.1"
// cout << "Do something!" << endl;
do {
if (curChar == '_')
token += ' ';
else
token += curChar;
curChar = GetNextChar ();
} while (isalnum (curChar) || (curChar == '_') || (curChar == '.') && (pos < text.length()));
result = STRING; //classify the token
}
return result;
}
//------------------------------------------------------------------------------
bool Parser::IsPunctuation (char ch)
{
char punctuation[22];
punctuation[0] = '(';
punctuation[1] = ')';
punctuation[2] = '[';
punctuation[3] = ']';
punctuation[4] = '{';
punctuation[5] = '}';
punctuation[6] = '/';
punctuation[7] = '\\';
punctuation[8] = ',';
punctuation[9] = ';';
punctuation[10] = ':';
punctuation[11] = '=';
punctuation[12] = '*';
punctuation[13] = '\'';
punctuation[14] = '"';
punctuation[15] = '`';
punctuation[16] = '+';
punctuation[17] = '-';
punctuation[18] = '<';
punctuation[19] = '>';
punctuation[20] = '!';
punctuation[21] = '#';
punctuation[22] = '\0';
return (bool)(strchr (punctuation, ch) != NULL);
}
//------------------------------------------------------------------------------
bool Parser::IsWhiteSpace (char ch)
{
char whitespace[4];
whitespace[0] = ' ';
whitespace[1] = '\t';
whitespace[2] = '\n';
whitespace[3] = '\0';
return (bool)(strchr (whitespace, ch) != NULL);
}
|