1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-16 07:10:08 +00:00
OpenMW/components/compiler/declarationparser.cpp

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

128 lines
3.5 KiB
C++
Raw Normal View History

2014-02-10 12:01:52 +00:00
#include "declarationparser.hpp"
#include <components/misc/strings/lower.hpp>
2014-02-10 12:01:52 +00:00
#include "context.hpp"
2014-02-10 12:01:52 +00:00
#include "errorhandler.hpp"
#include "extensions.hpp"
2014-02-10 12:01:52 +00:00
#include "locals.hpp"
#include "scanner.hpp"
#include "skipparser.hpp"
2014-02-14 11:23:00 +00:00
Compiler::DeclarationParser::DeclarationParser(ErrorHandler& errorHandler, const Context& context, Locals& locals)
2014-02-10 12:01:52 +00:00
: Parser(errorHandler, context)
, mLocals(locals)
, mState(State_Begin)
, mType(0)
{
}
bool Compiler::DeclarationParser::parseName(const std::string& name, const TokenLoc& loc, Scanner& scanner)
{
if (mState == State_Name)
{
std::string name2 = ::Misc::StringUtils::lowerCase(name);
2014-02-10 12:01:52 +00:00
char type = mLocals.getType(name2);
if (type != ' ')
2019-03-28 21:59:26 +00:00
getErrorHandler().warning("Local variable re-declaration", loc);
else
mLocals.declare(mType, name2);
2014-02-10 12:01:52 +00:00
mState = State_End;
return true;
}
else if (mState == State_End)
{
2019-03-28 21:59:26 +00:00
getErrorHandler().warning("Extra text after local variable declaration", loc);
SkipParser skip(getErrorHandler(), getContext());
scanner.scan(skip);
return false;
}
2014-02-10 12:01:52 +00:00
return Parser::parseName(name, loc, scanner);
}
bool Compiler::DeclarationParser::parseKeyword(int keyword, const TokenLoc& loc, Scanner& scanner)
{
if (mState == State_Begin)
{
switch (keyword)
{
case Scanner::K_short:
mType = 's';
break;
case Scanner::K_long:
mType = 'l';
break;
case Scanner::K_float:
mType = 'f';
break;
default:
mType = 0;
}
if (mType)
{
mState = State_Name;
return true;
}
}
else if (mState == State_Name)
{
// allow keywords to be used as local variable names. MW script compiler, you suck!
if (const Extensions* extensions = getContext().getExtensions())
{
std::string argumentType;
bool hasExplicit = false;
char returnType;
if (extensions->isFunction(keyword, returnType, argumentType, hasExplicit))
getErrorHandler().warning("Local variable declaration shadows a function", loc);
}
2014-02-10 12:01:52 +00:00
return parseName(loc.mLiteral, loc, scanner);
}
else if (mState == State_End)
{
2019-03-28 21:59:26 +00:00
getErrorHandler().warning("Extra text after local variable declaration", loc);
SkipParser skip(getErrorHandler(), getContext());
scanner.scan(skip);
return false;
}
2014-02-10 12:01:52 +00:00
return Parser::parseKeyword(keyword, loc, scanner);
}
bool Compiler::DeclarationParser::parseSpecial(int code, const TokenLoc& loc, Scanner& scanner)
{
if (mState == State_End)
{
if (code != Scanner::S_newline)
{
2019-03-28 21:59:26 +00:00
getErrorHandler().warning("Extra text after local variable declaration", loc);
SkipParser skip(getErrorHandler(), getContext());
scanner.scan(skip);
}
2014-02-10 12:01:52 +00:00
return false;
}
2014-02-10 12:01:52 +00:00
return Parser::parseSpecial(code, loc, scanner);
}
2021-10-13 20:44:18 +00:00
bool Compiler::DeclarationParser::parseInt(int value, const TokenLoc& loc, Scanner& scanner)
{
if (mState == State_Name)
{
// Allow integers to be used as variable names
getErrorHandler().warning("Integer is used as a local variable name", loc);
2021-10-13 20:44:18 +00:00
return parseName(loc.mLiteral, loc, scanner);
}
return Parser::parseInt(value, loc, scanner);
}
void Compiler::DeclarationParser::reset()
{
mState = State_Begin;
}