Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

Parser.h

Go to the documentation of this file.
00001 #ifndef PARSER_H_
00002 #define PARSER_H_
00003 
00004 // C++ Library
00005 #include <cstdlib>
00006 #include <fstream>
00007 #include <iostream>
00008 #include <string>
00009 #include <sstream>
00010 #include <vector>
00011 
00012 // Tekkodu Library
00013 // Primitives
00014 // Actions
00015 #include "Kodu/Primitives/KoduAction.h"
00016 #include "Kodu/Primitives/KoduActionDoNothing.h"
00017 #include "Kodu/Primitives/KoduActionDrop.h"
00018 #include "Kodu/Primitives/KoduActionGrab.h"
00019 #include "Kodu/Primitives/KoduActionMotion.h"
00020 #include "Kodu/Primitives/KoduActionPageSwitch.h"
00021 #include "Kodu/Primitives/KoduActionPlay.h"
00022 #include "Kodu/Primitives/KoduActionSay.h"
00023 #include "Kodu/Primitives/KoduActionScore.h"
00024 #include "Kodu/Primitives/KoduActionGive.h"
00025 // Conditions
00026 #include "Kodu/Primitives/KoduCondition.h"
00027 #include "Kodu/Primitives/KoduConditionAlways.h"
00028 #include "Kodu/Primitives/KoduConditionBump.h"
00029 #include "Kodu/Primitives/KoduConditionGot.h"
00030 #include "Kodu/Primitives/KoduConditionSee.h"
00031 #include "Kodu/Primitives/KoduConditionScored.h"
00032 #include "Kodu/Primitives/KoduConditionTimer.h"
00033 #include "Kodu/Primitives/KoduConditionGamepad.h"
00034 #include "Kodu/Primitives/KoduConditionHear.h"
00035 
00036 #include "Kodu/Primitives/PerceptionSearch.h"
00037 
00038 // General Functions
00039 #include "Kodu/General/GeneralFncs.h"
00040 #include "Kodu/General/GeneralMacros.h"
00041 
00042 // Generators
00043 #include "Kodu/Generators/KoduGenerators.h"
00044 
00045 // Parsing
00046 #include "Kodu/Parsing/ParsedPage.h"
00047 #include "Kodu/Parsing/ParsedPhrase.h"
00048 #include "Kodu/Parsing/ParsedRule.h"
00049 #include "Kodu/Parsing/Token.h"
00050 
00051 #include "Kodu/KoduPage.h"
00052 #include "Kodu/KoduRule.h"
00053 
00054 namespace Kodu {
00055 
00056     class Parser {
00057     public:
00058         #define RED_ERR "\x1b[31m"
00059         #define YEL_ERR "\x1b[33m"
00060         #define NON_ERR "\x1b[0m"
00061 
00062         #define ERROR   false
00063 
00064         #define PARSER_ASSERT(CONDITION, ERROR_STATEMENT)                           \
00065             if ((CONDITION) == false) {                                             \
00066                 std::stringstream errorMessage;                                     \
00067                 ERROR_STATEMENT;                                                    \
00068                 std::cerr << RED_ERR << errorMessage.str() << NON_ERR << std::endl; \
00069                 return NULL;                                                        \
00070             }
00071 
00072         //! Used to create the pages, rules, conditions, and actions of a Kodu program
00073         class KodeCreator {
00074         public:
00075             //! Takes the parsed tokens and creates a Kodu game
00076             static bool createKode(const std::vector<ParsedPage*>&, std::vector<KoduPage*>&);
00077 
00078         private:
00079             //! Used to call the appropriate Kode action parser depending on the Phrase head
00080             static KoduAction* getActionKode(ParsedPhrase*);
00081 
00082             //! Used to call the appropriate Kode condition parser depending on the Phrase head
00083             static KoduCondition* getConditionKode(ParsedPhrase*);
00084 
00085             //! Checks if a modifier is a comparison operator
00086             static bool isComparisonOperator(TokenBase*);
00087 
00088             //! Checks if a modifier is a numeric specifier (random modifier or number(s))
00089             static bool isNumericSpecifier(TokenBase*);
00090 
00091             //! Checks if a modifier is a score designator
00092             static bool isScoreDesignator(TokenBase*);
00093 
00094             //! Creates a Numeric Generator from numeric specifiers (random modifier and/or number(s))
00095             static bool numericGenParser(std::vector<TokenBase*>&, NumericGenerator&);
00096             
00097             // action parsers
00098             //! Creates the drop action
00099             static KoduActionDrop* createDropKode(std::vector<TokenBase*>&);
00100 
00101             //! Creates the give action
00102             static KoduActionGive* createGiveKode(std::vector<TokenBase*>&);
00103 
00104             //! Creates the grab action
00105             static KoduActionGrab* createGrabKode(std::vector<TokenBase*>&);
00106 
00107             //! Creates the move action
00108             static KoduActionMotion* createMoveKode(std::vector<TokenBase*>&);
00109 
00110             //! Creates the play action
00111             static KoduActionPlay* createPlayKode(std::vector<TokenBase*>&);
00112             
00113             //! Creates the say action
00114             static KoduActionSay* createSayKode(std::vector<TokenBase*>&);
00115 
00116             //! Creates the score action
00117             static KoduActionScore* createScoreKode(const std::string&, std::vector<TokenBase*>&);
00118 
00119             //! Creates the move action
00120             static KoduActionMotion* createTurnKode(std::vector<TokenBase*>&);
00121 
00122             // condition parsers
00123             //! Creates the bump condition
00124             static KoduConditionBump* createBumpKode(std::vector<TokenBase*>&);
00125 
00126             //! Creates the got condition
00127             static KoduConditionGot* createGotKode(std::vector<TokenBase*>&);
00128 
00129             //! Creates the see condition
00130             static KoduConditionSee* createSeeKode(std::vector<TokenBase*>&);
00131 
00132             //! Creates the scored condition
00133             static KoduConditionScored* createScoredKode(std::vector<TokenBase*>&);
00134             
00135             //! Creates the timer condition
00136             static KoduConditionTimer* createTimerKode(std::vector<TokenBase*>&);
00137       
00138       //! Create Gamepad Button Condition
00139       static KoduConditionGamepad* createGamepadKode(std::vector<TokenBase*>&);
00140       
00141       static KoduConditionHear* createHearKode(std::vector<TokenBase*>&);
00142       
00143 
00144             //! Disallows anyone from creating an instance of this class
00145             DISALLOW_INSTANTIATION(KodeCreator);
00146         };
00147 
00148         class TokenParser {
00149         public:
00150             //! Parses the retrieved Kodu code into the appropriate data structures
00151             static bool parseTokens(const std::vector<std::string>&, std::vector<ParsedPage*>&);
00152         
00153             //! Reads the Kodu written code from a file
00154             static bool readText(std::vector<std::string>&);
00155 
00156         private:
00157             //! Contains all the allowable Kodu "keywords"
00158             static std::set<std::string> koduKeywords;
00159 
00160             //! Returns the index of the search item
00161             static int contains(const std::vector<TokenBase*>&, const std::string&);
00162             
00163             //! Returns a vector of tokens parsed from a string
00164             static bool tokenize(const std::string&, std::vector<TokenBase*>&);
00165 
00166             // ==============================
00167             //! Clear the keyword set (it's no longer needed after parsing)
00168             static void clearKeywordSet();
00169 
00170             //! Initializes the keyword set
00171             static void initializeKeywordSet();
00172 
00173             //! Disallows anyone from creating an instance of this class
00174             DISALLOW_INSTANTIATION(TokenParser);
00175         };
00176 
00177         //! Initializes all the keyword sets and default string values
00178         static void initializeKeywordsAndDefaults();
00179 
00180         //! Clears all the keyword sets and default string values
00181         static void clearKeywordsAndDefaults();
00182 
00183         //! Checks if a specified string is a valid color
00184         static bool isValidColor(const std::string&);
00185 
00186         //! Checks if a specified string is a valid comparison operator
00187         static bool isValidComparisonOperator(const std::string&);
00188 
00189         //! Checks if a specified string is a valid score letter
00190         static bool isValidScoreLetter(const std::string&);
00191 
00192         //! Contains all the allowable color names
00193         static std::set<std::string> koduColorKeywords;
00194 
00195         //! Contains all the allowable comparison operators
00196         static std::set<std::string> koduCompKeywords;
00197 
00198         //! Contains all the allowable score letters (such as Score_A, Score_B ... Score_Y, Score_Z)
00199         static std::set<std::string> koduScoreLetterKeywords;
00200 
00201         static std::string koduDefaultDesignator;
00202         static std::string koduDefaultCompOperator;
00203 
00204         //! Disallows anyone from creating an instance of this class
00205         DISALLOW_INSTANTIATION(Parser);
00206 
00207     //public:
00208         static bool parseAndCreateKoduProgram(std::vector<KoduPage*>& pages) {
00209             std::vector<std::string> koduStrings;
00210             std::vector<Kodu::ParsedPage*> tempPages;
00211             
00212             initializeKeywordsAndDefaults();
00213 
00214             if (TokenParser::readText(koduStrings) == false) {
00215                 std::cerr << "File reading failed.\n";
00216                 return false;
00217             }
00218             
00219             // parses the code
00220             if (TokenParser::parseTokens(koduStrings, tempPages) == false) {
00221                 std::cerr << "Code parsing failed.\n";
00222                 return false;
00223             }
00224 
00225             // create the Kodu pages, rules, conditions, and actions
00226             if (KodeCreator::createKode(tempPages, pages) == false) {
00227                 std::cerr << "Kode creation failed.\n";
00228                 return false;
00229             }
00230 
00231             clearKeywordsAndDefaults();
00232             return true;
00233         }
00234     };
00235 }
00236 
00237 # endif // PARSER_H_

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:45 2016 by Doxygen 1.6.3