1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 21:29:56 +00:00
openmw/components/compiler/parser.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

177 lines
3.9 KiB
C++
Raw Normal View History

2010-06-27 17:20:21 +00:00
#include "parser.hpp"
#include "errorhandler.hpp"
#include "exception.hpp"
2012-04-05 12:27:39 +00:00
#include "scanner.hpp"
2010-06-27 17:20:21 +00:00
#include <components/misc/strings/lower.hpp>
2013-01-09 19:51:52 +00:00
2010-06-27 17:20:21 +00:00
namespace Compiler
{
// Report the error and throw an exception.
[[noreturn]] void Parser::reportSeriousError(const std::string& message, const TokenLoc& loc)
2010-06-27 17:20:21 +00:00
{
mErrorHandler.error(message, loc);
throw SourceException();
}
// 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.
[[noreturn]] void Parser::reportEOF()
2010-06-27 17:20:21 +00:00
{
mErrorHandler.endOfFile();
throw EOFException();
}
// Return error handler
ErrorHandler& Parser::getErrorHandler()
{
return mErrorHandler;
}
// Return context
2014-02-14 11:23:00 +00:00
const Context& Parser::getContext() const
2010-06-27 17:20:21 +00:00
{
return mContext;
}
std::string Parser::toLower(const std::string& name)
{
2013-01-09 19:51:52 +00:00
std::string lowerCase = Misc::StringUtils::lowerCase(name);
2010-08-22 10:47:56 +00:00
return lowerCase;
}
2014-02-14 11:23:00 +00:00
Parser::Parser(ErrorHandler& errorHandler, const Context& context)
2010-08-22 10:47:56 +00:00
: mErrorHandler(errorHandler)
, mContext(context)
, mOptional(false)
, mEmpty(true)
2010-06-27 17:20:21 +00:00
{
}
// destructor
Parser::~Parser() = default;
2010-06-27 17:20:21 +00:00
// Handle an int token.
// \return fetch another token?
//
// - Default-implementation: Report an error.
bool Parser::parseInt(int value, const TokenLoc& loc, Scanner& scanner)
{
2010-08-22 10:47:56 +00:00
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected numeric value", loc);
2012-04-05 12:27:39 +00:00
else
scanner.putbackInt(value, loc);
2010-08-22 10:47:56 +00:00
2010-06-27 17:20:21 +00:00
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)
{
2010-08-22 10:47:56 +00:00
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected floating point value", loc);
2012-04-05 12:27:39 +00:00
else
scanner.putbackFloat(value, loc);
2010-08-22 10:47:56 +00:00
2010-06-27 17:20:21 +00:00
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)
{
2010-08-22 10:47:56 +00:00
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected name", loc);
2012-04-05 12:27:39 +00:00
else
scanner.putbackName(name, loc);
2010-08-22 10:47:56 +00:00
2010-06-27 17:20:21 +00:00
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)
{
2010-08-22 10:47:56 +00:00
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected keyword", loc);
2012-04-05 12:27:39 +00:00
else
scanner.putbackKeyword(keyword, loc);
2010-08-22 10:47:56 +00:00
2010-06-27 17:20:21 +00:00
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)
{
2010-08-22 10:47:56 +00:00
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected special token", loc);
2012-04-05 12:27:39 +00:00
else
scanner.putbackSpecial(code, loc);
2010-08-22 10:47:56 +00:00
2010-06-27 17:20:21 +00:00
return false;
}
bool Parser::parseComment(const std::string& comment, const TokenLoc& loc, Scanner& scanner)
{
return true;
}
2010-06-27 17:20:21 +00:00
// Handle an EOF token.
//
// - Default-implementation: Report an error.
void Parser::parseEOF(Scanner& scanner)
{
reportEOF();
}
2010-08-22 10:47:56 +00:00
void Parser::reset()
{
mOptional = false;
mEmpty = true;
}
void Parser::setOptional(bool optional)
{
mOptional = optional;
}
void Parser::start()
{
mEmpty = false;
}
bool Parser::isEmpty() const
{
return mEmpty;
}
}