diff --git a/CMakeLists.txt b/CMakeLists.txt index d9e793754a..8e044d3d00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,6 +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/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 new file mode 100644 index 0000000000..8d1fa70aaa --- /dev/null +++ b/components/compiler/lineparser.cpp @@ -0,0 +1,43 @@ + +#include "lineparser.hpp" + +#include "scanner.hpp" + +namespace Compiler +{ + LineParser::LineParser (ErrorHandler& errorHandler, Context& context) + : Parser (errorHandler, context) + {} + + bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner) + { + return Parser::parseInt (value, loc, scanner); + } + + bool LineParser::parseFloat (float value, const TokenLoc& loc, Scanner& scanner) + { + return Parser::parseFloat (value, loc, scanner); + } + + bool LineParser::parseName (const std::string& name, const TokenLoc& loc, + Scanner& scanner) + { + return Parser::parseName (name, loc, scanner); + } + + bool LineParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner) + { + return Parser::parseKeyword (keyword, loc, scanner); + } + + bool LineParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner) + { + return Parser::parseSpecial (code, loc, scanner); + } + + void LineParser::reset() + { + + } +} + diff --git a/components/compiler/lineparser.hpp b/components/compiler/lineparser.hpp new file mode 100644 index 0000000000..8b11dcf4a3 --- /dev/null +++ b/components/compiler/lineparser.hpp @@ -0,0 +1,42 @@ +#ifndef COMPILER_LINEPARSER_H_INCLUDED +#define COMPILER_LINEPARSER_H_INCLUDED + +#include "parser.hpp" + +namespace Compiler +{ + /// \brief Line parser, to be used in console scripts and as part of ScriptParser + + class LineParser : public Parser + { + public: + + LineParser (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? + + void reset(); + ///< Reset parser to clean state. + }; +} + +#endif diff --git a/components/compiler/parser.hpp b/components/compiler/parser.hpp index e54d170138..5bb541f92e 100644 --- a/components/compiler/parser.hpp +++ b/components/compiler/parser.hpp @@ -56,7 +56,7 @@ namespace Compiler /// - Default-implementation: Report an error. virtual bool parseFloat (float value, const TokenLoc& loc, Scanner& scanner); - ///< Handle a double token. + ///< Handle a float token. /// \return fetch another token? /// /// - Default-implementation: Report an error. diff --git a/components/compiler/scriptparser.cpp b/components/compiler/scriptparser.cpp index 2247ed4082..769cc5ab2b 100644 --- a/components/compiler/scriptparser.cpp +++ b/components/compiler/scriptparser.cpp @@ -6,13 +6,17 @@ namespace Compiler { ScriptParser::ScriptParser (ErrorHandler& errorHandler, Context& context, bool end) - : Parser (errorHandler, context), mEnd (end) + : Parser (errorHandler, context), mLineParser (errorHandler, context), mEnd (end) {} bool ScriptParser::parseName (const std::string& name, const TokenLoc& loc, Scanner& scanner) { - return Parser::parseName (name, loc, scanner); + mLineParser.reset(); + if (mLineParser.parseName (name, loc, scanner)) + scanner.scan (mLineParser); + + return true; } bool ScriptParser::parseKeyword (int keyword, const TokenLoc& loc, Scanner& scanner) @@ -22,7 +26,11 @@ namespace Compiler return false; } - return Parser::parseKeyword (keyword, loc, scanner); + mLineParser.reset(); + if (mLineParser.parseKeyword (keyword, loc, scanner)) + scanner.scan (mLineParser); + + return true; } bool ScriptParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner) @@ -30,7 +38,11 @@ namespace Compiler if (code==Scanner::S_newline) // empty line return true; - return Parser::parseSpecial (code, loc, scanner); + mLineParser.reset(); + if (mLineParser.parseSpecial (code, loc, scanner)) + scanner.scan (mLineParser); + + return true; } void ScriptParser::parseEOF (Scanner& scanner) @@ -41,7 +53,7 @@ namespace Compiler void ScriptParser::reset() { - + mLineParser.reset(); } } diff --git a/components/compiler/scriptparser.hpp b/components/compiler/scriptparser.hpp index 1a726b86e9..6933b7d083 100644 --- a/components/compiler/scriptparser.hpp +++ b/components/compiler/scriptparser.hpp @@ -2,6 +2,7 @@ #define COMPILER_SCRIPTPARSER_H_INCLUDED #include "parser.hpp" +#include "lineparser.hpp" namespace Compiler { @@ -9,6 +10,7 @@ namespace Compiler class ScriptParser : public Parser { + LineParser mLineParser; bool mEnd; public: