2010-06-28 09:38:04 +00:00
|
|
|
#include "scriptparser.hpp"
|
|
|
|
|
2013-03-18 11:05:54 +00:00
|
|
|
#include "errorhandler.hpp"
|
2010-06-28 09:38:04 +00:00
|
|
|
#include "scanner.hpp"
|
2013-03-18 11:05:54 +00:00
|
|
|
#include "skipparser.hpp"
|
2010-06-28 09:38:04 +00:00
|
|
|
|
|
|
|
namespace Compiler
|
|
|
|
{
|
2014-02-14 11:23:00 +00:00
|
|
|
ScriptParser::ScriptParser(ErrorHandler& errorHandler, const Context& context, Locals& locals, bool end)
|
2010-06-28 14:48:19 +00:00
|
|
|
: Parser(errorHandler, context)
|
|
|
|
, mOutput(locals)
|
|
|
|
, mLineParser(errorHandler, context, locals, mOutput.getLiterals(), mOutput.getCode())
|
2010-06-30 17:58:25 +00:00
|
|
|
, mControlParser(errorHandler, context, locals, mOutput.getLiterals())
|
2010-06-28 14:48:19 +00:00
|
|
|
, mEnd(end)
|
2010-06-28 09:38:04 +00:00
|
|
|
{
|
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2023-01-10 03:10:18 +00:00
|
|
|
Interpreter::Program ScriptParser::getProgram() const
|
2010-06-28 12:07:55 +00:00
|
|
|
{
|
2023-01-10 03:10:18 +00:00
|
|
|
return mOutput.getProgram();
|
2010-06-28 12:07:55 +00:00
|
|
|
}
|
|
|
|
|
2010-06-28 09:38:04 +00:00
|
|
|
bool ScriptParser::parseName(const std::string& name, const TokenLoc& loc, Scanner& scanner)
|
|
|
|
{
|
2010-06-28 10:12:47 +00:00
|
|
|
mLineParser.reset();
|
|
|
|
if (mLineParser.parseName(name, loc, scanner))
|
|
|
|
scanner.scan(mLineParser);
|
2012-03-18 13:27:49 +00:00
|
|
|
|
2010-06-28 10:12:47 +00:00
|
|
|
return true;
|
2010-06-28 09:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptParser::parseKeyword(int keyword, const TokenLoc& loc, Scanner& scanner)
|
|
|
|
{
|
2014-02-02 13:24:58 +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();
|
|
|
|
if (mControlParser.parseKeyword(keyword, loc, scanner))
|
2012-03-18 13:27:49 +00:00
|
|
|
scanner.scan(mControlParser);
|
|
|
|
|
2010-06-30 17:58:25 +00:00
|
|
|
mControlParser.appendCode(mOutput.getCode());
|
2012-03-18 13:27:49 +00:00
|
|
|
|
2010-06-30 17:58:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-18 13:27:49 +00:00
|
|
|
|
2013-03-18 11:05:54 +00:00
|
|
|
/// \todo add an option to disable this nonsense
|
|
|
|
if (keyword == Scanner::K_endif)
|
|
|
|
{
|
|
|
|
// surplus endif
|
|
|
|
getErrorHandler().warning("endif without matching if/elseif", loc);
|
|
|
|
|
|
|
|
SkipParser skip(getErrorHandler(), getContext());
|
|
|
|
scanner.scan(skip);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-28 09:38:04 +00:00
|
|
|
if (keyword == Scanner::K_end && mEnd)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-18 13:27:49 +00:00
|
|
|
|
2010-06-28 10:12:47 +00:00
|
|
|
mLineParser.reset();
|
|
|
|
if (mLineParser.parseKeyword(keyword, loc, scanner))
|
|
|
|
scanner.scan(mLineParser);
|
2012-03-18 13:27:49 +00:00
|
|
|
|
2010-06-28 10:12:47 +00:00
|
|
|
return true;
|
2010-06-28 09:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptParser::parseSpecial(int code, const TokenLoc& loc, Scanner& scanner)
|
|
|
|
{
|
|
|
|
if (code == Scanner::S_newline) // empty line
|
|
|
|
return true;
|
2012-03-18 13:27:49 +00:00
|
|
|
|
2014-02-13 07:49:40 +00:00
|
|
|
if (code == Scanner::S_open) /// \todo Option to switch this off
|
|
|
|
{
|
|
|
|
scanner.putbackSpecial(code, loc);
|
|
|
|
return parseKeyword(Scanner::K_if, loc, scanner);
|
|
|
|
}
|
|
|
|
|
2010-06-28 10:12:47 +00:00
|
|
|
mLineParser.reset();
|
|
|
|
if (mLineParser.parseSpecial(code, loc, scanner))
|
|
|
|
scanner.scan(mLineParser);
|
2012-03-18 13:27:49 +00:00
|
|
|
|
2010-06-28 10:12:47 +00:00
|
|
|
return true;
|
2010-06-28 09:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::parseEOF(Scanner& scanner)
|
|
|
|
{
|
|
|
|
if (mEnd)
|
|
|
|
Parser::parseEOF(scanner);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::reset()
|
|
|
|
{
|
2010-06-28 10:12:47 +00:00
|
|
|
mLineParser.reset();
|
2010-06-28 14:48:19 +00:00
|
|
|
mOutput.clear();
|
2010-06-28 09:38:04 +00:00
|
|
|
}
|
|
|
|
}
|