File indexing completed on 2024-02-25 03:50:21

0001 /*
0002     SPDX-FileCopyrightText: 2003-2006 Cies Breijs <cies AT kde DOT nl>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef _PARSER_H_
0008 #define _PARSER_H_
0009 
0010 
0011 
0012 #include "errormsg.h"
0013 #include "tokenizer.h"
0014 #include "treenode.h"
0015 
0016 
0017 /**
0018  * @short Uses a Tokenizer to step-wise parse KTurtle code to a node tree.
0019  *
0020  * The Parser uses a Tokenizer to read the KTurtle code as tokens.
0021  * It step-by-step parses the tokens to a node tree.
0022  * When errors occur they are added to the error list.
0023  *
0024  * A large part of the code of this class is generated code.
0025  *
0026  * @author Cies Breijs
0027  */
0028 class Parser
0029 {
0030     public:
0031         /**
0032          * @short Constructor. Initialses the Parser.
0033          * does nothing special. @see initialize().
0034          */
0035         explicit Parser(bool testing = false) : m_testing(testing) {}
0036 
0037         /**
0038          * @short Destructor. Does nothing special.
0039          */
0040         ~Parser();
0041 
0042 
0043         /**
0044          * @short Initializes (resets) the Parser
0045          * Use this method to reset the Parser.
0046          *
0047          * It creates the 'root' node, and gets the first token.
0048          * @param tokenizer pointer to a Tokenizer
0049          * @param errorList pointer to a QList for ErrorMessage objects, when
0050          *                  error occur they will be stored here
0051          */
0052         void initialize(Tokenizer* _tokenizer, ErrorList* _errorList);
0053 
0054         /**
0055          * @short Parses one 'step' (usually one statement).
0056          * It calls @ref parseStatement() to receive a little piece of tree (some nodes)
0057          * and appends them to the current scope.
0058          *
0059          * When it detects an EndOfInput token it sets finished to TRUE and stops
0060          * working. @see isFinished
0061          */
0062         void         parse();
0063 
0064         /**
0065          * @short Reflects if the Parser has finished parsing (got an EndOfInput token).
0066          * @return TRUE when parsing has finished otherwise FALSE.
0067          */
0068         bool         isFinished() const { return finished; }
0069 
0070         /**
0071          * @short Method to get the root node of the node tree (pointer based data structure).
0072          * The Parser does not delete the node tree so this pointer can be passed
0073          * to the Executer.
0074          * @returns The pointer to the root node.
0075          */
0076         TreeNode*    getRootNode() const    { return rootNode; }
0077 
0078         void         printTree() const;
0079 
0080     private:
0081         void         nextToken();
0082         bool         skipToken(int expectedTokenType);
0083         bool         skipToken(int expectedTokenType, Token& byToken);
0084         void         addError(const QString& s, const Token& t, int code);
0085         void         appendArguments(TreeNode* node);
0086 
0087         TreeNode*    parseStatement();
0088         TreeNode*    parseFactor();
0089         TreeNode*    parseSignedFactor();
0090         TreeNode*    parseTerm();
0091         TreeNode*    parseExpression();
0092 
0093         Tokenizer   *tokenizer;
0094         ErrorList   *errorList;
0095 
0096         TreeNode    *rootNode;
0097         TreeNode    *currentScope;
0098         TreeNode    *newScope;
0099         Token       *currentToken;
0100         bool         finished;
0101 
0102         bool         m_testing;
0103 
0104 
0105 // Next you find individual parse functions as generated:
0106 
0107 //BEGIN GENERATED parser_h CODE
0108 
0109 /* The code between the line that start with "//BEGIN GENERATED" and "//END GENERATED"
0110  * is generated by "generate.rb" according to the definitions specified in
0111  * "definitions.rb". Please make all changes in the "definitions.rb" file, since all
0112  * all change you make here will be overwritten the next time "generate.rb" is run.
0113  * Thanks for looking at the code!
0114  */
0115 
0116         TreeNode* parseVariable();
0117         TreeNode* parseFunctionCall();
0118         TreeNode* parseScopeOpen();
0119         TreeNode* parseScopeClose();
0120         TreeNode* parseExit();
0121         TreeNode* parseIf();
0122         TreeNode* parseElse();
0123         TreeNode* parseRepeat();
0124         TreeNode* parseWhile();
0125         TreeNode* parseFor();
0126         TreeNode* parseBreak();
0127         TreeNode* parseReturn();
0128         TreeNode* parseWait();
0129         TreeNode* parseAssert();
0130         TreeNode* parseLearn();
0131         TreeNode* parseReset();
0132         TreeNode* parseClear();
0133         TreeNode* parseCenter();
0134         TreeNode* parseGo();
0135         TreeNode* parseGoX();
0136         TreeNode* parseGoY();
0137         TreeNode* parseForward();
0138         TreeNode* parseBackward();
0139         TreeNode* parseDirection();
0140         TreeNode* parseTurnLeft();
0141         TreeNode* parseTurnRight();
0142         TreeNode* parsePenWidth();
0143         TreeNode* parsePenUp();
0144         TreeNode* parsePenDown();
0145         TreeNode* parsePenColor();
0146         TreeNode* parseCanvasColor();
0147         TreeNode* parseCanvasSize();
0148         TreeNode* parseSpriteShow();
0149         TreeNode* parseSpriteHide();
0150         TreeNode* parsePrint();
0151         TreeNode* parseFontSize();
0152         TreeNode* parseRandom();
0153         TreeNode* parseGetX();
0154         TreeNode* parseGetY();
0155         TreeNode* parseMessage();
0156         TreeNode* parseAsk();
0157         TreeNode* parsePi();
0158         TreeNode* parseTan();
0159         TreeNode* parseSin();
0160         TreeNode* parseCos();
0161         TreeNode* parseArcTan();
0162         TreeNode* parseArcSin();
0163         TreeNode* parseArcCos();
0164         TreeNode* parseSqrt();
0165         TreeNode* parseRound();
0166         TreeNode* parseGetDirection();
0167         TreeNode* parseMod();
0168 
0169 //END GENERATED parser_h CODE
0170 
0171 };
0172 
0173 
0174 #endif // _TOKENIZER_H_