2010-07-01 14:40:03 +00:00
|
|
|
#include "stringparser.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2020-10-25 13:33:23 +00:00
|
|
|
#include <iterator>
|
2010-07-01 14:40:03 +00:00
|
|
|
|
2022-08-02 22:00:54 +00:00
|
|
|
#include <components/misc/strings/lower.hpp>
|
2015-08-05 15:20:01 +00:00
|
|
|
|
|
|
|
#include "context.hpp"
|
|
|
|
#include "extensions.hpp"
|
2022-09-22 18:26:05 +00:00
|
|
|
#include "generator.hpp"
|
|
|
|
#include "scanner.hpp"
|
2010-07-01 14:40:03 +00:00
|
|
|
|
|
|
|
namespace Compiler
|
|
|
|
{
|
2022-09-22 18:26:05 +00:00
|
|
|
StringParser::StringParser(ErrorHandler& errorHandler, const Context& context, Literals& literals)
|
|
|
|
: Parser(errorHandler, context)
|
|
|
|
, mLiterals(literals)
|
|
|
|
, mSmashCase(false)
|
|
|
|
, mDiscard(false)
|
2010-07-01 14:40:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
bool StringParser::parseName(const std::string& name, const TokenLoc& loc, Scanner& scanner)
|
2010-07-01 14:40:03 +00:00
|
|
|
{
|
2021-11-13 13:06:21 +00:00
|
|
|
start();
|
|
|
|
mTokenLoc = loc;
|
2010-08-22 10:47:56 +00:00
|
|
|
|
2021-11-13 13:06:21 +00:00
|
|
|
if (!mDiscard)
|
|
|
|
{
|
|
|
|
if (mSmashCase)
|
2022-09-22 18:26:05 +00:00
|
|
|
Generator::pushString(mCode, mLiterals, Misc::StringUtils::lowerCase(name));
|
2021-11-13 13:06:21 +00:00
|
|
|
else
|
2022-09-22 18:26:05 +00:00
|
|
|
Generator::pushString(mCode, mLiterals, name);
|
2010-07-01 14:40:03 +00:00
|
|
|
}
|
2010-08-22 10:47:56 +00:00
|
|
|
|
2021-11-13 13:06:21 +00:00
|
|
|
return false;
|
2010-07-01 14:40:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
bool StringParser::parseKeyword(int keyword, const TokenLoc& loc, Scanner& scanner)
|
2015-08-05 15:20:01 +00:00
|
|
|
{
|
2022-09-22 18:26:05 +00:00
|
|
|
if (const Extensions* extensions = getContext().getExtensions())
|
2015-08-05 15:20:01 +00:00
|
|
|
{
|
|
|
|
std::string argumentType; // ignored
|
|
|
|
bool hasExplicit = false; // ignored
|
2022-09-22 18:26:05 +00:00
|
|
|
if (extensions->isInstruction(keyword, argumentType, hasExplicit))
|
2015-08-05 15:20:01 +00:00
|
|
|
{
|
|
|
|
// pretend this is not a keyword
|
|
|
|
std::string name = loc.mLiteral;
|
2022-09-22 18:26:05 +00:00
|
|
|
if (name.size() >= 2 && name[0] == '"' && name[name.size() - 1] == '"')
|
|
|
|
name = name.substr(1, name.size() - 2);
|
|
|
|
return parseName(name, loc, scanner);
|
2015-08-05 15:20:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
if (keyword == Scanner::K_end || keyword == Scanner::K_begin || keyword == Scanner::K_short
|
|
|
|
|| keyword == Scanner::K_long || keyword == Scanner::K_float || keyword == Scanner::K_if
|
|
|
|
|| keyword == Scanner::K_endif || keyword == Scanner::K_else || keyword == Scanner::K_elseif
|
|
|
|
|| keyword == Scanner::K_while || keyword == Scanner::K_endwhile || keyword == Scanner::K_return
|
|
|
|
|| keyword == Scanner::K_messagebox || keyword == Scanner::K_set || keyword == Scanner::K_to)
|
2019-08-18 16:43:26 +00:00
|
|
|
{
|
2021-06-20 16:02:11 +00:00
|
|
|
// pretend this is not a keyword
|
|
|
|
std::string name = loc.mLiteral;
|
2022-09-22 18:26:05 +00:00
|
|
|
if (name.size() >= 2 && name[0] == '"' && name[name.size() - 1] == '"')
|
|
|
|
name = name.substr(1, name.size() - 2);
|
|
|
|
return parseName(name, loc, scanner);
|
2019-08-18 16:43:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
return Parser::parseKeyword(keyword, loc, scanner);
|
2015-08-05 15:20:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
bool StringParser::parseInt(int value, const TokenLoc& loc, Scanner& scanner)
|
2021-10-21 14:54:32 +00:00
|
|
|
{
|
|
|
|
reportWarning("Treating integer argument as a string", loc);
|
|
|
|
return parseName(loc.mLiteral, loc, scanner);
|
|
|
|
}
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
void StringParser::append(std::vector<Interpreter::Type_Code>& code)
|
2010-07-01 14:40:03 +00:00
|
|
|
{
|
2022-09-22 18:26:05 +00:00
|
|
|
std::copy(mCode.begin(), mCode.end(), std::back_inserter(code));
|
2010-07-01 14:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringParser::reset()
|
|
|
|
{
|
|
|
|
mCode.clear();
|
2010-07-04 14:00:32 +00:00
|
|
|
mSmashCase = false;
|
2016-01-07 14:54:22 +00:00
|
|
|
mTokenLoc = TokenLoc();
|
|
|
|
mDiscard = false;
|
2010-08-22 10:47:56 +00:00
|
|
|
Parser::reset();
|
2010-07-04 14:00:32 +00:00
|
|
|
}
|
2010-08-22 10:47:56 +00:00
|
|
|
|
2010-07-04 14:00:32 +00:00
|
|
|
void StringParser::smashCase()
|
|
|
|
{
|
2010-07-05 11:33:17 +00:00
|
|
|
mSmashCase = true;
|
2010-07-01 14:40:03 +00:00
|
|
|
}
|
2016-01-07 14:54:22 +00:00
|
|
|
|
|
|
|
const TokenLoc& StringParser::getTokenLoc() const
|
|
|
|
{
|
|
|
|
return mTokenLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringParser::discard()
|
|
|
|
{
|
|
|
|
mDiscard = true;
|
|
|
|
}
|
2010-07-01 14:40:03 +00:00
|
|
|
}
|