1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include "parser.hpp"
#include "errorhandler.hpp"
#include "exception.hpp"
#include "scanner.hpp"
#include <components/misc/strings/lower.hpp>
namespace Compiler
{
// Report the error and throw an exception.
[[noreturn]] void Parser::reportSeriousError(const std::string& message, const TokenLoc& loc)
{
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()
{
mErrorHandler.endOfFile();
throw EOFException();
}
// Return error handler
ErrorHandler& Parser::getErrorHandler()
{
return mErrorHandler;
}
// Return context
const Context& Parser::getContext() const
{
return mContext;
}
std::string Parser::toLower(const std::string& name)
{
std::string lowerCase = Misc::StringUtils::lowerCase(name);
return lowerCase;
}
Parser::Parser(ErrorHandler& errorHandler, const Context& context)
: mErrorHandler(errorHandler)
, mContext(context)
, mOptional(false)
, mEmpty(true)
{
}
// destructor
Parser::~Parser() = default;
// Handle an int token.
// \return fetch another token?
//
// - Default-implementation: Report an error.
bool Parser::parseInt(int value, const TokenLoc& loc, Scanner& scanner)
{
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected numeric value", loc);
else
scanner.putbackInt(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)
{
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected floating point value", loc);
else
scanner.putbackFloat(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)
{
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected name", loc);
else
scanner.putbackName(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)
{
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected keyword", loc);
else
scanner.putbackKeyword(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)
{
if (!(mOptional && mEmpty))
reportSeriousError("Unexpected special token", loc);
else
scanner.putbackSpecial(code, loc);
return false;
}
bool Parser::parseComment(const std::string& comment, const TokenLoc& loc, Scanner& scanner)
{
return true;
}
// Handle an EOF token.
//
// - Default-implementation: Report an error.
void Parser::parseEOF(Scanner& scanner)
{
reportEOF();
}
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;
}
}
|