From a068ca78d71571e944848d43372188a2b2a3f78a Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Mon, 28 Jun 2010 00:50:48 +0200 Subject: [PATCH] replaced dummy parser with top level file parser --- apps/mwcompiler/main.cpp | 2 + components/compiler/fileparser.cpp | 78 ++++++++++++++++++++++-------- components/compiler/fileparser.hpp | 23 ++++++--- 3 files changed, 75 insertions(+), 28 deletions(-) diff --git a/apps/mwcompiler/main.cpp b/apps/mwcompiler/main.cpp index 03cc5a62cd..1143159302 100644 --- a/apps/mwcompiler/main.cpp +++ b/apps/mwcompiler/main.cpp @@ -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) { diff --git a/components/compiler/fileparser.cpp b/components/compiler/fileparser.cpp index 416854c1cf..388f8df03c 100644 --- a/components/compiler/fileparser.cpp +++ b/components/compiler/fileparser.cpp @@ -3,54 +3,90 @@ #include #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::cout << "integer: " << value << std::endl; - return true; - } - - bool FileParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner) + std::string FileParser::getName() const { - 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; + } - 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) { - std::cout << "end of file" << std::endl; + if (mState!=EndNameState && mState!=EndCompleteState) + Parser::parseEOF (scanner); + } + + void FileParser::reset() + { + mState = BeginState; + mName.clear(); } } diff --git a/components/compiler/fileparser.hpp b/components/compiler/fileparser.hpp index 2007c7ef2e..6ae0862166 100644 --- a/components/compiler/fileparser.hpp +++ b/components/compiler/fileparser.hpp @@ -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. }; }