diff --git a/CMakeLists.txt b/CMakeLists.txt index 52704c2bc..5377ea920 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,7 +68,7 @@ source_group(misc FILES ${MISC} ${MISC_HEADER}) set(COMPILER components/compiler/errorhandler.cpp components/compiler/fileparser.cpp components/compiler/scriptparser.cpp - components/compiler/lineparser.cpp + components/compiler/lineparser.cpp components/compiler/skipparser.cpp components/compiler/parser.cpp components/compiler/scanner.cpp components/compiler/streamerrorhandler.cpp) file(GLOB COMPILER_HEADER components/compiler/*.hpp) diff --git a/components/compiler/lineparser.cpp b/components/compiler/lineparser.cpp index f90352559..6c8c3f735 100644 --- a/components/compiler/lineparser.cpp +++ b/components/compiler/lineparser.cpp @@ -6,6 +6,7 @@ #include "scanner.hpp" #include "context.hpp" #include "errorhandler.hpp" +#include "skipparser.hpp" namespace Compiler { @@ -29,7 +30,12 @@ namespace Compiler if (mState==ShortState || mState==LongState || mState==FloatState) { if (!getContext().canDeclareLocals()) + { getErrorHandler().error ("local variables can't be declared in this context", loc); + SkipParser skip (getErrorHandler(), getContext()); + scanner.scan (skip); + return false; + } std::cout << "declaring local variable: " << name << std::endl; mState = EndState; diff --git a/components/compiler/skipparser.cpp b/components/compiler/skipparser.cpp new file mode 100644 index 000000000..2d09b63ba --- /dev/null +++ b/components/compiler/skipparser.cpp @@ -0,0 +1,41 @@ + +#include "skipparser.hpp" + +#include "scanner.hpp" + +namespace Compiler +{ + SkipParser::SkipParser (ErrorHandler& errorHandler, Context& context) + : Parser (errorHandler, context) + {} + + bool SkipParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner) + { + return true; + } + + bool SkipParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner) + { + return true; + } + + bool SkipParser::parseName (const std::string& name, const TokenLoc& loc, + Scanner& scanner) + { + return true; + } + + bool SkipParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner) + { + return true; + } + + bool SkipParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner) + { + if (code==Scanner::S_newline) + return false; + + return true; + } +} + diff --git a/components/compiler/skipparser.hpp b/components/compiler/skipparser.hpp new file mode 100644 index 000000000..17f1c8d98 --- /dev/null +++ b/components/compiler/skipparser.hpp @@ -0,0 +1,42 @@ +#ifndef COMPILER_SKIPPARSER_H_INCLUDED +#define COMPILER_SKIPPARSER_H_INCLUDED + +#include "parser.hpp" + +namespace Compiler +{ + // \brief Skip parser for skipping a line + // + // This parser is mainly intended for skipping the rest of a faulty line. + + class SkipParser : public Parser + { + public: + + SkipParser (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 float token. + /// \return fetch another token? + + 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 +