#ifndef PARSER_H
#define PARSER_H
#include <string>
#include <vector>
#include <cstdlib>
#include "Token.h"
#include "ContextManager.h"
#include "AST/AllTypesOfASTNodes.h"
#include "AST/AST.h"

using namespace std;

/**
 * Parser does parsing and produces AST. It provides root of AST which can be used to execute statements.
 */
class Parser
{
    public:
		/**
		 * Parser is initialized with pointer or reference to ContextManager,
		 * which is then used to manage contexts in AST created by Parser.
		 */
        Parser(ContextManager * context_manager);
        ContextManager * context_manager;


        virtual ~Parser();
        /**
         * interpret() is essential method in Parser. If given tokens, Parser prepares
         * AST and returns ProgramNode (root node).
         */
        ProgramNode * interpret();

        string error_message;
        void add_tokens(vector<Token> tokens);

        /**
         * show_token() shows token which Parser stores and uses for creating an AST.
         * It's useful during debugging.
         */
        string show_tokens();
        AST tree;
        ProgramNode * program;

        /**
         * resets all info, best to use, when one wants to parse whole new tree
         */
        void erase_all();


    protected:

    private:
        Token current_token;
        string tok_value;
        vector<Token> token_stream;
        unsigned int position_in_stream;

        bool in_statement;
        bool read_next();
        bool peek(string s);
        bool peek_one_ahead(string s);
        bool accept(string s);
        bool expect(string s);
        bool is_type();

        void error(string s);

        /**
         * statement() is function used for parsing statements,
         * it is used as a first in sequence of recurrent calls.
         */
        ASTStatement * statement();
        ASTExpression * expr();
        ASTExpression * log_expr();
        ASTExpression * eq_expr();
        ASTExpression * rel_expr();
        ASTExpression * add_expr();
        ASTExpression * mul_expr();
        ASTExpression * unary_expr();
        ASTExpression * postfix_expr();
        ASTExpression * prim_expr();

};

#endif // PARSER_H
