replaced dummy parser with top level file parser

pull/7/head
Marc Zinnschlag 15 years ago
parent d02257ca88
commit a068ca78d7

@ -21,6 +21,8 @@ int main (int argc, char **argv)
Compiler::Scanner scanner (errorHandler, file);
scanner.scan (parser);
std::cout << "parsed script: " << parser.getName() << std::endl;
}
catch (const std::exception &e)
{

@ -3,54 +3,90 @@
#include <iostream>
#include "tokenloc.hpp"
#include "scanner.hpp"
namespace Compiler
{
FileParser::FileParser (ErrorHandler& errorHandler, Context& context)
: Parser (errorHandler, context)
: Parser (errorHandler, context), mState (BeginState)
{}
bool FileParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
std::string FileParser::getName() const
{
std::cout << "integer: " << value << std::endl;
return true;
}
bool FileParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner)
{
std::cout << "float: " << value << std::endl;
return true;
return mName;
}
bool FileParser::parseName (const std::string& name, const TokenLoc& loc,
Scanner& scanner)
{
std::cout << "name: " << name << std::endl;
return true;
if (mState==NameState)
{
mName = name;
mState = BeginCompleteState;
return true;
}
if (mState==EndNameState)
{
// optional repeated name after end statement
if (mName!=name)
reportWarning ("Names for script " + mName + " do not match", loc);
mState = EndCompleteState;
return true;
}
return Parser::parseName (name, loc, scanner);
}
bool FileParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
{
std::cout << "keyword: " << loc.mLiteral << std::endl;
return true;
if (mState==BeginState && keyword==Scanner::K_begin)
{
mState = NameState;
return true;
}
if (mState==EndState && keyword==Scanner::K_end)
{
mState = EndNameState;
return true;
}
return Parser::parseKeyword (keyword, loc, scanner);
}
bool FileParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
{
if (code==Scanner::S_newline)
std::cout << "newline" << std::endl;
else
std::cout << "special: " << loc.mLiteral << std::endl;
{
if (mState==BeginCompleteState)
{
// TODO: add script parser here
mState = EndState;
return true;
}
if (mState==EndCompleteState || mState==EndNameState)
{
// we are done here -> ignore the rest of the script
return false;
}
}
return true;
return Parser::parseSpecial (code, loc, scanner);
}
void FileParser::parseEOF (Scanner& scanner)
{
std::cout << "end of file" << std::endl;
if (mState!=EndNameState && mState!=EndCompleteState)
Parser::parseEOF (scanner);
}
void FileParser::reset()
{
mState = BeginState;
mName.clear();
}
}

@ -5,19 +5,25 @@
namespace Compiler
{
// Top-level parser, to be used for global scripts, local scripts and targeted scripts
class FileParser : public Parser
{
enum State
{
BeginState, NameState, BeginCompleteState, EndState, EndNameState,
EndCompleteState
};
State mState;
std::string mName;
public:
FileParser (ErrorHandler& errorHandler, Context& context);
virtual bool parseInt (int value, const TokenLoc& loc, Scanner& scanner);
///< Handle an int token.
/// \return fetch another token?
virtual bool parseFloat (float value, const TokenLoc& loc, Scanner& scanner);
///< Handle a double token.
/// \return fetch another token?
std::string getName() const;
///< Return script name.
virtual bool parseName (const std::string& name, const TokenLoc& loc,
Scanner& scanner);
@ -34,6 +40,9 @@ namespace Compiler
virtual void parseEOF (Scanner& scanner);
///< Handle EOF token.
void reset();
///< Reset parser to clean state.
};
}

Loading…
Cancel
Save