factored out declaration parser
parent
2b2ac6f62b
commit
3147aebf75
@ -0,0 +1,79 @@
|
||||
|
||||
#include "declarationparser.hpp"
|
||||
|
||||
#include <components/misc/stringops.hpp>
|
||||
|
||||
#include "scanner.hpp"
|
||||
#include "errorhandler.hpp"
|
||||
#include "skipparser.hpp"
|
||||
#include "locals.hpp"
|
||||
|
||||
Compiler::DeclarationParser::DeclarationParser (ErrorHandler& errorHandler, Context& context,
|
||||
Locals& locals)
|
||||
: 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);
|
||||
|
||||
char type = mLocals.getType (name2);
|
||||
|
||||
if (type!=' ')
|
||||
{
|
||||
/// \todo add option to make re-declared local variables an error
|
||||
getErrorHandler().warning ("can't re-declare local variable (ignoring declaration)",
|
||||
loc);
|
||||
SkipParser skip (getErrorHandler(), getContext());
|
||||
scanner.scan (skip);
|
||||
mState = State_End;
|
||||
return true;
|
||||
}
|
||||
|
||||
mLocals.declare (mType, name2);
|
||||
|
||||
mState = State_End;
|
||||
return true;
|
||||
}
|
||||
|
||||
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!
|
||||
/// \todo option to disable this atrocity.
|
||||
return parseName (loc.mLiteral, loc, scanner);
|
||||
}
|
||||
|
||||
return Parser::parseKeyword (keyword, loc, scanner);
|
||||
}
|
||||
|
||||
bool Compiler::DeclarationParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
|
||||
{
|
||||
if (code==Scanner::S_newline && mState==State_End)
|
||||
return false;
|
||||
|
||||
return Parser::parseSpecial (code, loc, scanner);
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
#ifndef COMPILER_DECLARATIONPARSER_H_INCLUDED
|
||||
#define COMPILER_DECLARATIONPARSER_H_INCLUDED
|
||||
|
||||
#include "parser.hpp"
|
||||
|
||||
namespace Compiler
|
||||
{
|
||||
class Locals;
|
||||
|
||||
class DeclarationParser : public Parser
|
||||
{
|
||||
enum State
|
||||
{
|
||||
State_Begin, State_Name, State_End
|
||||
};
|
||||
|
||||
Locals& mLocals;
|
||||
State mState;
|
||||
char mType;
|
||||
|
||||
public:
|
||||
|
||||
DeclarationParser (ErrorHandler& errorHandler, Context& context, Locals& locals);
|
||||
|
||||
virtual bool parseName (const std::string& name, const TokenLoc& loc,
|
||||
Scanner& scanner);
|
||||
///< Handle a name token.
|
||||
/// \return fetch another token?
|
||||
|
||||
virtual bool parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner);
|
||||
///< Handle a keyword token.
|
||||
/// \return fetch another token?
|
||||
|
||||
virtual bool parseSpecial (int code, const TokenLoc& loc, Scanner& scanner);
|
||||
///< Handle a special character token.
|
||||
/// \return fetch another token?
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue