added compiler extensions class

This commit is contained in:
Marc Zinnschlag 2010-07-03 09:54:01 +02:00
parent 474b412b47
commit a1beffc3cd
5 changed files with 80 additions and 5 deletions

View file

@ -3,14 +3,23 @@
namespace Compiler namespace Compiler
{ {
class Extensions;
class Context class Context
{ {
const Extensions *mExtensions;
public: public:
Context() : mExtensions (0) {}
virtual ~Context() {} virtual ~Context() {}
virtual bool canDeclareLocals() const = 0; virtual bool canDeclareLocals() const = 0;
///< Is the compiler allowed to declare local variables? ///< Is the compiler allowed to declare local variables?
void setExtensions (const Extensions *extensions = 0);
///< Set compiler extensions.
}; };
} }

View file

@ -0,0 +1,18 @@
#include "extensions.hpp"
namespace Compiler
{
Extensions::Extensions() : mNextKeywordIndex (-1) {}
int Extensions::searchKeyword (const std::string& keyword) const
{
std::map<std::string, int>::const_iterator iter = mKeywords.find (keyword);
if (iter==mKeywords.end())
return 0;
return iter->second;
}
}

View file

@ -0,0 +1,28 @@
#ifndef COMPILER_EXTENSIONS_H_INCLUDED
#define COMPILER_EXTENSINOS_H_INCLUDED
#include <string>
#include <map>
namespace Compiler
{
/// \brief Collection of compiler extensions
class Extensions
{
int mNextKeywordIndex;
std::map<std::string, int> mKeywords;
public:
Extensions();
int searchKeyword (const std::string& keyword) const;
///< Return extension keyword code, that is assigned to the string \a keyword.
/// - if no match is found 0 is returned.
/// - keyword must be all lower case.
};
}
#endif

View file

@ -9,6 +9,7 @@
#include "exception.hpp" #include "exception.hpp"
#include "errorhandler.hpp" #include "errorhandler.hpp"
#include "parser.hpp" #include "parser.hpp"
#include "extensions.hpp"
namespace Compiler namespace Compiler
{ {
@ -271,8 +272,22 @@ namespace Compiler
if (lowerCase==keywords[i]) if (lowerCase==keywords[i])
break; break;
cont = if (keywords[i])
keywords[i] ? parser.parseKeyword (i, loc, *this) : parser.parseName (name, loc, *this); {
cont = parser.parseKeyword (i, loc, *this);
return true;
}
if (mExtensions)
{
if (int keyword = mExtensions->searchKeyword (lowerCase))
{
cont = parser.parseKeyword (keyword, loc, *this);
return true;
}
}
cont = parser.parseName (name, loc, *this);
return true; return true;
} }
@ -445,8 +460,10 @@ namespace Compiler
// constructor // constructor
Scanner::Scanner (ErrorHandler& errorHandler, std::istream& inputStream) Scanner::Scanner (ErrorHandler& errorHandler, std::istream& inputStream,
: mErrorHandler (errorHandler), mStream (inputStream), mPutback (Putback_None) const Extensions *extensions)
: mErrorHandler (errorHandler), mStream (inputStream), mExtensions (extensions),
mPutback (Putback_None)
{ {
} }

View file

@ -10,6 +10,7 @@ namespace Compiler
{ {
class ErrorHandler; class ErrorHandler;
class Parser; class Parser;
class Extensions;
/// \brief Scanner /// \brief Scanner
/// ///
@ -28,6 +29,7 @@ namespace Compiler
TokenLoc mLoc; TokenLoc mLoc;
TokenLoc mPrevLoc; TokenLoc mPrevLoc;
std::istream& mStream; std::istream& mStream;
const Extensions *mExtensions;
putback_type mPutback; putback_type mPutback;
int mPutbackCode; int mPutbackCode;
int mPutbackInteger; int mPutbackInteger;
@ -86,7 +88,8 @@ namespace Compiler
public: public:
Scanner (ErrorHandler& errorHandler, std::istream& inputStream); Scanner (ErrorHandler& errorHandler, std::istream& inputStream,
const Extensions *extensions = 0);
///< constructor ///< constructor
void scan (Parser& parser); void scan (Parser& parser);