os/graphics/graphicstools/bitmapfonttools/src/LEXICAL.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicstools/bitmapfonttools/src/LEXICAL.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,311 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Header LEXICAL.CPP
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +#include "LEXICAL.H"
    1.24 +
    1.25 +Lexical::Lexical()
    1.26 + :	iType(ELexNL), iNumber(0)
    1.27 +	{
    1.28 +	iText[0] = '\0';
    1.29 +	}
    1.30 +
    1.31 +Lexical::Lexical(const Lexical& aLex)
    1.32 +	{
    1.33 +	iType = aLex.iType;
    1.34 +	iNumber = aLex.iNumber;
    1.35 +	strcpy(iText, aLex.iText);
    1.36 +	}
    1.37 +
    1.38 +Lexical& Lexical::operator = (const Lexical& aLex)
    1.39 +	{
    1.40 +	iType = aLex.iType;
    1.41 +	iNumber = aLex.iNumber;
    1.42 +	strcpy(iText, aLex.iText);
    1.43 +	return *this;
    1.44 +	}
    1.45 +
    1.46 +int Lexical::CovertStringToHex()
    1.47 +	{
    1.48 +	char* curPtr = iText; // Position of current lexical in line
    1.49 +	int hexDigit;
    1.50 +	int number = 0;
    1.51 +
    1.52 +	while (HexDigit(*curPtr, hexDigit))
    1.53 +		{
    1.54 +		number = (16 * number) + hexDigit;
    1.55 +		curPtr++;
    1.56 +		}
    1.57 +	return number;
    1.58 +	}
    1.59 +
    1.60 +int Lexical::HexDigit(char aDigit, int& decimalEquivalent)
    1.61 +	{
    1.62 +	boolean validDigit = efalse;
    1.63 +	if ((aDigit >= '0') && (aDigit <= '9'))
    1.64 +		{
    1.65 +		decimalEquivalent = (aDigit - '0');
    1.66 +		validDigit = etrue;
    1.67 +		}
    1.68 +	else if ((aDigit >= 'a') && (aDigit <= 'f'))
    1.69 +		{
    1.70 +		decimalEquivalent = 10 + (aDigit - 'a');
    1.71 +		validDigit = etrue;
    1.72 +		}
    1.73 +	else if ((aDigit >= 'A') && (aDigit <= 'F'))
    1.74 +		{
    1.75 +		decimalEquivalent = 10 + (aDigit - 'A');
    1.76 +		validDigit = etrue;
    1.77 +		}
    1.78 +	return validDigit;
    1.79 +	}
    1.80 +
    1.81 +ostream& operator << (ostream& out, const Lexical& aLex)
    1.82 +	{
    1.83 +	switch (aLex.iType)
    1.84 +		{
    1.85 +		case ELexEOF:
    1.86 +			{
    1.87 +			out << "EOF";
    1.88 +			break;
    1.89 +			}
    1.90 +		case ELexNL:
    1.91 +			{
    1.92 +			out << "NL";
    1.93 +			break;
    1.94 +			}
    1.95 +		case ELexNumber:
    1.96 +			{
    1.97 +			out << aLex.iNumber;
    1.98 +			break;
    1.99 +			}
   1.100 +		case ELexOperator:
   1.101 +			{
   1.102 +			out << aLex.iText[0];
   1.103 +			break;
   1.104 +			}
   1.105 +		default:
   1.106 +			{
   1.107 +			out << aLex.iText;
   1.108 +			}
   1.109 +		}	 
   1.110 +	return out;
   1.111 +	}
   1.112 +
   1.113 +LexAnal::LexAnal(const char* aFilename)
   1.114 + :	iFilename(aFilename)
   1.115 +	{
   1.116 +	iFin.open(aFilename);
   1.117 +	iLex.iType = ELexNL;
   1.118 +	iLineNo = 0;
   1.119 +	}
   1.120 +
   1.121 +Lexical LexAnal::Read() // read next lexical into iLex
   1.122 +	{
   1.123 +	if (iLex.iType == ELexNL)
   1.124 +		{
   1.125 +		do
   1.126 +			{
   1.127 +			GetNextLex();
   1.128 +			}
   1.129 +		while (iLex.iType == ELexNL);
   1.130 +		}
   1.131 +	else
   1.132 +		GetNextLex();
   1.133 +	return iLex;
   1.134 +	}
   1.135 +
   1.136 +Lexical LexAnal::ReadNextLine() // read first lex on next line
   1.137 +	{
   1.138 +	GetNextLine();
   1.139 +	return iLex;
   1.140 +	}
   1.141 +
   1.142 +void LexAnal::Report()
   1.143 +	{
   1.144 +	cerr << iFilename.Text() << '(' << iLineNo << "): \n";
   1.145 +	cerr << iLine << '\n';
   1.146 +	for	(char* p = iLine; p < iLexPtr; p++)
   1.147 +		cerr << ' ';
   1.148 +	cerr << "^\n";
   1.149 +	}
   1.150 +
   1.151 +LexAnal::~LexAnal()
   1.152 +	{
   1.153 +	iFin.close();
   1.154 +	}
   1.155 +
   1.156 +void LexAnal::GetNextLex()
   1.157 +	{
   1.158 +	char ch;
   1.159 +	if (iLex.iType == ELexNL)
   1.160 +		{
   1.161 +		iFin.getline(iLine, MaxLineLen);
   1.162 +		// Remove any CR character that appear at the end when
   1.163 +		// reading a dos file on unix.
   1.164 +		PurgeLastCR(iLine);
   1.165 +		iCurPtr = iLine;
   1.166 +		iLineNo++;
   1.167 +		}
   1.168 +
   1.169 +	while ((*iCurPtr == ' ') || (*iCurPtr == '\t'))
   1.170 +		iCurPtr++;
   1.171 +	ch = *iCurPtr;
   1.172 +	iLexPtr = iCurPtr;
   1.173 +
   1.174 +	if ((ch == '\0') && (iFin.eof()))	// finds lexical type
   1.175 +		iLex = ReadEOF();
   1.176 +	else if ((ch == '\0') || (ch == '!'))	// ! is a comment
   1.177 +		iLex = ReadNewLine();
   1.178 +	else if ((ch == '-') || ((ch >= '0') && (ch <= '9')))
   1.179 +		iLex = ReadNumber();
   1.180 +	else if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || (ch == '_'))
   1.181 +		iLex = ReadIdent();
   1.182 +	else if	(ch == '"')
   1.183 +		iLex = ReadString();
   1.184 +	else
   1.185 +		iLex = ReadOperator();	
   1.186 +	}
   1.187 +
   1.188 +void LexAnal::GetNextLine()
   1.189 +	{
   1.190 +	iFin.getline(iLine, MaxLineLen);
   1.191 +	// Remove any CR character that appear at the end when
   1.192 +	// reading a dos file on unix.
   1.193 +	PurgeLastCR(iLine);
   1.194 +	iCurPtr = iLine;
   1.195 +	iLineNo++;
   1.196 +
   1.197 +	char ch;
   1.198 +	while ((*iCurPtr == ' ') || (*iCurPtr == '\t'))
   1.199 +		iCurPtr++;
   1.200 +	ch = *iCurPtr;
   1.201 +	iLexPtr = iCurPtr;
   1.202 +
   1.203 +	if ((ch == '\0') && (iFin.eof()))	// finds lexical type
   1.204 +		iLex = ReadEOF();
   1.205 +	else if ((ch == '\0') || (ch == '!'))
   1.206 +		iLex = ReadNewLine();
   1.207 +	else if ((ch == '-') || ((ch >= '0') && (ch <= '9')))
   1.208 +		iLex=ReadNumber();
   1.209 +	else if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || (ch == '_'))
   1.210 +		iLex = ReadIdent();
   1.211 +	else if	(ch == '"')
   1.212 +		iLex = ReadString();
   1.213 +	else
   1.214 +		iLex = ReadOperator();	
   1.215 +	}
   1.216 +
   1.217 +void LexAnal::PurgeLastCR(char *aLine)
   1.218 +	{
   1.219 +	int len = strlen(aLine) - 1;
   1.220 +	if (len >= 0 && aLine[len] == '\r')
   1.221 +		{
   1.222 +		aLine[len] = '\0';
   1.223 +		}
   1.224 +	}
   1.225 +
   1.226 +Lexical LexAnal::ReadEOF()
   1.227 +	{
   1.228 +	Lexical lex;
   1.229 +	lex.iType = ELexEOF;
   1.230 +	return lex;
   1.231 +	}
   1.232 +
   1.233 +Lexical LexAnal::ReadNewLine()
   1.234 +	{
   1.235 +	Lexical lex;
   1.236 +	lex.iType = ELexNL;
   1.237 +	while (*iCurPtr != '\0')
   1.238 +		iCurPtr++;
   1.239 +	return lex;
   1.240 +	}
   1.241 +
   1.242 +Lexical LexAnal::ReadNumber()
   1.243 +	{
   1.244 +	Lexical lex;
   1.245 +	char ch;
   1.246 +	boolean negative = efalse;
   1.247 +	lex.iType = ELexNumber;
   1.248 +	if (*iCurPtr == '-')
   1.249 +		{
   1.250 +		negative = etrue;
   1.251 +		iCurPtr++;
   1.252 +		}
   1.253 +	ch = *iCurPtr;
   1.254 +	while ((ch >= '0') && (ch <= '9'))
   1.255 +		{
   1.256 +		if (negative)
   1.257 +			lex.iNumber = (10 * lex.iNumber) - (*iCurPtr - '0');
   1.258 +		else
   1.259 +			lex.iNumber=(10 * lex.iNumber) + (*iCurPtr - '0');
   1.260 +		iCurPtr++;
   1.261 +		ch = *iCurPtr;
   1.262 +		}
   1.263 +	return lex;
   1.264 +	}
   1.265 +
   1.266 +
   1.267 +Lexical LexAnal::ReadIdent()
   1.268 +	{
   1.269 +	Lexical lex;
   1.270 +	char ch;
   1.271 +	lex.iType = ELexIdent;
   1.272 +	do
   1.273 +		{
   1.274 +		iCurPtr++;
   1.275 +		ch = *iCurPtr;
   1.276 +		}
   1.277 +	while (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || (ch == '_') || ((ch >= '0') && (ch <= '9')));
   1.278 +	strncpy(lex.iText, iLexPtr, iCurPtr - iLexPtr);
   1.279 +	lex.iText[iCurPtr - iLexPtr] = '\0';
   1.280 +	return lex;
   1.281 +	}
   1.282 +
   1.283 +Lexical LexAnal::ReadString()
   1.284 +	{
   1.285 +	Lexical lex;
   1.286 +	char ch;
   1.287 +	lex.iType = ELexString;
   1.288 +	iCurPtr++;
   1.289 +	ch = *iCurPtr;
   1.290 +	while ((ch != '"') && (*iCurPtr != '\0'))
   1.291 +		{
   1.292 +		iCurPtr++;
   1.293 +		ch = *iCurPtr;
   1.294 +		}
   1.295 +	strncpy(lex.iText, iLexPtr + 1, iCurPtr - (iLexPtr + 1));
   1.296 +	lex.iText[iCurPtr - (iLexPtr + 1)] = '\0';
   1.297 +	if (ch == '"')
   1.298 +		iCurPtr++;	// finds position after last double quotes 
   1.299 +	else
   1.300 +		{
   1.301 +		cerr << "Warning: missing quotes\n";
   1.302 +		Report();
   1.303 +		}
   1.304 +	return lex;
   1.305 +	}
   1.306 +
   1.307 +Lexical LexAnal::ReadOperator()
   1.308 +	{
   1.309 +	Lexical lex;
   1.310 +	lex.iType = ELexOperator;
   1.311 +	lex.iText[0] = *iCurPtr;
   1.312 +	iCurPtr++;
   1.313 +	return lex;
   1.314 +	}