1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-21 12:23:53 +00:00
openmw/components/compiler/scriptparser.cpp

98 lines
2.6 KiB
C++
Raw Normal View History

#include "scriptparser.hpp"
2022-09-22 18:26:05 +00:00
#include "errorhandler.hpp"
#include "scanner.hpp"
#include "skipparser.hpp"
namespace Compiler
{
2022-09-22 18:26:05 +00:00
ScriptParser::ScriptParser(ErrorHandler& errorHandler, const Context& context, Locals& locals, bool end)
: Parser(errorHandler, context)
, mOutput(locals)
, mLineParser(errorHandler, context, locals, mOutput.getLiterals(), mOutput.getCode())
, mControlParser(errorHandler, context, locals, mOutput.getLiterals())
, mEnd(end)
{
}
void ScriptParser::getCode(std::vector<Interpreter::Type_Code>& code) const
{
2022-09-22 18:26:05 +00:00
mOutput.getCode(code);
}
2022-09-22 18:26:05 +00:00
bool ScriptParser::parseName(const std::string& name, const TokenLoc& loc, Scanner& scanner)
{
mLineParser.reset();
2022-09-22 18:26:05 +00:00
if (mLineParser.parseName(name, loc, scanner))
scanner.scan(mLineParser);
return true;
}
2022-09-22 18:26:05 +00:00
bool ScriptParser::parseKeyword(int keyword, const TokenLoc& loc, Scanner& scanner)
{
2022-09-22 18:26:05 +00:00
if (keyword == Scanner::K_while || keyword == Scanner::K_if || keyword == Scanner::K_elseif)
2010-06-30 17:58:25 +00:00
{
mControlParser.reset();
2022-09-22 18:26:05 +00:00
if (mControlParser.parseKeyword(keyword, loc, scanner))
scanner.scan(mControlParser);
2022-09-22 18:26:05 +00:00
mControlParser.appendCode(mOutput.getCode());
2010-06-30 17:58:25 +00:00
return true;
}
/// \todo add an option to disable this nonsense
2022-09-22 18:26:05 +00:00
if (keyword == Scanner::K_endif)
{
// surplus endif
2022-09-22 18:26:05 +00:00
getErrorHandler().warning("endif without matching if/elseif", loc);
2022-09-22 18:26:05 +00:00
SkipParser skip(getErrorHandler(), getContext());
scanner.scan(skip);
return true;
}
2022-09-22 18:26:05 +00:00
if (keyword == Scanner::K_end && mEnd)
{
return false;
}
mLineParser.reset();
2022-09-22 18:26:05 +00:00
if (mLineParser.parseKeyword(keyword, loc, scanner))
scanner.scan(mLineParser);
return true;
}
2022-09-22 18:26:05 +00:00
bool ScriptParser::parseSpecial(int code, const TokenLoc& loc, Scanner& scanner)
{
2022-09-22 18:26:05 +00:00
if (code == Scanner::S_newline) // empty line
return true;
2022-09-22 18:26:05 +00:00
if (code == Scanner::S_open) /// \todo Option to switch this off
{
2022-09-22 18:26:05 +00:00
scanner.putbackSpecial(code, loc);
return parseKeyword(Scanner::K_if, loc, scanner);
}
mLineParser.reset();
2022-09-22 18:26:05 +00:00
if (mLineParser.parseSpecial(code, loc, scanner))
scanner.scan(mLineParser);
return true;
}
2022-09-22 18:26:05 +00:00
void ScriptParser::parseEOF(Scanner& scanner)
{
if (mEnd)
2022-09-22 18:26:05 +00:00
Parser::parseEOF(scanner);
}
void ScriptParser::reset()
{
mLineParser.reset();
mOutput.clear();
}
}