#ifndef _CLI_PARSER_H_
#define _CLI_PARSER_H_

#include <ast/Statement.h>
#include <ast/Option.h>
#include <ast/Arg.h>

#include <ast/options/TypeOption.h>
#include <ast/options/CategoryOption.h>

#include <command/CommandList.h>

#include <ast/statements/StatementList.h>

#include <lexer/Lexer.h>

#include <exception/CommonException.h>

#include <alib/algorithm>

namespace cli {

class Parser {
	cli::Lexer m_lexer;
	cli::Lexer::Token m_current;

public:
	Parser ( cli::Lexer lexer ) : m_lexer ( std::move ( lexer ) ), m_current ( m_lexer.nextToken ( true ) ) {
	}

	void setHint ( Lexer::Hint hint ) {
		m_lexer.setHint ( hint );
		m_lexer.putback ( std::move ( m_current ) );
		m_current = m_lexer.nextToken ( false );
		m_lexer.setHint ( Lexer::Hint::NONE );
	}

	template < class ... Tokens >
	bool check ( Tokens ... tokens ) const {
		return ( ... || ( m_current.m_type == tokens ) );
	}

	template < class ... NonreservedTokens >
	bool check_nonreserved_kw ( const NonreservedTokens & ... kw ) const {
		return m_current.m_type == Lexer::TokenType::IDENTIFIER && ( ... || ( m_current.m_value == kw ) );
	}

	template < class Token, class ... Tokens >
	bool match ( Token token, Tokens ... tokens ) {
		if ( ! check ( token, tokens ... ) )
			throw exception::CommonException ( std::string ( "Mismatched token while matching a token " ) + ( Lexer::tokenTypeToString ( token ) + ... + ( ", " + Lexer::tokenTypeToString ( tokens ) ) ) + ". Actual was " + Lexer::tokenTypeToString ( m_current.m_type ) + "." );
		m_current = m_lexer.nextToken ( false );
		return true;
	}

	bool match_nonreserved_kw ( const std::string & kw ) {
		if ( ! check_nonreserved_kw ( kw ) )
			throw exception::CommonException ( "Mismatched token while matching a non reseved keyword: " + kw + "." );
		m_current = m_lexer.nextToken ( false );
		return true;
	}

	std::string matchIdentifier ( ) {
		if ( ! check ( Lexer::TokenType::IDENTIFIER ) )
			throw exception::CommonException ( "Mismatched token while matching an identifier." );
		std::string res = m_current.m_value;
		m_current = m_lexer.nextToken ( false );
		return res;
	}

	std::string matchString ( ) {
		if ( ! check ( Lexer::TokenType::STRING ) )
			throw exception::CommonException ( "Mismatched token while matching a string." );
		std::string res = m_current.m_value;
		m_current = m_lexer.nextToken ( false );
		return res;
	}

	std::string matchType ( ) {
		if ( ! check ( Lexer::TokenType::TYPE ) )
			throw exception::CommonException ( "Mismatched token while matching a type." );
		std::string res = m_current.m_value;
		m_current = m_lexer.nextToken ( false );
		return res;
	}

	std::string matchFile ( ) {
		if ( ! check ( Lexer::TokenType::FILE ) )
			throw exception::CommonException ( "Mismatched token while matching a file." );
		std::string res = m_current.m_value;
		m_current = m_lexer.nextToken ( false );
		return res;
	}

	int matchInteger ( ) {
		if ( ! check ( Lexer::TokenType::INTEGER ) )
			throw exception::CommonException ( "Mismatched token while matching an integer."  );
		int res = ext::from_string < int > ( m_current.m_value );
		m_current = m_lexer.nextToken ( false );
		return res;
	}

	std::string getTokenValue ( ) {
		return m_current.m_value;
	}

	std::unique_ptr < CategoryOption > category_option ( );

	std::unique_ptr < TypeOption > type_option ( );

	std::unique_ptr < TypeOption > optional_type_option ( );

	std::unique_ptr < Arg > file ( );

	std::unique_ptr < Arg > type ( );

	std::unique_ptr < Arg > arg ( );

	std::unique_ptr < Arg > optional_arg ( );

	std::unique_ptr < Arg > template_arg ( );

	std::unique_ptr < Arg > optional_variable ( );

	std::unique_ptr < Arg > optional_binding ( );

	bool move_arg ( );

	std::shared_ptr < Statement > in_redirect_file ( );

	std::shared_ptr < Statement > in_redirect ( );

	std::shared_ptr < Statement > common ( );

	std::shared_ptr < Statement > param ( );

	std::shared_ptr < Statement > statement ( );

	std::shared_ptr < StatementList > statement_list ( );

	void out_redirect_file ( std::shared_ptr < StatementList > & list );

	void out_redirect ( std::shared_ptr < StatementList > & list );

	void result ( std::shared_ptr < StatementList > & list );

	std::pair < bool, bool > introspect_cast_from_to ( );

	std::unique_ptr < Command > introspect_command ( );

	std::unique_ptr < Command > command ( );

	std::unique_ptr < CommandList > parse ( );

};

} /* namespace cli */

#endif /* _CLI_PARSER_H_ */
