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); Compiler::Scanner scanner (errorHandler, file);
scanner.scan (parser); scanner.scan (parser);
std::cout << "parsed script: " << parser.getName() << std::endl;
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {

@ -3,54 +3,90 @@
#include <iostream> #include <iostream>
#include "tokenloc.hpp" #include "tokenloc.hpp"
#include "scanner.hpp" #include "scanner.hpp"
namespace Compiler namespace Compiler
{ {
FileParser::FileParser (ErrorHandler& errorHandler, Context& context) 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 mName;
return true;
} }
bool FileParser::parseName (const std::string& name, const TokenLoc& loc, bool FileParser::parseName (const std::string& name, const TokenLoc& loc,
Scanner& scanner) Scanner& scanner)
{ {
std::cout << "name: " << name << std::endl; if (mState==NameState)
return true; {
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) bool FileParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner)
{ {
std::cout << "keyword: " << loc.mLiteral << std::endl; if (mState==BeginState && keyword==Scanner::K_begin)
return true; {
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) bool FileParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
{ {
if (code==Scanner::S_newline) if (code==Scanner::S_newline)
std::cout << "newline" << std::endl; {
else if (mState==BeginCompleteState)
std::cout << "special: " << loc.mLiteral << std::endl; {
// TODO: add script parser here
mState = EndState;
return true;
}
return true; if (mState==EndCompleteState || mState==EndNameState)
{
// we are done here -> ignore the rest of the script
return false;
}
}
return Parser::parseSpecial (code, loc, scanner);
} }
void FileParser::parseEOF (Scanner& 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 namespace Compiler
{ {
// Top-level parser, to be used for global scripts, local scripts and targeted scripts
class FileParser : public Parser class FileParser : public Parser
{ {
enum State
{
BeginState, NameState, BeginCompleteState, EndState, EndNameState,
EndCompleteState
};
State mState;
std::string mName;
public: public:
FileParser (ErrorHandler& errorHandler, Context& context); FileParser (ErrorHandler& errorHandler, Context& context);
virtual bool parseInt (int value, const TokenLoc& loc, Scanner& scanner); std::string getName() const;
///< Handle an int token. ///< Return script name.
/// \return fetch another token?
virtual bool parseFloat (float value, const TokenLoc& loc, Scanner& scanner);
///< Handle a double token.
/// \return fetch another token?
virtual bool parseName (const std::string& name, const TokenLoc& loc, virtual bool parseName (const std::string& name, const TokenLoc& loc,
Scanner& scanner); Scanner& scanner);
@ -34,6 +40,9 @@ namespace Compiler
virtual void parseEOF (Scanner& scanner); virtual void parseEOF (Scanner& scanner);
///< Handle EOF token. ///< Handle EOF token.
void reset();
///< Reset parser to clean state.
}; };
} }

Loading…
Cancel
Save