This commit is contained in:
dario 1998-05-15 18:49:17 +00:00
Родитель 203b2f54b4
Коммит 62b8228fb5
21 изменённых файлов: 427 добавлений и 33 удалений

Просмотреть файл

@ -90,6 +90,13 @@ public:
~ParameterParsingException() {}
};
class ConstParsingException : public ParserException
{
public:
ConstParsingException(char *aMessage) : ParserException(aMessage) {}
~ConstParsingException() {}
};
class EndOfFileException : public Exception
{
public:

Просмотреть файл

@ -17,6 +17,19 @@
*/
#include "IdlAttribute.h"
#include <ostream.h>
ostream& operator<<(ostream &s, IdlAttribute &aAttribute)
{
if (aAttribute.GetReadOnly()) {
s << "readonly ";
}
s << "attribute ";
char type[128];
aAttribute.GetTypeAsString(type, 128);
return s << type << " " << aAttribute.GetName() << ";";
}
IdlAttribute::IdlAttribute()
{

Просмотреть файл

@ -35,5 +35,8 @@ public:
};
class ostream;
ostream& operator<<(ostream &s, IdlAttribute &aAttribute);
#endif

Просмотреть файл

@ -19,6 +19,33 @@
#include "IdlEnum.h"
#include "nsVoidArray.h"
#include "IdlVariable.h"
#include <ostream.h>
ostream& operator<<(ostream &s, IdlEnum &aEnum)
{
s << "enum { \n";
long count = aEnum.EnumeratorCount();
if (count) {
for (int i = 0; i < count - 1; i++) {
IdlVariable *enumerator = aEnum.GetEnumeratorAt(i);
s << " " << enumerator->GetName();
if (TYPE_INT == enumerator->GetType()) {
s << " = " << enumerator->GetLongValue();
}
s << ",\n";
}
IdlVariable *enumerator = aEnum.GetEnumeratorAt(i);
s << " " << enumerator->GetName();
if (TYPE_INT == enumerator->GetType()) {
s << " = " << enumerator->GetLongValue();
}
s << "\n";
}
return s << "}; \n";
}
IdlEnum::IdlEnum()
{

Просмотреть файл

@ -37,5 +37,8 @@ public:
IdlVariable* GetEnumeratorAt(long aIndex);
};
class ostream;
ostream& operator<<(ostream &s, IdlEnum &aEnum);
#endif

Просмотреть файл

@ -21,7 +21,35 @@
#include "IdlObject.h"
#include "nsVoidArray.h"
#include "IdlParameter.h"
#include <string.h>
#include <ostream.h>
ostream& operator<<(ostream &s, IdlFunction &aFunction)
{
char type[128];
aFunction.GetReturnValue()->GetTypeAsString(type, 128);
s << type << " " << aFunction.GetName() << "(";
long count = aFunction.ParameterCount();
if (count) {
for (int i = 0; i < count - 1; i++) {
s << *(aFunction.GetParameterAt(i)) << ", ";
}
s << *(aFunction.GetParameterAt(count - 1));
}
s << ")";
count = aFunction.ExceptionCount();
if (count) {
s << " raises (";
for (int i = 0; i < count - 1; i++) {
s << aFunction.GetExceptionAt(i) << ", ";
}
s << aFunction.GetExceptionAt(count - 1) << ")";
}
return s << ";";
}
IdlFunction::IdlFunction()
{
@ -43,8 +71,8 @@ IdlFunction::~IdlFunction()
}
if (mExceptions) {
for (int i = 0; i < mExceptions->Count(); i++) {
IdlException *excObj = (IdlException*)mExceptions->ElementAt(i);
delete excObj;
char *exc = (char*)mExceptions->ElementAt(i);
delete[] exc;
}
}
}
@ -100,7 +128,9 @@ void IdlFunction::AddException(char *aException)
if (!mExceptions) {
mExceptions = new nsVoidArray();
}
mExceptions->AppendElement((void*)aException);
char *exc = new char[strlen(aException) + 1];
strcpy(exc, aException);
mExceptions->AppendElement((void*)exc);
}
}

Просмотреть файл

@ -48,5 +48,8 @@ public:
char* GetExceptionAt(long aIndex);
};
class ostream;
ostream& operator<<(ostream &s, IdlFunction &aFunction);
#endif

Просмотреть файл

@ -24,6 +24,61 @@
#include "IdlFunction.h"
#include <string.h>
#include <ostream.h>
ostream& operator<<(ostream &s, IdlInterface &aInterface)
{
int i;
// write the interface header
s << "interface " << aInterface.GetName();
long count = aInterface.BaseClasseCount();
if (count) {
s << " : ";
for (int i = 0; i < count - 1; i++) {
s << aInterface.GetBaseClassAt(i) << ", ";
}
s << aInterface.GetBaseClassAt(count - 1);
}
// write the interface body
s << " { \n";
// all the consts
for (i = 0; i < aInterface.ConstCount(); i++) {
IdlVariable *constObj = aInterface.GetConstAt(i);
char type[128];
constObj->GetTypeAsString(type, 128);
s << " const " << type << " " << constObj->GetName() << " = ";
Type constType = constObj->GetType();
if (constType == TYPE_INT ||
constType == TYPE_LONG ||
constType == TYPE_SHORT ||
constType == TYPE_UINT ||
constType == TYPE_ULONG ||
constType == TYPE_USHORT) {
s << constObj->GetLongValue() << ";\n";
}
//XXX finish the other cases (string, double,...)
}
// all the enums
for (i = 0; i < aInterface.EnumCount(); i++) {
s << *(aInterface.GetEnumAt(i));
}
// all the attribute
for (i = 0; i < aInterface.AttributeCount(); i++) {
s << " " << *(aInterface.GetAttributeAt(i)) << "\n";
}
// all the functions
for (i = 0; i < aInterface.FunctionCount(); i++) {
s << " " << *(aInterface.GetFunctionAt(i)) << "\n";
}
return s << "}; \n";
}
IdlInterface::IdlInterface()
{
@ -78,7 +133,7 @@ IdlInterface::~IdlInterface()
}
if (mConsts) {
for (int i = 0; i < mConsts->Count(); i++) {
IdlConst *constObj = (IdlConst*)mConsts->ElementAt(i);
IdlVariable *constObj = (IdlVariable*)mConsts->ElementAt(i);
delete constObj;
}
}
@ -99,8 +154,7 @@ IdlInterface::~IdlInterface()
void IdlInterface::InheritsFrom(char *aBase)
{
if (aBase) {
size_t length = strlen(aBase) + 1;
char *baseName = new char[length];
char *baseName = new char[strlen(aBase) + 1];
strcpy(baseName, aBase);
if (!mBaseClasses) {
mBaseClasses = new nsVoidArray();
@ -236,7 +290,7 @@ IdlUnion* IdlInterface::GetUnionAt(long aIndex)
return unionObj;
}
void IdlInterface::AddConst(IdlConst *aConst)
void IdlInterface::AddConst(IdlVariable *aConst)
{
if (aConst) {
if (!mConsts) {
@ -254,11 +308,11 @@ long IdlInterface::ConstCount()
return 0;
}
IdlConst* IdlInterface::GetConstAt(long aIndex)
IdlVariable* IdlInterface::GetConstAt(long aIndex)
{
IdlConst *constObj = (IdlConst*)0;
IdlVariable *constObj = (IdlVariable*)0;
if (mConsts) {
constObj = (IdlConst*)mConsts->ElementAt(aIndex);
constObj = (IdlVariable*)mConsts->ElementAt(aIndex);
}
return constObj;
}

Просмотреть файл

@ -28,7 +28,7 @@ class IdlUnion;
class IdlEnum;
class IdlAttribute;
class IdlFunction;
class IdlConst;
class IdlVariable;
class IdlException;
class IdlInterface : public IdlObject {
@ -62,9 +62,9 @@ public:
void AddUnion(IdlUnion *aUnion);
long UnionCount();
IdlUnion* GetUnionAt(long aIndex);
void AddConst(IdlConst *aConst);
void AddConst(IdlVariable *aConst);
long ConstCount();
IdlConst* GetConstAt(long aIndex);
IdlVariable* GetConstAt(long aIndex);
void AddException(IdlException *aException);
long ExceptionCount();
IdlException* GetExceptionAt(long aIndex);
@ -77,5 +77,8 @@ public:
};
class ostream;
ostream& operator<<(ostream &s, IdlInterface &aInterface);
#endif

Просмотреть файл

@ -35,7 +35,6 @@ public:
#define IdlTypedef IdlObject
#define IdlStruct IdlObject
#define IdlUnion IdlObject
#define IdlConst IdlObject
#define IdlException IdlObject
#endif

Просмотреть файл

@ -17,6 +17,25 @@
*/
#include "IdlParameter.h"
#include <ostream.h>
ostream& operator<<(ostream &s, IdlParameter &aParameter)
{
switch (aParameter.GetAttribute()) {
case IdlParameter::INPUT:
s << "in ";
break;
case IdlParameter::OUTPUT:
s << "out ";
break;
case IdlParameter::INOUT:
s << "inout ";
break;
}
char type[128];
aParameter.GetTypeAsString(type, 128);
return s << type << " " << aParameter.GetName();
}
IdlParameter::IdlParameter()
{

Просмотреть файл

@ -42,5 +42,8 @@ public:
};
class ostream;
ostream& operator<<(ostream &s, IdlParameter &aParameter);
#endif

Просмотреть файл

@ -298,6 +298,7 @@ void IdlParser::ParseInterfaceBody(IdlSpecification &aSpecification, IdlInterfac
if (TERMINATOR_TOKEN != token->id) {
throw InterfaceParsingException("Missing ';'.");
}
TrimComments();
}
// eat "}"
@ -354,7 +355,7 @@ IdlEnum* IdlParser::ParseEnum(IdlSpecification &aSpecification)
// must be an identifier
if (IDENTIFIER_TOKEN == token->id) {
IdlVariable* enumerator = new IdlVariable();
enumerator->SetType(TYPE_INT); // it does not really matter
enumerator->SetType(TYPE_INT); // it does not really matter which type
enumerator->SetName(mScanner->NextToken()->stringID);
enumObj->AddEnumerator(enumerator);
TrimComments();
@ -413,12 +414,101 @@ IdlUnion* IdlParser::ParseUnion(IdlSpecification &aSpecification)
}
/**
* XXX NOT IMPLEMENTED YET
*
*/
IdlConst* IdlParser::ParseConst(IdlSpecification &aSpecification)
IdlVariable* IdlParser::ParseConst(IdlSpecification &aSpecification)
{
throw NotImplementedException();
return (IdlConst*)0;
IdlVariable *constObj = new IdlVariable();
Token *token;
TrimComments();
token = mScanner->NextToken();
// must be the type
switch(token->id) {
// base type
case BOOLEAN_TOKEN:
constObj->SetType(TYPE_BOOLEAN);
break;
case FLOAT_TOKEN:
constObj->SetType(TYPE_FLOAT);
break;
case DOUBLE_TOKEN:
constObj->SetType(TYPE_DOUBLE);
break;
case LONG_TOKEN:
constObj->SetType(TYPE_LONG);
break;
case SHORT_TOKEN:
constObj->SetType(TYPE_SHORT);
break;
case ULONG_TOKEN:
constObj->SetType(TYPE_ULONG);
break;
case USHORT_TOKEN:
constObj->SetType(TYPE_USHORT);
break;
case CHAR_TOKEN:
constObj->SetType(TYPE_CHAR);
break;
case INT_TOKEN:
constObj->SetType(TYPE_INT);
break;
case UINT_TOKEN:
constObj->SetType(TYPE_UINT);
break;
// string type
case STRING_TOKEN:
constObj->SetType(TYPE_STRING);
break;
// scoped name
case IDENTIFIER_TOKEN:
//if (aSpecification.ContainInterface(aToken.stringID)) {
constObj->SetType(TYPE_OBJECT);
constObj->SetTypeName(token->stringID);
break;
//}
default:
delete constObj;
throw ConstParsingException("Unknown type.");
}
TrimComments();
token = mScanner->NextToken();
// an identifier must follow
if (IDENTIFIER_TOKEN == token->id) {
constObj->SetName(token->stringID);
}
else {
delete constObj;
throw ConstParsingException("Missing identifire. Const name undefined.");
}
// the "=" sign
TrimComments();
token = mScanner->NextToken();
if (ASSIGNEMENT_TOKEN != token->id) {
delete constObj;
throw ConstParsingException("Missing identifire. Const name undefined.");
}
TrimComments();
token = mScanner->NextToken();
// must be some kind of constant. Constants token ids start at
// position INTEGER_CONSTANT
if (token->id < INTEGER_CONSTANT) {
delete constObj;
throw ConstParsingException("Missing identifire. Const name undefined.");
}
else if (INTEGER_CONSTANT == token->id) {
constObj->SetValue(token->value.vLong);
}
else if (STRING_CONSTANT == token->id) {
constObj->SetValue(token->value.vString);
}
return constObj;
}
/**
@ -616,6 +706,9 @@ IdlFunction* IdlParser::ParseFunction(IdlSpecification &aSpecification, Token &a
// is it throwing an exception?
TrimComments();
token = mScanner->PeekToken();
//XXX this is how exceptions are declared now. This should change
// to the correct way so that this while loop can be deleted
while (INTERFACE_TOKEN == token->id) {
mScanner->NextToken(); // eat "interface"
TrimComments();
@ -639,6 +732,35 @@ IdlFunction* IdlParser::ParseFunction(IdlSpecification &aSpecification, Token &a
throw FunctionParsingException("Undeclared exception name.");
}
}
if (RAISES_TOKEN == token->id) {
mScanner->NextToken(); // eat "raises"
TrimComments();
token = mScanner->NextToken(); // must be '('
while (FUNC_PARAMS_SPEC_BEGIN_TOKEN == token->id ||
SEPARATOR_TOKEN == token->id) {
TrimComments();
token = mScanner->NextToken(); // must be the exception name
if (IDENTIFIER_TOKEN == token->id) {
funcObj->AddException(token->stringID);
TrimComments();
token = mScanner->NextToken(); // must be ',' or ')'
}
else {
delete funcObj;
throw FunctionParsingException("Undeclared exception name.");
}
}
if (FUNC_PARAMS_SPEC_END_TOKEN != token->id) {
delete funcObj;
throw FunctionParsingException("Missing ')'. Exceptions declaration non terminated.");
}
TrimComments();
token = mScanner->PeekToken();
}
}
else {
delete funcObj;

Просмотреть файл

@ -28,11 +28,11 @@ class IdlTypedef;
class IdlStruct;
class IdlEnum;
class IdlUnion;
class IdlConst;
class IdlException;
class IdlAttribute;
class IdlFunction;
class IdlParameter;
class IdlVariable;
class IdlParser {
private:
@ -53,7 +53,7 @@ protected:
IdlStruct* ParseStruct(IdlSpecification &aSpecification);
IdlEnum* ParseEnum(IdlSpecification &aSpecification);
IdlUnion* ParseUnion(IdlSpecification &aSpecification);
IdlConst* ParseConst(IdlSpecification &aSpecification);
IdlVariable* ParseConst(IdlSpecification &aSpecification);
IdlException* ParseException(IdlSpecification &aSpecification);
IdlAttribute* ParseAttribute(IdlSpecification &aSpecification, int aTokenID);
IdlFunction* ParseFunction(IdlSpecification &aSpecification, Token &aToken);

Просмотреть файл

@ -740,8 +740,8 @@ void IdlScanner::OKeywords(char *aCurrentPos, Token *aToken)
}
//
// 'readonly' is the only keyword starting with 'r'.
// If that is not it, it must be an identifier
// it's either 'readonly' or 'raises' if it is a keyword.
// Otherwise, it must be an identifier
//
void IdlScanner::RKeywords(char *aCurrentPos, Token *aToken)
{
@ -771,6 +771,29 @@ void IdlScanner::RKeywords(char *aCurrentPos, Token *aToken)
aToken->SetToken(READONLY_TOKEN);
}
}
else if (c != EOF && c == 'a' && (*aCurrentPos++ = c) && (c = mInputFile->get()) &&
c != EOF && c == 'i' && (*aCurrentPos++ = c) && (c = mInputFile->get()) &&
c != EOF && c == 's' && (*aCurrentPos++ = c) && (c = mInputFile->get()) &&
c != EOF && c == 'e' && (*aCurrentPos++ = c) && (c = mInputFile->get()) &&
c != EOF && c == 's' && (*aCurrentPos++ = c)) {
// if terminated is a keyword
c = mInputFile->get();
if (c != EOF) {
if (isalpha(c) || isdigit(c) || c == '_') {
// more characters, it must be an identifier
*aCurrentPos++ = c;
Identifier(aCurrentPos, aToken);
}
else {
// it is a keyword
aToken->SetToken(RAISES_TOKEN);
mInputFile->putback(c);
}
}
else {
aToken->SetToken(RAISES_TOKEN);
}
}
else {
// it must be an identifier
KeywordMismatch(c, aCurrentPos, aToken);
@ -786,7 +809,7 @@ void IdlScanner::SKeywords(char *aCurrentPos, Token *aToken)
int c = mInputFile->get();
// chaeck for 'tr'
if (c != EOF && c == 't' && (*aCurrentPos++ = c) && (c = mInputFile->get()) &&
c != EOF && c == 'r' && (*aCurrentPos++ = c)) {
c != EOF && c == 'r' && (*aCurrentPos++ = c) && (c = mInputFile->get())) {
if (c != EOF && c == 'i' && (*aCurrentPos++ = c) && (c = mInputFile->get()) &&
c != EOF && c == 'n' && (*aCurrentPos++ = c) && (c = mInputFile->get()) &&
@ -1059,6 +1082,9 @@ void IdlScanner::Number(int aStartChar, Token *aToken)
if (aStartChar == '.') {
// double. Deal with it later
}
else {
mInputFile->putback(aStartChar);
}
aToken->SetTokenValue(INTEGER_CONSTANT, value * sign);
}

Просмотреть файл

@ -53,6 +53,7 @@ enum TokenType {
INPUT_PARAM_TOKEN,
OUTPUT_PARAM_TOKEN,
INOUT_PARAM_TOKEN,
RAISES_TOKEN,
INHERITANCE_SPEC_TOKEN, // ':'
SEPARATOR_TOKEN, // ','
BEGIN_BLOCK_TOKEN, // '{'
@ -61,8 +62,6 @@ enum TokenType {
ASSIGNEMENT_TOKEN, // '='
FUNC_PARAMS_SPEC_BEGIN_TOKEN, // '('
FUNC_PARAMS_SPEC_END_TOKEN, // ')'
// temp, it changes to ushort or uint or ulong
UNSIGNED_TOKEN,
// constant values
INTEGER_CONSTANT = 1000,
STRING_CONSTANT

Просмотреть файл

@ -22,6 +22,16 @@
#include "nsVoidArray.h"
#include <string.h>
#include <ostream.h>
ostream& operator<<(ostream &s, IdlSpecification &aSpecification)
{
for (int i = 0; i < aSpecification.InterfaceCount(); i++) {
s << *(aSpecification.GetInterfaceAt(i)) << '\n';
}
return s;
}
IdlSpecification::IdlSpecification()
{

Просмотреть файл

@ -40,5 +40,8 @@ public:
};
class ostream;
ostream& operator<<(ostream &s, IdlSpecification &aSpecification);
#endif

Просмотреть файл

@ -43,7 +43,7 @@ void IdlVariable::SetType(Type aType)
mType = aType;
}
Type IdlVariable::GeType()
Type IdlVariable::GetType()
{
return mType;
}
@ -67,24 +67,87 @@ char* IdlVariable::GetTypeName()
return mTypeName;
}
void IdlVariable::GetTypeAsString(char *aString, size_t aStringSize)
{
switch(mType) {
case TYPE_BOOLEAN:
if (aStringSize > 7) {
strcpy(aString, "boolean");
}
break;
case TYPE_FLOAT:
if (aStringSize > 5) {
strcpy(aString, "float");
}
break;
case TYPE_DOUBLE:
if (aStringSize > 6) {
strcpy(aString, "double");
}
break;
case TYPE_LONG:
if (aStringSize > 4) {
strcpy(aString, "long");
}
break;
case TYPE_SHORT:
if (aStringSize > 5) {
strcpy(aString, "short");
}
break;
case TYPE_ULONG:
if (aStringSize > 13) {
strcpy(aString, "unsigned long");
}
break;
case TYPE_USHORT:
if (aStringSize > 14) {
strcpy(aString, "unsigned short");
}
break;
case TYPE_CHAR:
if (aStringSize > 4) {
strcpy(aString, "char");
}
break;
case TYPE_INT:
if (aStringSize > 3) {
strcpy(aString, "int");
}
break;
case TYPE_UINT:
if (aStringSize > 12) {
strcpy(aString, "unsigned int");
}
break;
case TYPE_STRING:
if (aStringSize > 6) {
strcpy(aString, "wstring");
}
break;
case TYPE_OBJECT:
if (aStringSize > strlen(mTypeName)) {
strcpy(aString, mTypeName);
}
break;
}
}
void IdlVariable::SetValue(unsigned long aValue)
{
DeleteStringType();
mType = TYPE_ULONG;
mValue.vLong = aValue;
}
void IdlVariable::SetValue(char aValue)
{
DeleteStringType();
mType = TYPE_CHAR;
mValue.vChar = aValue;
}
void IdlVariable::SetValue(char *aValue)
{
DeleteStringType();
mType = TYPE_STRING;
size_t length = strlen(aValue) + 1;
mValue.vString = new char[length];
strcpy(mValue.vString, aValue);
@ -93,14 +156,12 @@ void IdlVariable::SetValue(char *aValue)
void IdlVariable::SetValue(double aValue)
{
DeleteStringType();
mType = TYPE_DOUBLE;
mValue.vDouble = aValue;
}
void IdlVariable::SetValue(void *aValue)
{
DeleteStringType();
mType = TYPE_OBJECT;
mValue.vObject = aValue;
}

Просмотреть файл

@ -53,9 +53,10 @@ public:
~IdlVariable();
void SetType(Type aType);
Type GeType();
Type GetType();
void SetTypeName(char *aTypeName);
char* GetTypeName();
void GetTypeAsString(char *aString, size_t aStringSize);
void SetValue(unsigned long aValue);
void SetValue(char aValue);

Просмотреть файл

@ -60,6 +60,9 @@ int main(int argc, char *argv[])
}
for (int i = 1; i <= args; i++) {
cout << "************** PARSING " << argv[i] << " **************" << "\n";
// create a specification object. On parser termination it will
// contain all parsed interfaces
IdlSpecification *specification = new IdlSpecification();
@ -75,6 +78,11 @@ int main(int argc, char *argv[])
} catch(...) {
cout << "Unknown Exception. Parser Aborted.";
}
cout << "************** PARSED **************" << "\n"
<< "************** DUMPING **************" << "\n"
<< *specification;
delete parser;
}