You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
2.8 KiB
C++
127 lines
2.8 KiB
C++
15 years ago
|
|
||
|
#include "parser.hpp"
|
||
|
|
||
|
#include "errorhandler.hpp"
|
||
|
#include "exception.hpp"
|
||
|
|
||
|
namespace Compiler
|
||
|
{
|
||
|
// Report the error and throw an exception.
|
||
|
|
||
|
void Parser::reportSeriousError (const std::string& message, const TokenLoc& loc)
|
||
|
{
|
||
|
mErrorHandler.error (message, loc);
|
||
|
throw SourceException();
|
||
|
}
|
||
|
|
||
|
// Report the error
|
||
|
|
||
|
void Parser::reportError (const std::string& message, const TokenLoc& loc)
|
||
|
{
|
||
|
mErrorHandler.error (message, loc);
|
||
|
}
|
||
|
|
||
|
// Report the warning without throwing an exception.
|
||
|
|
||
|
void Parser::reportWarning (const std::string& message, const TokenLoc& loc)
|
||
|
{
|
||
|
mErrorHandler.warning (message, loc);
|
||
|
}
|
||
|
|
||
|
// Report an unexpected EOF condition.
|
||
|
|
||
|
void Parser::reportEOF()
|
||
|
{
|
||
|
mErrorHandler.endOfFile();
|
||
|
throw EOFException();
|
||
|
}
|
||
|
|
||
|
// Return error handler
|
||
|
|
||
|
ErrorHandler& Parser::getErrorHandler()
|
||
|
{
|
||
|
return mErrorHandler;
|
||
|
}
|
||
|
|
||
|
// Return context
|
||
|
|
||
|
Context& Parser::getContext()
|
||
|
{
|
||
|
return mContext;
|
||
|
}
|
||
|
|
||
|
Parser::Parser (ErrorHandler& errorHandler, Context& context)
|
||
|
: mErrorHandler (errorHandler), mContext (context)
|
||
|
{}
|
||
|
|
||
|
// destructor
|
||
|
|
||
|
Parser::~Parser() {}
|
||
|
|
||
|
// Handle an int token.
|
||
|
// \return fetch another token?
|
||
|
//
|
||
|
// - Default-implementation: Report an error.
|
||
|
|
||
|
bool Parser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
|
||
|
{
|
||
|
reportSeriousError ("Unexpected numeric value", loc);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Handle a float token.
|
||
|
// \return fetch another token?
|
||
|
//
|
||
|
// - Default-implementation: Report an error.
|
||
|
|
||
|
bool Parser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner)
|
||
|
{
|
||
|
reportSeriousError ("Unexpected floating point value", loc);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Handle a name token.
|
||
|
// \return fetch another token?
|
||
|
//
|
||
|
// - Default-implementation: Report an error.
|
||
|
|
||
|
bool Parser::parseName (const std::string& name, const TokenLoc& loc,
|
||
|
Scanner& scanner)
|
||
|
{
|
||
|
reportSeriousError ("Unexpected name", loc);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Handle a keyword token.
|
||
|
// \return fetch another token?
|
||
|
//
|
||
|
// - Default-implementation: Report an error.
|
||
|
|
||
|
bool Parser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
|
||
|
{
|
||
|
reportSeriousError ("Unexpected keyword", loc);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Handle a special character token.
|
||
|
// \return fetch another token?
|
||
|
//
|
||
|
// - Default-implementation: Report an error.
|
||
|
|
||
|
bool Parser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
|
||
|
{
|
||
|
reportSeriousError ("Unexpected special token", loc);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Handle an EOF token.
|
||
|
//
|
||
|
// - Default-implementation: Report an error.
|
||
|
|
||
|
void Parser::parseEOF (Scanner& scanner)
|
||
|
{
|
||
|
reportEOF();
|
||
|
}
|
||
|
}
|
||
|
|